c_pvector c_vector_assign(c_pvector thiz, const c_pvector V) { if(V != thiz) { _c_vector_impl * pl = (_c_vector_impl *)thiz->_l; const size_type vlen = c_vector_size(V); if(vlen > c_vector_capacity(thiz)) { c_iterator tmp = _A_allocate_and_copy(thiz, vlen, c_vector_begin(V), c_vector_end(V)); _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start)); pl->_start = (pnode_t)tmp._i; pl->_end_of_storage = pl->_start + vlen; } else if(c_vector_size(thiz) >= vlen) { c_copy(c_vector_begin(V), c_vector_end(V), c_vector_begin(thiz)); } else { c_iterator bg = c_vector_begin(thiz); c_copy(bg, ITER_POSITIVE_N(bg, c_vector_size(thiz)), _A_get_iterator(pl->_start)); c_uninitialized_copy(ITER_POSITIVE_N(bg, c_vector_size(thiz)), c_vector_end(thiz), _A_get_iterator(pl->_finish)); } pl->_finish = pl->_start + vlen; } return thiz; }
void c_vector_resize(c_pvector thiz, size_t n) { c_iterator begin = c_vector_begin(thiz); if(n < c_vector_size(thiz)) c_vector_erase2(thiz, ITER_POSITIVE_N(begin, n), c_vector_end(thiz)); else c_vector_fill_insert(thiz, c_vector_end(thiz), n, NULL); }
c_bool c_vector_less(c_pvector thiz, const c_pvector V) { return c_lexicographical_compare(c_vector_begin(thiz), c_vector_end(thiz), c_vector_begin(V), c_vector_end(V), thiz->_cmp); }
void c_vector_fill_insert(c_pvector thiz, c_iterator pos, size_type n, const value_type val) { _c_vector_impl * pl = (_c_vector_impl *)thiz->_l; if(n != 0) { if(abs(pl->_end_of_storage - pl->_finish) >= (int)n) { value_type val_copy = val; const size_type elems_after = pl->_finish - (pnode_t)pos._i; c_iterator old_finish = c_vector_end(thiz); if(elems_after > n) { c_uninitialized_copy(_A_get_iterator(pl->_finish - n), _A_get_iterator(pl->_finish), _A_get_iterator(pl->_finish)); pl->_finish += n; c_copy_backward(pos, ITER_NEGATIVE_N(old_finish, n), old_finish); c_fill(pos, ITER_POSITIVE_N(pos, n), val_copy); } else { c_uninitialized_fill_n(c_vector_end(thiz), n - elems_after, val_copy); pl->_finish += n - elems_after; c_uninitialized_copy(pos, old_finish, c_vector_end(thiz)); pl->_finish += elems_after; c_fill(pos, old_finish, val_copy); } } else { const size_type old_size = c_vector_size(thiz); const size_type len = old_size + C_MAX(old_size, n); c_iterator new_start = _A_get_iterator(_A_allocate(thiz, len)); c_iterator new_finish = new_start; new_finish = c_uninitialized_copy(_A_get_iterator(pl->_start), pos, new_start); new_finish = c_uninitialized_fill_n(new_finish, n, val); new_finish = c_uninitialized_copy(pos, _A_get_iterator(pl->_finish), new_finish); _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start)); pl->_start = (pnode_t)new_start._i; pl->_finish = (pnode_t)new_finish._i; pl->_end_of_storage = pl->_start + len; } } }
static void print_vector(c_vector * p) { c_iterator first, last; first = c_vector_begin(p); last = c_vector_end(p); print_vector2(first, last); }
static int print_queue(c_pqueue ps) { c_vector tmpvt; __c_vector(&tmpvt, int_comparer); printf("queue is : \n"); while(!c_queue_empty(ps)) { int * tmp = c_queue_front(ps); printf("front is : %d, size is : %d, back is : %d\n", *tmp, c_queue_size(ps), *(int *)c_queue_back(ps)); c_queue_pop(ps); c_vector_push_back(&tmpvt, tmp); } // recover queue do { c_iterator iter = c_vector_begin(&tmpvt); c_iterator end = c_vector_end(&tmpvt); for(; !ITER_EQUAL(iter, end); ITER_INC(iter)) { c_queue_push(ps, ITER_REF(iter)); } } while(0); __c_rotcev(&tmpvt); return 0; }
static void vector_insert(c_vector * p) { c_iterator iter; c_vector_insert(p, c_vector_end(p), &array[0]); c_vector_insert(p, c_vector_begin(p), &array[2]); c_vector_insert(p, c_vector_end(p), &array[4]); c_vector_insert(p, c_vector_end(p), &array[6]); c_vector_insert(p, c_vector_begin(p), &array[8]); iter = c_vector_begin(p); iter = ITER_POSITIVE_N(iter, 3); c_vector_insert(p, iter, &array[5]); printf("vector after insertion\n"); print_vector(p); }
c_bool c_vector_equal(c_pvector thiz, const c_pvector V) { c_binary_predicate bp = c_binary_negate(thiz->_cmp); return (c_vector_size(thiz) == c_vector_size(V)) && c_equal2(c_vector_begin(thiz), c_vector_end(thiz), c_vector_begin(V), bp); }
c_iterator c_vector_erase(c_pvector thiz, c_iterator pos) { c_iterator pos_1 = ITER_POSITIVE_N(pos, 1); c_iterator end = c_vector_end(thiz); _c_vector_impl * pl = (_c_vector_impl *)thiz->_l; if(!ITER_EQUAL(pos_1, end)) c_copy(pos_1, _A_get_iterator(pl->_finish), pos); -- pl->_finish; return pos; }
void c_vector_push_back(c_pvector thiz, const value_type val) { _c_vector_impl * pl = (_c_vector_impl *)thiz->_l; if(pl->_finish != pl->_end_of_storage) { *pl->_finish = val; ++ pl->_finish; } else _A_insert_aux1(thiz, c_vector_end(thiz), val); }
void c_vector_reserve(c_pvector thiz, size_t n) { if(c_vector_capacity(thiz) < n) { _c_vector_impl * pl = (_c_vector_impl *)thiz->_l; const size_type old_size = c_vector_size(thiz); c_iterator tmp = _A_allocate_and_copy(thiz, n, c_vector_begin(thiz), c_vector_end(thiz)); _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start)); pl->_start = (pnode_t)tmp._i; pl->_finish = pl->_start + old_size; pl->_end_of_storage = pl->_start + n; } }
static void vector_insert2(c_vector * p) { c_vector vt; __c_vector(&vt, int_comparer); c_vector_insert2(&vt, c_vector_begin(&vt), c_vector_begin(p), c_vector_end(p)); printf("after insert2\n"); print_vector(&vt); __c_rotcev(&vt); }
void c_vector_insert2(c_pvector thiz, c_iterator pos, c_iterator first, c_iterator last) { if(!ITER_EQUAL(first, last)) { _c_vector_impl * pl = (_c_vector_impl *)thiz->_l; difference_type dn = 0; size_type n = 0; c_distance1(first, last, &dn); n = abs(dn); if(abs(pl->_end_of_storage - pl->_finish) >= (int)n) { const size_type elems_after = pl->_finish - (pnode_t)pos._i; c_iterator old_finish = c_vector_end(thiz); if(elems_after > n) { c_uninitialized_copy(_A_get_iterator(pl->_finish - n), _A_get_iterator(pl->_finish), _A_get_iterator(pl->_finish)); pl->_finish += n; c_copy_backward(pos, ITER_NEGATIVE_N(old_finish, n), old_finish); c_copy(first, last, pos); } else { c_uninitialized_copy(ITER_POSITIVE_N(first, elems_after), last, _A_get_iterator(pl->_finish)); pl->_finish += n - elems_after; c_uninitialized_copy(pos, old_finish, _A_get_iterator(pl->_finish)); pl->_finish += elems_after; c_copy(first, ITER_POSITIVE_N(first, elems_after), pos); } } else { const size_type old_size = c_vector_size(thiz); const size_type len = old_size + C_MAX(old_size, n); c_iterator new_start = _A_get_iterator(_A_allocate(thiz, len)); c_iterator new_finish = new_start; new_finish = c_uninitialized_copy(_A_get_iterator(pl->_start), pos, new_start); new_finish = c_uninitialized_copy(first, last, new_finish); new_finish = c_uninitialized_copy(pos, _A_get_iterator(pl->_finish), new_finish); _A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start)); pl->_start = (pnode_t)new_start._i; pl->_finish = (pnode_t)new_finish._i; pl->_end_of_storage = (pnode_t)ITER_POSITIVE_N(new_start, len)._i; } } }
c_iterator c_vector_insert(c_pvector thiz, c_iterator pos, const value_type val) { c_iterator begin = c_vector_begin(thiz); c_iterator end = c_vector_end(thiz); size_type n = abs(ITER_DIFF(pos, begin)); _c_vector_impl * pl = (_c_vector_impl *)thiz->_l; if((pl->_finish != pl->_end_of_storage) && ITER_EQUAL(pos, end)) { *pl->_finish = val; ++ pl->_finish; } else _A_insert_aux1(thiz, pos, val); return ITER_POSITIVE_N(begin, n); }
static void vector_fill_insert(c_vector * p) { c_iterator iter = c_vector_begin(p); c_vector_fill_insert(p, ITER_INC(iter), 10, &array[8]); printf("after fill insert middle\n"); print_vector(p); c_vector_fill_insert(p, c_vector_begin(p), 10, &array[6]); printf("after fill insert begin\n"); print_vector(p); c_vector_fill_insert(p, c_vector_end(p), 10, &array[4]); printf("after fill insert end\n"); print_vector(p); }
static int create_with_insert_unique2(c_pmap thiz) { c_vector vt; int i = 0; c_vector_create(&vt, int_comparer); for(i = 0; i < sizeof(values) / sizeof(int); ++ i) { c_iterator v_beg, v_end; c_vector_push_back(&vt, &pairs[i]); v_beg = c_vector_begin(&vt); v_end = c_vector_end(&vt); c_map_insert2(thiz, v_beg, v_end); assert(__c_rb_tree_verify(thiz->_l)); } c_vector_destroy(&vt); return 0; }
static void erase_vector() { c_vector vt; c_vector * p = &vt; c_iterator i1, i3, i4, i6, i10; __c_vector(&vt, int_comparer); create_with_push_back(&vt); i1 = c_vector_begin(p); print_vector(p); printf("erase last :\n"); i10 = ITER_POSITIVE_N(i1, 10); c_vector_erase(p, i10); print_vector(p); printf("erase middle 1 : \n"); i4 = ITER_POSITIVE_N(i1, 4); c_vector_erase(p, i4); print_vector(p); printf("erase middle n : \n"); i3 = ITER_POSITIVE_N(i1, 3); i6 = ITER_POSITIVE_N(i1, 6); c_vector_erase2(p, i3, i6); print_vector(p); printf("erase first :\n"); c_vector_erase(p, i1); print_vector(p); printf("erase all :\n"); c_vector_erase2(p, c_vector_begin(p), c_vector_end(p)); print_vector(p); __c_rotcev(&vt); }
void c_vector_clear(c_pvector thiz) { c_vector_erase2(thiz, c_vector_begin(thiz), c_vector_end(thiz)); }
size_type c_vector_size(c_pvector thiz) { c_iterator b = c_vector_begin(thiz), e = c_vector_end(thiz); return abs(ITER_DIFF(e, b)); }