ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/estl.h
(Generate patch)

Comparing libptytty/src/estl.h (file contents):
Revision 1.2 by sf-exg, Sat Jan 21 13:44:38 2012 UTC vs.
Revision 1.3 by root, Sat Jan 21 13:56:07 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines