1 Star 2 Fork 4

KigKrazy/tstl2cl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
c_vector.c 17.19 KB
一键复制 编辑 原始数据 按行查看 历史
KigKrazy 提交于 2016-05-16 10:24 . 1.0
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/*
Copyright (c) 2007 Wenbo Lao(Rob Lao)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Wenbo Lao(Rob Lao)
viewpl(at)gmail.com
*/
#include "c_vector.h"
#include "c_algo.h"
#include "c_memory.h"
typedef value_type node_t;
typedef node_t * pnode_t;
typedef struct _c_vector_impl
{
pnode_t _start;
pnode_t _finish;
pnode_t _end_of_storage;
} _c_vector_impl;
static c_iterator _A_get_iterator(pnode_t pn);
static c_reverse_iterator _A_get_reverse_iterator(pnode_t pn);
static c_iterator _c_vector_iterator_assign(c_piterator thiz, const c_piterator val)
{
if(thiz != val)
thiz->_i = val->_i;
return *thiz;
}
static value_type _c_vector_iterator_ref(c_piterator thiz)
{
return *(pnode_t)thiz->_i;
}
static value_type _c_vector_iterator_ref_assign(c_piterator thiz, const value_type val)
{
return *(pnode_t)thiz->_i = val;
}
static c_iterator _c_vector_iterator_inc(c_piterator thiz)
{
pnode_t pn = thiz->_i;
++ pn;
thiz->_i = pn;
return *thiz;
}
static c_iterator _c_vector_iterator_inc_n(c_piterator thiz, difference_type n)
{
pnode_t pn = thiz->_i;
pn += n;
thiz->_i = pn;
return *thiz;
}
static c_iterator _c_vector_iterator_dec(c_piterator thiz)
{
pnode_t pn = thiz->_i;
-- pn;
thiz->_i = pn;
return *thiz;
}
static c_iterator _c_vector_iterator_dec_n(c_piterator thiz, difference_type n)
{
pnode_t pn = thiz->_i;
pn -= n;
thiz->_i = pn;
return *thiz;
}
static difference_type _c_vector_iterator_diff(c_piterator thiz, const c_piterator val)
{
return ((pnode_t)thiz->_i - (pnode_t)val->_i);
}
static value_type _c_vector_iterator_at(c_piterator thiz, difference_type n)
{
return *((pnode_t)thiz->_i + n);
}
static c_iterator _c_vector_iterator_positive_n(c_piterator thiz, difference_type n)
{
c_iterator iter;
pnode_t pn = thiz->_i;
pn += n;
iter = _A_get_iterator(pn);
return iter;
}
static c_iterator _c_vector_iterator_negative_n(c_piterator thiz, difference_type n)
{
c_iterator iter;
pnode_t pn = thiz->_i;
pn -= n;
iter = _A_get_iterator(pn);
return iter;
}
static c_bool _c_vector_iterator_equal(c_piterator thiz, const c_piterator val)
{
return (thiz->_i == val->_i &&
thiz->_pft == val->_pft);
}
static c_bool _c_vector_iterator_less(c_piterator thiz, const c_piterator val)
{
return ((pnode_t)thiz->_i < (pnode_t)val->_i);
}
static c_iterator_ft _c_vector_iter_ft =
{
_c_vector_iterator_assign,
_c_vector_iterator_ref,
_c_vector_iterator_ref_assign,
_c_vector_iterator_inc,
_c_vector_iterator_inc_n,
_c_vector_iterator_dec,
_c_vector_iterator_dec_n,
_c_vector_iterator_diff,
_c_vector_iterator_at,
_c_vector_iterator_positive_n,
_c_vector_iterator_negative_n,
_c_vector_iterator_equal,
_c_vector_iterator_less
};
static c_reverse_iterator _c_vector_reverse_iterator_assign(c_preverse_iterator thiz, const c_preverse_iterator val)
{
if(thiz != val)
thiz->_i = val->_i;
return *thiz;
}
static value_type _c_vector_reverse_iterator_ref(c_preverse_iterator thiz)
{
return *(pnode_t)thiz->_i;
}
static value_type _c_vector_reverse_iterator_ref_assign(c_preverse_iterator thiz, const value_type val)
{
return *(pnode_t)thiz->_i = val;
}
static c_reverse_iterator _c_vector_reverse_iterator_inc(c_preverse_iterator thiz)
{
pnode_t pn = thiz->_i;
-- pn;
thiz->_i = pn;
return *thiz;
}
static c_reverse_iterator _c_vector_reverse_iterator_inc_n(c_preverse_iterator thiz, difference_type n)
{
pnode_t pn = thiz->_i;
pn -= n;
thiz->_i = pn;
return *thiz;
}
static c_reverse_iterator _c_vector_reverse_iterator_dec(c_preverse_iterator thiz)
{
pnode_t pn = thiz->_i;
++ pn;
thiz->_i = pn;
return *thiz;
}
static c_reverse_iterator _c_vector_reverse_iterator_dec_n(c_preverse_iterator thiz, difference_type n)
{
pnode_t pn = thiz->_i;
pn += n;
thiz->_i = pn;
return *thiz;
}
static difference_type _c_vector_reverse_iterator_diff(c_preverse_iterator thiz, const c_preverse_iterator val)
{
return ((pnode_t)val->_i - (pnode_t)thiz->_i);
}
static value_type _c_vector_reverse_iterator_at(c_preverse_iterator thiz, difference_type n)
{
return *((pnode_t)thiz->_i - n);
}
static c_reverse_iterator _c_vector_reverse_iterator_positive_n(c_preverse_iterator thiz, difference_type n)
{
c_reverse_iterator iter;
pnode_t pn = thiz->_i;
pn -= n;
iter = _A_get_reverse_iterator(pn);
return iter;
}
static c_reverse_iterator _c_vector_reverse_iterator_negative_n(c_preverse_iterator thiz, difference_type n)
{
c_reverse_iterator iter;
pnode_t pn = thiz->_i;
pn += n;
iter = _A_get_reverse_iterator(pn);
return iter;
}
static c_bool _c_vector_reverse_iterator_equal(c_preverse_iterator thiz, const c_preverse_iterator val)
{
return (thiz->_i == val->_i &&
thiz->_pft == val->_pft);
}
static c_bool _c_vector_reverse_iterator_less(c_preverse_iterator thiz, const c_preverse_iterator val)
{
return ((pnode_t)thiz->_i > (pnode_t)val->_i);
}
static c_reverse_iterator_ft _c_vector_rev_iter_ft =
{
_c_vector_reverse_iterator_assign,
_c_vector_reverse_iterator_ref,
_c_vector_reverse_iterator_ref_assign,
_c_vector_reverse_iterator_inc,
_c_vector_reverse_iterator_inc_n,
_c_vector_reverse_iterator_dec,
_c_vector_reverse_iterator_dec_n,
_c_vector_reverse_iterator_diff,
_c_vector_reverse_iterator_at,
_c_vector_reverse_iterator_positive_n,
_c_vector_reverse_iterator_negative_n,
_c_vector_reverse_iterator_equal,
_c_vector_reverse_iterator_less
};
static c_iterator _A_get_iterator(pnode_t pn)
{
c_iterator iter;
iter._pft = &_c_vector_iter_ft;
iter._i = pn;
return iter;
}
static c_reverse_iterator _A_get_reverse_iterator(pnode_t pn)
{
c_reverse_iterator iter;
iter._pft = &_c_vector_rev_iter_ft;
iter._i = pn;
return iter;
}
static pnode_t _A_allocate(c_pvector thiz, size_t n)
{
return __c_malloc(n * sizeof(node_t));
}
static void _A_deallocate(c_pvector thiz, pnode_t p, size_t n)
{
if(n > 0)
__c_free(p);
}
static c_iterator _A_allocate_and_copy(c_pvector thiz, size_type n, c_const_iterator first, c_const_iterator last)
{
c_iterator result = _A_get_iterator(_A_allocate(thiz, n));
c_uninitialized_copy(first, last, result);
return result;
}
static void _A_insert_aux1(c_pvector thiz, c_iterator pos, node_t val)
{
_c_vector_impl * pl = thiz->_l;
if(pl->_finish != pl->_end_of_storage)
{
node_t node;
*pl->_finish = *(pl->_finish - 1);
++ pl->_finish;
node = val;
c_copy_backward(pos,
_A_get_iterator(pl->_finish - 2),
_A_get_iterator(pl->_finish - 1));
ITER_REF_ASSIGN(pos, node);
}
else
{
const size_type old_size = c_vector_size(thiz);
const size_type len = old_size != 0 ? 2 * old_size : 1;
c_iterator new_start = _A_get_iterator(_A_allocate(thiz, len));
c_iterator new_finish = new_start;
new_finish = c_copy(_A_get_iterator(pl->_start), pos, new_start);
ITER_REF_ASSIGN(new_finish, val);
ITER_INC(new_finish);
new_finish = c_copy(pos, _A_get_iterator(pl->_finish), new_finish);
_A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start));
pl->_start = new_start._i;
pl->_finish = new_finish._i;
pl->_end_of_storage = pl->_start + len;
}
}
void __c_vector(c_pvector thiz, COMPARER pcmp)
{
thiz->_cmp = pcmp;
thiz->_l = __c_malloc(sizeof(_c_vector_impl));
((_c_vector_impl *)thiz->_l)->_start = NULL;
((_c_vector_impl *)thiz->_l)->_finish = NULL;
((_c_vector_impl *)thiz->_l)->_end_of_storage = NULL;
}
void __c_rotcev(c_pvector thiz)
{
_c_vector_impl * pl = thiz->_l;
_A_deallocate(thiz, pl->_start, abs(pl->_end_of_storage - pl->_start));
__c_free(thiz->_l);
}
c_iterator c_vector_begin(c_pvector thiz)
{
return _A_get_iterator(((_c_vector_impl*)thiz->_l)->_start);
}
c_iterator c_vector_end(c_pvector thiz)
{
return _A_get_iterator(((_c_vector_impl*)thiz->_l)->_finish);
}
c_reverse_iterator c_vector_rbegin(c_pvector thiz)
{
return _A_get_reverse_iterator(((_c_vector_impl*)thiz->_l)->_finish - 1);
}
c_reverse_iterator c_vector_rend(c_pvector thiz)
{
return _A_get_reverse_iterator(((_c_vector_impl*)thiz->_l)->_start - 1);
}
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));
}
size_type c_vector_max_size(c_pvector thiz)
{
return (size_type)(-1) / sizeof(node_t);
}
size_type c_vector_capacity(c_pvector thiz)
{
_c_vector_impl * pl = thiz->_l;
return abs(pl->_end_of_storage - pl->_start);
}
c_bool c_vector_empty(c_pvector thiz)
{
_c_vector_impl * pl = thiz->_l;
return (pl->_start == pl->_finish);
}
value_type c_vector_at(c_pvector thiz, size_type n)
{
_c_vector_impl * pl = thiz->_l;
return *(pl->_start + n);
}
c_pvector c_vector_assign(c_pvector thiz, const c_pvector V)
{
if(V != thiz)
{
_c_vector_impl * pl = 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 = 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_reserve(c_pvector thiz, size_t n)
{
if(c_vector_capacity(thiz) < n)
{
_c_vector_impl * pl = 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 = tmp._i;
pl->_finish = pl->_start + old_size;
pl->_end_of_storage = pl->_start + n;
}
}
value_type c_vector_front(c_pvector thiz)
{
_c_vector_impl * pl = thiz->_l;
return *pl->_start;
}
value_type c_vector_back(c_pvector thiz)
{
_c_vector_impl * pl = thiz->_l;
return *(pl->_finish - 1);
}
void c_vector_push_back(c_pvector thiz, const value_type val)
{
_c_vector_impl * pl = 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_pop_back(c_pvector thiz)
{
_c_vector_impl * pl = thiz->_l;
-- pl->_finish;
}
void c_vector_swap(c_pvector thiz, c_pvector V)
{
c_vector tmp;
C_SWAP(*thiz, *V, tmp);
}
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 = 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);
}
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 = 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) >= 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 = new_start._i;
pl->_finish = new_finish._i;
pl->_end_of_storage = ITER_POSITIVE_N(new_start, len)._i;
}
}
}
void c_vector_fill_insert(c_pvector thiz, c_iterator pos, size_type n, const value_type val)
{
_c_vector_impl * pl = thiz->_l;
if(n != 0)
{
if(abs(pl->_end_of_storage - pl->_finish) >= 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 = new_start._i;
pl->_finish = new_finish._i;
pl->_end_of_storage = pl->_start + len;
}
}
}
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 = thiz->_l;
if(!ITER_EQUAL(pos_1, end))
c_copy(pos_1, _A_get_iterator(pl->_finish), pos);
-- pl->_finish;
return pos;
}
c_iterator c_vector_erase2(c_pvector thiz, c_iterator first, c_iterator last)
{
_c_vector_impl * pl = thiz->_l;
c_copy(last, _A_get_iterator(pl->_finish), first);
pl->_finish = pl->_finish - abs(ITER_DIFF(last, first));
return first;
}
void c_vector_clear(c_pvector thiz)
{
c_vector_erase2(thiz, c_vector_begin(thiz), c_vector_end(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_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_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);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/KigKrazy/tstl2cl.git
git@gitee.com:KigKrazy/tstl2cl.git
KigKrazy
tstl2cl
tstl2cl
master

搜索帮助