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.10 by root, Sat May 19 01:19:03 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 ())
93 while (n--) 93 while (n--)
94 (*a++).~T (); 94 (*a++).~T ();
95 } 95 }
96 96
97 template<class I>
97 static void cop_new (iterator a, iterator b) { new (a) T (*b); } 98 static void cop_new (iterator a, I b) { new (a) T (*b); }
99 template<class I>
98 static void cop_set (iterator a, iterator b) { *a = *b ; } 100 static void cop_set (iterator a, I b) { *a = *b ; }
99 101
100 // these copy helpers actually use the copy constructor, not assignment 102 // MUST copy forwards
103 template<class I>
101 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 104 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I))
102 { 105 {
103 if (is_simple_enough ())
104 memmove (dst, src, sizeof (T) * n);
105 else
106 while (n--) 106 while (n--)
107 op (dst++, src++); 107 op (dst++, src++);
108 } 108 }
109 109
110 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
111 {
112 if (is_simple_enough ())
113 memmove (dst, src, sizeof (T) * n);
114 else
115 while (n--)
116 op (dst + n, src + n);
117 }
118
119 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))
120 { 111 {
121 if (is_simple_enough ()) 112 if (is_simple_enough ())
122 memcpy (dst, src, sizeof (T) * n); 113 memcpy (dst, src, sizeof (T) * n);
123 else 114 else
124 copy_lower (dst, src, n, op); 115 copy<iterator> (dst, src, n, op);
125 } 116 }
126 117
127 static T *alloc (size_type n) ecb_cold 118 static T *alloc (size_type n) ecb_cold
128 { 119 {
129 return (T *)::operator new ((size_t) (sizeof (T) * n)); 120 return (T *)::operator new ((size_t) (sizeof (T) * n));
153 dealloc (); 144 dealloc ();
154 buf = nbuf; 145 buf = nbuf;
155 } 146 }
156 147
157 construct (buf + sze, n); 148 construct (buf + sze, n);
158 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
159 sze += n; 157 sze += n;
160 } 158 }
161 159
162public: 160public:
163 size_type capacity () const { return res; } 161 size_type capacity () const { return res; }
164 size_type size () const { return sze; } 162 size_type size () const { return sze; }
165 bool empty () const { return size () == 0; } 163 bool empty () const { return size () == 0; }
164
165 size_t max_size () const
166 {
167 return (~(size_type)0) >> 1;
168 }
166 169
167 const_iterator begin () const { return &buf [ 0]; } 170 const_iterator begin () const { return &buf [ 0]; }
168 iterator begin () { return &buf [ 0]; } 171 iterator begin () { return &buf [ 0]; }
169 const_iterator end () const { return &buf [sze ]; } 172 const_iterator end () const { return &buf [sze ]; }
170 iterator end () { return &buf [sze ]; } 173 iterator end () { return &buf [sze ]; }
179 return; 182 return;
180 183
181 sz = good_size (sz); 184 sz = good_size (sz);
182 T *nbuf = alloc (sz); 185 T *nbuf = alloc (sz);
183 186
184 copy (nbuf, begin (), sze); 187 copy (nbuf, begin (), sze, cop_new);
185 dealloc (); 188 dealloc ();
186 189
187 buf = nbuf; 190 buf = nbuf;
188 res = sz; 191 res = sz;
189 } 192 }
205 : sze(0), res(0), buf(0) 208 : sze(0), res(0), buf(0)
206 { 209 {
207 } 210 }
208 211
209 simplevec (size_type n, const T &t = T ()) 212 simplevec (size_type n, const T &t = T ())
210 : sze(0), res(0), buf(0)
211 { 213 {
212 insert (begin (), n, t); 214 sze = res = n;
213 } 215 buf = alloc (sze);
214 216
215 simplevec (const_iterator first, const_iterator last) 217 while (n--)
216 : sze(0), res(0), buf(0) 218 new (buf + n) T (t);
219 }
220
221 template<class I>
222 simplevec (I first, I last)
217 { 223 {
218 insert (begin (), first, last); 224 sze = res = last - first;
225 buf = alloc (sze);
226 copy (buf, first, sze, cop_new);
219 } 227 }
220 228
221 simplevec (const simplevec<T> &v) 229 simplevec (const simplevec<T> &v)
222 : sze(0), res(0), buf(0) 230 : sze(0), res(0), buf(0)
223 { 231 {
224 insert (begin (), v.begin (), v.end ()); 232 sze = res = v.size ();
225 } 233 buf = alloc (sze);
226 234 copy (buf, v.begin (), sze, cop_new);
227 simplevec<T> &operator= (const simplevec<T> &v)
228 {
229 swap (simplevec<T> (v));
230 return *this;
231 } 235 }
232 236
233 ~simplevec () 237 ~simplevec ()
234 { 238 {
235 dealloc (); 239 dealloc ();
257 void pop_back () 261 void pop_back ()
258 { 262 {
259 destruct (buf + --sze); 263 destruct (buf + --sze);
260 } 264 }
261 265
262 const T &operator [](size_type idx) const { return buf[idx]; } 266 const_reference operator [](size_type idx) const { return buf[idx]; }
263 T &operator [](size_type idx) { return buf[idx]; } 267 reference operator [](size_type idx) { return buf[idx]; }
268
269 const_reference at (size_type idx) const { return buf [idx]; }
270 reference at (size_type idx) { return buf [idx]; }
271
272 template<class I>
273 void assign (I first, I last)
274 {
275 swap (simplevec<T> (first, last));
276 }
277
278 void assign (size_type n, const T &t)
279 {
280 swap (simplevec<T> (n, t));
281 }
282
283 simplevec<T> &operator= (const simplevec<T> &v)
284 {
285 assign (v.begin (), v.end ());
286 return *this;
287 }
264 288
265 iterator insert (iterator pos, const T &t) 289 iterator insert (iterator pos, const T &t)
266 { 290 {
267 size_type at = pos - begin (); 291 size_type at = pos - begin ();
268 292
270 buf [at] = t; 294 buf [at] = t;
271 295
272 return buf + at; 296 return buf + at;
273 } 297 }
274 298
299 template<class I>
275 iterator insert (iterator pos, const_iterator first, const_iterator last) 300 iterator insert (iterator pos, I first, I last)
276 { 301 {
277 size_type n = last - first; 302 size_type n = last - first;
278 size_type at = pos - begin (); 303 size_type at = pos - begin ();
279 304
280 ins (pos, n); 305 ins (pos, n);
293 *i++ = t; 318 *i++ = t;
294 319
295 return buf + at; 320 return buf + at;
296 } 321 }
297 322
298 void erase (iterator first, iterator last) 323 iterator erase (iterator first, iterator last)
299 { 324 {
300 size_t n = last - first; 325 size_type n = last - first;
326 size_type c = end () - last;
301 327
302 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
303 sze -= n; 333 sze -= n;
304 destruct (buf + sze, n); 334 destruct (buf + sze, n);
305 }
306 335
336 return first;
337 }
338
307 void erase (iterator pos) 339 iterator erase (iterator pos)
308 { 340 {
309 if (pos != end ()) 341 if (pos != end ())
310 erase (pos, pos + 1); 342 erase (pos, pos + 1);
343
344 return pos;
311 } 345 }
312}; 346};
313 347
314template<class T> 348template<class T>
315bool 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