1 |
/* |
2 |
* $Id: Vector.h,v 1.4 2007-08-18 08:52:18 maya Exp $ |
3 |
*/ |
4 |
|
5 |
#ifndef _YCL_VECTOR_H_ |
6 |
#define _YCL_VECTOR_H_ |
7 |
|
8 |
#if _MSC_VER >= 1000 |
9 |
#pragma once |
10 |
#endif // _MSC_VER >= 1000 |
11 |
|
12 |
#include <YCL/common.h> |
13 |
|
14 |
#include <YCL/ValueCtrl.h> |
15 |
#include <YCL/Pointer.h> |
16 |
#include <YCL/Enumeration.h> |
17 |
#include <YCL/Array.h> |
18 |
|
19 |
namespace yebisuya { |
20 |
|
21 |
template<class TYPE, class CTRL = ValueCtrl<TYPE> > |
22 |
class Vector { |
23 |
private: |
24 |
enum { |
25 |
BOUNDARY = 8, |
26 |
}; |
27 |
class EnumElements : public Enumeration<TYPE> { |
28 |
private: |
29 |
TYPE* array; |
30 |
int size; |
31 |
mutable int index; |
32 |
public: |
33 |
EnumElements(TYPE* array, int size):array(array), size(size), index(0) { |
34 |
} |
35 |
virtual bool hasMoreElements()const { |
36 |
return index < size; |
37 |
} |
38 |
virtual TYPE nextElement()const { |
39 |
return array[index++]; |
40 |
} |
41 |
}; |
42 |
|
43 |
TYPE* array; |
44 |
int arraySize; |
45 |
int arrayLength; |
46 |
|
47 |
Vector(Vector<TYPE,CTRL>&); |
48 |
void operator=(Vector<TYPE,CTRL>&); |
49 |
public: |
50 |
Vector():array(NULL), arraySize(0), arrayLength(0) { |
51 |
} |
52 |
Vector(int size):array(NULL), arraySize(0), arrayLength(0) { |
53 |
setSize(size); |
54 |
} |
55 |
~Vector() { |
56 |
removeAll(); |
57 |
} |
58 |
int size()const { |
59 |
return arraySize; |
60 |
} |
61 |
TYPE get(int index)const { |
62 |
TYPE result; |
63 |
if (0 <= index && index < arraySize) { |
64 |
result = array[index]; |
65 |
}else{ |
66 |
CTRL::initialize(result); |
67 |
} |
68 |
return result; |
69 |
} |
70 |
|
71 |
void setSize(int newSize) { |
72 |
if (newSize != arraySize) { |
73 |
if (newSize > arrayLength) { |
74 |
int newLength = (newSize + 7) & ~7; |
75 |
TYPE* newArray = new TYPE[newLength]; |
76 |
int i; |
77 |
for (i = 0; i < arraySize; i++) { |
78 |
newArray[i] = array[i]; |
79 |
} |
80 |
for (; i < newSize; i++) { |
81 |
CTRL::initialize(newArray[i]); |
82 |
} |
83 |
arrayLength = newLength; |
84 |
delete[] array; |
85 |
array = newArray; |
86 |
} |
87 |
for (int i = newSize; i < arraySize && i < arrayLength; i++) { |
88 |
CTRL::initialize(array[i]); |
89 |
} |
90 |
arraySize = newSize; |
91 |
} |
92 |
} |
93 |
void set(int index, const TYPE& value) { |
94 |
if (0 <= index && index < arraySize) { |
95 |
array[index] = value; |
96 |
} |
97 |
} |
98 |
void add(const TYPE& value) { |
99 |
add(arraySize, value); |
100 |
} |
101 |
void add(int index, const TYPE& value) { |
102 |
if (index < 0) |
103 |
index = 0; |
104 |
if (index > arraySize) |
105 |
index = arraySize; |
106 |
setSize(arraySize + 1); |
107 |
for (int i = arraySize - 1; i > index; i--) { |
108 |
array[i] = array[i - 1]; |
109 |
} |
110 |
array[index] = value; |
111 |
} |
112 |
Pointer<Enumeration<TYPE> > elements()const { |
113 |
return new EnumElements(array, arraySize); |
114 |
} |
115 |
int indexOf(const TYPE& value)const { |
116 |
return indexOf(value, 0); |
117 |
} |
118 |
int indexOf(const TYPE& value, int index)const { |
119 |
for (int i = index; i < arraySize; i++) { |
120 |
if (array[i] == value) |
121 |
return i; |
122 |
} |
123 |
return -1; |
124 |
} |
125 |
int lastIndexOf(const TYPE& value)const { |
126 |
return indexOf(value, arraySize - 1); |
127 |
} |
128 |
int lastIndexOf(const TYPE& value, int index)const { |
129 |
for (int i = index; i >= 0; i--) { |
130 |
if (array[i] == value) |
131 |
return i; |
132 |
} |
133 |
return -1; |
134 |
} |
135 |
bool isEmpty()const { |
136 |
return arraySize == 0; |
137 |
} |
138 |
TYPE firstElement()const { |
139 |
return get(0); |
140 |
} |
141 |
TYPE lastElement()const { |
142 |
return get(arraySize - 1); |
143 |
} |
144 |
TYPE remove(int index) { |
145 |
TYPE removed = get(index); |
146 |
for (int i = index; i < arraySize - 1; i++) { |
147 |
array[i] = array[i + 1]; |
148 |
} |
149 |
setSize(arraySize - 1); |
150 |
return removed; |
151 |
} |
152 |
bool remove(const TYPE& value) { |
153 |
int index = indexOf(value); |
154 |
if (index < 0) |
155 |
return false; |
156 |
remove(index); |
157 |
return true; |
158 |
} |
159 |
void removeAll() { |
160 |
delete[] array; |
161 |
array = NULL; |
162 |
arraySize = 0; |
163 |
arrayLength = 0; |
164 |
} |
165 |
PointerArray<TYPE> toArray()const { |
166 |
return toArray(new Array<TYPE>(arraySize)); |
167 |
} |
168 |
PointerArray<TYPE> toArray(PointerArray<TYPE> copyto)const { |
169 |
int i; |
170 |
for (i = 0; i < (int) copyto->length && i < arraySize; i++) { |
171 |
copyto[i] = array[i]; |
172 |
} |
173 |
for (; i < (int) copyto->length; i++) { |
174 |
CTRL::initialize(copyto[i]); |
175 |
} |
176 |
return copyto; |
177 |
} |
178 |
void trimToSize() { |
179 |
TYPE* newArray = array; |
180 |
if (arraySize == 0) { |
181 |
newArray = NULL; |
182 |
}else if (arraySize < arrayLength) { |
183 |
newArray = new TYPE[arraySize]; |
184 |
for (int i = 0; i < arraySize; i++) { |
185 |
newArray[i] = array[i]; |
186 |
} |
187 |
arrayLength = arraySize; |
188 |
} |
189 |
if (newArray != array) { |
190 |
delete[] array; |
191 |
array = newArray; |
192 |
} |
193 |
} |
194 |
TYPE operator[](int index)const { |
195 |
return get(index); |
196 |
} |
197 |
}; |
198 |
|
199 |
} |
200 |
|
201 |
#endif//_YCL_VECTOR_H_ |