ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
(Generate patch)

Comparing libptytty/src/estl.h (file contents):
Revision 1.8 by root, Fri May 18 00:10:51 2012 UTC vs.
Revision 1.28 by sf-exg, Mon Nov 10 11:32:00 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines