Lines Matching +full:- +full:v
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
45 void bc_vec_grow(BcVec *restrict v, size_t n) { in bc_vec_grow() argument
50 cap = v->cap; in bc_vec_grow()
51 len = v->len + n; in bc_vec_grow()
62 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(cap, v->size)); in bc_vec_grow()
63 v->cap = cap; in bc_vec_grow()
68 void bc_vec_init(BcVec *restrict v, size_t esize, BcDtorType dtor) { in bc_vec_init() argument
72 assert(v != NULL && esize); in bc_vec_init()
74 v->v = bc_vm_malloc(bc_vm_arraySize(BC_VEC_START_CAP, esize)); in bc_vec_init()
76 v->size = (BcSize) esize; in bc_vec_init()
77 v->cap = BC_VEC_START_CAP; in bc_vec_init()
78 v->len = 0; in bc_vec_init()
79 v->dtor = (BcSize) dtor; in bc_vec_init()
82 void bc_vec_expand(BcVec *restrict v, size_t req) { in bc_vec_expand() argument
84 assert(v != NULL); in bc_vec_expand()
87 if (v->cap < req) { in bc_vec_expand()
93 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(req, v->size)); in bc_vec_expand()
94 v->cap = req; in bc_vec_expand()
100 void bc_vec_npop(BcVec *restrict v, size_t n) { in bc_vec_npop() argument
104 assert(v != NULL && n <= v->len); in bc_vec_npop()
108 if (!v->dtor) v->len -= n; in bc_vec_npop()
111 const BcVecFree d = bc_vec_dtors[v->dtor]; in bc_vec_npop()
112 size_t esize = v->size; in bc_vec_npop()
113 size_t len = v->len - n; in bc_vec_npop()
116 while (v->len > len) d(v->v + (esize * --v->len)); in bc_vec_npop()
122 void bc_vec_npopAt(BcVec *restrict v, size_t n, size_t idx) { in bc_vec_npopAt() argument
127 assert(v != NULL); in bc_vec_npopAt()
128 assert(idx + n < v->len); in bc_vec_npopAt()
131 ptr = bc_vec_item(v, idx); in bc_vec_npopAt()
132 data = bc_vec_item(v, idx + n); in bc_vec_npopAt()
136 if (v->dtor) { in bc_vec_npopAt()
139 const BcVecFree d = bc_vec_dtors[v->dtor]; in bc_vec_npopAt()
142 for (i = 0; i < n; ++i) d(bc_vec_item(v, idx + i)); in bc_vec_npopAt()
145 v->len -= n; in bc_vec_npopAt()
146 memmove(ptr, data, (v->len - idx) * v->size); in bc_vec_npopAt()
151 void bc_vec_npush(BcVec *restrict v, size_t n, const void *data) { in bc_vec_npush() argument
156 assert(v != NULL && data != NULL); in bc_vec_npush()
161 if (v->len + n > v->cap) bc_vec_grow(v, n); in bc_vec_npush()
163 esize = v->size; in bc_vec_npush()
166 memcpy(v->v + (esize * v->len), data, esize * n); in bc_vec_npush()
167 v->len += n; in bc_vec_npush()
172 inline void bc_vec_push(BcVec *restrict v, const void *data) { in bc_vec_push() argument
173 bc_vec_npush(v, 1, data); in bc_vec_push()
176 void* bc_vec_pushEmpty(BcVec *restrict v) { in bc_vec_pushEmpty() argument
181 assert(v != NULL); in bc_vec_pushEmpty()
186 if (v->len + 1 > v->cap) bc_vec_grow(v, 1); in bc_vec_pushEmpty()
188 ptr = v->v + v->size * v->len; in bc_vec_pushEmpty()
189 v->len += 1; in bc_vec_pushEmpty()
196 inline void bc_vec_pushByte(BcVec *restrict v, uchar data) { in bc_vec_pushByte() argument
197 assert(v != NULL && v->size == sizeof(uchar)); in bc_vec_pushByte()
198 bc_vec_npush(v, 1, &data); in bc_vec_pushByte()
201 void bc_vec_pushIndex(BcVec *restrict v, size_t idx) { in bc_vec_pushIndex() argument
205 assert(v != NULL); in bc_vec_pushIndex()
206 assert(v->size == sizeof(uchar)); in bc_vec_pushIndex()
218 bc_vec_npush(v, amt + 1, nums); in bc_vec_pushIndex()
221 void bc_vec_pushAt(BcVec *restrict v, const void *data, size_t idx) { in bc_vec_pushAt() argument
223 assert(v != NULL && data != NULL && idx <= v->len); in bc_vec_pushAt()
228 if (idx == v->len) bc_vec_push(v, data); in bc_vec_pushAt()
235 if (v->len == v->cap) bc_vec_grow(v, 1); in bc_vec_pushAt()
237 esize = v->size; in bc_vec_pushAt()
239 ptr = v->v + esize * idx; in bc_vec_pushAt()
241 memmove(ptr + esize, ptr, esize * (v->len++ - idx)); in bc_vec_pushAt()
246 void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str) { in bc_vec_string() argument
250 assert(v != NULL && v->size == sizeof(char)); in bc_vec_string()
251 assert(!v->dtor); in bc_vec_string()
252 assert(!v->len || !v->v[v->len - 1]); in bc_vec_string()
253 assert(v->v != str); in bc_vec_string()
257 bc_vec_popAll(v); in bc_vec_string()
258 bc_vec_expand(v, bc_vm_growSize(len, 1)); in bc_vec_string()
259 memcpy(v->v, str, len); in bc_vec_string()
260 v->len = len; in bc_vec_string()
262 bc_vec_pushByte(v, '\0'); in bc_vec_string()
267 void bc_vec_concat(BcVec *restrict v, const char *restrict str) { in bc_vec_concat() argument
271 assert(v != NULL && v->size == sizeof(char)); in bc_vec_concat()
272 assert(!v->dtor); in bc_vec_concat()
273 assert(!v->len || !v->v[v->len - 1]); in bc_vec_concat()
274 assert(v->v != str); in bc_vec_concat()
279 if (v->len) v->len -= 1; in bc_vec_concat()
281 bc_vec_npush(v, strlen(str) + 1, str); in bc_vec_concat()
286 void bc_vec_empty(BcVec *restrict v) { in bc_vec_empty() argument
290 assert(v != NULL && v->size == sizeof(char)); in bc_vec_empty()
291 assert(!v->dtor); in bc_vec_empty()
295 bc_vec_popAll(v); in bc_vec_empty()
296 bc_vec_pushByte(v, '\0'); in bc_vec_empty()
302 void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data) { in bc_vec_replaceAt() argument
308 assert(v != NULL); in bc_vec_replaceAt()
310 ptr = bc_vec_item(v, idx); in bc_vec_replaceAt()
312 if (v->dtor) bc_vec_dtors[v->dtor](ptr); in bc_vec_replaceAt()
314 memcpy(ptr, data, v->size); in bc_vec_replaceAt()
318 inline void* bc_vec_item(const BcVec *restrict v, size_t idx) { in bc_vec_item() argument
319 assert(v != NULL && v->len && idx < v->len); in bc_vec_item()
320 return v->v + v->size * idx; in bc_vec_item()
323 inline void* bc_vec_item_rev(const BcVec *restrict v, size_t idx) { in bc_vec_item_rev() argument
324 assert(v != NULL && v->len && idx < v->len); in bc_vec_item_rev()
325 return v->v + v->size * (v->len - idx - 1); in bc_vec_item_rev()
328 inline void bc_vec_clear(BcVec *restrict v) { in bc_vec_clear() argument
330 v->v = NULL; in bc_vec_clear()
331 v->len = 0; in bc_vec_clear()
332 v->dtor = BC_DTOR_NONE; in bc_vec_clear()
336 BcVec *v = (BcVec*) vec; in bc_vec_free() local
338 bc_vec_popAll(v); in bc_vec_free()
339 free(v->v); in bc_vec_free()
348 * @param v The map.
353 static size_t bc_map_find(const BcVec *restrict v, const char *name) { in bc_map_find() argument
355 size_t low = 0, high = v->len; in bc_map_find()
360 const BcId *id = bc_vec_item(v, mid); in bc_map_find()
361 int result = strcmp(name, id->name); in bc_map_find()
371 bool bc_map_insert(BcVec *restrict v, const char *name, in bc_map_insert() argument
379 assert(v != NULL && name != NULL && i != NULL); in bc_map_insert()
381 *i = bc_map_find(v, name); in bc_map_insert()
383 assert(*i <= v->len); in bc_map_insert()
385 if (*i != v->len && !strcmp(name, ((BcId*) bc_vec_item(v, *i))->name)) in bc_map_insert()
397 bc_vec_pushAt(v, &id, *i); in bc_map_insert()
402 size_t bc_map_index(const BcVec *restrict v, const char *name) { in bc_map_index() argument
406 assert(v != NULL && name != NULL); in bc_map_index()
408 i = bc_map_find(v, name); in bc_map_index()
411 if (i >= v->len) return BC_VEC_INVALID_IDX; in bc_map_index()
414 return strcmp(name, ((BcId*) bc_vec_item(v, i))->name) ? in bc_map_index()
419 const char* bc_map_name(const BcVec *restrict v, size_t idx) { in bc_map_name() argument
421 size_t i, len = v->len; in bc_map_name()
424 BcId* id = (BcId*) bc_vec_item(v, i); in bc_map_name()
425 if (id->idx == idx) return id->name; in bc_map_name()
439 s->s = bc_vm_malloc(BC_SLAB_SIZE); in bc_slab_init()
440 s->len = 0; in bc_slab_init()
460 if (s->len + len > BC_SLAB_SIZE) return NULL; in bc_slab_add()
462 ptr = (char*) (s->s + s->len); in bc_slab_add()
466 s->len += len; in bc_slab_add()
472 free(((BcSlab*) slab)->s); in bc_slab_free()
475 void bc_slabvec_init(BcVec* v) { in bc_slabvec_init() argument
479 assert(v != NULL); in bc_slabvec_init()
481 bc_vec_init(v, sizeof(BcSlab), BC_DTOR_SLAB); in bc_slabvec_init()
484 slab = bc_vec_pushEmpty(v); in bc_slabvec_init()
488 char* bc_slabvec_strdup(BcVec *v, const char *str) { in bc_slabvec_strdup() argument
497 assert(v != NULL && v->len); in bc_slabvec_strdup()
511 bc_vec_pushAt(v, &slab, v->len - 1); in bc_slabvec_strdup()
517 slab_ptr = bc_vec_top(v); in bc_slabvec_strdup()
523 slab_ptr = bc_vec_pushEmpty(v); in bc_slabvec_strdup()
534 void bc_slabvec_clear(BcVec *v) { in bc_slabvec_clear() argument
544 s = bc_vec_item(v, 0); in bc_slabvec_clear()
548 assert(s->len != SIZE_MAX || v->len > 1); in bc_slabvec_clear()
551 again = (s->len == SIZE_MAX); in bc_slabvec_clear()
554 if (again) bc_vec_npopAt(v, 1, 0); in bc_slabvec_clear()
560 if (v->len > 1) bc_vec_npop(v, v->len - 1); in bc_slabvec_clear()
563 s->len = 0; in bc_slabvec_clear()
569 void bc_slabvec_print(BcVec *v, const char *func) { in bc_slabvec_print() argument
576 for (i = 0; i < v->len; ++i) { in bc_slabvec_print()
577 s = bc_vec_item(v, i); in bc_slabvec_print()
579 i, (uintptr_t) s->s, s->len); in bc_slabvec_print()