ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
Revision: 1.26
Committed: Thu Nov 6 18:13:31 2014 UTC (9 years, 8 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.25: +1 -1 lines
Log Message:
Fix typo.

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