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.11 by root, Sat May 19 01:56:09 2012 UTC vs.
Revision 1.28 by sf-exg, Mon Nov 10 11:32:00 2014 UTC

20 return first; 20 return first;
21} 21}
22 22
23#include <new> 23#include <new>
24 24
25#if __cplusplus >= 201103L 25#if ECB_CPP11
26 #include <type_traits> 26 #include <type_traits>
27#endif 27#endif
28 28
29// original version taken from MICO, but this has been completely rewritten 29namespace estl
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
37template<class T>
38struct simplevec
39{ 30{
40#if ESTL_BIG_VECTOR 31#if ESTL_LARGE_MEMORY_MODEL
41 // shoudl use size_t/ssize_t, but that's not portable enough for us 32 // should use size_t/ssize_t, but that's not portable enough for us
42 typedef unsigned long size_type; 33 typedef unsigned long size_type;
43 typedef long difference_type; 34 typedef long difference_type;
44#else 35#else
45 typedef uint32_t size_type; 36 typedef uint32_t size_type;
46 typedef int32_t difference_type; 37 typedef int32_t difference_type;
47#endif 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
105template<class T>
106struct simplevec
107{
108 typedef estl::size_type size_type;
48 109
49 typedef T value_type; 110 typedef T value_type;
50 typedef T *iterator; 111 typedef T *iterator;
51 typedef const T *const_iterator; 112 typedef const T *const_iterator;
52 typedef T *pointer; 113 typedef T *pointer;
62 123
63 // we shamelessly optimise for "simple" types. everything 124 // we shamelessly optimise for "simple" types. everything
64 // "not simple enough" will use the slow path. 125 // "not simple enough" will use the slow path.
65 static bool is_simple_enough () 126 static bool is_simple_enough ()
66 { 127 {
67 return 1; // we are not there yet 128 #if ECB_CPP11
68 #if __cplusplus >= 201103L
69 return std::is_trivially_assignable<T, T>::value 129 return std::is_trivially_assignable<T, T>::value
70 && std::is_trivially_constructable<T>::value 130 && std::is_trivially_constructible<T>::value
71 && std::is_trivially_copyable<T>::value 131 && std::is_trivially_copyable<T>::value
72 && std::is_trivially_destructible<T>::value; 132 && std::is_trivially_destructible<T>::value;
73 #elif ECB_GCC_VERSION(4,4) 133 #elif ECB_GCC_VERSION(4,4) || ECB_CLANG_VERSION(2,8)
74 return __has_trivial_assign (T) 134 return __has_trivial_assign (T)
75 && __has_trivial_constructor (T) 135 && __has_trivial_constructor (T)
76 && __has_trivial_copy (T) 136 && __has_trivial_copy (T)
77 && __has_trivial_destructor (T); 137 && __has_trivial_destructor (T);
78 #else 138 #else
82 142
83 static void construct (iterator a, size_type n = 1) 143 static void construct (iterator a, size_type n = 1)
84 { 144 {
85 if (!is_simple_enough ()) 145 if (!is_simple_enough ())
86 while (n--) 146 while (n--)
87 new (*a++) T (); 147 new (a++) T ();
88 } 148 }
89 149
90 static void destruct (iterator a, size_type n = 1) 150 static void destruct (iterator a, size_type n = 1)
91 { 151 {
92 if (!is_simple_enough ()) 152 if (!is_simple_enough ())
97 template<class I> 157 template<class I>
98 static void cop_new (iterator a, I b) { new (a) T (*b); } 158 static void cop_new (iterator a, I b) { new (a) T (*b); }
99 template<class I> 159 template<class I>
100 static void cop_set (iterator a, I b) { *a = *b ; } 160 static void cop_set (iterator a, I b) { *a = *b ; }
101 161
162 // MUST copy forwards
102 template<class I> 163 template<class I>
103 static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new) 164 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I))
104 { 165 {
105 while (n--) 166 while (n--)
106 op (dst++, src++); 167 op (dst++, src++);
107 } 168 }
108 169
109 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
110 {
111 if (is_simple_enough ())
112 memmove (dst, src, sizeof (T) * n);
113 else
114 copy_lower<iterator> (dst, src, n, cop_new);
115 }
116
117 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
118 {
119 if (is_simple_enough ())
120 memmove (dst, src, sizeof (T) * n);
121 else
122 while (n--)
123 op (dst + n, src + n);
124 }
125
126 template<class I>
127 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new)
128 {
129 copy_lower<I> (dst, src, n, op);
130 }
131
132 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 170 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator))
133 { 171 {
134 if (is_simple_enough ()) 172 if (is_simple_enough ())
135 memcpy (dst, src, sizeof (T) * n); 173 memcpy (dst, src, sizeof (T) * n);
136 else 174 else
137 copy<iterator> (dst, src, n, op); 175 copy<iterator> (dst, src, n, op);
166 dealloc (); 204 dealloc ();
167 buf = nbuf; 205 buf = nbuf;
168 } 206 }
169 207
170 construct (buf + sze, n); 208 construct (buf + sze, n);
171 copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); 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
172 sze += n; 217 sze += n;
173 } 218 }
174 219
175public: 220public:
176 size_type capacity () const { return res; } 221 size_type capacity () const { return res; }
197 return; 242 return;
198 243
199 sz = good_size (sz); 244 sz = good_size (sz);
200 T *nbuf = alloc (sz); 245 T *nbuf = alloc (sz);
201 246
202 copy (nbuf, begin (), sze); 247 copy (nbuf, begin (), sze, cop_new);
203 dealloc (); 248 dealloc ();
204 249
205 buf = nbuf; 250 buf = nbuf;
206 res = sz; 251 res = sz;
207 } 252 }
231 276
232 while (n--) 277 while (n--)
233 new (buf + n) T (t); 278 new (buf + n) T (t);
234 } 279 }
235 280
236 template<class I> 281 simplevec (const_iterator first, const_iterator last)
237 simplevec (I first, I last)
238 { 282 {
239 sze = res = last - first; 283 sze = res = last - first;
240 buf = alloc (sze); 284 buf = alloc (sze);
241 copy (buf, first, sze); 285 copy (buf, first, sze, cop_new);
242 } 286 }
243 287
244 simplevec (const simplevec<T> &v) 288 simplevec (const simplevec<T> &v)
245 : sze(0), res(0), buf(0) 289 : sze(0), res(0), buf(0)
246 { 290 {
247 sze = res = v.size (); 291 sze = res = v.size ();
248 buf = alloc (sze); 292 buf = alloc (sze);
249 copy (buf, v.begin (), sze); 293 copy (buf, v.begin (), sze, cop_new);
250 } 294 }
251 295
252 ~simplevec () 296 ~simplevec ()
253 { 297 {
254 dealloc (); 298 dealloc ();
282 reference operator [](size_type idx) { return buf[idx]; } 326 reference operator [](size_type idx) { return buf[idx]; }
283 327
284 const_reference at (size_type idx) const { return buf [idx]; } 328 const_reference at (size_type idx) const { return buf [idx]; }
285 reference at (size_type idx) { return buf [idx]; } 329 reference at (size_type idx) { return buf [idx]; }
286 330
287 template<class I> 331 void assign (const_iterator first, const_iterator last)
288 void assign (I first, I last)
289 { 332 {
290 swap (simplevec<T> (first, last)); 333 simplevec<T> v (first, last);
334 swap (v);
291 } 335 }
292 336
293 void assign (size_type n, const T &t) 337 void assign (size_type n, const T &t)
294 { 338 {
295 swap (simplevec<T> (n, t)); 339 simplevec<T> v (n, t);
340 swap (v);
296 } 341 }
297 342
298 simplevec<T> &operator= (const simplevec<T> &v) 343 simplevec<T> &operator= (const simplevec<T> &v)
299 { 344 {
300 assign (v.begin (), v.end ()); 345 assign (v.begin (), v.end ());
309 buf [at] = t; 354 buf [at] = t;
310 355
311 return buf + at; 356 return buf + at;
312 } 357 }
313 358
314 template<class I>
315 iterator insert (iterator pos, I first, I last) 359 iterator insert (iterator pos, const_iterator first, const_iterator last)
316 { 360 {
317 size_type n = last - first; 361 size_type n = last - first;
318 size_type at = pos - begin (); 362 size_type at = pos - begin ();
319 363
320 ins (pos, n); 364 ins (pos, n);
333 *i++ = t; 377 *i++ = t;
334 378
335 return buf + at; 379 return buf + at;
336 } 380 }
337 381
338 void erase (iterator first, iterator last) 382 iterator erase (iterator first, iterator last)
339 { 383 {
340 size_t n = last - first; 384 size_type n = last - first;
385 size_type c = end () - last;
341 386
342 copy_lower (last, first, end () - last, cop_set); 387 if (is_simple_enough ())
388 memmove (first, last, sizeof (T) * c);
389 else
390 copy (first, last, c, cop_set);
391
343 sze -= n; 392 sze -= n;
344 destruct (buf + sze, n); 393 destruct (buf + sze, n);
345 }
346 394
395 return first;
396 }
397
347 void erase (iterator pos) 398 iterator erase (iterator pos)
348 { 399 {
349 if (pos != end ()) 400 if (pos != end ())
350 erase (pos, pos + 1); 401 erase (pos, pos + 1);
402
403 return pos;
351 } 404 }
352}; 405};
353 406
354template<class T> 407template<class T>
355bool 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