forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector_tpl.h
290 lines (249 loc) · 6.05 KB
/
vector_tpl.h
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
#ifndef TPL_VECTOR_H
#define TPL_VECTOR_H
#include <typeinfo>
#include "../macros.h"
#include "../simtypes.h"
#include "../simdebug.h"
template<class T> class vector_tpl;
template<class T> inline void swap(vector_tpl<T>& a, vector_tpl<T>& b);
/** A template class for a simple vector type */
template<class T> class vector_tpl
{
public:
typedef const T* const_iterator;
typedef T* iterator;
/** Construct a vector for cap elements */
vector_tpl() : data(NULL), size(0), count(0) {}
explicit vector_tpl(const uint32 cap) :
data(cap > 0 ? new T[cap] : NULL),
size(cap),
count(0) {}
vector_tpl(const vector_tpl& copy_from) :
data( copy_from.get_size() > 0 ? new T[ copy_from.get_size() ] : 0 ),
size( copy_from.get_size() ),
count( copy_from.get_count() ) {
for( uint32 i = 0; i < count; i++ ) {
data[i] = copy_from.data[i];
}
}
~vector_tpl() { delete [] data; }
/** sets the vector to empty */
void clear() { count = 0; }
/**
* Resizes the maximum data that can be hold by this vector.
* Existing entries are preserved, new_size must be big enough to hold them
*/
void resize(const uint32 new_size)
{
if (new_size <= size) return; // do nothing
T* new_data = new T[new_size];
if(size>0) {
for (uint32 i = 0; i < count; i++) {
new_data[i] = data[i];
}
delete [] data;
}
size = new_size;
data = new_data;
}
/**
* Checks if element elem is contained in vector.
* Uses the == operator for comparison.
*/
bool is_contained(const T& elem) const
{
for (uint32 i = 0; i < count; i++) {
if (data[i] == elem) {
return true;
}
}
return false;
}
/**
* Checks if element elem is contained in vector.
* Uses the == operator for comparison.
*/
uint32 index_of(const T& elem) const
{
for (uint32 i = 0; i < count; i++) {
if (data[i] == elem) {
return i;
}
}
assert(false);
return 0xFFFFFFFFu;
}
void append(const T& elem)
{
if( count == size ) {
resize(size == 0 ? 1 : size * 2);
}
data[count++] = elem;
}
/**
* Checks if element is contained. Appends only new elements.
* extend vector if nessesary
*/
bool append_unique(const T& elem)
{
if (is_contained(elem)) {
return false;
}
append(elem);
return true;
}
/** insert data at a certain pos */
void insert_at(const uint32 pos, const T& elem)
{
if (pos < count) {
if (count == size) {
resize(size == 0 ? 1 : size * 2);
}
for (uint i = count; i > pos; i--) {
data[i] = data[i - 1];
}
data[pos] = elem;
count++;
}
else {
append(elem);
}
}
/**
* Insert `elem' with respect to ordering.
*/
template<class StrictWeakOrdering>
void insert_ordered(const T& elem, StrictWeakOrdering comp)
{
sint32 low = -1, high = count;
while( high - low>1 ) {
const sint32 mid = ((uint32) (low + high)) >> 1;
T &mid_elem = data[mid];
if( comp(elem, mid_elem) ) {
high = mid;
}
else {
low = mid;
}
}
insert_at(high, elem);
}
/**
* Only insert `elem' if not already contained in this vector.
* Respects the ordering and assumes the vector is ordered.
* Returns NULL if insertion is successful;
* otherwise return the address of the element in conflict
*/
template<class StrictWeakOrdering>
T* insert_unique_ordered(const T& elem, StrictWeakOrdering comp)
{
sint32 low = -1, high = count;
while( high - low>1 ) {
const sint32 mid = ((uint32) (low + high)) >> 1;
T &mid_elem = data[mid];
if( elem==mid_elem ) {
return &mid_elem;
}
else if( comp(elem, mid_elem) ) {
high = mid;
}
else {
low = mid;
}
}
insert_at(high, elem);
return NULL;
}
/**
* put the data at a certain position
* BEWARE: using this function will create default objects, depending on
* the type of the vector
*/
void store_at(const uint32 pos, const T& elem)
{
if (pos >= size) {
resize((pos & 0xFFFFFFF7) + 8);
}
data[pos] = elem;
if (pos >= count) {
count = pos + 1;
}
}
/** removes element, if contained */
bool remove(const T& elem)
{
for (uint32 i = 0; i < count; i++) {
if (data[i] == elem) {
return remove_at(i);
}
}
return false;
}
/** removes element at position */
bool remove_at(const uint32 pos)
{
assert(pos<count);
for (uint i = pos; i < count - 1; i++) {
data[i] = data[i + 1];
}
count--;
return true;
}
void pop_back()
{
--count;
}
T& operator [](uint i)
{
if (i >= count) {
dbg->fatal("vector_tpl<T>::[]", "%s: index out of bounds: %i not in 0..%d", typeid(T).name(), i, count - 1);
}
return data[i];
}
const T& operator [](uint i) const
{
if (i >= count) {
dbg->fatal("vector_tpl<T>::[]", "%s: index out of bounds: %i not in 0..%d", typeid(T).name(), i, count - 1);
}
return data[i];
}
T& front() const { return data[0]; }
T& back() const { return data[count - 1]; }
iterator begin() { return data; }
iterator end() { return data + count; }
const_iterator begin() const { return data; }
const_iterator end() const { return data + count; }
/** Get the number of elements in the vector */
uint32 get_count() const { return count; }
/** Get the capacity */
uint32 get_size() const { return size; }
bool empty() const { return count == 0; }
private:
T* data;
uint32 size; ///< Capacity
uint32 count; ///< Number of elements in vector
vector_tpl& operator=( vector_tpl const& other ) {
vector_tpl tmp(other);
swap(tmp, *this);
return *this;
}
friend void swap<>(vector_tpl<T>& a, vector_tpl<T>& b);
};
template<class T> void swap(vector_tpl<T>& a, vector_tpl<T>& b)
{
sim::swap(a.data, b.data);
sim::swap(a.size, b.size);
sim::swap(a.count, b.count);
}
/**
* Clears vectors of the type vector_tpl<someclass*>
* Deletes all objects pointed to by pointers in the vector
*/
template<class T> void clear_ptr_vector(vector_tpl<T*>& v)
{
FORT(vector_tpl<T*>, const i, v) {
delete i;
}
v.clear();
}
#endif