1 /*
2 *
3 * Copyright (c) 1994
4 * Hewlett-Packard Company
5 *
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 *
15 * Copyright (c) 1996
16 * Silicon Graphics Computer Systems, Inc.
17 *
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
25 */
26
27 /*
28 Copyright (c) 2007 Lao wen bo
29
30 This software is provided 'as-is', without any express or implied
31 warranty. In no event will the authors be held liable for any damages
32 arising from the use of this software.
33
34 Permission is granted to anyone to use this software for any purpose,
35 including commercial applications, and to alter it and redistribute it
36 freely, subject to the following restrictions:
37
38 1. The origin of this software must not be misrepresented; you must not
39 claim that you wrote the original software. If you use this software
40 in a product, an acknowledgment in the product documentation would be
41 appreciated but is not required.
42
43 2. Altered source versions must be plainly marked as such, and must not be
44 misrepresented as being the original software.
45
46 3. This notice may not be removed or altered from any source
47 distribution.
48
49 Lao wen bo
50 viewpl(at)gmail.com
51 */
52
53 #include "c_vector.h"
54 #include "c_algo.h"
55 #include "c_memory.h"
56
57 typedef value_type node_t;
58 typedef node_t * pnode_t;
59
60 typedef struct _c_vector_impl
61 {
62 pnode_t _start;
63 pnode_t _finish;
64 pnode_t _end_of_storage;
65 } _c_vector_impl;
66
67 static c_iterator _A_get_iterator(pnode_t pn);
68 static c_reverse_iterator _A_get_reverse_iterator(pnode_t pn);
69
_abs(signed int i)70 int _abs(signed int i){
71 return ((i >> 31) ^ i) - (i >> 31);
72 }
73
_c_vector_iterator_assign(c_piterator thiz,const c_piterator val)74 static c_iterator _c_vector_iterator_assign(c_piterator thiz, const c_piterator val)
75 {
76 if(thiz != val)
77 thiz->_i = val->_i;
78 return *thiz;
79 }
80
_c_vector_iterator_ref(c_piterator thiz)81 static value_type _c_vector_iterator_ref(c_piterator thiz)
82 {
83 return *(pnode_t)thiz->_i;
84 }
85
_c_vector_iterator_ref_assign(c_piterator thiz,const value_type val)86 static value_type _c_vector_iterator_ref_assign(c_piterator thiz, const value_type val)
87 {
88 return *(pnode_t)thiz->_i = val;
89 }
90
_c_vector_iterator_inc(c_piterator thiz)91 static c_iterator _c_vector_iterator_inc(c_piterator thiz)
92 {
93 pnode_t pn = thiz->_i;
94 ++ pn;
95 thiz->_i = pn;
96 return *thiz;
97 }
98
_c_vector_iterator_inc_n(c_piterator thiz,difference_type n)99 static c_iterator _c_vector_iterator_inc_n(c_piterator thiz, difference_type n)
100 {
101 pnode_t pn = thiz->_i;
102 pn += n;
103 thiz->_i = pn;
104 return *thiz;
105 }
106
_c_vector_iterator_dec(c_piterator thiz)107 static c_iterator _c_vector_iterator_dec(c_piterator thiz)
108 {
109 pnode_t pn = thiz->_i;
110 -- pn;
111 thiz->_i = pn;
112 return *thiz;
113 }
114
_c_vector_iterator_dec_n(c_piterator thiz,difference_type n)115 static c_iterator _c_vector_iterator_dec_n(c_piterator thiz, difference_type n)
116 {
117 pnode_t pn = thiz->_i;
118 pn -= n;
119 thiz->_i = pn;
120 return *thiz;
121 }
122
_c_vector_iterator_diff(c_piterator thiz,const c_piterator val)123 static difference_type _c_vector_iterator_diff(c_piterator thiz, const c_piterator val)
124 {
125 return ((pnode_t)thiz->_i - (pnode_t)val->_i);
126 }
127
_c_vector_iterator_at(c_piterator thiz,difference_type n)128 static value_type _c_vector_iterator_at(c_piterator thiz, difference_type n)
129 {
130 return *((pnode_t)thiz->_i + n);
131 }
132
_c_vector_iterator_positive_n(c_piterator thiz,difference_type n)133 static c_iterator _c_vector_iterator_positive_n(c_piterator thiz, difference_type n)
134 {
135 c_iterator iter;
136 pnode_t pn = thiz->_i;
137 pn += n;
138 iter = _A_get_iterator(pn);
139 return iter;
140 }
141
_c_vector_iterator_negative_n(c_piterator thiz,difference_type n)142 static c_iterator _c_vector_iterator_negative_n(c_piterator thiz, difference_type n)
143 {
144 c_iterator iter;
145 pnode_t pn = thiz->_i;
146 pn -= n;
147 iter = _A_get_iterator(pn);
148 return iter;
149 }
150
_c_vector_iterator_equal(c_piterator thiz,const c_piterator val)151 static c_bool _c_vector_iterator_equal(c_piterator thiz, const c_piterator val)
152 {
153 return (thiz->_i == val->_i &&
154 thiz->_pft == val->_pft);
155 }
156
_c_vector_iterator_less(c_piterator thiz,const c_piterator val)157 static c_bool _c_vector_iterator_less(c_piterator thiz, const c_piterator val)
158 {
159 return ((pnode_t)thiz->_i < (pnode_t)val->_i);
160 }
161
162 static c_iterator_ft _c_vector_iter_ft =
163 {
164 _c_vector_iterator_assign,
165 _c_vector_iterator_ref,
166 _c_vector_iterator_ref_assign,
167 _c_vector_iterator_inc,
168 _c_vector_iterator_inc_n,
169 _c_vector_iterator_dec,
170 _c_vector_iterator_dec_n,
171 _c_vector_iterator_diff,
172 _c_vector_iterator_at,
173 _c_vector_iterator_positive_n,
174 _c_vector_iterator_negative_n,
175 _c_vector_iterator_equal,
176 _c_vector_iterator_less
177 };
178
_c_vector_reverse_iterator_assign(c_preverse_iterator thiz,const c_preverse_iterator val)179 static c_reverse_iterator _c_vector_reverse_iterator_assign(c_preverse_iterator thiz, const c_preverse_iterator val)
180 {
181 if(thiz != val)
182 thiz->_i = val->_i;
183 return *thiz;
184 }
185
_c_vector_reverse_iterator_ref(c_preverse_iterator thiz)186 static value_type _c_vector_reverse_iterator_ref(c_preverse_iterator thiz)
187 {
188 return *(pnode_t)thiz->_i;
189 }
190
_c_vector_reverse_iterator_ref_assign(c_preverse_iterator thiz,const value_type val)191 static value_type _c_vector_reverse_iterator_ref_assign(c_preverse_iterator thiz, const value_type val)
192 {
193 return *(pnode_t)thiz->_i = val;
194 }
195
_c_vector_reverse_iterator_inc(c_preverse_iterator thiz)196 static c_reverse_iterator _c_vector_reverse_iterator_inc(c_preverse_iterator thiz)
197 {
198 pnode_t pn = thiz->_i;
199 -- pn;
200 thiz->_i = pn;
201 return *thiz;
202 }
203
_c_vector_reverse_iterator_inc_n(c_preverse_iterator thiz,difference_type n)204 static c_reverse_iterator _c_vector_reverse_iterator_inc_n(c_preverse_iterator thiz, difference_type n)
205 {
206 pnode_t pn = thiz->_i;
207 pn -= n;
208 thiz->_i = pn;
209 return *thiz;
210 }
211
_c_vector_reverse_iterator_dec(c_preverse_iterator thiz)212 static c_reverse_iterator _c_vector_reverse_iterator_dec(c_preverse_iterator thiz)
213 {
214 pnode_t pn = thiz->_i;
215 ++ pn;
216 thiz->_i = pn;
217 return *thiz;
218 }
219
_c_vector_reverse_iterator_dec_n(c_preverse_iterator thiz,difference_type n)220 static c_reverse_iterator _c_vector_reverse_iterator_dec_n(c_preverse_iterator thiz, difference_type n)
221 {
222 pnode_t pn = thiz->_i;
223 pn += n;
224 thiz->_i = pn;
225 return *thiz;
226 }
227
_c_vector_reverse_iterator_diff(c_preverse_iterator thiz,const c_preverse_iterator val)228 static difference_type _c_vector_reverse_iterator_diff(c_preverse_iterator thiz, const c_preverse_iterator val)
229 {
230 return ((pnode_t)val->_i - (pnode_t)thiz->_i);
231 }
232
_c_vector_reverse_iterator_at(c_preverse_iterator thiz,difference_type n)233 static value_type _c_vector_reverse_iterator_at(c_preverse_iterator thiz, difference_type n)
234 {
235 return *((pnode_t)thiz->_i - n);
236 }
237
_c_vector_reverse_iterator_positive_n(c_preverse_iterator thiz,difference_type n)238 static c_reverse_iterator _c_vector_reverse_iterator_positive_n(c_preverse_iterator thiz, difference_type n)
239 {
240 c_reverse_iterator iter;
241 pnode_t pn = thiz->_i;
242 pn -= n;
243 iter = _A_get_reverse_iterator(pn);
244 return iter;
245 }
246
_c_vector_reverse_iterator_negative_n(c_preverse_iterator thiz,difference_type n)247 static c_reverse_iterator _c_vector_reverse_iterator_negative_n(c_preverse_iterator thiz, difference_type n)
248 {
249 c_reverse_iterator iter;
250 pnode_t pn = thiz->_i;
251 pn += n;
252 iter = _A_get_reverse_iterator(pn);
253 return iter;
254 }
255
_c_vector_reverse_iterator_equal(c_preverse_iterator thiz,const c_preverse_iterator val)256 static c_bool _c_vector_reverse_iterator_equal(c_preverse_iterator thiz, const c_preverse_iterator val)
257 {
258 return (thiz->_i == val->_i &&
259 thiz->_pft == val->_pft);
260 }
261
_c_vector_reverse_iterator_less(c_preverse_iterator thiz,const c_preverse_iterator val)262 static c_bool _c_vector_reverse_iterator_less(c_preverse_iterator thiz, const c_preverse_iterator val)
263 {
264 return ((pnode_t)thiz->_i > (pnode_t)val->_i);
265 }
266
267 static c_reverse_iterator_ft _c_vector_rev_iter_ft =
268 {
269 _c_vector_reverse_iterator_assign,
270 _c_vector_reverse_iterator_ref,
271 _c_vector_reverse_iterator_ref_assign,
272 _c_vector_reverse_iterator_inc,
273 _c_vector_reverse_iterator_inc_n,
274 _c_vector_reverse_iterator_dec,
275 _c_vector_reverse_iterator_dec_n,
276 _c_vector_reverse_iterator_diff,
277 _c_vector_reverse_iterator_at,
278 _c_vector_reverse_iterator_positive_n,
279 _c_vector_reverse_iterator_negative_n,
280 _c_vector_reverse_iterator_equal,
281 _c_vector_reverse_iterator_less
282 };
283
_A_get_iterator(pnode_t pn)284 static c_iterator _A_get_iterator(pnode_t pn)
285 {
286 c_iterator iter;
287 iter._pft = &_c_vector_iter_ft;
288 iter._i = pn;
289 return iter;
290 }
291
_A_get_reverse_iterator(pnode_t pn)292 static c_reverse_iterator _A_get_reverse_iterator(pnode_t pn)
293 {
294 c_reverse_iterator iter;
295 iter._pft = &_c_vector_rev_iter_ft;
296 iter._i = pn;
297 return iter;
298 }
299
_A_allocate(c_pvector thiz,size_t n)300 static pnode_t _A_allocate(c_pvector thiz, size_t n)
301 {
302 return __c_malloc(n * sizeof(node_t));
303 }
304
_A_deallocate(c_pvector thiz,pnode_t p,size_t n)305 static void _A_deallocate(c_pvector thiz, pnode_t p, size_t n)
306 {
307 if(n > 0)
308 __c_free(p);
309 }
310
_A_allocate_and_copy(c_pvector thiz,size_type n,c_const_iterator first,c_const_iterator last)311 static c_iterator _A_allocate_and_copy(c_pvector thiz, size_type n, c_const_iterator first, c_const_iterator last)
312 {
313 c_iterator result = _A_get_iterator(_A_allocate(thiz, n));
314 c_uninitialized_copy(first, last, result);
315 return result;
316 }
317
_A_insert_aux1(c_pvector thiz,c_iterator pos,node_t val)318 static void _A_insert_aux1(c_pvector thiz, c_iterator pos, node_t val)
319 {
320 _c_vector_impl * pl = thiz->_l;
321 if(pl->_finish != pl->_end_of_storage)
322 {
323 node_t node;
324 *pl->_finish = *(pl->_finish - 1);
325 ++ pl->_finish;
326 node = val;
327 c_copy_backward(pos,
328 _A_get_iterator(pl->_finish - 2),
329 _A_get_iterator(pl->_finish - 1));
330 ITER_REF_ASSIGN(pos, node);
331 }
332 else
333 {
334 const size_type old_size = c_vector_size(thiz);
335 const size_type len = old_size != 0 ? 2 * old_size : 1;
336 c_iterator new_start = _A_get_iterator(_A_allocate(thiz, len));
337 c_iterator new_finish = new_start;
338 new_finish = c_copy(_A_get_iterator(pl->_start), pos, new_start);
339 ITER_REF_ASSIGN(new_finish, val);
340 ITER_INC(new_finish);
341 new_finish = c_copy(pos, _A_get_iterator(pl->_finish), new_finish);
342 _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start));
343 pl->_start = new_start._i;
344 pl->_finish = new_finish._i;
345 pl->_end_of_storage = pl->_start + len;
346 }
347 }
348
__c_vector(c_pvector thiz,COMPARER pcmp)349 void __c_vector(c_pvector thiz, COMPARER pcmp)
350 {
351 thiz->_cmp = pcmp;
352 thiz->_l = __c_malloc(sizeof(_c_vector_impl));
353 ((_c_vector_impl *)thiz->_l)->_start = NULL;
354 ((_c_vector_impl *)thiz->_l)->_finish = NULL;
355 ((_c_vector_impl *)thiz->_l)->_end_of_storage = NULL;
356 }
357
__c_rotcev(c_pvector thiz)358 void __c_rotcev(c_pvector thiz)
359 {
360 _c_vector_impl * pl = thiz->_l;
361 _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start));
362 __c_free(thiz->_l);
363 }
364
c_vector_begin(c_pvector thiz)365 c_iterator c_vector_begin(c_pvector thiz)
366 {
367 return _A_get_iterator(((_c_vector_impl*)thiz->_l)->_start);
368 }
369
c_vector_end(c_pvector thiz)370 c_iterator c_vector_end(c_pvector thiz)
371 {
372 return _A_get_iterator(((_c_vector_impl*)thiz->_l)->_finish);
373 }
374
c_vector_rbegin(c_pvector thiz)375 c_reverse_iterator c_vector_rbegin(c_pvector thiz)
376 {
377 return _A_get_reverse_iterator(((_c_vector_impl*)thiz->_l)->_finish - 1);
378 }
379
c_vector_rend(c_pvector thiz)380 c_reverse_iterator c_vector_rend(c_pvector thiz)
381 {
382 return _A_get_reverse_iterator(((_c_vector_impl*)thiz->_l)->_start - 1);
383 }
384
c_vector_size(c_pvector thiz)385 size_type c_vector_size(c_pvector thiz)
386 {
387 c_iterator b = c_vector_begin(thiz), e = c_vector_end(thiz);
388 return abs(ITER_DIFF(e, b));
389 }
390
c_vector_max_size(c_pvector thiz)391 size_type c_vector_max_size(c_pvector thiz)
392 {
393 return (size_type)(-1) / sizeof(node_t);
394 }
395
c_vector_capacity(c_pvector thiz)396 size_type c_vector_capacity(c_pvector thiz)
397 {
398 _c_vector_impl * pl = thiz->_l;
399 return abs(pl->_end_of_storage - pl->_start);
400 }
401
c_vector_empty(c_pvector thiz)402 c_bool c_vector_empty(c_pvector thiz)
403 {
404 _c_vector_impl * pl = thiz->_l;
405 return (pl->_start == pl->_finish);
406 }
407
c_vector_at(c_pvector thiz,size_type n)408 value_type c_vector_at(c_pvector thiz, size_type n)
409 {
410 _c_vector_impl * pl = thiz->_l;
411 return *(pl->_start + n);
412 }
413
c_vector_assign(c_pvector thiz,const c_pvector V)414 c_pvector c_vector_assign(c_pvector thiz, const c_pvector V)
415 {
416 if(V != thiz)
417 {
418 _c_vector_impl * pl = thiz->_l;
419 const size_type vlen = c_vector_size(V);
420 if(vlen > c_vector_capacity(thiz))
421 {
422 c_iterator tmp = _A_allocate_and_copy(thiz,
423 vlen,
424 c_vector_begin(V),
425 c_vector_end(V));
426 _A_deallocate(thiz,
427 pl->_start,
428 abs(pl->_end_of_storage - pl->_start));
429 pl->_start = tmp._i;
430 pl->_end_of_storage = pl->_start + vlen;
431 }
432 else if(c_vector_size(thiz) >= vlen)
433 {
434 c_copy(c_vector_begin(V), c_vector_end(V), c_vector_begin(thiz));
435 }
436 else
437 {
438 c_iterator bg = c_vector_begin(thiz);
439 c_copy(bg, ITER_POSITIVE_N(bg, c_vector_size(thiz)), _A_get_iterator(pl->_start));
440 c_uninitialized_copy(ITER_POSITIVE_N(bg, c_vector_size(thiz)),
441 c_vector_end(thiz),
442 _A_get_iterator(pl->_finish));
443 }
444 pl->_finish = pl->_start + vlen;
445 }
446 return thiz;
447 }
448
c_vector_reserve(c_pvector thiz,size_t n)449 void c_vector_reserve(c_pvector thiz, size_t n)
450 {
451 if(c_vector_capacity(thiz) < n)
452 {
453 _c_vector_impl * pl = thiz->_l;
454 const size_type old_size = c_vector_size(thiz);
455 c_iterator tmp = _A_allocate_and_copy(thiz, n, c_vector_begin(thiz), c_vector_end(thiz));
456 _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start));
457 pl->_start = tmp._i;
458 pl->_finish = pl->_start + old_size;
459 pl->_end_of_storage = pl->_start + n;
460 }
461 }
462
c_vector_front(c_pvector thiz)463 value_type c_vector_front(c_pvector thiz)
464 {
465 _c_vector_impl * pl = thiz->_l;
466 return *pl->_start;
467 }
468
c_vector_back(c_pvector thiz)469 value_type c_vector_back(c_pvector thiz)
470 {
471 _c_vector_impl * pl = thiz->_l;
472 return *(pl->_finish - 1);
473 }
474
c_vector_push_back(c_pvector thiz,const value_type val)475 void c_vector_push_back(c_pvector thiz, const value_type val)
476 {
477 _c_vector_impl * pl = thiz->_l;
478 if(pl->_finish != pl->_end_of_storage)
479 {
480 *pl->_finish = val;
481 ++ pl->_finish;
482 }
483 else
484 _A_insert_aux1(thiz, c_vector_end(thiz), val);
485 }
486
c_vector_pop_back(c_pvector thiz)487 void c_vector_pop_back(c_pvector thiz)
488 {
489 _c_vector_impl * pl = thiz->_l;
490 -- pl->_finish;
491 }
492
c_vector_swap(c_pvector thiz,c_pvector V)493 void c_vector_swap(c_pvector thiz, c_pvector V)
494 {
495 c_vector tmp;
496 C_SWAP(*thiz, *V, tmp);
497 }
498
c_vector_insert(c_pvector thiz,c_iterator pos,const value_type val)499 c_iterator c_vector_insert(c_pvector thiz, c_iterator pos, const value_type val)
500 {
501 c_iterator begin = c_vector_begin(thiz);
502 c_iterator end = c_vector_end(thiz);
503 size_type n = abs(ITER_DIFF(pos, begin));
504 _c_vector_impl * pl = thiz->_l;
505 if((pl->_finish != pl->_end_of_storage) &&
506 ITER_EQUAL(pos, end))
507 {
508 *pl->_finish = val;
509 ++ pl->_finish;
510 }
511 else
512 _A_insert_aux1(thiz, pos, val);
513 return ITER_POSITIVE_N(begin, n);
514 }
515
c_vector_insert2(c_pvector thiz,c_iterator pos,c_iterator first,c_iterator last)516 void c_vector_insert2(c_pvector thiz, c_iterator pos, c_iterator first, c_iterator last)
517 {
518 if(!ITER_EQUAL(first, last))
519 {
520 _c_vector_impl * pl = thiz->_l;
521 difference_type dn = 0;
522 size_type n = 0;
523 c_distance1(first, last, &dn);
524 n = abs(dn);
525 if(abs(pl->_end_of_storage - pl->_finish) >= n)
526 {
527 const size_type elems_after = pl->_finish - (pnode_t)pos._i;
528 c_iterator old_finish = c_vector_end(thiz);
529 if(elems_after > n)
530 {
531 c_uninitialized_copy(_A_get_iterator(pl->_finish - n),
532 _A_get_iterator(pl->_finish),
533 _A_get_iterator(pl->_finish));
534 pl->_finish += n;
535 c_copy_backward(pos, ITER_NEGATIVE_N(old_finish, n), old_finish);
536 c_copy(first, last, pos);
537 }
538 else
539 {
540 c_uninitialized_copy(ITER_POSITIVE_N(first, elems_after),
541 last,
542 _A_get_iterator(pl->_finish));
543 pl->_finish += n - elems_after;
544 c_uninitialized_copy(pos, old_finish, _A_get_iterator(pl->_finish));
545 pl->_finish += elems_after;
546 c_copy(first, ITER_POSITIVE_N(first, elems_after), pos);
547 }
548 }
549 else
550 {
551 const size_type old_size = c_vector_size(thiz);
552 const size_type len = old_size + C_MAX(old_size, n);
553 c_iterator new_start = _A_get_iterator(_A_allocate(thiz, len));
554 c_iterator new_finish = new_start;
555 new_finish = c_uninitialized_copy(_A_get_iterator(pl->_start), pos, new_start);
556 new_finish = c_uninitialized_copy(first, last, new_finish);
557 new_finish = c_uninitialized_copy(pos, _A_get_iterator(pl->_finish), new_finish);
558 _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start));
559 pl->_start = new_start._i;
560 pl->_finish = new_finish._i;
561 pl->_end_of_storage = ITER_POSITIVE_N(new_start, len)._i;
562 }
563 }
564 }
565
c_vector_fill_insert(c_pvector thiz,c_iterator pos,size_type n,const value_type val)566 void c_vector_fill_insert(c_pvector thiz, c_iterator pos, size_type n, const value_type val)
567 {
568 _c_vector_impl * pl = thiz->_l;
569 if(n != 0)
570 {
571 if(abs(pl->_end_of_storage - pl->_finish) >= n)
572 {
573 value_type val_copy = val;
574 const size_type elems_after = pl->_finish - (pnode_t)pos._i;
575 c_iterator old_finish = c_vector_end(thiz);
576 if(elems_after > n)
577 {
578 c_uninitialized_copy(_A_get_iterator(pl->_finish - n),
579 _A_get_iterator(pl->_finish),
580 _A_get_iterator(pl->_finish));
581 pl->_finish += n;
582 c_copy_backward(pos,
583 ITER_NEGATIVE_N(old_finish, n),
584 old_finish);
585 c_fill(pos, ITER_POSITIVE_N(pos, n), val_copy);
586 }
587 else
588 {
589 c_uninitialized_fill_n(c_vector_end(thiz), n - elems_after, val_copy);
590 pl->_finish += n - elems_after;
591 c_uninitialized_copy(pos, old_finish, c_vector_end(thiz));
592 pl->_finish += elems_after;
593 c_fill(pos, old_finish, val_copy);
594 }
595 }
596 else
597 {
598 const size_type old_size = c_vector_size(thiz);
599 const size_type len = old_size + C_MAX(old_size, n);
600 c_iterator new_start = _A_get_iterator(_A_allocate(thiz, len));
601 c_iterator new_finish = new_start;
602 new_finish = c_uninitialized_copy(_A_get_iterator(pl->_start),
603 pos,
604 new_start);
605 new_finish = c_uninitialized_fill_n(new_finish, n, val);
606 new_finish = c_uninitialized_copy(pos, _A_get_iterator(pl->_finish), new_finish);
607 _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start));
608 pl->_start = new_start._i;
609 pl->_finish = new_finish._i;
610 pl->_end_of_storage = pl->_start + len;
611 }
612 }
613 }
614
c_vector_erase(c_pvector thiz,c_iterator pos)615 c_iterator c_vector_erase(c_pvector thiz, c_iterator pos)
616 {
617 c_iterator pos_1 = ITER_POSITIVE_N(pos, 1);
618 c_iterator end = c_vector_end(thiz);
619 _c_vector_impl * pl = thiz->_l;
620 if(!ITER_EQUAL(pos_1, end))
621 c_copy(pos_1, _A_get_iterator(pl->_finish), pos);
622 -- pl->_finish;
623 return pos;
624 }
625
c_vector_erase2(c_pvector thiz,c_iterator first,c_iterator last)626 c_iterator c_vector_erase2(c_pvector thiz, c_iterator first, c_iterator last)
627 {
628 _c_vector_impl * pl = thiz->_l;
629 c_copy(last, _A_get_iterator(pl->_finish), first);
630 pl->_finish = pl->_finish - abs(ITER_DIFF(last, first));
631 return first;
632 }
633
c_vector_clear(c_pvector thiz)634 void c_vector_clear(c_pvector thiz)
635 {
636 c_vector_erase2(thiz, c_vector_begin(thiz), c_vector_end(thiz));
637 }
638
c_vector_resize(c_pvector thiz,size_t n)639 void c_vector_resize(c_pvector thiz, size_t n)
640 {
641 c_iterator begin = c_vector_begin(thiz);
642 if(n < c_vector_size(thiz))
643 c_vector_erase2(thiz, ITER_POSITIVE_N(begin, n), c_vector_end(thiz));
644 else
645 c_vector_fill_insert(thiz, c_vector_end(thiz), n, NULL);
646 }
647
c_vector_equal(c_pvector thiz,const c_pvector V)648 c_bool c_vector_equal(c_pvector thiz, const c_pvector V)
649 {
650 c_binary_predicate bp = c_binary_negate(thiz->_cmp);
651 return (c_vector_size(thiz) == c_vector_size(V)) &&
652 c_equal2(c_vector_begin(thiz),
653 c_vector_end(thiz),
654 c_vector_begin(V),
655 bp);
656 }
657
c_vector_less(c_pvector thiz,const c_pvector V)658 c_bool c_vector_less(c_pvector thiz, const c_pvector V)
659 {
660 return c_lexicographical_compare(c_vector_begin(thiz),
661 c_vector_end(thiz),
662 c_vector_begin(V),
663 c_vector_end(V),
664 thiz->_cmp);
665 }
666