2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Definitions for bc vectors (resizable arrays).
45 #define BC_VEC_INVALID_IDX (SIZE_MAX)
46 #define BC_VEC_START_CAP (UINTMAX_C(1)<<5)
48 typedef unsigned char uchar;
50 typedef void (*BcVecFree)(void*);
52 // Forward declaration.
55 typedef struct BcVec {
63 void bc_vec_init(BcVec *restrict v, size_t esize, BcVecFree dtor);
64 void bc_vec_expand(BcVec *restrict v, size_t req);
66 void bc_vec_npop(BcVec *restrict v, size_t n);
67 void bc_vec_npopAt(BcVec *restrict v, size_t n, size_t idx);
69 void bc_vec_push(BcVec *restrict v, const void *data);
70 void bc_vec_npush(BcVec *restrict v, size_t n, const void *data);
71 void bc_vec_pushByte(BcVec *restrict v, uchar data);
72 void bc_vec_pushIndex(BcVec *restrict v, size_t idx);
73 void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str);
74 void bc_vec_concat(BcVec *restrict v, const char *restrict str);
75 void bc_vec_empty(BcVec *restrict v);
78 void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data);
79 #endif // BC_ENABLE_HISTORY
81 void* bc_vec_item(const BcVec *restrict v, size_t idx);
82 void* bc_vec_item_rev(const BcVec *restrict v, size_t idx);
84 void bc_vec_clear(BcVec *restrict v);
86 void bc_vec_free(void *vec);
88 bool bc_map_insert(BcVec *restrict v, const char *name,
89 size_t idx, size_t *restrict i);
90 size_t bc_map_index(const BcVec *restrict v, const char *name);
92 #define bc_vec_pop(v) (bc_vec_npop((v), 1))
93 #define bc_vec_top(v) (bc_vec_item_rev((v), 0))
96 #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), bc_id_free))
98 #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), NULL))
101 #endif // BC_VECTOR_H