Lines Matching +full:- +full:v
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
46 bc_vec_grow(BcVec* restrict v, size_t n) in bc_vec_grow() argument
53 cap = v->cap; in bc_vec_grow()
54 len = v->len + n; in bc_vec_grow()
69 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(cap, v->size)); in bc_vec_grow()
70 v->cap = cap; in bc_vec_grow()
76 bc_vec_init(BcVec* restrict v, size_t esize, BcDtorType dtor) in bc_vec_init() argument
80 assert(v != NULL && esize); in bc_vec_init()
82 v->v = bc_vm_malloc(bc_vm_arraySize(BC_VEC_START_CAP, esize)); in bc_vec_init()
84 v->size = (BcSize) esize; in bc_vec_init()
85 v->cap = BC_VEC_START_CAP; in bc_vec_init()
86 v->len = 0; in bc_vec_init()
87 v->dtor = (BcSize) dtor; in bc_vec_init()
91 bc_vec_expand(BcVec* restrict v, size_t req) in bc_vec_expand() argument
93 assert(v != NULL); in bc_vec_expand()
96 if (v->cap < req) in bc_vec_expand()
104 v->v = bc_vm_realloc(v->v, bc_vm_arraySize(req, v->size)); in bc_vec_expand()
105 v->cap = req; in bc_vec_expand()
112 bc_vec_npop(BcVec* restrict v, size_t n) in bc_vec_npop() argument
118 assert(v != NULL && n <= v->len); in bc_vec_npop()
122 if (!v->dtor) v->len -= n; in bc_vec_npop()
125 const BcVecFree d = bc_vec_dtors[v->dtor]; in bc_vec_npop()
126 size_t esize = v->size; in bc_vec_npop()
127 size_t len = v->len - n; in bc_vec_npop()
130 while (v->len > len) in bc_vec_npop()
132 d(v->v + (esize * --v->len)); in bc_vec_npop()
140 bc_vec_npopAt(BcVec* restrict v, size_t n, size_t idx) in bc_vec_npopAt() argument
148 assert(v != NULL); in bc_vec_npopAt()
149 assert(idx + n < v->len); in bc_vec_npopAt()
152 ptr = bc_vec_item(v, idx); in bc_vec_npopAt()
153 data = bc_vec_item(v, idx + n); in bc_vec_npopAt()
157 if (v->dtor) in bc_vec_npopAt()
160 const BcVecFree d = bc_vec_dtors[v->dtor]; in bc_vec_npopAt()
165 d(bc_vec_item(v, idx + i)); in bc_vec_npopAt()
169 v->len -= n; in bc_vec_npopAt()
171 memmove(ptr, data, (v->len - idx) * v->size); in bc_vec_npopAt()
177 bc_vec_npush(BcVec* restrict v, size_t n, const void* data) in bc_vec_npush() argument
184 assert(v != NULL && data != NULL); in bc_vec_npush()
189 if (v->len + n > v->cap) bc_vec_grow(v, n); in bc_vec_npush()
191 esize = v->size; in bc_vec_npush()
195 memcpy(v->v + (esize * v->len), data, esize * n); in bc_vec_npush()
196 v->len += n; in bc_vec_npush()
202 bc_vec_push(BcVec* restrict v, const void* data) in bc_vec_push() argument
204 bc_vec_npush(v, 1, data); in bc_vec_push()
208 bc_vec_pushEmpty(BcVec* restrict v) in bc_vec_pushEmpty() argument
215 assert(v != NULL); in bc_vec_pushEmpty()
220 if (v->len + 1 > v->cap) bc_vec_grow(v, 1); in bc_vec_pushEmpty()
222 ptr = v->v + v->size * v->len; in bc_vec_pushEmpty()
223 v->len += 1; in bc_vec_pushEmpty()
231 bc_vec_pushByte(BcVec* restrict v, uchar data) in bc_vec_pushByte() argument
233 assert(v != NULL && v->size == sizeof(uchar)); in bc_vec_pushByte()
234 bc_vec_npush(v, 1, &data); in bc_vec_pushByte()
238 bc_vec_pushIndex(BcVec* restrict v, size_t idx) in bc_vec_pushIndex() argument
242 assert(v != NULL); in bc_vec_pushIndex()
243 assert(v->size == sizeof(uchar)); in bc_vec_pushIndex()
256 bc_vec_npush(v, amt + 1, nums); in bc_vec_pushIndex()
260 bc_vec_pushAt(BcVec* restrict v, const void* data, size_t idx) in bc_vec_pushAt() argument
262 assert(v != NULL && data != NULL && idx <= v->len); in bc_vec_pushAt()
267 if (idx == v->len) bc_vec_push(v, data); in bc_vec_pushAt()
274 if (v->len == v->cap) bc_vec_grow(v, 1); in bc_vec_pushAt()
276 esize = v->size; in bc_vec_pushAt()
278 ptr = v->v + esize * idx; in bc_vec_pushAt()
281 memmove(ptr + esize, ptr, esize * (v->len++ - idx)); in bc_vec_pushAt()
288 bc_vec_string(BcVec* restrict v, size_t len, const char* restrict str) in bc_vec_string() argument
294 assert(v != NULL && v->size == sizeof(char)); in bc_vec_string()
295 assert(!v->dtor); in bc_vec_string()
296 assert(!v->len || !v->v[v->len - 1]); in bc_vec_string()
297 assert(v->v != str); in bc_vec_string()
301 bc_vec_popAll(v); in bc_vec_string()
302 bc_vec_expand(v, bc_vm_growSize(len, 1)); in bc_vec_string()
304 memcpy(v->v, str, len); in bc_vec_string()
305 v->len = len; in bc_vec_string()
307 bc_vec_pushByte(v, '\0'); in bc_vec_string()
313 bc_vec_concat(BcVec* restrict v, const char* restrict str) in bc_vec_concat() argument
319 assert(v != NULL && v->size == sizeof(char)); in bc_vec_concat()
320 assert(!v->dtor); in bc_vec_concat()
321 assert(!v->len || !v->v[v->len - 1]); in bc_vec_concat()
322 assert(v->v != str); in bc_vec_concat()
327 if (v->len) v->len -= 1; in bc_vec_concat()
329 bc_vec_npush(v, strlen(str) + 1, str); in bc_vec_concat()
335 bc_vec_empty(BcVec* restrict v) in bc_vec_empty() argument
341 assert(v != NULL && v->size == sizeof(char)); in bc_vec_empty()
342 assert(!v->dtor); in bc_vec_empty()
346 bc_vec_popAll(v); in bc_vec_empty()
347 bc_vec_pushByte(v, '\0'); in bc_vec_empty()
354 bc_vec_replaceAt(BcVec* restrict v, size_t idx, const void* data) in bc_vec_replaceAt() argument
360 assert(v != NULL); in bc_vec_replaceAt()
362 ptr = bc_vec_item(v, idx); in bc_vec_replaceAt()
364 if (v->dtor) bc_vec_dtors[v->dtor](ptr); in bc_vec_replaceAt()
367 memcpy(ptr, data, v->size); in bc_vec_replaceAt()
372 bc_vec_item(const BcVec* restrict v, size_t idx) in bc_vec_item() argument
374 assert(v != NULL && v->len && idx < v->len); in bc_vec_item()
375 return v->v + v->size * idx; in bc_vec_item()
379 bc_vec_item_rev(const BcVec* restrict v, size_t idx) in bc_vec_item_rev() argument
381 assert(v != NULL && v->len && idx < v->len); in bc_vec_item_rev()
382 return v->v + v->size * (v->len - idx - 1); in bc_vec_item_rev()
386 bc_vec_clear(BcVec* restrict v) in bc_vec_clear() argument
389 v->v = NULL; in bc_vec_clear()
390 v->len = 0; in bc_vec_clear()
391 v->dtor = BC_DTOR_NONE; in bc_vec_clear()
397 BcVec* v = (BcVec*) vec; in bc_vec_free() local
399 bc_vec_popAll(v); in bc_vec_free()
400 free(v->v); in bc_vec_free()
409 * @param v The map.
415 bc_map_find(const BcVec* restrict v, const char* name) in bc_map_find() argument
417 size_t low = 0, high = v->len; in bc_map_find()
421 size_t mid = low + (high - low) / 2; in bc_map_find()
422 const BcId* id = bc_vec_item(v, mid); in bc_map_find()
423 int result = strcmp(name, id->name); in bc_map_find()
434 bc_map_insert(BcVec* restrict v, const char* name, size_t idx, in bc_map_insert() argument
441 assert(v != NULL && name != NULL && i != NULL); in bc_map_insert()
443 *i = bc_map_find(v, name); in bc_map_insert()
445 assert(*i <= v->len); in bc_map_insert()
447 if (*i != v->len && !strcmp(name, ((BcId*) bc_vec_item(v, *i))->name)) in bc_map_insert()
452 id.name = bc_slabvec_strdup(&vm->slabs, name); in bc_map_insert()
455 bc_vec_pushAt(v, &id, *i); in bc_map_insert()
461 bc_map_index(const BcVec* restrict v, const char* name) in bc_map_index() argument
466 assert(v != NULL && name != NULL); in bc_map_index()
468 i = bc_map_find(v, name); in bc_map_index()
471 if (i >= v->len) return BC_VEC_INVALID_IDX; in bc_map_index()
473 id = (BcId*) bc_vec_item(v, i); in bc_map_index()
476 return strcmp(name, id->name) ? BC_VEC_INVALID_IDX : i; in bc_map_index()
481 bc_map_name(const BcVec* restrict v, size_t idx) in bc_map_name() argument
483 size_t i, len = v->len; in bc_map_name()
487 BcId* id = (BcId*) bc_vec_item(v, i); in bc_map_name()
488 if (id->idx == idx) return id->name; in bc_map_name()
506 s->s = bc_vm_malloc(BC_SLAB_SIZE); in bc_slab_init()
507 s->len = 0; in bc_slab_init()
528 if (s->len + len > BC_SLAB_SIZE) return NULL; in bc_slab_add()
530 ptr = (char*) (s->s + s->len); in bc_slab_add()
535 s->len += len; in bc_slab_add()
543 free(((BcSlab*) slab)->s); in bc_slab_free()
547 bc_slabvec_init(BcVec* v) in bc_slabvec_init() argument
551 assert(v != NULL); in bc_slabvec_init()
553 bc_vec_init(v, sizeof(BcSlab), BC_DTOR_SLAB); in bc_slabvec_init()
556 slab = bc_vec_pushEmpty(v); in bc_slabvec_init()
561 bc_slabvec_strdup(BcVec* v, const char* str) in bc_slabvec_strdup() argument
570 assert(v != NULL && v->len); in bc_slabvec_strdup()
584 bc_vec_pushAt(v, &slab, v->len - 1); in bc_slabvec_strdup()
590 slab_ptr = bc_vec_top(v); in bc_slabvec_strdup()
596 slab_ptr = bc_vec_pushEmpty(v); in bc_slabvec_strdup()
608 bc_slabvec_clear(BcVec* v) in bc_slabvec_clear() argument
618 s = bc_vec_item(v, 0); in bc_slabvec_clear()
622 assert(s->len != SIZE_MAX || v->len > 1); in bc_slabvec_clear()
625 again = (s->len == SIZE_MAX); in bc_slabvec_clear()
628 if (again) bc_vec_npopAt(v, 1, 0); in bc_slabvec_clear()
634 if (v->len > 1) bc_vec_npop(v, v->len - 1); in bc_slabvec_clear()
637 s->len = 0; in bc_slabvec_clear()
644 bc_slabvec_print(BcVec* v, const char* func) in bc_slabvec_print() argument
649 bc_file_printf(&vm->ferr, "%s\n", func); in bc_slabvec_print()
651 for (i = 0; i < v->len; ++i) in bc_slabvec_print()
653 s = bc_vec_item(v, i); in bc_slabvec_print()
654 bc_file_printf(&vm->ferr, "%zu { s = %zu, len = %zu }\n", i, in bc_slabvec_print()
655 (uintptr_t) s->s, s->len); in bc_slabvec_print()
658 bc_file_puts(&vm->ferr, bc_flush_none, "\n"); in bc_slabvec_print()
659 bc_file_flush(&vm->ferr, bc_flush_none); in bc_slabvec_print()