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.24 by root, Mon Oct 27 14:57:30 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 29// original version taken from MICO, but this has been completely rewritten
30// known limitations w.r.t. std::vector 30// known limitations w.r.t. std::vector
31// - many methods missing 31// - many methods missing
32// - no error checking, no exceptions thrown 32// - no error checking, no exceptions thrown (e.g. at())
33// - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements 33// - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements
34// - no allocator support 34// - no allocator support
35// - we don't really care about const correctness, but we try 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 36// - we don't care about namespaces and stupid macros the user might define
37// - no bool specialisation
37template<class T> 38template<class T>
38struct simplevec 39struct simplevec
39{ 40{
40#if ESTL_BIG_VECTOR 41#if ESTL_BIG_VECTOR
41 // shoudl use size_t/ssize_t, but that's not portable enough for us 42 // should use size_t/ssize_t, but that's not portable enough for us
42 typedef unsigned long size_type; 43 typedef unsigned long size_type;
43 typedef long difference_type; 44 typedef long difference_type;
44#else 45#else
45 typedef uint32_t size_type; 46 typedef uint32_t size_type;
46 typedef int32_t difference_type; 47 typedef int32_t difference_type;
62 63
63 // we shamelessly optimise for "simple" types. everything 64 // we shamelessly optimise for "simple" types. everything
64 // "not simple enough" will use the slow path. 65 // "not simple enough" will use the slow path.
65 static bool is_simple_enough () 66 static bool is_simple_enough ()
66 { 67 {
67 return 1; // we are not there yet 68 #if ECB_CPP11
68 #if __cplusplus >= 201103L
69 return std::is_trivially_assignable<T, T>::value 69 return std::is_trivially_assignable<T, T>::value
70 && std::is_trivially_constructable<T>::value 70 && std::is_trivially_constructable<T>::value
71 && std::is_trivially_copyable<T>::value 71 && std::is_trivially_copyable<T>::value
72 && std::is_trivially_destructible<T>::value; 72 && std::is_trivially_destructible<T>::value;
73 #elif ECB_GCC_VERSION(4,4) 73 #elif ECB_GCC_VERSION(4,4)
82 82
83 static void construct (iterator a, size_type n = 1) 83 static void construct (iterator a, size_type n = 1)
84 { 84 {
85 if (!is_simple_enough ()) 85 if (!is_simple_enough ())
86 while (n--) 86 while (n--)
87 new (*a++) T (); 87 new (a++) T ();
88 } 88 }
89 89
90 static void destruct (iterator a, size_type n = 1) 90 static void destruct (iterator a, size_type n = 1)
91 { 91 {
92 if (!is_simple_enough ()) 92 if (!is_simple_enough ())
97 template<class I> 97 template<class I>
98 static void cop_new (iterator a, I b) { new (a) T (*b); } 98 static void cop_new (iterator a, I b) { new (a) T (*b); }
99 template<class I> 99 template<class I>
100 static void cop_set (iterator a, I b) { *a = *b ; } 100 static void cop_set (iterator a, I b) { *a = *b ; }
101 101
102 // MUST copy forwards
102 template<class I> 103 template<class I>
103 static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new) 104 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I))
104 { 105 {
105 while (n--) 106 while (n--)
106 op (dst++, src++); 107 op (dst++, src++);
107 } 108 }
108 109
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) 110 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator))
133 { 111 {
134 if (is_simple_enough ()) 112 if (is_simple_enough ())
135 memcpy (dst, src, sizeof (T) * n); 113 memcpy (dst, src, sizeof (T) * n);
136 else 114 else
137 copy<iterator> (dst, src, n, op); 115 copy<iterator> (dst, src, n, op);
166 dealloc (); 144 dealloc ();
167 buf = nbuf; 145 buf = nbuf;
168 } 146 }
169 147
170 construct (buf + sze, n); 148 construct (buf + sze, n);
171 copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); 149
150 iterator src = buf + pos;
151 if (is_simple_enough ())
152 memmove (src + n, src, sizeof (T) * (sze - pos));
153 else
154 for (size_type i = sze - pos; i--; )
155 cop_set (src + n + i, src + i);
156
172 sze += n; 157 sze += n;
173 } 158 }
174 159
175public: 160public:
176 size_type capacity () const { return res; } 161 size_type capacity () const { return res; }
197 return; 182 return;
198 183
199 sz = good_size (sz); 184 sz = good_size (sz);
200 T *nbuf = alloc (sz); 185 T *nbuf = alloc (sz);
201 186
202 copy (nbuf, begin (), sze); 187 copy (nbuf, begin (), sze, cop_new);
203 dealloc (); 188 dealloc ();
204 189
205 buf = nbuf; 190 buf = nbuf;
206 res = sz; 191 res = sz;
207 } 192 }
236 template<class I> 221 template<class I>
237 simplevec (I first, I last) 222 simplevec (I first, I last)
238 { 223 {
239 sze = res = last - first; 224 sze = res = last - first;
240 buf = alloc (sze); 225 buf = alloc (sze);
241 copy (buf, first, sze); 226 copy (buf, first, sze, cop_new);
242 } 227 }
243 228
244 simplevec (const simplevec<T> &v) 229 simplevec (const simplevec<T> &v)
245 : sze(0), res(0), buf(0) 230 : sze(0), res(0), buf(0)
246 { 231 {
247 sze = res = v.size (); 232 sze = res = v.size ();
248 buf = alloc (sze); 233 buf = alloc (sze);
249 copy (buf, v.begin (), sze); 234 copy (buf, v.begin (), sze, cop_new);
250 } 235 }
251 236
252 ~simplevec () 237 ~simplevec ()
253 { 238 {
254 dealloc (); 239 dealloc ();
333 *i++ = t; 318 *i++ = t;
334 319
335 return buf + at; 320 return buf + at;
336 } 321 }
337 322
338 void erase (iterator first, iterator last) 323 iterator erase (iterator first, iterator last)
339 { 324 {
340 size_t n = last - first; 325 size_type n = last - first;
326 size_type c = end () - last;
341 327
342 copy_lower (last, first, end () - last, cop_set); 328 if (is_simple_enough ())
329 memmove (first, last, sizeof (T) * c);
330 else
331 copy (first, last, c, cop_set);
332
343 sze -= n; 333 sze -= n;
344 destruct (buf + sze, n); 334 destruct (buf + sze, n);
345 }
346 335
336 return first;
337 }
338
347 void erase (iterator pos) 339 iterator erase (iterator pos)
348 { 340 {
349 if (pos != end ()) 341 if (pos != end ())
350 erase (pos, pos + 1); 342 erase (pos, pos + 1);
343
344 return pos;
351 } 345 }
352}; 346};
353 347
354template<class T> 348template<class T>
355bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) 349bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines