-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathcstl_vector_aux.c
240 lines (202 loc) · 8.16 KB
/
cstl_vector_aux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* The implement of vector auxiliary function.
* Copyright (C) 2008 - 2013 Wangbo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author e-mail: activesys.wb@gmail.com
* activesys@sina.com.cn
*/
/** include section **/
#include <cstl/cstl_def.h>
#include <cstl/cstl_alloc.h>
#include <cstl/cstl_types.h>
#include <cstl/citerator.h>
#include <cstl/cvector.h>
#include "cstl_vector_aux.h"
/** local constant declaration and local macro section **/
#define _VECTOR_CAPACITY_THRESHOLD_SIZE 16 /* capacity threshold size */
/** local data type declaration and local struct, union, enum section **/
/** local function prototype section **/
/** exported global variable definition section **/
/** local global variable definition section **/
/** exported function implementation section **/
#ifndef NDEBUG
/**
* Test iterator referenced data is within the vector.
*/
bool_t _vector_iterator_belong_to_vector(const vector_t* cpvec_vector, vector_iterator_t it_iter)
{
assert(cpvec_vector != NULL);
assert(_vector_is_inited(cpvec_vector));
assert(_VECTOR_ITERATOR_ITERATOR_TYPE(it_iter) == _RANDOM_ACCESS_ITERATOR);
assert(_VECTOR_ITERATOR_CONTAINER_TYPE(it_iter) == _VECTOR_CONTAINER);
assert(_VECTOR_ITERATOR_CONTAINER(it_iter) == cpvec_vector);
if (_VECTOR_ITERATOR_COREPOS(it_iter) >= cpvec_vector->_pby_start &&
_VECTOR_ITERATOR_COREPOS(it_iter) <= cpvec_vector->_pby_finish) {
return true;
} else {
return false;
}
}
/**
* Test the type that saved in the vector container and referenced by it_iter are same.
*/
bool_t _vector_same_vector_iterator_type(const vector_t* cpvec_vector, vector_iterator_t it_iter)
{
assert(cpvec_vector != NULL);
assert(_VECTOR_ITERATOR_CONTAINER(it_iter) != NULL);
assert(_VECTOR_ITERATOR_CONTAINER_TYPE(it_iter) == _VECTOR_CONTAINER);
assert(_VECTOR_ITERATOR_ITERATOR_TYPE(it_iter) == _RANDOM_ACCESS_ITERATOR);
return _vector_same_type(cpvec_vector, _VECTOR_ITERATOR_CONTAINER(it_iter));
}
/**
* Test the type that saved in the vector container and referenced by it_iter are same.
*/
bool_t _vector_same_iterator_type(const vector_t* cpvec_vector, iterator_t it_iter)
{
assert(cpvec_vector != NULL);
assert(_vector_is_inited(cpvec_vector) || _vector_is_created(cpvec_vector));
assert(_iterator_is_valid(it_iter));
return _type_is_same_ex(&cpvec_vector->_t_typeinfo, _iterator_get_typeinfo(it_iter));
}
/**
* Test vector is created by create_vector.
*/
bool_t _vector_is_created(const vector_t* cpvec_vector)
{
assert(cpvec_vector != NULL);
if (cpvec_vector->_t_typeinfo._t_style != _TYPE_C_BUILTIN &&
cpvec_vector->_t_typeinfo._t_style != _TYPE_CSTL_BUILTIN &&
cpvec_vector->_t_typeinfo._t_style != _TYPE_USER_DEFINE) {
return false;
}
if (cpvec_vector->_t_typeinfo._pt_type == NULL) {
return false;
}
if (cpvec_vector->_pby_start != NULL || cpvec_vector->_pby_finish != NULL || cpvec_vector->_pby_endofstorage != NULL) {
return false;
}
return _alloc_is_inited(&cpvec_vector->_t_allocator);
}
/**
* Test vector is initialized by vector initialization functions.
*/
bool_t _vector_is_inited(const vector_t* cpvec_vector)
{
assert(cpvec_vector != NULL);
if (cpvec_vector->_t_typeinfo._t_style != _TYPE_C_BUILTIN &&
cpvec_vector->_t_typeinfo._t_style != _TYPE_CSTL_BUILTIN &&
cpvec_vector->_t_typeinfo._t_style != _TYPE_USER_DEFINE) {
return false;
}
if (cpvec_vector->_t_typeinfo._pt_type == NULL) {
return false;
}
if (cpvec_vector->_pby_start == NULL && cpvec_vector->_pby_finish == NULL && cpvec_vector->_pby_endofstorage == NULL) {
return true;
} else if (cpvec_vector->_pby_start != NULL &&
cpvec_vector->_pby_finish != NULL &&
cpvec_vector->_pby_endofstorage != NULL &&
cpvec_vector->_pby_finish >= cpvec_vector->_pby_start &&
cpvec_vector->_pby_endofstorage >= cpvec_vector->_pby_start &&
cpvec_vector->_pby_endofstorage >= cpvec_vector->_pby_finish) {
return true;
} else {
return false;
}
}
#endif /* NDEBUG */
/**
* Test the type that saved in the vector container is same.
*/
bool_t _vector_same_type(const vector_t* cpvec_first, const vector_t* cpvec_second)
{
assert(cpvec_first != NULL);
assert(cpvec_second != NULL);
assert(_vector_is_inited(cpvec_first) || _vector_is_created(cpvec_first));
assert(_vector_is_inited(cpvec_second) || _vector_is_created(cpvec_second));
if (cpvec_first == cpvec_second) {
return true;
}
return (cpvec_first->_t_typeinfo._pt_type == cpvec_second->_t_typeinfo._pt_type) &&
(cpvec_first->_t_typeinfo._t_style == cpvec_second->_t_typeinfo._t_style) &&
_type_is_same(_GET_VECTOR_TYPE_NAME(cpvec_first), _GET_VECTOR_TYPE_NAME(cpvec_second));
}
/**
* Initialize data within range [pby_start, pby_finish) according to vector element data type.
*/
void _vector_init_elem_range_auxiliary(vector_t* pvec_vector, _byte_t* pby_start, _byte_t* pby_finish)
{
_byte_t* pby_pos = NULL;
assert(pvec_vector != NULL);
assert(pby_start != NULL);
assert(pby_finish != NULL);
assert(pby_start <= pby_finish);
assert(_vector_is_inited(pvec_vector) || _vector_is_created(pvec_vector));
/* initialize new elements */
if (_GET_VECTOR_TYPE_STYLE(pvec_vector) == _TYPE_CSTL_BUILTIN) {
/* get element type name */
char s_elemtypename[_TYPE_NAME_SIZE + 1];
_type_get_elem_typename(_GET_VECTOR_TYPE_NAME(pvec_vector), s_elemtypename);
for (pby_pos = pby_start; pby_pos < pby_finish; pby_pos += _GET_VECTOR_TYPE_SIZE(pvec_vector)) {
_GET_VECTOR_TYPE_INIT_FUNCTION(pvec_vector)(pby_pos, s_elemtypename);
}
} else {
for (pby_pos = pby_start; pby_pos < pby_finish; pby_pos += _GET_VECTOR_TYPE_SIZE(pvec_vector)) {
bool_t b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
_GET_VECTOR_TYPE_INIT_FUNCTION(pvec_vector)(pby_pos, &b_result);
assert(b_result);
}
}
}
/**
* Obtain data from variable argument list, the data type and vector element data type are same.
*/
void _vector_get_varg_value_auxiliary(vector_t* pvec_vector, va_list val_elemlist, void* pv_varg)
{
assert(pvec_vector != NULL);
assert(pv_varg != NULL);
assert(_vector_is_inited(pvec_vector) || _vector_is_created(pvec_vector));
_vector_init_elem_auxiliary(pvec_vector, pv_varg);
_type_get_varg_value(&pvec_vector->_t_typeinfo, val_elemlist, pv_varg);
}
/**
* Destroy data, the data type and vector element data type are same.
*/
void _vector_destroy_varg_value_auxiliary(vector_t* pvec_vector, void* pv_varg)
{
bool_t b_result = false;
assert(pvec_vector != NULL);
assert(pv_varg != NULL);
assert(_vector_is_inited(pvec_vector) || _vector_is_created(pvec_vector));
/* destroy varg value and free memory */
b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
_GET_VECTOR_TYPE_DESTROY_FUNCTION(pvec_vector)(pv_varg, &b_result);
assert(b_result);
}
/**
* Calculate new capacity according to old size and insert size.
*/
size_t _vector_calculate_new_capacity(size_t t_oldsize, size_t t_insertsize)
{
size_t t_growsize = (t_oldsize + t_insertsize) / 2;
if (t_growsize < _VECTOR_CAPACITY_THRESHOLD_SIZE) {
t_growsize = _VECTOR_CAPACITY_THRESHOLD_SIZE;
}
return t_oldsize + t_insertsize + t_growsize;
}
/** local function implementation section **/
/** eof **/