… | |
… | |
2 | #define ESTL_H_ |
2 | #define ESTL_H_ |
3 | |
3 | |
4 | #include <stdlib.h> |
4 | #include <stdlib.h> |
5 | #include <string.h> |
5 | #include <string.h> |
6 | |
6 | |
|
|
7 | #include "ecb.h" |
|
|
8 | |
7 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
9 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
8 | template<typename T, typename U> static inline T max (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; } |
9 | |
11 | |
10 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t = a; a = (T)b; b = (U)t; } |
12 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t = a; a = (T)b; b = (U)t; } |
11 | |
13 | |
… | |
… | |
16 | ++first; |
18 | ++first; |
17 | |
19 | |
18 | return first; |
20 | return first; |
19 | } |
21 | } |
20 | |
22 | |
21 | /* simplevec taken (and heavily modified), from: |
23 | #include <new> |
22 | * |
24 | |
23 | * MICO --- a free CORBA implementation |
25 | #if __cplusplus >= 201103L |
24 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
26 | #include <type_traits> |
25 | * originally GPLv2 or any later |
27 | #endif |
26 | */ |
28 | |
|
|
29 | // original version taken from MICO, but this has been completely rewritten |
|
|
30 | // known limitations w.r.t. std::vector |
|
|
31 | // - many methods missing |
|
|
32 | // - no error checking, no exceptions thrown |
|
|
33 | // - 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 |
27 | template<class T> |
37 | template<class T> |
28 | struct simplevec |
38 | struct simplevec |
29 | { |
39 | { |
|
|
40 | #if ESTL_BIG_VECTOR |
|
|
41 | // shoudl use size_t/ssize_t, but that's not portable enough for us |
|
|
42 | typedef unsigned long size_type; |
|
|
43 | typedef long difference_type; |
|
|
44 | #else |
|
|
45 | typedef uint32_t size_type; |
|
|
46 | typedef int32_t difference_type; |
|
|
47 | #endif |
|
|
48 | |
|
|
49 | typedef T value_type; |
30 | typedef T *iterator; |
50 | typedef T *iterator; |
31 | typedef const T *const_iterator; |
51 | typedef const T *const_iterator; |
32 | typedef unsigned long size_type; |
52 | typedef T *pointer; |
|
|
53 | typedef const T *const_pointer; |
|
|
54 | typedef T &reference; |
|
|
55 | typedef const T &const_reference; |
|
|
56 | // missing: allocator_type |
|
|
57 | // missing: reverse iterator |
33 | |
58 | |
34 | private: |
59 | private: |
35 | size_type _last, _size; |
60 | size_type sze, res; |
36 | T *_buf; |
61 | T *buf; |
|
|
62 | |
|
|
63 | // we shamelessly optimise for "simple" types. everything |
|
|
64 | // "not simple enough" will use the slow path. |
|
|
65 | static bool is_simple_enough () |
|
|
66 | { |
|
|
67 | return 1; // we are not there yet |
|
|
68 | #if __cplusplus >= 201103L |
|
|
69 | 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 | static void construct (iterator a, size_type n = 1) |
|
|
84 | { |
|
|
85 | if (!is_simple_enough ()) |
|
|
86 | while (n--) |
|
|
87 | new (*a++) T (); |
|
|
88 | } |
|
|
89 | |
|
|
90 | static void destruct (iterator a, size_type n = 1) |
|
|
91 | { |
|
|
92 | if (!is_simple_enough ()) |
|
|
93 | while (n--) |
|
|
94 | (*a++).~T (); |
|
|
95 | } |
|
|
96 | |
|
|
97 | static void cop_new (iterator a, iterator b) { new (a) T (*b); } |
|
|
98 | static void cop_set (iterator a, iterator b) { *a = *b ; } |
|
|
99 | |
|
|
100 | // these copy helpers actually use the copy constructor, not assignment |
|
|
101 | static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
102 | { |
|
|
103 | if (is_simple_enough ()) |
|
|
104 | memmove (dst, src, sizeof (T) * n); |
|
|
105 | else |
|
|
106 | while (n--) |
|
|
107 | op (dst++, src++); |
|
|
108 | } |
|
|
109 | |
|
|
110 | static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
111 | { |
|
|
112 | if (is_simple_enough ()) |
|
|
113 | memmove (dst, src, sizeof (T) * n); |
|
|
114 | else |
|
|
115 | while (n--) |
|
|
116 | op (dst + n, src + n); |
|
|
117 | } |
|
|
118 | |
|
|
119 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
120 | { |
|
|
121 | if (is_simple_enough ()) |
|
|
122 | memcpy (dst, src, sizeof (T) * n); |
|
|
123 | else |
|
|
124 | copy_lower (dst, src, n, op); |
|
|
125 | } |
|
|
126 | |
|
|
127 | static T *alloc (size_type n) ecb_cold |
|
|
128 | { |
|
|
129 | return (T *)::operator new ((size_t) (sizeof (T) * n)); |
|
|
130 | } |
|
|
131 | |
|
|
132 | void dealloc () ecb_cold |
|
|
133 | { |
|
|
134 | destruct (buf, sze); |
|
|
135 | ::operator delete (buf); |
|
|
136 | } |
|
|
137 | |
|
|
138 | size_type good_size (size_type n) ecb_cold |
|
|
139 | { |
|
|
140 | return n ? 2UL << ecb_ld32 (n) : 5; |
|
|
141 | } |
|
|
142 | |
|
|
143 | void ins (iterator where, size_type n) |
|
|
144 | { |
|
|
145 | size_type pos = where - begin (); |
|
|
146 | |
|
|
147 | if (ecb_expect_false (sze + n > res)) |
|
|
148 | { |
|
|
149 | res = good_size (sze + n); |
|
|
150 | |
|
|
151 | T *nbuf = alloc (res); |
|
|
152 | copy (nbuf, buf, sze, cop_new); |
|
|
153 | dealloc (); |
|
|
154 | buf = nbuf; |
|
|
155 | } |
|
|
156 | |
|
|
157 | construct (buf + sze, n); |
|
|
158 | copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); |
|
|
159 | sze += n; |
|
|
160 | } |
37 | |
161 | |
38 | public: |
162 | public: |
39 | const_iterator begin () const { return &_buf[0]; } |
|
|
40 | iterator begin () { return &_buf[0]; } |
|
|
41 | |
|
|
42 | const_iterator end () const { return &_buf[_last]; } |
|
|
43 | iterator end () { return &_buf[_last]; } |
|
|
44 | |
|
|
45 | size_type capacity () const { return _size; } |
163 | size_type capacity () const { return res; } |
46 | size_type size () const { return _last; } |
164 | size_type size () const { return sze; } |
|
|
165 | bool empty () const { return size () == 0; } |
47 | |
166 | |
48 | private: |
167 | const_iterator begin () const { return &buf [ 0]; } |
49 | static T *alloc (size_type n) |
168 | iterator begin () { return &buf [ 0]; } |
50 | { |
169 | const_iterator end () const { return &buf [sze ]; } |
51 | return (T *)::operator new ((size_t) (n * sizeof (T))); |
170 | iterator end () { return &buf [sze ]; } |
52 | } |
171 | const_reference front () const { return buf [ 0]; } |
|
|
172 | reference front () { return buf [ 0]; } |
|
|
173 | const_reference back () const { return buf [sze - 1]; } |
|
|
174 | reference back () { return buf [sze - 1]; } |
53 | |
175 | |
54 | static void dealloc (T *buf) |
176 | void reserve (size_type sz) |
55 | { |
177 | { |
56 | if (buf) |
178 | if (ecb_expect_true (sz <= res)) |
57 | ::operator delete (buf); |
179 | return; |
58 | } |
|
|
59 | |
180 | |
60 | size_type good_size (size_type n) |
181 | sz = good_size (sz); |
61 | { |
182 | T *nbuf = alloc (sz); |
62 | return max (n, _size ? _size * 2 : 5); |
|
|
63 | } |
|
|
64 | |
183 | |
65 | void reserve (iterator where, size_type n) |
184 | copy (nbuf, begin (), sze); |
|
|
185 | dealloc (); |
|
|
186 | |
|
|
187 | buf = nbuf; |
|
|
188 | res = sz; |
|
|
189 | } |
|
|
190 | |
|
|
191 | void resize (size_type sz) |
66 | { |
192 | { |
67 | if (_last + n <= _size) |
193 | reserve (sz); |
68 | memmove (where + n, where, (end () - where) * sizeof (T)); |
194 | |
|
|
195 | if (is_simple_enough ()) |
|
|
196 | sze = sz; |
69 | else |
197 | else |
70 | { |
198 | { |
71 | size_type sz = _last + n; |
199 | while (sze < sz) construct (buf + sze++); |
72 | sz = good_size (sz); |
200 | while (sze > sz) destruct (buf + --sze); |
73 | T *nbuf = alloc (sz); |
|
|
74 | |
|
|
75 | if (_buf) |
|
|
76 | { |
|
|
77 | memcpy (nbuf, begin (), (where - begin ()) * sizeof (T)); |
|
|
78 | memcpy (nbuf + (where - begin ()) + n, where, (end () - where) * sizeof (T)); |
|
|
79 | dealloc (_buf); |
|
|
80 | } |
|
|
81 | |
|
|
82 | _buf = nbuf; |
|
|
83 | _size = sz; |
|
|
84 | } |
201 | } |
85 | } |
202 | } |
86 | |
203 | |
87 | public: |
|
|
88 | void reserve (size_type sz) |
|
|
89 | { |
|
|
90 | if (_size < sz) |
|
|
91 | { |
|
|
92 | sz = good_size (sz); |
|
|
93 | T *nbuf = alloc (sz); |
|
|
94 | |
|
|
95 | if (_buf) |
|
|
96 | { |
|
|
97 | memcpy (nbuf, begin (), size () * sizeof (T)); |
|
|
98 | dealloc (_buf); |
|
|
99 | } |
|
|
100 | |
|
|
101 | _buf = nbuf; |
|
|
102 | _size = sz; |
|
|
103 | } |
|
|
104 | } |
|
|
105 | |
|
|
106 | simplevec () |
204 | simplevec () |
107 | : _last(0), _size(0), _buf(0) |
205 | : sze(0), res(0), buf(0) |
108 | { |
206 | { |
109 | } |
207 | } |
110 | |
208 | |
111 | simplevec (size_type n, const T& t = T ()) |
209 | simplevec (size_type n, const T &t = T ()) |
112 | : _last(0), _size(0), _buf(0) |
210 | : sze(0), res(0), buf(0) |
113 | { |
211 | { |
114 | insert (begin (), n, t); |
212 | insert (begin (), n, t); |
115 | } |
213 | } |
116 | |
214 | |
117 | simplevec (const_iterator first, const_iterator last) |
215 | simplevec (const_iterator first, const_iterator last) |
118 | : _last(0), _size(0), _buf(0) |
216 | : sze(0), res(0), buf(0) |
119 | { |
217 | { |
120 | insert (begin (), first, last); |
218 | insert (begin (), first, last); |
121 | } |
219 | } |
122 | |
220 | |
123 | simplevec (const simplevec<T> &v) |
221 | simplevec (const simplevec<T> &v) |
124 | : _last(0), _size(0), _buf(0) |
222 | : sze(0), res(0), buf(0) |
125 | { |
223 | { |
126 | reserve (v._last); |
224 | insert (begin (), v.begin (), v.end ()); |
127 | memcpy (_buf, v.begin (), v.size () * sizeof (T)); |
|
|
128 | _last = v._last; |
|
|
129 | } |
225 | } |
130 | |
226 | |
131 | simplevec<T> &operator= (const simplevec<T> &v) |
227 | simplevec<T> &operator= (const simplevec<T> &v) |
132 | { |
228 | { |
133 | if (this != &v) |
229 | swap (simplevec<T> (v)); |
134 | { |
|
|
135 | _last = 0; |
|
|
136 | reserve (v._last); |
|
|
137 | memcpy (_buf, v.begin (), v.size () * sizeof (T)); |
|
|
138 | _last = v._last; |
|
|
139 | } |
|
|
140 | |
|
|
141 | return *this; |
230 | return *this; |
142 | } |
231 | } |
143 | |
232 | |
144 | ~simplevec () |
233 | ~simplevec () |
145 | { |
234 | { |
146 | dealloc (_buf); |
235 | dealloc (); |
147 | } |
236 | } |
148 | |
237 | |
149 | const T &front () const { return _buf[ 0]; } |
238 | void swap (simplevec<T> &t) |
150 | T &front () { return _buf[ 0]; } |
|
|
151 | const T &back () const { return _buf[_last-1]; } |
|
|
152 | T &back () { return _buf[_last-1]; } |
|
|
153 | |
|
|
154 | bool empty () const |
|
|
155 | { |
239 | { |
156 | return _last == 0; |
240 | ::swap (sze, t.sze); |
|
|
241 | ::swap (res, t.res); |
|
|
242 | ::swap (buf, t.buf); |
157 | } |
243 | } |
158 | |
244 | |
159 | void clear () |
245 | void clear () |
160 | { |
246 | { |
|
|
247 | destruct (buf, sze); |
161 | _last = 0; |
248 | sze = 0; |
162 | } |
249 | } |
163 | |
250 | |
164 | void push_back (const T &t) |
251 | void push_back (const T &t) |
165 | { |
252 | { |
166 | reserve (_last+1); |
253 | reserve (sze + 1); |
167 | *end () = t; |
254 | new (buf + sze++) T (t); |
168 | ++_last; |
|
|
169 | } |
|
|
170 | |
|
|
171 | void push_back (T &t) |
|
|
172 | { |
|
|
173 | reserve (_last+1); |
|
|
174 | *end () = t; |
|
|
175 | ++_last; |
|
|
176 | } |
255 | } |
177 | |
256 | |
178 | void pop_back () |
257 | void pop_back () |
179 | { |
258 | { |
180 | --_last; |
259 | destruct (buf + --sze); |
181 | } |
260 | } |
182 | |
261 | |
183 | const T &operator [](size_type idx) const { return _buf[idx]; } |
262 | const T &operator [](size_type idx) const { return buf[idx]; } |
184 | T &operator [](size_type idx) { return _buf[idx]; } |
263 | T &operator [](size_type idx) { return buf[idx]; } |
185 | |
264 | |
186 | iterator insert (iterator pos, const T &t) |
265 | iterator insert (iterator pos, const T &t) |
187 | { |
266 | { |
188 | long at = pos - begin (); |
267 | size_type at = pos - begin (); |
|
|
268 | |
189 | reserve (pos, 1); |
269 | ins (pos, 1); |
190 | pos = begin () + at; |
270 | buf [at] = t; |
191 | *pos = t; |
271 | |
192 | ++_last; |
272 | return buf + at; |
193 | return pos; |
|
|
194 | } |
273 | } |
195 | |
274 | |
196 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
275 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
197 | { |
276 | { |
198 | long n = last - first; |
277 | size_type n = last - first; |
199 | long at = pos - begin (); |
278 | size_type at = pos - begin (); |
200 | |
279 | |
201 | if (n > 0) |
280 | ins (pos, n); |
202 | { |
281 | copy (buf + at, first, n, cop_set); |
203 | reserve (pos, n); |
|
|
204 | pos = begin ()+at; |
|
|
205 | memcpy (pos, first, (last - first) * sizeof (T)); |
|
|
206 | _last += n; |
|
|
207 | } |
|
|
208 | |
282 | |
209 | return pos; |
283 | return buf + at; |
210 | } |
284 | } |
211 | |
285 | |
212 | iterator insert (iterator pos, size_type n, const T &t) |
286 | iterator insert (iterator pos, size_type n, const T &t) |
213 | { |
287 | { |
214 | long at = pos - begin (); |
288 | size_type at = pos - begin (); |
215 | |
289 | |
216 | if (n > 0) |
290 | ins (pos, n); |
217 | { |
|
|
218 | reserve (pos, n); |
|
|
219 | pos = begin () + at; |
|
|
220 | for (int i = 0; i < n; ++i) |
|
|
221 | pos[i] = t; |
|
|
222 | _last += n; |
|
|
223 | } |
|
|
224 | |
291 | |
225 | return pos; |
292 | for (iterator i = buf + at; n--; ) |
|
|
293 | *i++ = t; |
|
|
294 | |
|
|
295 | return buf + at; |
226 | } |
296 | } |
227 | |
297 | |
228 | void erase (iterator first, iterator last) |
298 | void erase (iterator first, iterator last) |
229 | { |
299 | { |
230 | if (last != first) |
300 | size_t n = last - first; |
231 | { |
301 | |
232 | memmove (first, last, (end () - last) * sizeof (T)); |
302 | copy_lower (last, first, end () - last, cop_set); |
233 | _last -= last - first; |
303 | sze -= n; |
234 | } |
304 | destruct (buf + sze, n); |
235 | } |
305 | } |
236 | |
306 | |
237 | void erase (iterator pos) |
307 | void erase (iterator pos) |
238 | { |
308 | { |
239 | if (pos != end ()) |
309 | if (pos != end ()) |
240 | { |
310 | erase (pos, pos + 1); |
241 | memmove (pos, pos + 1, (end () - (pos + 1)) * sizeof (T)); |
|
|
242 | --_last; |
|
|
243 | } |
|
|
244 | } |
|
|
245 | |
|
|
246 | void swap (simplevec<T> &t) |
|
|
247 | { |
|
|
248 | ::swap (_last, t._last); |
|
|
249 | ::swap (_size, t._size); |
|
|
250 | ::swap (_buf, t._buf); |
|
|
251 | } |
311 | } |
252 | }; |
312 | }; |
253 | |
313 | |
254 | template<class T> |
314 | template<class T> |
255 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
315 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |