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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines