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.9 by root, Sat May 19 01:03:36 2012 UTC vs.
Revision 1.12 by root, Sat May 19 01:57:09 2012 UTC

27#endif 27#endif
28 28
29// original version taken from MICO, but this has been completely rewritten 29// original version taken from MICO, but this has been completely rewritten
30// known limitations w.r.t. std::vector 30// known limitations w.r.t. std::vector
31// - many methods missing 31// - many methods missing
32// - no error checking, no exceptions thrown 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 33// - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements
34// - no allocator support 34// - no allocator support
35// - we don't really care about const correctness, but we try 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 36// - we don't care about namespaces and stupid macros the user might define
37// - no bool specialisation
37template<class T> 38template<class T>
38struct simplevec 39struct simplevec
39{ 40{
40#if ESTL_BIG_VECTOR 41#if ESTL_BIG_VECTOR
41 // shoudl use size_t/ssize_t, but that's not portable enough for us 42 // shoudl use size_t/ssize_t, but that's not portable enough for us
92 if (!is_simple_enough ()) 93 if (!is_simple_enough ())
93 while (n--) 94 while (n--)
94 (*a++).~T (); 95 (*a++).~T ();
95 } 96 }
96 97
98 template<class I>
97 static void cop_new (iterator a, iterator b) { new (a) T (*b); } 99 static void cop_new (iterator a, I b) { new (a) T (*b); }
100 template<class I>
98 static void cop_set (iterator a, iterator b) { *a = *b ; } 101 static void cop_set (iterator a, I b) { *a = *b ; }
99 102
100 // these copy helpers actually use the copy constructor, not assignment 103 template<class I>
104 static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new)
105 {
106 while (n--)
107 op (dst++, src++);
108 }
109
101 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 110 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
102 { 111 {
103 if (is_simple_enough ()) 112 if (is_simple_enough ())
104 memmove (dst, src, sizeof (T) * n); 113 memmove (dst, src, sizeof (T) * n);
105 else 114 else
106 while (n--) 115 copy_lower<iterator> (dst, src, n, cop_new);
107 op (dst++, src++);
108 } 116 }
109 117
110 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 118 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
111 { 119 {
112 if (is_simple_enough ()) 120 if (is_simple_enough ())
114 else 122 else
115 while (n--) 123 while (n--)
116 op (dst + n, src + n); 124 op (dst + n, src + n);
117 } 125 }
118 126
127 template<class I>
128 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new)
129 {
130 copy_lower<I> (dst, src, n, op);
131 }
132
119 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 133 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
120 { 134 {
121 if (is_simple_enough ()) 135 if (is_simple_enough ())
122 memcpy (dst, src, sizeof (T) * n); 136 memcpy (dst, src, sizeof (T) * n);
123 else 137 else
124 copy_lower (dst, src, n, op); 138 copy<iterator> (dst, src, n, op);
125 } 139 }
126 140
127 static T *alloc (size_type n) ecb_cold 141 static T *alloc (size_type n) ecb_cold
128 { 142 {
129 return (T *)::operator new ((size_t) (sizeof (T) * n)); 143 return (T *)::operator new ((size_t) (sizeof (T) * n));
161 175
162public: 176public:
163 size_type capacity () const { return res; } 177 size_type capacity () const { return res; }
164 size_type size () const { return sze; } 178 size_type size () const { return sze; }
165 bool empty () const { return size () == 0; } 179 bool empty () const { return size () == 0; }
180
181 size_t max_size () const
182 {
183 return (~(size_type)0) >> 1;
184 }
166 185
167 const_iterator begin () const { return &buf [ 0]; } 186 const_iterator begin () const { return &buf [ 0]; }
168 iterator begin () { return &buf [ 0]; } 187 iterator begin () { return &buf [ 0]; }
169 const_iterator end () const { return &buf [sze ]; } 188 const_iterator end () const { return &buf [sze ]; }
170 iterator end () { return &buf [sze ]; } 189 iterator end () { return &buf [sze ]; }
205 : sze(0), res(0), buf(0) 224 : sze(0), res(0), buf(0)
206 { 225 {
207 } 226 }
208 227
209 simplevec (size_type n, const T &t = T ()) 228 simplevec (size_type n, const T &t = T ())
210 : sze(0), res(0), buf(0)
211 { 229 {
212 insert (begin (), n, t); 230 sze = res = n;
213 } 231 buf = alloc (sze);
214 232
215 simplevec (const_iterator first, const_iterator last) 233 while (n--)
216 : sze(0), res(0), buf(0) 234 new (buf + n) T (t);
235 }
236
237 template<class I>
238 simplevec (I first, I last)
217 { 239 {
218 insert (begin (), first, last); 240 sze = res = last - first;
241 buf = alloc (sze);
242 copy (buf, first, sze);
219 } 243 }
220 244
221 simplevec (const simplevec<T> &v) 245 simplevec (const simplevec<T> &v)
222 : sze(0), res(0), buf(0) 246 : sze(0), res(0), buf(0)
223 { 247 {
224 insert (begin (), v.begin (), v.end ()); 248 sze = res = v.size ();
225 } 249 buf = alloc (sze);
226 250 copy (buf, v.begin (), sze);
227 simplevec<T> &operator= (const simplevec<T> &v)
228 {
229 swap (simplevec<T> (v));
230 return *this;
231 } 251 }
232 252
233 ~simplevec () 253 ~simplevec ()
234 { 254 {
235 dealloc (); 255 dealloc ();
257 void pop_back () 277 void pop_back ()
258 { 278 {
259 destruct (buf + --sze); 279 destruct (buf + --sze);
260 } 280 }
261 281
262 const T &operator [](size_type idx) const { return buf[idx]; } 282 const_reference operator [](size_type idx) const { return buf[idx]; }
263 T &operator [](size_type idx) { return buf[idx]; } 283 reference operator [](size_type idx) { return buf[idx]; }
284
285 const_reference at (size_type idx) const { return buf [idx]; }
286 reference at (size_type idx) { return buf [idx]; }
287
288 template<class I>
289 void assign (I first, I last)
290 {
291 swap (simplevec<T> (first, last));
292 }
293
294 void assign (size_type n, const T &t)
295 {
296 swap (simplevec<T> (n, t));
297 }
298
299 simplevec<T> &operator= (const simplevec<T> &v)
300 {
301 assign (v.begin (), v.end ());
302 return *this;
303 }
264 304
265 iterator insert (iterator pos, const T &t) 305 iterator insert (iterator pos, const T &t)
266 { 306 {
267 size_type at = pos - begin (); 307 size_type at = pos - begin ();
308
268 ins (pos, 1); 309 ins (pos, 1);
269 buf [pos] = t; 310 buf [at] = t;
270 return pos;
271 }
272 311
312 return buf + at;
313 }
314
315 template<class I>
273 iterator insert (iterator pos, const_iterator first, const_iterator last) 316 iterator insert (iterator pos, I first, I last)
274 { 317 {
275 size_type n = last - first; 318 size_type n = last - first;
276 size_type at = pos - begin (); 319 size_type at = pos - begin ();
277 320
278 ins (pos, n); 321 ins (pos, n);
279 copy (pos, first, n, cop_set); 322 copy (buf + at, first, n, cop_set);
280 323
281 return pos; 324 return buf + at;
282 } 325 }
283 326
284 iterator insert (iterator pos, size_type n, const T &t) 327 iterator insert (iterator pos, size_type n, const T &t)
285 { 328 {
286 size_type at = pos - begin (); 329 size_type at = pos - begin ();
287 330
288 ins (pos, n); 331 ins (pos, n);
289 332
290 for (size_type i = 0; i < n; ++i) 333 for (iterator i = buf + at; n--; )
291 buf [at + i] = t; 334 *i++ = t;
292 335
293 return pos; 336 return buf + at;
294 } 337 }
295 338
296 void erase (iterator first, iterator last) 339 void erase (iterator first, iterator last)
297 { 340 {
298 size_t n = last - first; 341 size_t n = last - first;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines