… | |
… | |
15 | while (first != last && *first != value) |
15 | while (first != last && *first != value) |
16 | ++first; |
16 | ++first; |
17 | |
17 | |
18 | return first; |
18 | return first; |
19 | } |
19 | } |
|
|
20 | |
|
|
21 | // see ecb.h for details |
|
|
22 | #ifndef ECB_GCC_VERSION |
|
|
23 | #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ |
|
|
24 | #define ECB_GCC_VERSION(major,minor) 0 |
|
|
25 | #else |
|
|
26 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
|
|
27 | #endif |
|
|
28 | #endif |
|
|
29 | |
|
|
30 | #include <new> |
|
|
31 | |
|
|
32 | #if __cplusplus >= 201103L |
|
|
33 | #include <type_traits> |
|
|
34 | #endif |
20 | |
35 | |
21 | /* simplevec taken (and heavily modified), from: |
36 | /* simplevec taken (and heavily modified), from: |
22 | * |
37 | * |
23 | * MICO --- a free CORBA implementation |
38 | * MICO --- a free CORBA implementation |
24 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
39 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
… | |
… | |
29 | { |
44 | { |
30 | typedef T *iterator; |
45 | typedef T *iterator; |
31 | typedef const T *const_iterator; |
46 | typedef const T *const_iterator; |
32 | typedef unsigned long size_type; |
47 | typedef unsigned long size_type; |
33 | |
48 | |
|
|
49 | static bool is_simple_enough () |
|
|
50 | { |
|
|
51 | #if __cplusplus >= 201103L |
|
|
52 | return std::is_trivially_assignable<T, T>::value |
|
|
53 | && std::is_trivially_constructable<T>::value |
|
|
54 | && std::is_trivially_copyable<T>::value |
|
|
55 | && std::is_trivially_destructible<T>::value; |
|
|
56 | #elif ECB_GCC_VERSION(4,4) |
|
|
57 | return __has_trivial_assign (T) |
|
|
58 | && __has_trivial_constructor (T) |
|
|
59 | && __has_trivial_copy (T) |
|
|
60 | && __has_trivial_destructor (T); |
|
|
61 | #else |
|
|
62 | return 0; |
|
|
63 | #endif |
|
|
64 | } |
|
|
65 | |
34 | private: |
66 | private: |
35 | size_type _last, _size; |
67 | size_type _last, _size; |
36 | T *_buf; |
68 | T *_buf; |
37 | |
69 | |
38 | public: |
70 | public: |
… | |
… | |
49 | static T *alloc (size_type n) |
81 | static T *alloc (size_type n) |
50 | { |
82 | { |
51 | return (T *)::operator new ((size_t) (n * sizeof (T))); |
83 | return (T *)::operator new ((size_t) (n * sizeof (T))); |
52 | } |
84 | } |
53 | |
85 | |
54 | static void dealloc (T *buf) |
86 | void dealloc () |
55 | { |
87 | { |
56 | if (buf) |
88 | if (!is_simple_enough ()) |
|
|
89 | for (size_type i = 0; i < _last; ++i) |
|
|
90 | _buf [i].~T (); |
|
|
91 | |
57 | ::operator delete (buf); |
92 | ::operator delete (_buf); |
58 | } |
93 | } |
59 | |
94 | |
60 | size_type good_size (size_type n) |
95 | size_type good_size (size_type n) |
61 | { |
96 | { |
62 | return max (n, _size ? _size * 2 : 5); |
97 | return max (n, _size ? _size * 2 : 5); |
63 | } |
98 | } |
64 | |
99 | |
|
|
100 | // these copy helpers actually use the copy constructor, not assignment |
|
|
101 | static void copy_lower (iterator dst, iterator src, size_type n) |
|
|
102 | { |
|
|
103 | if (is_simple_enough ()) |
|
|
104 | memmove (dst, src, sizeof (T) * n); |
|
|
105 | else |
|
|
106 | while (n--) |
|
|
107 | new (dst++) T (*src++); |
|
|
108 | } |
|
|
109 | |
|
|
110 | static void copy_higher (iterator dst, iterator src, size_type n) |
|
|
111 | { |
|
|
112 | if (is_simple_enough ()) |
|
|
113 | memmove (dst, src, sizeof (T) * n); |
|
|
114 | else |
|
|
115 | while (n--) |
|
|
116 | new (dst + n) T (*(src + n)); |
|
|
117 | } |
|
|
118 | |
|
|
119 | static void copy (iterator dst, iterator src, size_type n) |
|
|
120 | { |
|
|
121 | if (is_simple_enough ()) |
|
|
122 | memcpy (dst, src, sizeof (T) * n); |
|
|
123 | else |
|
|
124 | copy_lower (dst, src, n); |
|
|
125 | } |
|
|
126 | |
65 | void reserve (iterator where, size_type n) |
127 | void ins (iterator where, size_type n) |
66 | { |
128 | { |
67 | if (_last + n <= _size) |
129 | if (_last + n <= _size) |
68 | memmove (where + n, where, (end () - where) * sizeof (T)); |
130 | copy_higher (where + n, where, end () - where); |
69 | else |
131 | else |
70 | { |
132 | { |
71 | size_type sz = _last + n; |
133 | size_type sz = _last + n; |
72 | sz = good_size (sz); |
134 | sz = good_size (sz); |
73 | T *nbuf = alloc (sz); |
135 | T *nbuf = alloc (sz); |
74 | |
136 | |
75 | if (_buf) |
137 | if (_buf) |
76 | { |
138 | { |
77 | memcpy (nbuf, begin (), (where - begin ()) * sizeof (T)); |
139 | copy (nbuf, begin (), where - begin ()); |
78 | memcpy (nbuf + (where - begin ()) + n, where, (end () - where) * sizeof (T)); |
140 | copy (nbuf + (where - begin ()) + n, where, end () - where); |
79 | dealloc (_buf); |
141 | dealloc (); |
80 | } |
142 | } |
81 | |
143 | |
82 | _buf = nbuf; |
144 | _buf = nbuf; |
83 | _size = sz; |
145 | _size = sz; |
84 | } |
146 | } |
… | |
… | |
92 | sz = good_size (sz); |
154 | sz = good_size (sz); |
93 | T *nbuf = alloc (sz); |
155 | T *nbuf = alloc (sz); |
94 | |
156 | |
95 | if (_buf) |
157 | if (_buf) |
96 | { |
158 | { |
97 | memcpy (nbuf, begin (), size () * sizeof (T)); |
159 | copy (nbuf, begin (), _last); |
98 | dealloc (_buf); |
160 | dealloc (); |
99 | } |
161 | } |
100 | |
162 | |
101 | _buf = nbuf; |
163 | _buf = nbuf; |
102 | _size = sz; |
164 | _size = sz; |
103 | } |
165 | } |
104 | } |
166 | } |
105 | |
167 | |
106 | void resize (size_type sz) |
168 | void resize (size_type sz) |
107 | { |
169 | { |
108 | reserve (sz); |
170 | reserve (sz); |
|
|
171 | |
|
|
172 | if (is_simple_enough ()) |
109 | _last = sz; |
173 | _last = sz; |
|
|
174 | else |
|
|
175 | { |
|
|
176 | while (_last < sz) |
|
|
177 | new (_buf + _last++) T (); |
|
|
178 | while (_last > sz) |
|
|
179 | _buf [--_last].~T (); |
|
|
180 | } |
110 | } |
181 | } |
111 | |
182 | |
112 | simplevec () |
183 | simplevec () |
113 | : _last(0), _size(0), _buf(0) |
184 | : _last(0), _size(0), _buf(0) |
114 | { |
185 | { |
… | |
… | |
127 | } |
198 | } |
128 | |
199 | |
129 | simplevec (const simplevec<T> &v) |
200 | simplevec (const simplevec<T> &v) |
130 | : _last(0), _size(0), _buf(0) |
201 | : _last(0), _size(0), _buf(0) |
131 | { |
202 | { |
132 | reserve (v._last); |
203 | insert (begin (), v.begin (), v.end ()); |
133 | memcpy (_buf, v.begin (), v.size () * sizeof (T)); |
|
|
134 | _last = v._last; |
|
|
135 | } |
204 | } |
136 | |
205 | |
137 | simplevec<T> &operator= (const simplevec<T> &v) |
206 | simplevec<T> &operator= (const simplevec<T> &v) |
138 | { |
207 | { |
139 | if (this != &v) |
208 | if (this != &v) |
140 | { |
209 | { |
|
|
210 | |
|
|
211 | dealloc (); |
|
|
212 | _size = 0; |
|
|
213 | _buf = 0; |
141 | _last = 0; |
214 | _last = 0; |
142 | reserve (v._last); |
215 | reserve (v._last); |
|
|
216 | |
143 | memcpy (_buf, v.begin (), v.size () * sizeof (T)); |
217 | copy (_buf, v.begin (), v.size ()); |
144 | _last = v._last; |
218 | _last = v._last; |
145 | } |
219 | } |
146 | |
220 | |
147 | return *this; |
221 | return *this; |
148 | } |
222 | } |
149 | |
223 | |
150 | ~simplevec () |
224 | ~simplevec () |
151 | { |
225 | { |
152 | dealloc (_buf); |
226 | dealloc (); |
153 | } |
227 | } |
154 | |
228 | |
155 | const T &front () const { return _buf[ 0]; } |
229 | const T &front () const { return _buf[ 0]; } |
156 | T &front () { return _buf[ 0]; } |
230 | T &front () { return _buf[ 0]; } |
157 | const T &back () const { return _buf[_last-1]; } |
231 | const T &back () const { return _buf[_last-1]; } |
… | |
… | |
167 | _last = 0; |
241 | _last = 0; |
168 | } |
242 | } |
169 | |
243 | |
170 | void push_back (const T &t) |
244 | void push_back (const T &t) |
171 | { |
245 | { |
172 | reserve (_last+1); |
246 | reserve (_last + 1); |
173 | *end () = t; |
247 | new (_buf + _last++) T (t); |
174 | ++_last; |
|
|
175 | } |
|
|
176 | |
|
|
177 | void push_back (T &t) |
|
|
178 | { |
|
|
179 | reserve (_last+1); |
|
|
180 | *end () = t; |
|
|
181 | ++_last; |
|
|
182 | } |
248 | } |
183 | |
249 | |
184 | void pop_back () |
250 | void pop_back () |
185 | { |
251 | { |
186 | --_last; |
252 | --_last; |
… | |
… | |
189 | const T &operator [](size_type idx) const { return _buf[idx]; } |
255 | const T &operator [](size_type idx) const { return _buf[idx]; } |
190 | T &operator [](size_type idx) { return _buf[idx]; } |
256 | T &operator [](size_type idx) { return _buf[idx]; } |
191 | |
257 | |
192 | iterator insert (iterator pos, const T &t) |
258 | iterator insert (iterator pos, const T &t) |
193 | { |
259 | { |
194 | long at = pos - begin (); |
260 | size_type at = pos - begin (); |
195 | reserve (pos, 1); |
261 | ins (pos, 1); |
196 | pos = begin () + at; |
262 | pos = begin () + at; |
197 | *pos = t; |
263 | *pos = t; |
198 | ++_last; |
264 | ++_last; |
199 | return pos; |
265 | return pos; |
200 | } |
266 | } |
201 | |
267 | |
202 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
268 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
203 | { |
269 | { |
204 | long n = last - first; |
270 | size_type n = last - first; |
205 | long at = pos - begin (); |
271 | size_type at = pos - begin (); |
206 | |
272 | |
207 | if (n > 0) |
273 | if (n > 0) |
208 | { |
274 | { |
209 | reserve (pos, n); |
275 | ins (pos, n); |
|
|
276 | _last += n; |
|
|
277 | copy (pos, first, n); |
|
|
278 | } |
|
|
279 | |
|
|
280 | return pos; |
|
|
281 | } |
|
|
282 | |
|
|
283 | iterator insert (iterator pos, size_type n, const T &t) |
|
|
284 | { |
|
|
285 | size_type at = pos - begin (); |
|
|
286 | |
|
|
287 | if (n > 0) |
|
|
288 | { |
|
|
289 | ins (pos, n); |
210 | pos = begin () + at; |
290 | pos = begin () + at; |
211 | memcpy (pos, first, (last - first) * sizeof (T)); |
|
|
212 | _last += n; |
|
|
213 | } |
|
|
214 | |
|
|
215 | return pos; |
|
|
216 | } |
|
|
217 | |
|
|
218 | iterator insert (iterator pos, size_type n, const T &t) |
|
|
219 | { |
|
|
220 | long at = pos - begin (); |
|
|
221 | |
|
|
222 | if (n > 0) |
|
|
223 | { |
|
|
224 | reserve (pos, n); |
|
|
225 | pos = begin () + at; |
|
|
226 | for (int i = 0; i < n; ++i) |
291 | for (size_type i = 0; i < n; ++i) |
227 | pos[i] = t; |
292 | pos[i] = t; |
228 | _last += n; |
293 | _last += n; |
229 | } |
294 | } |
230 | |
295 | |
231 | return pos; |
296 | return pos; |
… | |
… | |
233 | |
298 | |
234 | void erase (iterator first, iterator last) |
299 | void erase (iterator first, iterator last) |
235 | { |
300 | { |
236 | if (last != first) |
301 | if (last != first) |
237 | { |
302 | { |
238 | memmove (first, last, (end () - last) * sizeof (T)); |
303 | if (!is_simple_enough ()) |
|
|
304 | for (iterator i = first; i < last; ++i) |
|
|
305 | i->~T (); |
|
|
306 | |
|
|
307 | copy_lower (last, first, end () - last); |
|
|
308 | |
239 | _last -= last - first; |
309 | _last -= last - first; |
240 | } |
310 | } |
241 | } |
311 | } |
242 | |
312 | |
243 | void erase (iterator pos) |
313 | void erase (iterator pos) |
244 | { |
314 | { |
245 | if (pos != end ()) |
315 | if (pos != end ()) |
246 | { |
316 | erase (pos, pos + 1); |
247 | memmove (pos, pos + 1, (end () - (pos + 1)) * sizeof (T)); |
|
|
248 | --_last; |
|
|
249 | } |
|
|
250 | } |
317 | } |
251 | |
318 | |
252 | void swap (simplevec<T> &t) |
319 | void swap (simplevec<T> &t) |
253 | { |
320 | { |
254 | ::swap (_last, t._last); |
321 | ::swap (_last, t._last); |
255 | ::swap (_size, t._size); |
322 | ::swap (_size, t._size); |
256 | ::swap (_buf, t._buf); |
323 | ::swap (_buf , t._buf ); |
257 | } |
324 | } |
258 | }; |
325 | }; |
259 | |
326 | |
260 | template<class T> |
327 | template<class T> |
261 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
328 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |