ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
Revision: 1.14
Committed: Sat May 19 02:13:59 2012 UTC (12 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +9 -10 lines
Log Message:
well, then we can just inline copy_higher as well

File Contents

# Content
1 #ifndef ESTL_H_
2 #define ESTL_H_
3
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "ecb.h"
8
9 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
10 template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
11
12 template<typename T, typename U> static inline void swap (T& a, U& b) { T t = a; a = (T)b; b = (U)t; }
13
14 template <typename I, typename T>
15 I find (I first, I last, const T& value)
16 {
17 while (first != last && *first != value)
18 ++first;
19
20 return first;
21 }
22
23 #include <new>
24
25 #if __cplusplus >= 201103L
26 #include <type_traits>
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 (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
38 template<class T>
39 struct simplevec
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;
51 typedef T *iterator;
52 typedef const T *const_iterator;
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
59
60 private:
61 size_type sze, res;
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) = cop_new)
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) = cop_new)
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 }
160
161 public:
162 size_type capacity () const { return res; }
163 size_type size () const { return sze; }
164 bool empty () const { return size () == 0; }
165
166 size_t max_size () const
167 {
168 return (~(size_type)0) >> 1;
169 }
170
171 const_iterator begin () const { return &buf [ 0]; }
172 iterator begin () { return &buf [ 0]; }
173 const_iterator end () const { return &buf [sze ]; }
174 iterator end () { return &buf [sze ]; }
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]; }
179
180 void reserve (size_type sz)
181 {
182 if (ecb_expect_true (sz <= res))
183 return;
184
185 sz = good_size (sz);
186 T *nbuf = alloc (sz);
187
188 copy (nbuf, begin (), sze);
189 dealloc ();
190
191 buf = nbuf;
192 res = sz;
193 }
194
195 void resize (size_type sz)
196 {
197 reserve (sz);
198
199 if (is_simple_enough ())
200 sze = sz;
201 else
202 {
203 while (sze < sz) construct (buf + sze++);
204 while (sze > sz) destruct (buf + --sze);
205 }
206 }
207
208 simplevec ()
209 : sze(0), res(0), buf(0)
210 {
211 }
212
213 simplevec (size_type n, const T &t = T ())
214 {
215 sze = res = n;
216 buf = alloc (sze);
217
218 while (n--)
219 new (buf + n) T (t);
220 }
221
222 template<class I>
223 simplevec (I first, I last)
224 {
225 sze = res = last - first;
226 buf = alloc (sze);
227 copy (buf, first, sze);
228 }
229
230 simplevec (const simplevec<T> &v)
231 : sze(0), res(0), buf(0)
232 {
233 sze = res = v.size ();
234 buf = alloc (sze);
235 copy (buf, v.begin (), sze);
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));
282 }
283
284 simplevec<T> &operator= (const simplevec<T> &v)
285 {
286 assign (v.begin (), v.end ());
287 return *this;
288 }
289
290 iterator insert (iterator pos, const T &t)
291 {
292 size_type at = pos - begin ();
293
294 ins (pos, 1);
295 buf [at] = t;
296
297 return buf + at;
298 }
299
300 template<class I>
301 iterator insert (iterator pos, I first, I last)
302 {
303 size_type n = last - first;
304 size_type at = pos - begin ();
305
306 ins (pos, n);
307 copy (buf + at, first, n, cop_set);
308
309 return buf + at;
310 }
311
312 iterator insert (iterator pos, size_type n, const T &t)
313 {
314 size_type at = pos - begin ();
315
316 ins (pos, n);
317
318 for (iterator i = buf + at; n--; )
319 *i++ = t;
320
321 return buf + at;
322 }
323
324 void erase (iterator first, iterator last)
325 {
326 size_t n = last - first;
327
328 if (is_simple_enough ())
329 memmove (first, last, sizeof (T) * n);
330 else
331 copy<iterator> (first, last, n, cop_new);
332
333 sze -= n;
334 destruct (buf + sze, n);
335 }
336
337 void erase (iterator pos)
338 {
339 if (pos != end ())
340 erase (pos, pos + 1);
341 }
342 };
343
344 template<class T>
345 bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)
346 {
347 if (v1.size () != v2.size ()) return false;
348
349 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size () * sizeof (T));
350 }
351
352 template<class T>
353 bool operator <(const simplevec<T> &v1, const simplevec<T> &v2)
354 {
355 unsigned long minlast = min (v1.size (), v2.size ());
356
357 for (unsigned long i = 0; i < minlast; ++i)
358 {
359 if (v1[i] < v2[i]) return true;
360 if (v2[i] < v1[i]) return false;
361 }
362 return v1.size () < v2.size ();
363 }
364
365 template<typename T>
366 struct vector : simplevec<T>
367 {
368 };
369
370 #endif
371