1 |
root |
1.4 |
#ifndef ESTL_H_ |
2 |
|
|
#define ESTL_H_ |
3 |
sf-exg |
1.2 |
|
4 |
sf-exg |
1.1 |
#include <stdlib.h> |
5 |
|
|
#include <string.h> |
6 |
|
|
|
7 |
root |
1.9 |
#include "ecb.h" |
8 |
|
|
|
9 |
sf-exg |
1.5 |
template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
10 |
|
|
template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
11 |
sf-exg |
1.1 |
|
12 |
root |
1.3 |
template<typename T, typename U> static inline void swap (T& a, U& b) { T t = a; a = (T)b; b = (U)t; } |
13 |
sf-exg |
1.1 |
|
14 |
|
|
template <typename I, typename T> |
15 |
|
|
I find (I first, I last, const T& value) |
16 |
|
|
{ |
17 |
|
|
while (first != last && *first != value) |
18 |
|
|
++first; |
19 |
|
|
|
20 |
|
|
return first; |
21 |
|
|
} |
22 |
|
|
|
23 |
root |
1.8 |
#include <new> |
24 |
|
|
|
25 |
root |
1.21 |
#if ECB_CPP11 |
26 |
root |
1.8 |
#include <type_traits> |
27 |
|
|
#endif |
28 |
|
|
|
29 |
root |
1.9 |
// original version taken from MICO, but this has been completely rewritten |
30 |
|
|
// known limitations w.r.t. std::vector |
31 |
|
|
// - many methods missing |
32 |
root |
1.12 |
// - no error checking, no exceptions thrown (e.g. at()) |
33 |
root |
1.9 |
// - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements |
34 |
|
|
// - no allocator support |
35 |
|
|
// - we don't really care about const correctness, but we try |
36 |
|
|
// - we don't care about namespaces and stupid macros the user might define |
37 |
root |
1.12 |
// - no bool specialisation |
38 |
sf-exg |
1.1 |
template<class T> |
39 |
|
|
struct simplevec |
40 |
|
|
{ |
41 |
root |
1.9 |
#if ESTL_BIG_VECTOR |
42 |
|
|
// shoudl use size_t/ssize_t, but that's not portable enough for us |
43 |
|
|
typedef unsigned long size_type; |
44 |
|
|
typedef long difference_type; |
45 |
|
|
#else |
46 |
|
|
typedef uint32_t size_type; |
47 |
|
|
typedef int32_t difference_type; |
48 |
|
|
#endif |
49 |
|
|
|
50 |
|
|
typedef T value_type; |
51 |
|
|
typedef T *iterator; |
52 |
root |
1.3 |
typedef const T *const_iterator; |
53 |
root |
1.9 |
typedef T *pointer; |
54 |
|
|
typedef const T *const_pointer; |
55 |
|
|
typedef T &reference; |
56 |
|
|
typedef const T &const_reference; |
57 |
|
|
// missing: allocator_type |
58 |
|
|
// missing: reverse iterator |
59 |
|
|
|
60 |
|
|
private: |
61 |
|
|
size_type sze, res; |
62 |
|
|
T *buf; |
63 |
sf-exg |
1.1 |
|
64 |
root |
1.9 |
// we shamelessly optimise for "simple" types. everything |
65 |
|
|
// "not simple enough" will use the slow path. |
66 |
root |
1.8 |
static bool is_simple_enough () |
67 |
|
|
{ |
68 |
root |
1.21 |
#if ECB_CPP11 |
69 |
root |
1.8 |
return std::is_trivially_assignable<T, T>::value |
70 |
|
|
&& std::is_trivially_constructable<T>::value |
71 |
|
|
&& std::is_trivially_copyable<T>::value |
72 |
|
|
&& std::is_trivially_destructible<T>::value; |
73 |
|
|
#elif ECB_GCC_VERSION(4,4) |
74 |
|
|
return __has_trivial_assign (T) |
75 |
|
|
&& __has_trivial_constructor (T) |
76 |
|
|
&& __has_trivial_copy (T) |
77 |
|
|
&& __has_trivial_destructor (T); |
78 |
|
|
#else |
79 |
|
|
return 0; |
80 |
|
|
#endif |
81 |
|
|
} |
82 |
|
|
|
83 |
root |
1.9 |
static void construct (iterator a, size_type n = 1) |
84 |
root |
1.3 |
{ |
85 |
root |
1.9 |
if (!is_simple_enough ()) |
86 |
|
|
while (n--) |
87 |
sf-exg |
1.17 |
new (a++) T (); |
88 |
root |
1.3 |
} |
89 |
|
|
|
90 |
root |
1.9 |
static void destruct (iterator a, size_type n = 1) |
91 |
root |
1.3 |
{ |
92 |
root |
1.8 |
if (!is_simple_enough ()) |
93 |
root |
1.9 |
while (n--) |
94 |
|
|
(*a++).~T (); |
95 |
root |
1.3 |
} |
96 |
|
|
|
97 |
root |
1.11 |
template<class I> |
98 |
|
|
static void cop_new (iterator a, I b) { new (a) T (*b); } |
99 |
|
|
template<class I> |
100 |
|
|
static void cop_set (iterator a, I b) { *a = *b ; } |
101 |
|
|
|
102 |
root |
1.14 |
// MUST copy forwards |
103 |
root |
1.11 |
template<class I> |
104 |
root |
1.15 |
static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
105 |
root |
1.11 |
{ |
106 |
root |
1.13 |
while (n--) |
107 |
|
|
op (dst++, src++); |
108 |
root |
1.11 |
} |
109 |
|
|
|
110 |
root |
1.15 |
static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator)) |
111 |
root |
1.8 |
{ |
112 |
|
|
if (is_simple_enough ()) |
113 |
|
|
memcpy (dst, src, sizeof (T) * n); |
114 |
|
|
else |
115 |
root |
1.11 |
copy<iterator> (dst, src, n, op); |
116 |
root |
1.9 |
} |
117 |
|
|
|
118 |
|
|
static T *alloc (size_type n) ecb_cold |
119 |
|
|
{ |
120 |
|
|
return (T *)::operator new ((size_t) (sizeof (T) * n)); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
void dealloc () ecb_cold |
124 |
|
|
{ |
125 |
|
|
destruct (buf, sze); |
126 |
|
|
::operator delete (buf); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
size_type good_size (size_type n) ecb_cold |
130 |
|
|
{ |
131 |
|
|
return n ? 2UL << ecb_ld32 (n) : 5; |
132 |
root |
1.8 |
} |
133 |
|
|
|
134 |
|
|
void ins (iterator where, size_type n) |
135 |
root |
1.3 |
{ |
136 |
root |
1.9 |
size_type pos = where - begin (); |
137 |
|
|
|
138 |
|
|
if (ecb_expect_false (sze + n > res)) |
139 |
root |
1.3 |
{ |
140 |
root |
1.9 |
res = good_size (sze + n); |
141 |
root |
1.3 |
|
142 |
root |
1.9 |
T *nbuf = alloc (res); |
143 |
|
|
copy (nbuf, buf, sze, cop_new); |
144 |
|
|
dealloc (); |
145 |
|
|
buf = nbuf; |
146 |
root |
1.3 |
} |
147 |
root |
1.9 |
|
148 |
|
|
construct (buf + sze, n); |
149 |
root |
1.14 |
|
150 |
root |
1.9 |
sze += n; |
151 |
root |
1.14 |
|
152 |
|
|
iterator src = buf + pos; |
153 |
|
|
if (is_simple_enough ()) |
154 |
|
|
memmove (src + n, src, sizeof (T) * n); |
155 |
|
|
else |
156 |
|
|
for (size_type i = n; i--; ) |
157 |
sf-exg |
1.17 |
cop_set (src + n + i, src + i); |
158 |
root |
1.3 |
} |
159 |
sf-exg |
1.1 |
|
160 |
|
|
public: |
161 |
root |
1.9 |
size_type capacity () const { return res; } |
162 |
|
|
size_type size () const { return sze; } |
163 |
|
|
bool empty () const { return size () == 0; } |
164 |
|
|
|
165 |
root |
1.11 |
size_t max_size () const |
166 |
|
|
{ |
167 |
|
|
return (~(size_type)0) >> 1; |
168 |
|
|
} |
169 |
|
|
|
170 |
root |
1.9 |
const_iterator begin () const { return &buf [ 0]; } |
171 |
|
|
iterator begin () { return &buf [ 0]; } |
172 |
|
|
const_iterator end () const { return &buf [sze ]; } |
173 |
|
|
iterator end () { return &buf [sze ]; } |
174 |
|
|
const_reference front () const { return buf [ 0]; } |
175 |
|
|
reference front () { return buf [ 0]; } |
176 |
|
|
const_reference back () const { return buf [sze - 1]; } |
177 |
|
|
reference back () { return buf [sze - 1]; } |
178 |
|
|
|
179 |
root |
1.3 |
void reserve (size_type sz) |
180 |
|
|
{ |
181 |
root |
1.9 |
if (ecb_expect_true (sz <= res)) |
182 |
|
|
return; |
183 |
root |
1.3 |
|
184 |
root |
1.9 |
sz = good_size (sz); |
185 |
|
|
T *nbuf = alloc (sz); |
186 |
root |
1.3 |
|
187 |
root |
1.15 |
copy (nbuf, begin (), sze, cop_new); |
188 |
root |
1.9 |
dealloc (); |
189 |
|
|
|
190 |
|
|
buf = nbuf; |
191 |
|
|
res = sz; |
192 |
root |
1.3 |
} |
193 |
|
|
|
194 |
root |
1.7 |
void resize (size_type sz) |
195 |
|
|
{ |
196 |
|
|
reserve (sz); |
197 |
root |
1.8 |
|
198 |
|
|
if (is_simple_enough ()) |
199 |
root |
1.9 |
sze = sz; |
200 |
root |
1.8 |
else |
201 |
|
|
{ |
202 |
root |
1.9 |
while (sze < sz) construct (buf + sze++); |
203 |
|
|
while (sze > sz) destruct (buf + --sze); |
204 |
root |
1.8 |
} |
205 |
root |
1.7 |
} |
206 |
|
|
|
207 |
root |
1.3 |
simplevec () |
208 |
root |
1.9 |
: sze(0), res(0), buf(0) |
209 |
root |
1.3 |
{ |
210 |
|
|
} |
211 |
|
|
|
212 |
root |
1.9 |
simplevec (size_type n, const T &t = T ()) |
213 |
root |
1.3 |
{ |
214 |
root |
1.11 |
sze = res = n; |
215 |
|
|
buf = alloc (sze); |
216 |
|
|
|
217 |
|
|
while (n--) |
218 |
|
|
new (buf + n) T (t); |
219 |
root |
1.3 |
} |
220 |
|
|
|
221 |
root |
1.11 |
template<class I> |
222 |
|
|
simplevec (I first, I last) |
223 |
root |
1.3 |
{ |
224 |
root |
1.11 |
sze = res = last - first; |
225 |
|
|
buf = alloc (sze); |
226 |
root |
1.15 |
copy (buf, first, sze, cop_new); |
227 |
root |
1.3 |
} |
228 |
|
|
|
229 |
|
|
simplevec (const simplevec<T> &v) |
230 |
root |
1.9 |
: sze(0), res(0), buf(0) |
231 |
root |
1.3 |
{ |
232 |
root |
1.11 |
sze = res = v.size (); |
233 |
|
|
buf = alloc (sze); |
234 |
root |
1.15 |
copy (buf, v.begin (), sze, cop_new); |
235 |
root |
1.3 |
} |
236 |
|
|
|
237 |
|
|
~simplevec () |
238 |
|
|
{ |
239 |
root |
1.8 |
dealloc (); |
240 |
root |
1.3 |
} |
241 |
|
|
|
242 |
root |
1.9 |
void swap (simplevec<T> &t) |
243 |
root |
1.3 |
{ |
244 |
root |
1.9 |
::swap (sze, t.sze); |
245 |
|
|
::swap (res, t.res); |
246 |
|
|
::swap (buf, t.buf); |
247 |
root |
1.3 |
} |
248 |
|
|
|
249 |
|
|
void clear () |
250 |
|
|
{ |
251 |
root |
1.9 |
destruct (buf, sze); |
252 |
|
|
sze = 0; |
253 |
root |
1.3 |
} |
254 |
|
|
|
255 |
|
|
void push_back (const T &t) |
256 |
|
|
{ |
257 |
root |
1.9 |
reserve (sze + 1); |
258 |
|
|
new (buf + sze++) T (t); |
259 |
root |
1.3 |
} |
260 |
|
|
|
261 |
|
|
void pop_back () |
262 |
|
|
{ |
263 |
root |
1.9 |
destruct (buf + --sze); |
264 |
root |
1.3 |
} |
265 |
|
|
|
266 |
root |
1.11 |
const_reference operator [](size_type idx) const { return buf[idx]; } |
267 |
|
|
reference operator [](size_type idx) { return buf[idx]; } |
268 |
|
|
|
269 |
|
|
const_reference at (size_type idx) const { return buf [idx]; } |
270 |
|
|
reference at (size_type idx) { return buf [idx]; } |
271 |
|
|
|
272 |
|
|
template<class I> |
273 |
|
|
void assign (I first, I last) |
274 |
|
|
{ |
275 |
|
|
swap (simplevec<T> (first, last)); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
void assign (size_type n, const T &t) |
279 |
|
|
{ |
280 |
|
|
swap (simplevec<T> (n, t)); |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
simplevec<T> &operator= (const simplevec<T> &v) |
284 |
|
|
{ |
285 |
|
|
assign (v.begin (), v.end ()); |
286 |
|
|
return *this; |
287 |
|
|
} |
288 |
root |
1.3 |
|
289 |
|
|
iterator insert (iterator pos, const T &t) |
290 |
|
|
{ |
291 |
root |
1.8 |
size_type at = pos - begin (); |
292 |
root |
1.10 |
|
293 |
root |
1.8 |
ins (pos, 1); |
294 |
root |
1.10 |
buf [at] = t; |
295 |
|
|
|
296 |
|
|
return buf + at; |
297 |
root |
1.3 |
} |
298 |
|
|
|
299 |
root |
1.11 |
template<class I> |
300 |
|
|
iterator insert (iterator pos, I first, I last) |
301 |
root |
1.3 |
{ |
302 |
root |
1.8 |
size_type n = last - first; |
303 |
|
|
size_type at = pos - begin (); |
304 |
root |
1.3 |
|
305 |
root |
1.9 |
ins (pos, n); |
306 |
root |
1.10 |
copy (buf + at, first, n, cop_set); |
307 |
root |
1.3 |
|
308 |
root |
1.10 |
return buf + at; |
309 |
root |
1.3 |
} |
310 |
|
|
|
311 |
|
|
iterator insert (iterator pos, size_type n, const T &t) |
312 |
|
|
{ |
313 |
root |
1.8 |
size_type at = pos - begin (); |
314 |
root |
1.3 |
|
315 |
root |
1.9 |
ins (pos, n); |
316 |
|
|
|
317 |
root |
1.10 |
for (iterator i = buf + at; n--; ) |
318 |
|
|
*i++ = t; |
319 |
root |
1.3 |
|
320 |
root |
1.10 |
return buf + at; |
321 |
root |
1.3 |
} |
322 |
|
|
|
323 |
sf-exg |
1.19 |
iterator erase (iterator first, iterator last) |
324 |
root |
1.3 |
{ |
325 |
root |
1.20 |
size_type n = last - first; |
326 |
|
|
size_type c = end () - last; |
327 |
root |
1.8 |
|
328 |
root |
1.13 |
if (is_simple_enough ()) |
329 |
root |
1.20 |
memmove (first, last, sizeof (T) * c); |
330 |
root |
1.13 |
else |
331 |
root |
1.20 |
copy<iterator> (first, last, c, cop_set); |
332 |
root |
1.13 |
|
333 |
root |
1.9 |
sze -= n; |
334 |
|
|
destruct (buf + sze, n); |
335 |
sf-exg |
1.19 |
|
336 |
|
|
return first; |
337 |
root |
1.3 |
} |
338 |
|
|
|
339 |
sf-exg |
1.19 |
iterator erase (iterator pos) |
340 |
root |
1.3 |
{ |
341 |
|
|
if (pos != end ()) |
342 |
root |
1.8 |
erase (pos, pos + 1); |
343 |
sf-exg |
1.19 |
|
344 |
|
|
return pos; |
345 |
root |
1.3 |
} |
346 |
sf-exg |
1.1 |
}; |
347 |
|
|
|
348 |
|
|
template<class T> |
349 |
root |
1.3 |
bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
350 |
sf-exg |
1.1 |
{ |
351 |
root |
1.3 |
if (v1.size () != v2.size ()) return false; |
352 |
|
|
|
353 |
|
|
return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size () * sizeof (T)); |
354 |
sf-exg |
1.1 |
} |
355 |
|
|
|
356 |
|
|
template<class T> |
357 |
root |
1.3 |
bool operator <(const simplevec<T> &v1, const simplevec<T> &v2) |
358 |
sf-exg |
1.1 |
{ |
359 |
root |
1.3 |
unsigned long minlast = min (v1.size (), v2.size ()); |
360 |
|
|
|
361 |
|
|
for (unsigned long i = 0; i < minlast; ++i) |
362 |
|
|
{ |
363 |
|
|
if (v1[i] < v2[i]) return true; |
364 |
|
|
if (v2[i] < v1[i]) return false; |
365 |
sf-exg |
1.1 |
} |
366 |
root |
1.3 |
return v1.size () < v2.size (); |
367 |
sf-exg |
1.1 |
} |
368 |
|
|
|
369 |
|
|
template<typename T> |
370 |
|
|
struct vector : simplevec<T> |
371 |
|
|
{ |
372 |
|
|
}; |
373 |
sf-exg |
1.2 |
|
374 |
|
|
#endif |
375 |
root |
1.3 |
|