ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
Revision: 1.11
Committed: Sat May 19 01:56:09 2012 UTC (12 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +61 -21 lines
Log Message:
schmorp was here, and bloated urxvt

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
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 template<class T>
38 struct simplevec
39 {
40 #if ESTL_BIG_VECTOR
41 // shoudl use size_t/ssize_t, but that's not portable enough for us
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
58
59 private:
60 size_type sze, res;
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 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 template<class I>
103 static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new)
104 {
105 while (n--)
106 op (dst++, src++);
107 }
108
109 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
110 {
111 if (is_simple_enough ())
112 memmove (dst, src, sizeof (T) * n);
113 else
114 copy_lower<iterator> (dst, src, n, cop_new);
115 }
116
117 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
118 {
119 if (is_simple_enough ())
120 memmove (dst, src, sizeof (T) * n);
121 else
122 while (n--)
123 op (dst + n, src + n);
124 }
125
126 template<class I>
127 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new)
128 {
129 copy_lower<I> (dst, src, n, op);
130 }
131
132 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
133 {
134 if (is_simple_enough ())
135 memcpy (dst, src, sizeof (T) * n);
136 else
137 copy<iterator> (dst, src, n, op);
138 }
139
140 static T *alloc (size_type n) ecb_cold
141 {
142 return (T *)::operator new ((size_t) (sizeof (T) * n));
143 }
144
145 void dealloc () ecb_cold
146 {
147 destruct (buf, sze);
148 ::operator delete (buf);
149 }
150
151 size_type good_size (size_type n) ecb_cold
152 {
153 return n ? 2UL << ecb_ld32 (n) : 5;
154 }
155
156 void ins (iterator where, size_type n)
157 {
158 size_type pos = where - begin ();
159
160 if (ecb_expect_false (sze + n > res))
161 {
162 res = good_size (sze + n);
163
164 T *nbuf = alloc (res);
165 copy (nbuf, buf, sze, cop_new);
166 dealloc ();
167 buf = nbuf;
168 }
169
170 construct (buf + sze, n);
171 copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set);
172 sze += n;
173 }
174
175 public:
176 size_type capacity () const { return res; }
177 size_type size () const { return sze; }
178 bool empty () const { return size () == 0; }
179
180 size_t max_size () const
181 {
182 return (~(size_type)0) >> 1;
183 }
184
185 const_iterator begin () const { return &buf [ 0]; }
186 iterator begin () { return &buf [ 0]; }
187 const_iterator end () const { return &buf [sze ]; }
188 iterator end () { return &buf [sze ]; }
189 const_reference front () const { return buf [ 0]; }
190 reference front () { return buf [ 0]; }
191 const_reference back () const { return buf [sze - 1]; }
192 reference back () { return buf [sze - 1]; }
193
194 void reserve (size_type sz)
195 {
196 if (ecb_expect_true (sz <= res))
197 return;
198
199 sz = good_size (sz);
200 T *nbuf = alloc (sz);
201
202 copy (nbuf, begin (), sze);
203 dealloc ();
204
205 buf = nbuf;
206 res = sz;
207 }
208
209 void resize (size_type sz)
210 {
211 reserve (sz);
212
213 if (is_simple_enough ())
214 sze = sz;
215 else
216 {
217 while (sze < sz) construct (buf + sze++);
218 while (sze > sz) destruct (buf + --sze);
219 }
220 }
221
222 simplevec ()
223 : sze(0), res(0), buf(0)
224 {
225 }
226
227 simplevec (size_type n, const T &t = T ())
228 {
229 sze = res = n;
230 buf = alloc (sze);
231
232 while (n--)
233 new (buf + n) T (t);
234 }
235
236 template<class I>
237 simplevec (I first, I last)
238 {
239 sze = res = last - first;
240 buf = alloc (sze);
241 copy (buf, first, sze);
242 }
243
244 simplevec (const simplevec<T> &v)
245 : sze(0), res(0), buf(0)
246 {
247 sze = res = v.size ();
248 buf = alloc (sze);
249 copy (buf, v.begin (), sze);
250 }
251
252 ~simplevec ()
253 {
254 dealloc ();
255 }
256
257 void swap (simplevec<T> &t)
258 {
259 ::swap (sze, t.sze);
260 ::swap (res, t.res);
261 ::swap (buf, t.buf);
262 }
263
264 void clear ()
265 {
266 destruct (buf, sze);
267 sze = 0;
268 }
269
270 void push_back (const T &t)
271 {
272 reserve (sze + 1);
273 new (buf + sze++) T (t);
274 }
275
276 void pop_back ()
277 {
278 destruct (buf + --sze);
279 }
280
281 const_reference operator [](size_type idx) const { return buf[idx]; }
282 reference operator [](size_type idx) { return buf[idx]; }
283
284 const_reference at (size_type idx) const { return buf [idx]; }
285 reference at (size_type idx) { return buf [idx]; }
286
287 template<class I>
288 void assign (I first, I last)
289 {
290 swap (simplevec<T> (first, last));
291 }
292
293 void assign (size_type n, const T &t)
294 {
295 swap (simplevec<T> (n, t));
296 }
297
298 simplevec<T> &operator= (const simplevec<T> &v)
299 {
300 assign (v.begin (), v.end ());
301 return *this;
302 }
303
304 iterator insert (iterator pos, const T &t)
305 {
306 size_type at = pos - begin ();
307
308 ins (pos, 1);
309 buf [at] = t;
310
311 return buf + at;
312 }
313
314 template<class I>
315 iterator insert (iterator pos, I first, I last)
316 {
317 size_type n = last - first;
318 size_type at = pos - begin ();
319
320 ins (pos, n);
321 copy (buf + at, first, n, cop_set);
322
323 return buf + at;
324 }
325
326 iterator insert (iterator pos, size_type n, const T &t)
327 {
328 size_type at = pos - begin ();
329
330 ins (pos, n);
331
332 for (iterator i = buf + at; n--; )
333 *i++ = t;
334
335 return buf + at;
336 }
337
338 void erase (iterator first, iterator last)
339 {
340 size_t n = last - first;
341
342 copy_lower (last, first, end () - last, cop_set);
343 sze -= n;
344 destruct (buf + sze, n);
345 }
346
347 void erase (iterator pos)
348 {
349 if (pos != end ())
350 erase (pos, pos + 1);
351 }
352 };
353
354 template<class T>
355 bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)
356 {
357 if (v1.size () != v2.size ()) return false;
358
359 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size () * sizeof (T));
360 }
361
362 template<class T>
363 bool operator <(const simplevec<T> &v1, const simplevec<T> &v2)
364 {
365 unsigned long minlast = min (v1.size (), v2.size ());
366
367 for (unsigned long i = 0; i < minlast; ++i)
368 {
369 if (v1[i] < v2[i]) return true;
370 if (v2[i] < v1[i]) return false;
371 }
372 return v1.size () < v2.size ();
373 }
374
375 template<typename T>
376 struct vector : simplevec<T>
377 {
378 };
379
380 #endif
381