1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
#ifndef __ARRAY_H__
#define __ARRAY_H__
#include "ref.h"
#include <assert.h>
template <class T>
class TArray : virtual public Referenc
{
public:
TArray();
TArray(const TArray<T> &arr)
{
data = NULL;
size = num = 0;
block_size = arr.block_size;
keep_order = arr.keep_order;
//assert(0 && "Direct assignment for arrays is NOT allowed");
// risky probably, but anyway
arr.CopyTo(*this);
}
~TArray();
bool Allocate(int new_num);
bool Resize(int new_size = 0);
bool Add(T &el);
bool Add(const T &el);
inline T &operator[](int i);
inline const T &operator[](int i) const;
inline const TArray<T> &operator =(const TArray<T> &arr)
{
block_size = arr.block_size;
keep_order = arr.keep_order;
//assert(0 && "Direct assignment for arrays is NOT allowed");
// risky probably, but anyway
arr.CopyTo(*this);
return *this;
}
T *Last()
{
if (num > 0)
return &data[num - 1];
return NULL;
}
inline int Length() const
{
return num;
}
inline int Size() const
{
return size;
}
inline int BlockSize() const
{
return block_size;
}
inline T *Data()
{
return data;
}
inline T **DataPtr()
{
return &data;
}
inline const T *Data() const
{
return data;
}
inline void SetKeepOrder(bool enable)
{
keep_order = enable;
}
bool Find(const T &el);
bool Add(TArray<T> &arr);
bool CopyTo(TArray<T> &dest) const;
bool Init(const T *arr, int len);
void Swap(int to, int from);
void Replace(int i);
bool SetBlockSize(int new_size);
void Set(T &el);
void Set(const T &el);
protected:
T *data;
int size;
int num;
int block_size;
// Some operations restricted, order of the elements is fixed
bool keep_order;
};
namespace Array
{
const int default_block_size = 16;
const int max_array_size = 0xffffff;
}
template <class T> TArray<T>::TArray()
: data(NULL), size(0), num(0), block_size(Array::default_block_size), keep_order(false)
{
}
template <class T> TArray<T>::~TArray()
{
if (data != NULL)
Resize();
}
template <class T> bool TArray<T>::Allocate(int new_num)
{
assert(new_num >= 0 && new_num <= Array::max_array_size);
if (new_num > size)
{
if (!Resize(new_num))
return false;
}
num = new_num;
return true;
}
template <class T> bool TArray<T>::Resize(int new_size)
{
assert(new_size >= 0 && new_size <= Array::max_array_size);
if (new_size == 0)
{
delete[] data;
data = NULL;
size = 0;
num = 0;
return true;
}
if (new_size == size)
return true;
T *new_data = new T[new_size];
if (new_data == NULL)
return false;
if (data != NULL && num > 0)
{
//CopyMemory(new_data, data, num * sizeof(T));
for (int i = 0; i < num && i < new_size; i++)
new_data[i] = data[i];
}
delete[] data;
data = new_data;
size = new_size;
return true;
}
template <class T> bool TArray<T>::Add(T &el)
{
if (data == NULL)
Resize(1);
if (num < size)
{
data[num++] = el;
return true;
}
// num >= size
int new_size = size + block_size;
if (!Resize(new_size))
return false;
data[num++] = el;
return true;
}
template <class T> bool TArray<T>::Add(const T &el)
{
if (data == NULL)
Resize(1);
if (num < size)
{
data[num++] = *(T *)⪙
return true;
}
// num >= size
int new_size = size + block_size;
if (!Resize(new_size))
return false;
data[num++] = *(T *)⪙
return true;
}
template <class T> bool TArray<T>::Add(TArray<T> &arr)
{
if (arr.Length() == 0)
return true;
int numf = num;
if (!Allocate(Length() + arr.Length()))
return false;
CopyMemory(&data[numf], arr.Data(), arr.Length() * sizeof(T));
return true;
}
template <class T> T &TArray<T>::operator [](int i)
{
assert(i >= 0 && i < num);
return data[i];
}
template <class T> const T &TArray<T>::operator [](int i) const
{
assert(i >= 0 && i < num);
return data[i];
}
template <class T> bool TArray<T>::SetBlockSize(int new_size)
{
assert(new_size >= 0 && new_size <= Array::max_array_size);
block_size = new_size;
return true;
}
template <class T> void TArray<T>::Set(T &el)
{
for (int i = 0; i < num; i++)
data[i] = el;
}
template <class T> void TArray<T>::Set(const T &el)
{
for (int i = 0; i < num; i++)
data[i] = el;
}
template <class T> bool TArray<T>::CopyTo(TArray<T> &dest) const
{
if (!dest.Resize(size))
return false;
dest.num = 0;
for (int i = 0; i < num; i++)
dest.Add(data[i]);
return true;
}
template <class T> bool TArray<T>::Init(const T *arr, int len)
{
assert(arr != NULL);
if (!Resize(len))
return false;
num = 0;
for (int i = 0; i < len; i++)
Add((T)arr[i]);
return true;
}
template <class T> void TArray<T>::Swap(int to, int from)
{
assert(to >= 0 && to < num && from >= 0 && from < num);
if (keep_order)
return;
T t = data[to];
data[to] = data[from];
data[from] = t;
}
template <class T> void TArray<T>::Replace(int i)
{
assert(i >= 0 && i < num);
if (keep_order)
return;
if (num >= 1)
{
data[i] = data[num - 1];
num--;
}
}
// operator == for type T should be defined
template <class T> bool TArray<T>::Find(const T &el)
{
for (int i = 0; i < num; i++)
{
if (data[i] == el)
return true;
}
return false;
}
#endif // __ARRAY_H__
|