ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
Revision: 1.7
Committed: Thu May 17 18:06:38 2012 UTC (12 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +6 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #ifndef ESTL_H_
2 #define ESTL_H_
3
4 #include <stdlib.h>
5 #include <string.h>
6
7 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
8 template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
9
10 template<typename T, typename U> static inline void swap (T& a, U& b) { T t = a; a = (T)b; b = (U)t; }
11
12 template <typename I, typename T>
13 I find (I first, I last, const T& value)
14 {
15 while (first != last && *first != value)
16 ++first;
17
18 return first;
19 }
20
21 /* simplevec taken (and heavily modified), from:
22 *
23 * MICO --- a free CORBA implementation
24 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
25 * originally GPLv2 or any later
26 */
27 template<class T>
28 struct simplevec
29 {
30 typedef T *iterator;
31 typedef const T *const_iterator;
32 typedef unsigned long size_type;
33
34 private:
35 size_type _last, _size;
36 T *_buf;
37
38 public:
39 const_iterator begin () const { return &_buf[0]; }
40 iterator begin () { return &_buf[0]; }
41
42 const_iterator end () const { return &_buf[_last]; }
43 iterator end () { return &_buf[_last]; }
44
45 size_type capacity () const { return _size; }
46 size_type size () const { return _last; }
47
48 private:
49 static T *alloc (size_type n)
50 {
51 return (T *)::operator new ((size_t) (n * sizeof (T)));
52 }
53
54 static void dealloc (T *buf)
55 {
56 if (buf)
57 ::operator delete (buf);
58 }
59
60 size_type good_size (size_type n)
61 {
62 return max (n, _size ? _size * 2 : 5);
63 }
64
65 void reserve (iterator where, size_type n)
66 {
67 if (_last + n <= _size)
68 memmove (where + n, where, (end () - where) * sizeof (T));
69 else
70 {
71 size_type sz = _last + n;
72 sz = good_size (sz);
73 T *nbuf = alloc (sz);
74
75 if (_buf)
76 {
77 memcpy (nbuf, begin (), (where - begin ()) * sizeof (T));
78 memcpy (nbuf + (where - begin ()) + n, where, (end () - where) * sizeof (T));
79 dealloc (_buf);
80 }
81
82 _buf = nbuf;
83 _size = sz;
84 }
85 }
86
87 public:
88 void reserve (size_type sz)
89 {
90 if (_size < sz)
91 {
92 sz = good_size (sz);
93 T *nbuf = alloc (sz);
94
95 if (_buf)
96 {
97 memcpy (nbuf, begin (), size () * sizeof (T));
98 dealloc (_buf);
99 }
100
101 _buf = nbuf;
102 _size = sz;
103 }
104 }
105
106 void resize (size_type sz)
107 {
108 reserve (sz);
109 _last = sz;
110 }
111
112 simplevec ()
113 : _last(0), _size(0), _buf(0)
114 {
115 }
116
117 simplevec (size_type n, const T& t = T ())
118 : _last(0), _size(0), _buf(0)
119 {
120 insert (begin (), n, t);
121 }
122
123 simplevec (const_iterator first, const_iterator last)
124 : _last(0), _size(0), _buf(0)
125 {
126 insert (begin (), first, last);
127 }
128
129 simplevec (const simplevec<T> &v)
130 : _last(0), _size(0), _buf(0)
131 {
132 reserve (v._last);
133 memcpy (_buf, v.begin (), v.size () * sizeof (T));
134 _last = v._last;
135 }
136
137 simplevec<T> &operator= (const simplevec<T> &v)
138 {
139 if (this != &v)
140 {
141 _last = 0;
142 reserve (v._last);
143 memcpy (_buf, v.begin (), v.size () * sizeof (T));
144 _last = v._last;
145 }
146
147 return *this;
148 }
149
150 ~simplevec ()
151 {
152 dealloc (_buf);
153 }
154
155 const T &front () const { return _buf[ 0]; }
156 T &front () { return _buf[ 0]; }
157 const T &back () const { return _buf[_last-1]; }
158 T &back () { return _buf[_last-1]; }
159
160 bool empty () const
161 {
162 return _last == 0;
163 }
164
165 void clear ()
166 {
167 _last = 0;
168 }
169
170 void push_back (const T &t)
171 {
172 reserve (_last+1);
173 *end () = t;
174 ++_last;
175 }
176
177 void push_back (T &t)
178 {
179 reserve (_last+1);
180 *end () = t;
181 ++_last;
182 }
183
184 void pop_back ()
185 {
186 --_last;
187 }
188
189 const T &operator [](size_type idx) const { return _buf[idx]; }
190 T &operator [](size_type idx) { return _buf[idx]; }
191
192 iterator insert (iterator pos, const T &t)
193 {
194 long at = pos - begin ();
195 reserve (pos, 1);
196 pos = begin () + at;
197 *pos = t;
198 ++_last;
199 return pos;
200 }
201
202 iterator insert (iterator pos, const_iterator first, const_iterator last)
203 {
204 long n = last - first;
205 long at = pos - begin ();
206
207 if (n > 0)
208 {
209 reserve (pos, n);
210 pos = begin () + at;
211 memcpy (pos, first, (last - first) * sizeof (T));
212 _last += n;
213 }
214
215 return pos;
216 }
217
218 iterator insert (iterator pos, size_type n, const T &t)
219 {
220 long at = pos - begin ();
221
222 if (n > 0)
223 {
224 reserve (pos, n);
225 pos = begin () + at;
226 for (int i = 0; i < n; ++i)
227 pos[i] = t;
228 _last += n;
229 }
230
231 return pos;
232 }
233
234 void erase (iterator first, iterator last)
235 {
236 if (last != first)
237 {
238 memmove (first, last, (end () - last) * sizeof (T));
239 _last -= last - first;
240 }
241 }
242
243 void erase (iterator pos)
244 {
245 if (pos != end ())
246 {
247 memmove (pos, pos + 1, (end () - (pos + 1)) * sizeof (T));
248 --_last;
249 }
250 }
251
252 void swap (simplevec<T> &t)
253 {
254 ::swap (_last, t._last);
255 ::swap (_size, t._size);
256 ::swap (_buf, t._buf);
257 }
258 };
259
260 template<class T>
261 bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2)
262 {
263 if (v1.size () != v2.size ()) return false;
264
265 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size () * sizeof (T));
266 }
267
268 template<class T>
269 bool operator <(const simplevec<T> &v1, const simplevec<T> &v2)
270 {
271 unsigned long minlast = min (v1.size (), v2.size ());
272
273 for (unsigned long i = 0; i < minlast; ++i)
274 {
275 if (v1[i] < v2[i]) return true;
276 if (v2[i] < v1[i]) return false;
277 }
278 return v1.size () < v2.size ();
279 }
280
281 template<typename T>
282 struct vector : simplevec<T>
283 {
284 };
285
286 #endif
287