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

# User Rev Content
1 root 1.4 #ifndef ESTL_H_
2     #define ESTL_H_
3 sf-exg 1.2
4 sf-exg 1.1 #include <stdlib.h>
5     #include <string.h>
6    
7 root 1.9 #include "ecb.h"
8    
9 sf-exg 1.5 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 sf-exg 1.1
12 root 1.3 template<typename T, typename U> static inline void swap (T& a, U& b) { T t = a; a = (T)b; b = (U)t; }
13 sf-exg 1.1
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 root 1.8 #include <new>
24    
25     #if __cplusplus >= 201103L
26     #include <type_traits>
27     #endif
28    
29 root 1.9 // 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 sf-exg 1.1 template<class T>
38     struct simplevec
39     {
40 root 1.9 #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 root 1.3 typedef const T *const_iterator;
52 root 1.9 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 sf-exg 1.1
63 root 1.9 // we shamelessly optimise for "simple" types. everything
64     // "not simple enough" will use the slow path.
65 root 1.8 static bool is_simple_enough ()
66     {
67 root 1.9 return 1; // we are not there yet
68 root 1.8 #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 root 1.9 static void construct (iterator a, size_type n = 1)
84 root 1.3 {
85 root 1.9 if (!is_simple_enough ())
86     while (n--)
87     new (*a++) T ();
88 root 1.3 }
89    
90 root 1.9 static void destruct (iterator a, size_type n = 1)
91 root 1.3 {
92 root 1.8 if (!is_simple_enough ())
93 root 1.9 while (n--)
94     (*a++).~T ();
95 root 1.3 }
96    
97 root 1.11 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 root 1.3
109 root 1.9 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
110 root 1.8 {
111     if (is_simple_enough ())
112     memmove (dst, src, sizeof (T) * n);
113     else
114 root 1.11 copy_lower<iterator> (dst, src, n, cop_new);
115 root 1.8 }
116    
117 root 1.9 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
118 root 1.8 {
119     if (is_simple_enough ())
120     memmove (dst, src, sizeof (T) * n);
121     else
122     while (n--)
123 root 1.9 op (dst + n, src + n);
124 root 1.8 }
125    
126 root 1.11 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 root 1.9 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
133 root 1.8 {
134     if (is_simple_enough ())
135     memcpy (dst, src, sizeof (T) * n);
136     else
137 root 1.11 copy<iterator> (dst, src, n, op);
138 root 1.9 }
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 root 1.8 }
155    
156     void ins (iterator where, size_type n)
157 root 1.3 {
158 root 1.9 size_type pos = where - begin ();
159    
160     if (ecb_expect_false (sze + n > res))
161 root 1.3 {
162 root 1.9 res = good_size (sze + n);
163 root 1.3
164 root 1.9 T *nbuf = alloc (res);
165     copy (nbuf, buf, sze, cop_new);
166     dealloc ();
167     buf = nbuf;
168 root 1.3 }
169 root 1.9
170     construct (buf + sze, n);
171     copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set);
172     sze += n;
173 root 1.3 }
174 sf-exg 1.1
175     public:
176 root 1.9 size_type capacity () const { return res; }
177     size_type size () const { return sze; }
178     bool empty () const { return size () == 0; }
179    
180 root 1.11 size_t max_size () const
181     {
182     return (~(size_type)0) >> 1;
183     }
184    
185 root 1.9 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 root 1.3 void reserve (size_type sz)
195     {
196 root 1.9 if (ecb_expect_true (sz <= res))
197     return;
198 root 1.3
199 root 1.9 sz = good_size (sz);
200     T *nbuf = alloc (sz);
201 root 1.3
202 root 1.9 copy (nbuf, begin (), sze);
203     dealloc ();
204    
205     buf = nbuf;
206     res = sz;
207 root 1.3 }
208    
209 root 1.7 void resize (size_type sz)
210     {
211     reserve (sz);
212 root 1.8
213     if (is_simple_enough ())
214 root 1.9 sze = sz;
215 root 1.8 else
216     {
217 root 1.9 while (sze < sz) construct (buf + sze++);
218     while (sze > sz) destruct (buf + --sze);
219 root 1.8 }
220 root 1.7 }
221    
222 root 1.3 simplevec ()
223 root 1.9 : sze(0), res(0), buf(0)
224 root 1.3 {
225     }
226    
227 root 1.9 simplevec (size_type n, const T &t = T ())
228 root 1.3 {
229 root 1.11 sze = res = n;
230     buf = alloc (sze);
231    
232     while (n--)
233     new (buf + n) T (t);
234 root 1.3 }
235    
236 root 1.11 template<class I>
237     simplevec (I first, I last)
238 root 1.3 {
239 root 1.11 sze = res = last - first;
240     buf = alloc (sze);
241     copy (buf, first, sze);
242 root 1.3 }
243    
244     simplevec (const simplevec<T> &v)
245 root 1.9 : sze(0), res(0), buf(0)
246 root 1.3 {
247 root 1.11 sze = res = v.size ();
248     buf = alloc (sze);
249     copy (buf, v.begin (), sze);
250 root 1.3 }
251    
252     ~simplevec ()
253     {
254 root 1.8 dealloc ();
255 root 1.3 }
256    
257 root 1.9 void swap (simplevec<T> &t)
258 root 1.3 {
259 root 1.9 ::swap (sze, t.sze);
260     ::swap (res, t.res);
261     ::swap (buf, t.buf);
262 root 1.3 }
263    
264     void clear ()
265     {
266 root 1.9 destruct (buf, sze);
267     sze = 0;
268 root 1.3 }
269    
270     void push_back (const T &t)
271     {
272 root 1.9 reserve (sze + 1);
273     new (buf + sze++) T (t);
274 root 1.3 }
275    
276     void pop_back ()
277     {
278 root 1.9 destruct (buf + --sze);
279 root 1.3 }
280    
281 root 1.11 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 root 1.3
304     iterator insert (iterator pos, const T &t)
305     {
306 root 1.8 size_type at = pos - begin ();
307 root 1.10
308 root 1.8 ins (pos, 1);
309 root 1.10 buf [at] = t;
310    
311     return buf + at;
312 root 1.3 }
313    
314 root 1.11 template<class I>
315     iterator insert (iterator pos, I first, I last)
316 root 1.3 {
317 root 1.8 size_type n = last - first;
318     size_type at = pos - begin ();
319 root 1.3
320 root 1.9 ins (pos, n);
321 root 1.10 copy (buf + at, first, n, cop_set);
322 root 1.3
323 root 1.10 return buf + at;
324 root 1.3 }
325    
326     iterator insert (iterator pos, size_type n, const T &t)
327     {
328 root 1.8 size_type at = pos - begin ();
329 root 1.3
330 root 1.9 ins (pos, n);
331    
332 root 1.10 for (iterator i = buf + at; n--; )
333     *i++ = t;
334 root 1.3
335 root 1.10 return buf + at;
336 root 1.3 }
337    
338     void erase (iterator first, iterator last)
339     {
340 root 1.9 size_t n = last - first;
341 root 1.8
342 root 1.9 copy_lower (last, first, end () - last, cop_set);
343     sze -= n;
344     destruct (buf + sze, n);
345 root 1.3 }
346    
347     void erase (iterator pos)
348     {
349     if (pos != end ())
350 root 1.8 erase (pos, pos + 1);
351 root 1.3 }
352 sf-exg 1.1 };
353    
354     template<class T>
355 root 1.3 bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)
356 sf-exg 1.1 {
357 root 1.3 if (v1.size () != v2.size ()) return false;
358    
359     return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size () * sizeof (T));
360 sf-exg 1.1 }
361    
362     template<class T>
363 root 1.3 bool operator <(const simplevec<T> &v1, const simplevec<T> &v2)
364 sf-exg 1.1 {
365 root 1.3 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 sf-exg 1.1 }
372 root 1.3 return v1.size () < v2.size ();
373 sf-exg 1.1 }
374    
375     template<typename T>
376     struct vector : simplevec<T>
377     {
378     };
379 sf-exg 1.2
380     #endif
381 root 1.3