ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
Revision: 1.13
Committed: Sat May 19 02:10:54 2012 UTC (12 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +7 -17 lines
Log Message:
cool, inlining this reduces codesize by 1.5kb, even smaller than original estl.h

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 root 1.12 // - no error checking, no exceptions thrown (e.g. at())
33 root 1.9 // - 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 root 1.12 // - no bool specialisation
38 sf-exg 1.1 template<class T>
39     struct simplevec
40     {
41 root 1.9 #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 root 1.3 typedef const T *const_iterator;
53 root 1.9 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 sf-exg 1.1
64 root 1.9 // we shamelessly optimise for "simple" types. everything
65     // "not simple enough" will use the slow path.
66 root 1.8 static bool is_simple_enough ()
67     {
68 root 1.9 return 1; // we are not there yet
69 root 1.8 #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 root 1.9 static void construct (iterator a, size_type n = 1)
85 root 1.3 {
86 root 1.9 if (!is_simple_enough ())
87     while (n--)
88     new (*a++) T ();
89 root 1.3 }
90    
91 root 1.9 static void destruct (iterator a, size_type n = 1)
92 root 1.3 {
93 root 1.8 if (!is_simple_enough ())
94 root 1.9 while (n--)
95     (*a++).~T ();
96 root 1.3 }
97    
98 root 1.11 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 root 1.9 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
104 root 1.8 {
105     if (is_simple_enough ())
106     memmove (dst, src, sizeof (T) * n);
107     else
108     while (n--)
109 root 1.9 op (dst + n, src + n);
110 root 1.8 }
111    
112 root 1.11 template<class I>
113     static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new)
114     {
115 root 1.13 while (n--)
116     op (dst++, src++);
117 root 1.11 }
118    
119 root 1.9 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
120 root 1.8 {
121     if (is_simple_enough ())
122     memcpy (dst, src, sizeof (T) * n);
123     else
124 root 1.11 copy<iterator> (dst, src, n, op);
125 root 1.9 }
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 root 1.8 }
142    
143     void ins (iterator where, size_type n)
144 root 1.3 {
145 root 1.9 size_type pos = where - begin ();
146    
147     if (ecb_expect_false (sze + n > res))
148 root 1.3 {
149 root 1.9 res = good_size (sze + n);
150 root 1.3
151 root 1.9 T *nbuf = alloc (res);
152     copy (nbuf, buf, sze, cop_new);
153     dealloc ();
154     buf = nbuf;
155 root 1.3 }
156 root 1.9
157     construct (buf + sze, n);
158     copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set);
159     sze += n;
160 root 1.3 }
161 sf-exg 1.1
162     public:
163 root 1.9 size_type capacity () const { return res; }
164     size_type size () const { return sze; }
165     bool empty () const { return size () == 0; }
166    
167 root 1.11 size_t max_size () const
168     {
169     return (~(size_type)0) >> 1;
170     }
171    
172 root 1.9 const_iterator begin () const { return &buf [ 0]; }
173     iterator begin () { return &buf [ 0]; }
174     const_iterator end () const { return &buf [sze ]; }
175     iterator end () { return &buf [sze ]; }
176     const_reference front () const { return buf [ 0]; }
177     reference front () { return buf [ 0]; }
178     const_reference back () const { return buf [sze - 1]; }
179     reference back () { return buf [sze - 1]; }
180    
181 root 1.3 void reserve (size_type sz)
182     {
183 root 1.9 if (ecb_expect_true (sz <= res))
184     return;
185 root 1.3
186 root 1.9 sz = good_size (sz);
187     T *nbuf = alloc (sz);
188 root 1.3
189 root 1.9 copy (nbuf, begin (), sze);
190     dealloc ();
191    
192     buf = nbuf;
193     res = sz;
194 root 1.3 }
195    
196 root 1.7 void resize (size_type sz)
197     {
198     reserve (sz);
199 root 1.8
200     if (is_simple_enough ())
201 root 1.9 sze = sz;
202 root 1.8 else
203     {
204 root 1.9 while (sze < sz) construct (buf + sze++);
205     while (sze > sz) destruct (buf + --sze);
206 root 1.8 }
207 root 1.7 }
208    
209 root 1.3 simplevec ()
210 root 1.9 : sze(0), res(0), buf(0)
211 root 1.3 {
212     }
213    
214 root 1.9 simplevec (size_type n, const T &t = T ())
215 root 1.3 {
216 root 1.11 sze = res = n;
217     buf = alloc (sze);
218    
219     while (n--)
220     new (buf + n) T (t);
221 root 1.3 }
222    
223 root 1.11 template<class I>
224     simplevec (I first, I last)
225 root 1.3 {
226 root 1.11 sze = res = last - first;
227     buf = alloc (sze);
228     copy (buf, first, sze);
229 root 1.3 }
230    
231     simplevec (const simplevec<T> &v)
232 root 1.9 : sze(0), res(0), buf(0)
233 root 1.3 {
234 root 1.11 sze = res = v.size ();
235     buf = alloc (sze);
236     copy (buf, v.begin (), sze);
237 root 1.3 }
238    
239     ~simplevec ()
240     {
241 root 1.8 dealloc ();
242 root 1.3 }
243    
244 root 1.9 void swap (simplevec<T> &t)
245 root 1.3 {
246 root 1.9 ::swap (sze, t.sze);
247     ::swap (res, t.res);
248     ::swap (buf, t.buf);
249 root 1.3 }
250    
251     void clear ()
252     {
253 root 1.9 destruct (buf, sze);
254     sze = 0;
255 root 1.3 }
256    
257     void push_back (const T &t)
258     {
259 root 1.9 reserve (sze + 1);
260     new (buf + sze++) T (t);
261 root 1.3 }
262    
263     void pop_back ()
264     {
265 root 1.9 destruct (buf + --sze);
266 root 1.3 }
267    
268 root 1.11 const_reference operator [](size_type idx) const { return buf[idx]; }
269     reference operator [](size_type idx) { return buf[idx]; }
270    
271     const_reference at (size_type idx) const { return buf [idx]; }
272     reference at (size_type idx) { return buf [idx]; }
273    
274     template<class I>
275     void assign (I first, I last)
276     {
277     swap (simplevec<T> (first, last));
278     }
279    
280     void assign (size_type n, const T &t)
281     {
282     swap (simplevec<T> (n, t));
283     }
284    
285     simplevec<T> &operator= (const simplevec<T> &v)
286     {
287     assign (v.begin (), v.end ());
288     return *this;
289     }
290 root 1.3
291     iterator insert (iterator pos, const T &t)
292     {
293 root 1.8 size_type at = pos - begin ();
294 root 1.10
295 root 1.8 ins (pos, 1);
296 root 1.10 buf [at] = t;
297    
298     return buf + at;
299 root 1.3 }
300    
301 root 1.11 template<class I>
302     iterator insert (iterator pos, I first, I last)
303 root 1.3 {
304 root 1.8 size_type n = last - first;
305     size_type at = pos - begin ();
306 root 1.3
307 root 1.9 ins (pos, n);
308 root 1.10 copy (buf + at, first, n, cop_set);
309 root 1.3
310 root 1.10 return buf + at;
311 root 1.3 }
312    
313     iterator insert (iterator pos, size_type n, const T &t)
314     {
315 root 1.8 size_type at = pos - begin ();
316 root 1.3
317 root 1.9 ins (pos, n);
318    
319 root 1.10 for (iterator i = buf + at; n--; )
320     *i++ = t;
321 root 1.3
322 root 1.10 return buf + at;
323 root 1.3 }
324    
325     void erase (iterator first, iterator last)
326     {
327 root 1.9 size_t n = last - first;
328 root 1.8
329 root 1.13 if (is_simple_enough ())
330     memmove (first, last, sizeof (T) * n);
331     else
332     copy<iterator> (first, last, n, cop_new);
333    
334 root 1.9 sze -= n;
335     destruct (buf + sze, n);
336 root 1.3 }
337    
338     void erase (iterator pos)
339     {
340     if (pos != end ())
341 root 1.8 erase (pos, pos + 1);
342 root 1.3 }
343 sf-exg 1.1 };
344    
345     template<class T>
346 root 1.3 bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)
347 sf-exg 1.1 {
348 root 1.3 if (v1.size () != v2.size ()) return false;
349    
350     return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size () * sizeof (T));
351 sf-exg 1.1 }
352    
353     template<class T>
354 root 1.3 bool operator <(const simplevec<T> &v1, const simplevec<T> &v2)
355 sf-exg 1.1 {
356 root 1.3 unsigned long minlast = min (v1.size (), v2.size ());
357    
358     for (unsigned long i = 0; i < minlast; ++i)
359     {
360     if (v1[i] < v2[i]) return true;
361     if (v2[i] < v1[i]) return false;
362 sf-exg 1.1 }
363 root 1.3 return v1.size () < v2.size ();
364 sf-exg 1.1 }
365    
366     template<typename T>
367     struct vector : simplevec<T>
368     {
369     };
370 sf-exg 1.2
371     #endif
372 root 1.3