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.7 by root, Thu May 17 18:06:38 2012 UTC vs.
Revision 1.19 by sf-exg, Thu May 24 11:48:18 2012 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/* simplevec taken (and heavily modified), from: 23#include <new>
22 * 24
23 * MICO --- a free CORBA implementation 25#if __cplusplus >= 201103L
24 * Copyright (C) 1997-98 Kay Roemer & Arno Puder 26 #include <type_traits>
25 * originally GPLv2 or any later 27#endif
26 */ 28
29// original version taken from MICO, but this has been completely rewritten
30// known limitations w.r.t. std::vector
31// - many methods missing
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
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
37// - no bool specialisation
27template<class T> 38template<class T>
28struct simplevec 39struct simplevec
29{ 40{
41#if ESTL_BIG_VECTOR
42 // shoudl use size_t/ssize_t, but that's not portable enough for us
43 typedef unsigned long size_type;
44 typedef long difference_type;
45#else
46 typedef uint32_t size_type;
47 typedef int32_t difference_type;
48#endif
49
50 typedef T value_type;
30 typedef T *iterator; 51 typedef T *iterator;
31 typedef const T *const_iterator; 52 typedef const T *const_iterator;
32 typedef unsigned long size_type; 53 typedef T *pointer;
54 typedef const T *const_pointer;
55 typedef T &reference;
56 typedef const T &const_reference;
57 // missing: allocator_type
58 // missing: reverse iterator
33 59
34private: 60private:
35 size_type _last, _size; 61 size_type sze, res;
36 T *_buf; 62 T *buf;
63
64 // we shamelessly optimise for "simple" types. everything
65 // "not simple enough" will use the slow path.
66 static bool is_simple_enough ()
67 {
68 #if __cplusplus >= 201103L
69 return std::is_trivially_assignable<T, T>::value
70 && std::is_trivially_constructable<T>::value
71 && std::is_trivially_copyable<T>::value
72 && std::is_trivially_destructible<T>::value;
73 #elif ECB_GCC_VERSION(4,4)
74 return __has_trivial_assign (T)
75 && __has_trivial_constructor (T)
76 && __has_trivial_copy (T)
77 && __has_trivial_destructor (T);
78 #else
79 return 0;
80 #endif
81 }
82
83 static void construct (iterator a, size_type n = 1)
84 {
85 if (!is_simple_enough ())
86 while (n--)
87 new (a++) T ();
88 }
89
90 static void destruct (iterator a, size_type n = 1)
91 {
92 if (!is_simple_enough ())
93 while (n--)
94 (*a++).~T ();
95 }
96
97 template<class I>
98 static void cop_new (iterator a, I b) { new (a) T (*b); }
99 template<class I>
100 static void cop_set (iterator a, I b) { *a = *b ; }
101
102 // MUST copy forwards
103 template<class I>
104 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I))
105 {
106 while (n--)
107 op (dst++, src++);
108 }
109
110 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator))
111 {
112 if (is_simple_enough ())
113 memcpy (dst, src, sizeof (T) * n);
114 else
115 copy<iterator> (dst, src, n, op);
116 }
117
118 static T *alloc (size_type n) ecb_cold
119 {
120 return (T *)::operator new ((size_t) (sizeof (T) * n));
121 }
122
123 void dealloc () ecb_cold
124 {
125 destruct (buf, sze);
126 ::operator delete (buf);
127 }
128
129 size_type good_size (size_type n) ecb_cold
130 {
131 return n ? 2UL << ecb_ld32 (n) : 5;
132 }
133
134 void ins (iterator where, size_type n)
135 {
136 size_type pos = where - begin ();
137
138 if (ecb_expect_false (sze + n > res))
139 {
140 res = good_size (sze + n);
141
142 T *nbuf = alloc (res);
143 copy (nbuf, buf, sze, cop_new);
144 dealloc ();
145 buf = nbuf;
146 }
147
148 construct (buf + sze, n);
149
150 sze += n;
151
152 iterator src = buf + pos;
153 if (is_simple_enough ())
154 memmove (src + n, src, sizeof (T) * n);
155 else
156 for (size_type i = n; i--; )
157 cop_set (src + n + i, src + i);
158 }
37 159
38public: 160public:
39 const_iterator begin () const { return &_buf[0]; }
40 iterator begin () { return &_buf[0]; }
41
42 const_iterator end () const { return &_buf[_last]; }
43 iterator end () { return &_buf[_last]; }
44
45 size_type capacity () const { return _size; } 161 size_type capacity () const { return res; }
46 size_type size () const { return _last; } 162 size_type size () const { return sze; }
163 bool empty () const { return size () == 0; }
47 164
48private: 165 size_t max_size () const
49 static T *alloc (size_type n)
50 { 166 {
51 return (T *)::operator new ((size_t) (n * sizeof (T))); 167 return (~(size_type)0) >> 1;
52 } 168 }
53 169
54 static void dealloc (T *buf) 170 const_iterator begin () const { return &buf [ 0]; }
55 { 171 iterator begin () { return &buf [ 0]; }
56 if (buf) 172 const_iterator end () const { return &buf [sze ]; }
57 ::operator delete (buf); 173 iterator end () { return &buf [sze ]; }
58 } 174 const_reference front () const { return buf [ 0]; }
175 reference front () { return buf [ 0]; }
176 const_reference back () const { return buf [sze - 1]; }
177 reference back () { return buf [sze - 1]; }
59 178
60 size_type good_size (size_type n) 179 void reserve (size_type sz)
61 { 180 {
62 return max (n, _size ? _size * 2 : 5); 181 if (ecb_expect_true (sz <= res))
63 } 182 return;
64 183
65 void reserve (iterator where, size_type n) 184 sz = good_size (sz);
185 T *nbuf = alloc (sz);
186
187 copy (nbuf, begin (), sze, cop_new);
188 dealloc ();
189
190 buf = nbuf;
191 res = sz;
192 }
193
194 void resize (size_type sz)
66 { 195 {
67 if (_last + n <= _size) 196 reserve (sz);
68 memmove (where + n, where, (end () - where) * sizeof (T)); 197
198 if (is_simple_enough ())
199 sze = sz;
69 else 200 else
70 { 201 {
71 size_type sz = _last + n; 202 while (sze < sz) construct (buf + sze++);
72 sz = good_size (sz); 203 while (sze > sz) destruct (buf + --sze);
73 T *nbuf = alloc (sz);
74
75 if (_buf)
76 {
77 memcpy (nbuf, begin (), (where - begin ()) * sizeof (T));
78 memcpy (nbuf + (where - begin ()) + n, where, (end () - where) * sizeof (T));
79 dealloc (_buf);
80 }
81
82 _buf = nbuf;
83 _size = sz;
84 } 204 }
85 } 205 }
86 206
87public:
88 void reserve (size_type sz)
89 {
90 if (_size < sz)
91 {
92 sz = good_size (sz);
93 T *nbuf = alloc (sz);
94
95 if (_buf)
96 {
97 memcpy (nbuf, begin (), size () * sizeof (T));
98 dealloc (_buf);
99 }
100
101 _buf = nbuf;
102 _size = sz;
103 }
104 }
105
106 void resize (size_type sz)
107 {
108 reserve (sz);
109 _last = sz;
110 }
111
112 simplevec () 207 simplevec ()
113 : _last(0), _size(0), _buf(0) 208 : sze(0), res(0), buf(0)
114 { 209 {
115 } 210 }
116 211
117 simplevec (size_type n, const T& t = T ()) 212 simplevec (size_type n, const T &t = T ())
118 : _last(0), _size(0), _buf(0)
119 { 213 {
120 insert (begin (), n, t); 214 sze = res = n;
121 } 215 buf = alloc (sze);
122 216
123 simplevec (const_iterator first, const_iterator last) 217 while (n--)
124 : _last(0), _size(0), _buf(0) 218 new (buf + n) T (t);
219 }
220
221 template<class I>
222 simplevec (I first, I last)
125 { 223 {
126 insert (begin (), first, last); 224 sze = res = last - first;
225 buf = alloc (sze);
226 copy (buf, first, sze, cop_new);
127 } 227 }
128 228
129 simplevec (const simplevec<T> &v) 229 simplevec (const simplevec<T> &v)
130 : _last(0), _size(0), _buf(0) 230 : sze(0), res(0), buf(0)
131 { 231 {
132 reserve (v._last); 232 sze = res = v.size ();
133 memcpy (_buf, v.begin (), v.size () * sizeof (T)); 233 buf = alloc (sze);
134 _last = v._last; 234 copy (buf, v.begin (), sze, cop_new);
235 }
236
237 ~simplevec ()
238 {
239 dealloc ();
240 }
241
242 void swap (simplevec<T> &t)
243 {
244 ::swap (sze, t.sze);
245 ::swap (res, t.res);
246 ::swap (buf, t.buf);
247 }
248
249 void clear ()
250 {
251 destruct (buf, sze);
252 sze = 0;
253 }
254
255 void push_back (const T &t)
256 {
257 reserve (sze + 1);
258 new (buf + sze++) T (t);
259 }
260
261 void pop_back ()
262 {
263 destruct (buf + --sze);
264 }
265
266 const_reference operator [](size_type idx) const { 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));
135 } 281 }
136 282
137 simplevec<T> &operator= (const simplevec<T> &v) 283 simplevec<T> &operator= (const simplevec<T> &v)
138 { 284 {
139 if (this != &v) 285 assign (v.begin (), v.end ());
140 {
141 _last = 0;
142 reserve (v._last);
143 memcpy (_buf, v.begin (), v.size () * sizeof (T));
144 _last = v._last;
145 }
146
147 return *this; 286 return *this;
148 } 287 }
149 288
150 ~simplevec ()
151 {
152 dealloc (_buf);
153 }
154
155 const T &front () const { return _buf[ 0]; }
156 T &front () { return _buf[ 0]; }
157 const T &back () const { return _buf[_last-1]; }
158 T &back () { return _buf[_last-1]; }
159
160 bool empty () const
161 {
162 return _last == 0;
163 }
164
165 void clear ()
166 {
167 _last = 0;
168 }
169
170 void push_back (const T &t)
171 {
172 reserve (_last+1);
173 *end () = t;
174 ++_last;
175 }
176
177 void push_back (T &t)
178 {
179 reserve (_last+1);
180 *end () = t;
181 ++_last;
182 }
183
184 void pop_back ()
185 {
186 --_last;
187 }
188
189 const T &operator [](size_type idx) const { return _buf[idx]; }
190 T &operator [](size_type idx) { return _buf[idx]; }
191
192 iterator insert (iterator pos, const T &t) 289 iterator insert (iterator pos, const T &t)
193 { 290 {
194 long at = pos - begin (); 291 size_type at = pos - begin ();
292
195 reserve (pos, 1); 293 ins (pos, 1);
196 pos = begin () + at; 294 buf [at] = t;
197 *pos = t; 295
198 ++_last; 296 return buf + at;
297 }
298
299 template<class I>
300 iterator insert (iterator pos, I first, I last)
301 {
302 size_type n = last - first;
303 size_type at = pos - begin ();
304
305 ins (pos, n);
306 copy (buf + at, first, n, cop_set);
307
308 return buf + at;
309 }
310
311 iterator insert (iterator pos, size_type n, const T &t)
312 {
313 size_type at = pos - begin ();
314
315 ins (pos, n);
316
317 for (iterator i = buf + at; n--; )
318 *i++ = t;
319
320 return buf + at;
321 }
322
323 iterator erase (iterator first, iterator last)
324 {
325 size_t n = last - first;
326
327 if (is_simple_enough ())
328 memmove (first, last, sizeof (T) * n);
329 else
330 copy<iterator> (first, last, n, cop_set);
331
332 sze -= n;
333 destruct (buf + sze, n);
334
335 return first;
336 }
337
338 iterator erase (iterator pos)
339 {
340 if (pos != end ())
341 erase (pos, pos + 1);
342
199 return pos; 343 return pos;
200 }
201
202 iterator insert (iterator pos, const_iterator first, const_iterator last)
203 {
204 long n = last - first;
205 long at = pos - begin ();
206
207 if (n > 0)
208 {
209 reserve (pos, n);
210 pos = begin () + at;
211 memcpy (pos, first, (last - first) * sizeof (T));
212 _last += n;
213 }
214
215 return pos;
216 }
217
218 iterator insert (iterator pos, size_type n, const T &t)
219 {
220 long at = pos - begin ();
221
222 if (n > 0)
223 {
224 reserve (pos, n);
225 pos = begin () + at;
226 for (int i = 0; i < n; ++i)
227 pos[i] = t;
228 _last += n;
229 }
230
231 return pos;
232 }
233
234 void erase (iterator first, iterator last)
235 {
236 if (last != first)
237 {
238 memmove (first, last, (end () - last) * sizeof (T));
239 _last -= last - first;
240 }
241 }
242
243 void erase (iterator pos)
244 {
245 if (pos != end ())
246 {
247 memmove (pos, pos + 1, (end () - (pos + 1)) * sizeof (T));
248 --_last;
249 }
250 }
251
252 void swap (simplevec<T> &t)
253 {
254 ::swap (_last, t._last);
255 ::swap (_size, t._size);
256 ::swap (_buf, t._buf);
257 } 344 }
258}; 345};
259 346
260template<class T> 347template<class T>
261bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) 348bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines