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.10 by root, Sat May 19 01:19:03 2012 UTC vs.
Revision 1.11 by root, Sat May 19 01:56:09 2012 UTC

92 if (!is_simple_enough ()) 92 if (!is_simple_enough ())
93 while (n--) 93 while (n--)
94 (*a++).~T (); 94 (*a++).~T ();
95 } 95 }
96 96
97 template<class I>
97 static void cop_new (iterator a, iterator b) { new (a) T (*b); } 98 static void cop_new (iterator a, I b) { new (a) T (*b); }
99 template<class I>
98 static void cop_set (iterator a, iterator b) { *a = *b ; } 100 static void cop_set (iterator a, I b) { *a = *b ; }
99 101
100 // these copy helpers actually use the copy constructor, not assignment 102 template<class I>
103 static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new)
104 {
105 while (n--)
106 op (dst++, src++);
107 }
108
101 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 109 static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
102 { 110 {
103 if (is_simple_enough ()) 111 if (is_simple_enough ())
104 memmove (dst, src, sizeof (T) * n); 112 memmove (dst, src, sizeof (T) * n);
105 else 113 else
106 while (n--) 114 copy_lower<iterator> (dst, src, n, cop_new);
107 op (dst++, src++);
108 } 115 }
109 116
110 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 117 static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
111 { 118 {
112 if (is_simple_enough ()) 119 if (is_simple_enough ())
114 else 121 else
115 while (n--) 122 while (n--)
116 op (dst + n, src + n); 123 op (dst + n, src + n);
117 } 124 }
118 125
126 template<class I>
127 static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new)
128 {
129 copy_lower<I> (dst, src, n, op);
130 }
131
119 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) 132 static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new)
120 { 133 {
121 if (is_simple_enough ()) 134 if (is_simple_enough ())
122 memcpy (dst, src, sizeof (T) * n); 135 memcpy (dst, src, sizeof (T) * n);
123 else 136 else
124 copy_lower (dst, src, n, op); 137 copy<iterator> (dst, src, n, op);
125 } 138 }
126 139
127 static T *alloc (size_type n) ecb_cold 140 static T *alloc (size_type n) ecb_cold
128 { 141 {
129 return (T *)::operator new ((size_t) (sizeof (T) * n)); 142 return (T *)::operator new ((size_t) (sizeof (T) * n));
161 174
162public: 175public:
163 size_type capacity () const { return res; } 176 size_type capacity () const { return res; }
164 size_type size () const { return sze; } 177 size_type size () const { return sze; }
165 bool empty () const { return size () == 0; } 178 bool empty () const { return size () == 0; }
179
180 size_t max_size () const
181 {
182 return (~(size_type)0) >> 1;
183 }
166 184
167 const_iterator begin () const { return &buf [ 0]; } 185 const_iterator begin () const { return &buf [ 0]; }
168 iterator begin () { return &buf [ 0]; } 186 iterator begin () { return &buf [ 0]; }
169 const_iterator end () const { return &buf [sze ]; } 187 const_iterator end () const { return &buf [sze ]; }
170 iterator end () { return &buf [sze ]; } 188 iterator end () { return &buf [sze ]; }
205 : sze(0), res(0), buf(0) 223 : sze(0), res(0), buf(0)
206 { 224 {
207 } 225 }
208 226
209 simplevec (size_type n, const T &t = T ()) 227 simplevec (size_type n, const T &t = T ())
210 : sze(0), res(0), buf(0)
211 { 228 {
212 insert (begin (), n, t); 229 sze = res = n;
213 } 230 buf = alloc (sze);
214 231
215 simplevec (const_iterator first, const_iterator last) 232 while (n--)
216 : sze(0), res(0), buf(0) 233 new (buf + n) T (t);
234 }
235
236 template<class I>
237 simplevec (I first, I last)
217 { 238 {
218 insert (begin (), first, last); 239 sze = res = last - first;
240 buf = alloc (sze);
241 copy (buf, first, sze);
219 } 242 }
220 243
221 simplevec (const simplevec<T> &v) 244 simplevec (const simplevec<T> &v)
222 : sze(0), res(0), buf(0) 245 : sze(0), res(0), buf(0)
223 { 246 {
224 insert (begin (), v.begin (), v.end ()); 247 sze = res = v.size ();
225 } 248 buf = alloc (sze);
226 249 copy (buf, v.begin (), sze);
227 simplevec<T> &operator= (const simplevec<T> &v)
228 {
229 swap (simplevec<T> (v));
230 return *this;
231 } 250 }
232 251
233 ~simplevec () 252 ~simplevec ()
234 { 253 {
235 dealloc (); 254 dealloc ();
257 void pop_back () 276 void pop_back ()
258 { 277 {
259 destruct (buf + --sze); 278 destruct (buf + --sze);
260 } 279 }
261 280
262 const T &operator [](size_type idx) const { return buf[idx]; } 281 const_reference operator [](size_type idx) const { return buf[idx]; }
263 T &operator [](size_type idx) { return buf[idx]; } 282 reference operator [](size_type idx) { return buf[idx]; }
283
284 const_reference at (size_type idx) const { return buf [idx]; }
285 reference at (size_type idx) { return buf [idx]; }
286
287 template<class I>
288 void assign (I first, I last)
289 {
290 swap (simplevec<T> (first, last));
291 }
292
293 void assign (size_type n, const T &t)
294 {
295 swap (simplevec<T> (n, t));
296 }
297
298 simplevec<T> &operator= (const simplevec<T> &v)
299 {
300 assign (v.begin (), v.end ());
301 return *this;
302 }
264 303
265 iterator insert (iterator pos, const T &t) 304 iterator insert (iterator pos, const T &t)
266 { 305 {
267 size_type at = pos - begin (); 306 size_type at = pos - begin ();
268 307
270 buf [at] = t; 309 buf [at] = t;
271 310
272 return buf + at; 311 return buf + at;
273 } 312 }
274 313
314 template<class I>
275 iterator insert (iterator pos, const_iterator first, const_iterator last) 315 iterator insert (iterator pos, I first, I last)
276 { 316 {
277 size_type n = last - first; 317 size_type n = last - first;
278 size_type at = pos - begin (); 318 size_type at = pos - begin ();
279 319
280 ins (pos, n); 320 ins (pos, n);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines