Newer
Older
/******************************************************************************
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* Contributors:
* Balasko, Jeno
* Delic, Adam
* Forstner, Matyas
* Gecse, Roland
* Raduly, Csaba
* Szabados, Kristof
* Szabo, Janos Zoltan – initial implementation
*
******************************************************************************/
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
#ifndef _Common_vector_HH
#define _Common_vector_HH
#ifdef __SUNPRO_CC
/**
* Inclusion of the STL vector header file prevents the Sun Forte 6.2 C++
* compiler from a mysterious internal error.
*/
#include <vector>
#include <stdio.h>
#endif
#include "error.h"
#include "../common/memory.h"
#include <string.h> // for memmove
/**
* Container optimized to store elements sequentially,
* and access them randomly, referenced by indices.
*
* Accessing an element has constant time cost.
* Adding an element at the end has amortized constant time const.
* Other operations (adding at the beginning, replacing/deleting elements)
* have linear time cost.
*
* If there aren't elements in the buffer, then no space is allocated.
* If there are, the size of the allocated buffer is the smallest power of 2
* that is not smaller than the number of elements (num_e).
*
* The container stores pointers to objects of type T; it doesn't own them.
* It is the responsibility of the caller to create and destroy the objects
* and supply the pointers to the container.
*
* \ingroup containers
*/
template<class T>
class vector {
private:
size_t num_e;
T **e_ptr;
static const size_t initial_size = 1, increment_factor = 2;
/** Copy constructor: DO NOT IMPLEMENT! */
vector(const vector&);
/** Copy assignment: DO NOT IMPLEMENT! */
vector& operator=(const vector&);
public:
static const size_t max_vector_length = static_cast<size_t>( -1 );
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
/** Creates an empty vector. */
vector() : num_e(0), e_ptr(NULL) { }
/** Deallocates its memory. If the container is not empty,
* FATAL_ERROR occurs, so before destructing, clear() must be
* invoked explicitly.
*/
~vector() {
if (num_e > 0) FATAL_ERROR("vector::~vector(): vector is not empty");
Free(e_ptr);
}
/** Returns the number of elements in the container. */
size_t size() const { return num_e; }
/** Returns true if the container has no elements. */
bool empty() const { return num_e == 0; }
/** Erases the entire container. */
void clear() {
num_e = 0;
Free(e_ptr);
e_ptr = NULL;
}
/** Appends the \a elem to the end of vector. */
void add(T *elem) {
if (e_ptr == NULL) {
e_ptr = static_cast<T**>(Malloc(initial_size * sizeof(*e_ptr)));
} else {
size_t max_e = initial_size;
while (max_e < num_e) max_e *= increment_factor;
if (max_e <= num_e) {
if (max_e >= max_vector_length / increment_factor)
FATAL_ERROR("vector::add(): vector index overflow");
e_ptr = static_cast<T**>
(Realloc(e_ptr, max_e * increment_factor * sizeof(*e_ptr)));
}
}
e_ptr[num_e++] = elem;
}
/** Inserts the \a elem to the beginning of vector. */
void add_front(T *elem) {
if (e_ptr == NULL) {
e_ptr = static_cast<T**>(Malloc(initial_size * sizeof(*e_ptr)));
} else {
size_t max_e = initial_size;
while (max_e < num_e) max_e *= increment_factor;
if (max_e <= num_e) {
if (max_e >= max_vector_length / increment_factor)
FATAL_ERROR("vector::add_front(): vector index overflow");
e_ptr = static_cast<T**>
(Realloc(e_ptr, max_e * increment_factor * sizeof(*e_ptr)));
}
}
memmove(e_ptr + 1, e_ptr, num_e * sizeof(*e_ptr));
num_e++;
e_ptr[0] = elem;
}
/** Inserts the elem to a specified position in the vector. */
void insert(T* elem, size_t pos) {
if (e_ptr == NULL) {
e_ptr = static_cast<T**>(Malloc(initial_size * sizeof(*e_ptr)));
}
else {
size_t max_e = initial_size;
while (max_e < num_e) {
max_e *= increment_factor;
}
if (max_e <= num_e) {
if (max_e >= max_vector_length / increment_factor) {
FATAL_ERROR("vector::insert(): vector index overflow");
}
e_ptr = static_cast<T**>
(Realloc(e_ptr, max_e * increment_factor * sizeof(*e_ptr)));
}
}
memmove(e_ptr + pos + 1, e_ptr + pos, (num_e - pos) * sizeof(*e_ptr));
num_e++;
e_ptr[pos] = elem;
}
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/** Returns the <em>n</em>th element. The index of the first element is
* zero. If no such index, then FATAL_ERROR occurs. */
T* operator[](size_t n) const {
if (n >= num_e)
FATAL_ERROR("vector::operator[](size_t) const: index overflow");
return e_ptr[n];
}
/** Returns the <em>n</em>th element. The index of the first element is
* zero. If no such index, then FATAL_ERROR occurs. */
T*& operator[](size_t n) {
if (n >= num_e)
FATAL_ERROR("vector::operator[](size_t): index overflow");
return e_ptr[n];
}
/** Replaces \a n elements beginning from position \a pos
* with elements in \a v. If \a pos+n > size() then FATAL_ERROR occurs.
* If \a v == NULL then deletes the elements.
*/
void replace(size_t pos, size_t n, const vector *v = NULL) {
if (pos > num_e) FATAL_ERROR("vector::replace(): position points over " \
"the last element");
else if (n > num_e - pos) FATAL_ERROR("vector::replace(): not enough " \
"elements after the start position");
else if (v == this) FATAL_ERROR("vector::replace(): trying to replace " \
"the original vector");
size_t v_len = v != NULL ? v->num_e : 0;
if (v_len > max_vector_length - num_e + n)
FATAL_ERROR("vector::replace(): resulting vector size exceeds maximal " \
"length");
size_t new_len = num_e - n + v_len;
if (new_len > num_e) {
size_t max_e = initial_size;
if (e_ptr == NULL) {
while (max_e < new_len) max_e *= increment_factor;
e_ptr = static_cast<T**>(Malloc(max_e * sizeof(*e_ptr)));
} else {
while (max_e < num_e) max_e *= increment_factor;
if (new_len > max_e) {
while (max_e < new_len) max_e *= increment_factor;
e_ptr = static_cast<T**>(Realloc(e_ptr, max_e * sizeof(*e_ptr)));
}
}
}
if (pos + n < num_e && v_len != n) memmove(e_ptr + pos + v_len,
e_ptr + pos + n, (num_e - pos - n) * sizeof(*e_ptr));
if (v_len > 0) memcpy(e_ptr + pos, v->e_ptr, v_len * sizeof(*e_ptr));
if (new_len < num_e) {
if (new_len == 0) {
Free(e_ptr);
e_ptr = NULL;
} else {
size_t max_e = initial_size;
while (max_e < num_e) max_e *= increment_factor;
size_t max_e2 = initial_size;
while (max_e2 < new_len) max_e2 *= increment_factor;
if (max_e2 < max_e)
e_ptr = static_cast<T**>(Realloc(e_ptr, max_e2 * sizeof(*e_ptr)));
}
}
num_e = new_len;
}
/**
* Copies a part of the vector to a new vector. The part is
* specified by the starting position (<em>pos</em>) and the number
* of elements (<em>n</em>) to copy. If <em>n</em> is greater than
* the number of elements beginning from the given <em>pos</em>,
* then the returned vector contains less elements.
*
* \note The pointers are copied, the objects they refer to will not
* be duplicated.
*/
vector* subvector(size_t pos = 0, size_t n = max_vector_length) const
{
if (pos > num_e) FATAL_ERROR("vector::subvector(): position points " \
"over last vector element");
if (n > num_e - pos) n = num_e - pos;
vector *tmp_vector = new vector;
if (n > 0) {
size_t max_e = initial_size;
while (max_e < n) max_e *= increment_factor;
tmp_vector->e_ptr = static_cast<T**>(Malloc(max_e * sizeof(*e_ptr)));
memcpy(tmp_vector->e_ptr, e_ptr + pos, n * sizeof(*e_ptr));
tmp_vector->num_e = n;
}
return tmp_vector;
}
}; // class vector
/**
* Container to store simple types (can have constructor, but it should be fast)
* that are simple and small, stores the objects and not the pointers (copy
* constructor must be implemented). The capacity is increased to be always the
* power of two, the container capacity is never decreased. An initial
* capacity value can be supplied to the constructor, this can be used to avoid
* any further memory allocations during the life of the container.
*/
template<class T>
class dynamic_array {
private:
size_t count;
size_t capacity; // 0,1,2,4,8,...
T* data;
void clean_up();
void copy_content(const dynamic_array& other);
public:
dynamic_array() : count(0), capacity(0), data(NULL) {}
// to avoid reallocations and copying
dynamic_array(size_t init_capacity) : count(0), capacity(init_capacity), data(NULL)
{ if (capacity>0) data = new T[capacity]; }
dynamic_array(const dynamic_array& other) : count(0), capacity(0), data(NULL) { copy_content(other); }
dynamic_array& operator=(const dynamic_array& other) { clean_up(); copy_content(other); return *this; }
~dynamic_array() { clean_up(); }
bool operator==(const dynamic_array& other) const;
bool operator!=(const dynamic_array& other) const { return (!(*this==other)); }
/** Returns the number of elements in the container. */
size_t size() const { return count; }
/** Erases the entire container. */
void clear() { clean_up(); }
/** Appends the \a elem to the end of vector. */
void add(const T& elem);
/** Removes an element that is at index \a idx */
void remove(size_t idx);
/** Returns the <em>n</em>th element. The index of the first element is
* zero. If no such index, then FATAL_ERROR occurs. */
const T& operator[](size_t n) const {
if (n>=count) FATAL_ERROR("dynamic_array::operator[] const: index overflow");
return data[n];
}
/** Returns the <em>n</em>th element. The index of the first element is
* zero. If no such index, then FATAL_ERROR occurs. */
T& operator[](size_t n) {
if (n>=count) FATAL_ERROR("dynamic_array::operator[]: index overflow");
return data[n];
}
}; // class dynamic_array
template<class T>
void dynamic_array<T>::clean_up()
{
delete[] data;
data = NULL;
capacity = 0;
count = 0;
}
template<class T>
void dynamic_array<T>::copy_content(const dynamic_array<T>& other)
{
capacity = other.capacity;
count = other.count;
if (capacity>0) {
data = new T[capacity];
for (size_t i=0; i<count; i++) data[i] = other.data[i];
}
}
template<class T>
bool dynamic_array<T>::operator==(const dynamic_array<T>& other) const
{
if (count!=other.count) return false;
for (size_t i=0; i<count; i++) if (!(data[i]==other.data[i])) return false;
return true;
}
template<class T>
void dynamic_array<T>::add(const T& elem)
{
if (data==NULL) {
capacity = 1;
count = 1;
data = new T[1];
data[0] = elem;
} else {
if (count<capacity) {
data[count] = elem;
count++;
} else {
// no more room, need to allocate new memory
if (capacity==0) FATAL_ERROR("dynamic_array::add()");
capacity *= 2;
T* new_data = new T[capacity];
for (size_t i=0; i<count; i++) new_data[i] = data[i];
delete[] data;
data = new_data;
data[count] = elem;
count++;
}
}
}
template<class T>
void dynamic_array<T>::remove(size_t idx)
{
if (idx>=count) FATAL_ERROR("dynamic_array::remove(): index overflow");
for (size_t i=idx+1; i<count; i++) data[i-1] = data[i];
count--;
}
#endif // _Common_vector_HH