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

# 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.14 // MUST copy forwards
104 root 1.11 template<class I>
105     static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new)
106     {
107 root 1.13 while (n--)
108     op (dst++, src++);
109 root 1.11 }
110    
111 root 1.9 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
112 root 1.8 {
113     if (is_simple_enough ())
114     memcpy (dst, src, sizeof (T) * n);
115     else
116 root 1.11 copy<iterator> (dst, src, n, op);
117 root 1.9 }
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 root 1.8 }
134    
135     void ins (iterator where, size_type n)
136 root 1.3 {
137 root 1.9 size_type pos = where - begin ();
138    
139     if (ecb_expect_false (sze + n > res))
140 root 1.3 {
141 root 1.9 res = good_size (sze + n);
142 root 1.3
143 root 1.9 T *nbuf = alloc (res);
144     copy (nbuf, buf, sze, cop_new);
145     dealloc ();
146     buf = nbuf;
147 root 1.3 }
148 root 1.9
149     construct (buf + sze, n);
150 root 1.14
151 root 1.9 sze += n;
152 root 1.14
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 root 1.3 }
160 sf-exg 1.1
161     public:
162 root 1.9 size_type capacity () const { return res; }
163     size_type size () const { return sze; }
164     bool empty () const { return size () == 0; }
165    
166 root 1.11 size_t max_size () const
167     {
168     return (~(size_type)0) >> 1;
169     }
170    
171 root 1.9 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 root 1.3 void reserve (size_type sz)
181     {
182 root 1.9 if (ecb_expect_true (sz <= res))
183     return;
184 root 1.3
185 root 1.9 sz = good_size (sz);
186     T *nbuf = alloc (sz);
187 root 1.3
188 root 1.9 copy (nbuf, begin (), sze);
189     dealloc ();
190    
191     buf = nbuf;
192     res = sz;
193 root 1.3 }
194    
195 root 1.7 void resize (size_type sz)
196     {
197     reserve (sz);
198 root 1.8
199     if (is_simple_enough ())
200 root 1.9 sze = sz;
201 root 1.8 else
202     {
203 root 1.9 while (sze < sz) construct (buf + sze++);
204     while (sze > sz) destruct (buf + --sze);
205 root 1.8 }
206 root 1.7 }
207    
208 root 1.3 simplevec ()
209 root 1.9 : sze(0), res(0), buf(0)
210 root 1.3 {
211     }
212    
213 root 1.9 simplevec (size_type n, const T &t = T ())
214 root 1.3 {
215 root 1.11 sze = res = n;
216     buf = alloc (sze);
217    
218     while (n--)
219     new (buf + n) T (t);
220 root 1.3 }
221    
222 root 1.11 template<class I>
223     simplevec (I first, I last)
224 root 1.3 {
225 root 1.11 sze = res = last - first;
226     buf = alloc (sze);
227     copy (buf, first, sze);
228 root 1.3 }
229    
230     simplevec (const simplevec<T> &v)
231 root 1.9 : sze(0), res(0), buf(0)
232 root 1.3 {
233 root 1.11 sze = res = v.size ();
234     buf = alloc (sze);
235     copy (buf, v.begin (), sze);
236 root 1.3 }
237    
238     ~simplevec ()
239     {
240 root 1.8 dealloc ();
241 root 1.3 }
242    
243 root 1.9 void swap (simplevec<T> &t)
244 root 1.3 {
245 root 1.9 ::swap (sze, t.sze);
246     ::swap (res, t.res);
247     ::swap (buf, t.buf);
248 root 1.3 }
249    
250     void clear ()
251     {
252 root 1.9 destruct (buf, sze);
253     sze = 0;
254 root 1.3 }
255    
256     void push_back (const T &t)
257     {
258 root 1.9 reserve (sze + 1);
259     new (buf + sze++) T (t);
260 root 1.3 }
261    
262     void pop_back ()
263     {
264 root 1.9 destruct (buf + --sze);
265 root 1.3 }
266    
267 root 1.11 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 root 1.3
290     iterator insert (iterator pos, const T &t)
291     {
292 root 1.8 size_type at = pos - begin ();
293 root 1.10
294 root 1.8 ins (pos, 1);
295 root 1.10 buf [at] = t;
296    
297     return buf + at;
298 root 1.3 }
299    
300 root 1.11 template<class I>
301     iterator insert (iterator pos, I first, I last)
302 root 1.3 {
303 root 1.8 size_type n = last - first;
304     size_type at = pos - begin ();
305 root 1.3
306 root 1.9 ins (pos, n);
307 root 1.10 copy (buf + at, first, n, cop_set);
308 root 1.3
309 root 1.10 return buf + at;
310 root 1.3 }
311    
312     iterator insert (iterator pos, size_type n, const T &t)
313     {
314 root 1.8 size_type at = pos - begin ();
315 root 1.3
316 root 1.9 ins (pos, n);
317    
318 root 1.10 for (iterator i = buf + at; n--; )
319     *i++ = t;
320 root 1.3
321 root 1.10 return buf + at;
322 root 1.3 }
323    
324     void erase (iterator first, iterator last)
325     {
326 root 1.9 size_t n = last - first;
327 root 1.8
328 root 1.13 if (is_simple_enough ())
329     memmove (first, last, sizeof (T) * n);
330     else
331     copy<iterator> (first, last, n, cop_new);
332    
333 root 1.9 sze -= n;
334     destruct (buf + sze, n);
335 root 1.3 }
336    
337     void erase (iterator pos)
338     {
339     if (pos != end ())
340 root 1.8 erase (pos, pos + 1);
341 root 1.3 }
342 sf-exg 1.1 };
343    
344     template<class T>
345 root 1.3 bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)
346 sf-exg 1.1 {
347 root 1.3 if (v1.size () != v2.size ()) return false;
348    
349     return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size () * sizeof (T));
350 sf-exg 1.1 }
351    
352     template<class T>
353 root 1.3 bool operator <(const simplevec<T> &v1, const simplevec<T> &v2)
354 sf-exg 1.1 {
355 root 1.3 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 sf-exg 1.1 }
362 root 1.3 return v1.size () < v2.size ();
363 sf-exg 1.1 }
364    
365     template<typename T>
366     struct vector : simplevec<T>
367     {
368     };
369 sf-exg 1.2
370     #endif
371 root 1.3