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.3 by root, Sat Jan 21 13:56:07 2012 UTC vs.
Revision 1.15 by root, Sat May 19 02:15:31 2012 UTC

1#ifndef ESTL_H 1#ifndef ESTL_H_
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
12template <typename I, typename T> 14template <typename I, typename T>
13I find (I first, I last, const T& value) 15I find (I first, I last, const T& value)
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 return 1; // we are not there yet
69 #if __cplusplus >= 201103L
70 return std::is_trivially_assignable<T, T>::value
71 && std::is_trivially_constructable<T>::value
72 && std::is_trivially_copyable<T>::value
73 && std::is_trivially_destructible<T>::value;
74 #elif ECB_GCC_VERSION(4,4)
75 return __has_trivial_assign (T)
76 && __has_trivial_constructor (T)
77 && __has_trivial_copy (T)
78 && __has_trivial_destructor (T);
79 #else
80 return 0;
81 #endif
82 }
83
84 static void construct (iterator a, size_type n = 1)
85 {
86 if (!is_simple_enough ())
87 while (n--)
88 new (*a++) T ();
89 }
90
91 static void destruct (iterator a, size_type n = 1)
92 {
93 if (!is_simple_enough ())
94 while (n--)
95 (*a++).~T ();
96 }
97
98 template<class I>
99 static void cop_new (iterator a, I b) { new (a) T (*b); }
100 template<class I>
101 static void cop_set (iterator a, I b) { *a = *b ; }
102
103 // MUST copy forwards
104 template<class I>
105 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I))
106 {
107 while (n--)
108 op (dst++, src++);
109 }
110
111 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator))
112 {
113 if (is_simple_enough ())
114 memcpy (dst, src, sizeof (T) * n);
115 else
116 copy<iterator> (dst, src, n, op);
117 }
118
119 static T *alloc (size_type n) ecb_cold
120 {
121 return (T *)::operator new ((size_t) (sizeof (T) * n));
122 }
123
124 void dealloc () ecb_cold
125 {
126 destruct (buf, sze);
127 ::operator delete (buf);
128 }
129
130 size_type good_size (size_type n) ecb_cold
131 {
132 return n ? 2UL << ecb_ld32 (n) : 5;
133 }
134
135 void ins (iterator where, size_type n)
136 {
137 size_type pos = where - begin ();
138
139 if (ecb_expect_false (sze + n > res))
140 {
141 res = good_size (sze + n);
142
143 T *nbuf = alloc (res);
144 copy (nbuf, buf, sze, cop_new);
145 dealloc ();
146 buf = nbuf;
147 }
148
149 construct (buf + sze, n);
150
151 sze += n;
152
153 iterator src = buf + pos;
154 if (is_simple_enough ())
155 memmove (src + n, src, sizeof (T) * n);
156 else
157 for (size_type i = n; i--; )
158 op (src + n + i, src + i);
159 }
37 160
38public: 161public:
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; } 162 size_type capacity () const { return res; }
46 size_type size () const { return _last; } 163 size_type size () const { return sze; }
164 bool empty () const { return size () == 0; }
47 165
48private: 166 size_t max_size () const
49 static T *alloc (size_type n)
50 { 167 {
51 return (T *)::operator new ((size_t) (n * sizeof (T))); 168 return (~(size_type)0) >> 1;
52 } 169 }
53 170
54 static void dealloc (T *buf) 171 const_iterator begin () const { return &buf [ 0]; }
55 { 172 iterator begin () { return &buf [ 0]; }
56 if (buf) 173 const_iterator end () const { return &buf [sze ]; }
57 ::operator delete (buf); 174 iterator end () { return &buf [sze ]; }
58 } 175 const_reference front () const { return buf [ 0]; }
176 reference front () { return buf [ 0]; }
177 const_reference back () const { return buf [sze - 1]; }
178 reference back () { return buf [sze - 1]; }
59 179
60 size_type good_size (size_type n) 180 void reserve (size_type sz)
61 { 181 {
62 return max (n, _size ? _size * 2 : 5); 182 if (ecb_expect_true (sz <= res))
63 } 183 return;
64 184
65 void reserve (iterator where, size_type n) 185 sz = good_size (sz);
186 T *nbuf = alloc (sz);
187
188 copy (nbuf, begin (), sze, cop_new);
189 dealloc ();
190
191 buf = nbuf;
192 res = sz;
193 }
194
195 void resize (size_type sz)
66 { 196 {
67 if (_last + n <= _size) 197 reserve (sz);
68 memmove (where + n, where, (end () - where) * sizeof (T)); 198
199 if (is_simple_enough ())
200 sze = sz;
69 else 201 else
70 { 202 {
71 size_type sz = _last + n; 203 while (sze < sz) construct (buf + sze++);
72 sz = good_size (sz); 204 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 } 205 }
85 } 206 }
86 207
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 simplevec () 208 simplevec ()
107 : _last(0), _size(0), _buf(0) 209 : sze(0), res(0), buf(0)
108 { 210 {
109 } 211 }
110 212
111 simplevec (size_type n, const T& t = T ()) 213 simplevec (size_type n, const T &t = T ())
112 : _last(0), _size(0), _buf(0)
113 { 214 {
114 insert (begin (), n, t); 215 sze = res = n;
115 } 216 buf = alloc (sze);
116 217
117 simplevec (const_iterator first, const_iterator last) 218 while (n--)
118 : _last(0), _size(0), _buf(0) 219 new (buf + n) T (t);
220 }
221
222 template<class I>
223 simplevec (I first, I last)
119 { 224 {
120 insert (begin (), first, last); 225 sze = res = last - first;
226 buf = alloc (sze);
227 copy (buf, first, sze, cop_new);
121 } 228 }
122 229
123 simplevec (const simplevec<T> &v) 230 simplevec (const simplevec<T> &v)
124 : _last(0), _size(0), _buf(0) 231 : sze(0), res(0), buf(0)
125 { 232 {
126 reserve (v._last); 233 sze = res = v.size ();
127 memcpy (_buf, v.begin (), v.size () * sizeof (T)); 234 buf = alloc (sze);
128 _last = v._last; 235 copy (buf, v.begin (), sze, cop_new);
236 }
237
238 ~simplevec ()
239 {
240 dealloc ();
241 }
242
243 void swap (simplevec<T> &t)
244 {
245 ::swap (sze, t.sze);
246 ::swap (res, t.res);
247 ::swap (buf, t.buf);
248 }
249
250 void clear ()
251 {
252 destruct (buf, sze);
253 sze = 0;
254 }
255
256 void push_back (const T &t)
257 {
258 reserve (sze + 1);
259 new (buf + sze++) T (t);
260 }
261
262 void pop_back ()
263 {
264 destruct (buf + --sze);
265 }
266
267 const_reference operator [](size_type idx) const { return buf[idx]; }
268 reference operator [](size_type idx) { return buf[idx]; }
269
270 const_reference at (size_type idx) const { return buf [idx]; }
271 reference at (size_type idx) { return buf [idx]; }
272
273 template<class I>
274 void assign (I first, I last)
275 {
276 swap (simplevec<T> (first, last));
277 }
278
279 void assign (size_type n, const T &t)
280 {
281 swap (simplevec<T> (n, t));
129 } 282 }
130 283
131 simplevec<T> &operator= (const simplevec<T> &v) 284 simplevec<T> &operator= (const simplevec<T> &v)
132 { 285 {
133 if (this != &v) 286 assign (v.begin (), v.end ());
134 {
135 _last = 0;
136 reserve (v._last);
137 memcpy (_buf, v.begin (), v.size () * sizeof (T));
138 _last = v._last;
139 }
140
141 return *this; 287 return *this;
142 } 288 }
143 289
144 ~simplevec ()
145 {
146 dealloc (_buf);
147 }
148
149 const T &front () const { return _buf[ 0]; }
150 T &front () { return _buf[ 0]; }
151 const T &back () const { return _buf[_last-1]; }
152 T &back () { return _buf[_last-1]; }
153
154 bool empty () const
155 {
156 return _last == 0;
157 }
158
159 void clear ()
160 {
161 _last = 0;
162 }
163
164 void push_back (const T &t)
165 {
166 reserve (_last+1);
167 *end () = t;
168 ++_last;
169 }
170
171 void push_back (T &t)
172 {
173 reserve (_last+1);
174 *end () = t;
175 ++_last;
176 }
177
178 void pop_back ()
179 {
180 --_last;
181 }
182
183 const T &operator [](size_type idx) const { return _buf[idx]; }
184 T &operator [](size_type idx) { return _buf[idx]; }
185
186 iterator insert (iterator pos, const T &t) 290 iterator insert (iterator pos, const T &t)
187 { 291 {
188 long at = pos - begin (); 292 size_type at = pos - begin ();
293
189 reserve (pos, 1); 294 ins (pos, 1);
190 pos = begin () + at; 295 buf [at] = t;
191 *pos = t;
192 ++_last;
193 return pos;
194 }
195 296
297 return buf + at;
298 }
299
300 template<class I>
196 iterator insert (iterator pos, const_iterator first, const_iterator last) 301 iterator insert (iterator pos, I first, I last)
197 { 302 {
198 long n = last - first; 303 size_type n = last - first;
199 long at = pos - begin (); 304 size_type at = pos - begin ();
200 305
201 if (n > 0) 306 ins (pos, n);
202 { 307 copy (buf + at, first, n, cop_set);
203 reserve (pos, n);
204 pos = begin ()+at;
205 memcpy (pos, first, (last - first) * sizeof (T));
206 _last += n;
207 }
208 308
209 return pos; 309 return buf + at;
210 } 310 }
211 311
212 iterator insert (iterator pos, size_type n, const T &t) 312 iterator insert (iterator pos, size_type n, const T &t)
213 { 313 {
214 long at = pos - begin (); 314 size_type at = pos - begin ();
215 315
216 if (n > 0) 316 ins (pos, n);
217 {
218 reserve (pos, n);
219 pos = begin () + at;
220 for (int i = 0; i < n; ++i)
221 pos[i] = t;
222 _last += n;
223 }
224 317
225 return pos; 318 for (iterator i = buf + at; n--; )
319 *i++ = t;
320
321 return buf + at;
226 } 322 }
227 323
228 void erase (iterator first, iterator last) 324 void erase (iterator first, iterator last)
229 { 325 {
230 if (last != first) 326 size_t n = last - first;
231 { 327
328 if (is_simple_enough ())
232 memmove (first, last, (end () - last) * sizeof (T)); 329 memmove (first, last, sizeof (T) * n);
233 _last -= last - first; 330 else
234 } 331 copy<iterator> (first, last, n, cop_new);
332
333 sze -= n;
334 destruct (buf + sze, n);
235 } 335 }
236 336
237 void erase (iterator pos) 337 void erase (iterator pos)
238 { 338 {
239 if (pos != end ()) 339 if (pos != end ())
240 { 340 erase (pos, pos + 1);
241 memmove (pos, pos + 1, (end () - (pos + 1)) * sizeof (T));
242 --_last;
243 }
244 }
245
246 void swap (simplevec<T> &t)
247 {
248 ::swap(_last, t._last);
249 ::swap(_size, t._size);
250 ::swap(_buf, t._buf);
251 } 341 }
252}; 342};
253 343
254template<class T> 344template<class T>
255bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) 345bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines