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.14 by root, Sat May 19 02:13:59 2012 UTC vs.
Revision 1.23 by sf-exg, Fri Oct 24 13:44:09 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
63 63
64 // we shamelessly optimise for "simple" types. everything 64 // we shamelessly optimise for "simple" types. everything
65 // "not simple enough" will use the slow path. 65 // "not simple enough" will use the slow path.
66 static bool is_simple_enough () 66 static bool is_simple_enough ()
67 { 67 {
68 return 1; // we are not there yet 68 #if ECB_CPP11
69 #if __cplusplus >= 201103L
70 return std::is_trivially_assignable<T, T>::value 69 return std::is_trivially_assignable<T, T>::value
71 && std::is_trivially_constructable<T>::value 70 && std::is_trivially_constructable<T>::value
72 && std::is_trivially_copyable<T>::value 71 && std::is_trivially_copyable<T>::value
73 && std::is_trivially_destructible<T>::value; 72 && std::is_trivially_destructible<T>::value;
74 #elif ECB_GCC_VERSION(4,4) 73 #elif ECB_GCC_VERSION(4,4)
83 82
84 static void construct (iterator a, size_type n = 1) 83 static void construct (iterator a, size_type n = 1)
85 { 84 {
86 if (!is_simple_enough ()) 85 if (!is_simple_enough ())
87 while (n--) 86 while (n--)
88 new (*a++) T (); 87 new (a++) T ();
89 } 88 }
90 89
91 static void destruct (iterator a, size_type n = 1) 90 static void destruct (iterator a, size_type n = 1)
92 { 91 {
93 if (!is_simple_enough ()) 92 if (!is_simple_enough ())
100 template<class I> 99 template<class I>
101 static void cop_set (iterator a, I b) { *a = *b ; } 100 static void cop_set (iterator a, I b) { *a = *b ; }
102 101
103 // MUST copy forwards 102 // MUST copy forwards
104 template<class I> 103 template<class I>
105 static void copy (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))
106 { 105 {
107 while (n--) 106 while (n--)
108 op (dst++, src++); 107 op (dst++, src++);
109 } 108 }
110 109
111 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))
112 { 111 {
113 if (is_simple_enough ()) 112 if (is_simple_enough ())
114 memcpy (dst, src, sizeof (T) * n); 113 memcpy (dst, src, sizeof (T) * n);
115 else 114 else
116 copy<iterator> (dst, src, n, op); 115 copy<iterator> (dst, src, n, op);
146 buf = nbuf; 145 buf = nbuf;
147 } 146 }
148 147
149 construct (buf + sze, n); 148 construct (buf + sze, n);
150 149
151 sze += n;
152
153 iterator src = buf + pos; 150 iterator src = buf + pos;
154 if (is_simple_enough ()) 151 if (is_simple_enough ())
155 memmove (src + n, src, sizeof (T) * n); 152 memmove (src + n, src, sizeof (T) * (sze - pos));
156 else 153 else
157 for (size_type i = n; i--; ) 154 for (size_type i = sze - pos; i--; )
158 op (src + n + i, src + i); 155 cop_set (src + n + i, src + i);
156
157 sze += n;
159 } 158 }
160 159
161public: 160public:
162 size_type capacity () const { return res; } 161 size_type capacity () const { return res; }
163 size_type size () const { return sze; } 162 size_type size () const { return sze; }
183 return; 182 return;
184 183
185 sz = good_size (sz); 184 sz = good_size (sz);
186 T *nbuf = alloc (sz); 185 T *nbuf = alloc (sz);
187 186
188 copy (nbuf, begin (), sze); 187 copy (nbuf, begin (), sze, cop_new);
189 dealloc (); 188 dealloc ();
190 189
191 buf = nbuf; 190 buf = nbuf;
192 res = sz; 191 res = sz;
193 } 192 }
222 template<class I> 221 template<class I>
223 simplevec (I first, I last) 222 simplevec (I first, I last)
224 { 223 {
225 sze = res = last - first; 224 sze = res = last - first;
226 buf = alloc (sze); 225 buf = alloc (sze);
227 copy (buf, first, sze); 226 copy (buf, first, sze, cop_new);
228 } 227 }
229 228
230 simplevec (const simplevec<T> &v) 229 simplevec (const simplevec<T> &v)
231 : sze(0), res(0), buf(0) 230 : sze(0), res(0), buf(0)
232 { 231 {
233 sze = res = v.size (); 232 sze = res = v.size ();
234 buf = alloc (sze); 233 buf = alloc (sze);
235 copy (buf, v.begin (), sze); 234 copy (buf, v.begin (), sze, cop_new);
236 } 235 }
237 236
238 ~simplevec () 237 ~simplevec ()
239 { 238 {
240 dealloc (); 239 dealloc ();
319 *i++ = t; 318 *i++ = t;
320 319
321 return buf + at; 320 return buf + at;
322 } 321 }
323 322
324 void erase (iterator first, iterator last) 323 iterator erase (iterator first, iterator last)
325 { 324 {
326 size_t n = last - first; 325 size_type n = last - first;
326 size_type c = end () - last;
327 327
328 if (is_simple_enough ()) 328 if (is_simple_enough ())
329 memmove (first, last, sizeof (T) * n); 329 memmove (first, last, sizeof (T) * c);
330 else 330 else
331 copy<iterator> (first, last, n, cop_new); 331 copy (first, last, c, cop_set);
332 332
333 sze -= n; 333 sze -= n;
334 destruct (buf + sze, n); 334 destruct (buf + sze, n);
335 }
336 335
336 return first;
337 }
338
337 void erase (iterator pos) 339 iterator erase (iterator pos)
338 { 340 {
339 if (pos != end ()) 341 if (pos != end ())
340 erase (pos, pos + 1); 342 erase (pos, pos + 1);
343
344 return pos;
341 } 345 }
342}; 346};
343 347
344template<class T> 348template<class T>
345bool 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