1 /* 2 * ***************************************************************************** 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * 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. 17 * 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. 29 * 30 * ***************************************************************************** 31 * 32 * Definitions for bc vectors (resizable arrays). 33 * 34 */ 35 36 #ifndef BC_VECTOR_H 37 #define BC_VECTOR_H 38 39 #include <stdbool.h> 40 #include <stddef.h> 41 #include <stdint.h> 42 43 #include <status.h> 44 45 #define BC_VEC_INVALID_IDX (SIZE_MAX) 46 #define BC_VEC_START_CAP (UINTMAX_C(1)<<5) 47 48 typedef unsigned char uchar; 49 50 typedef void (*BcVecFree)(void*); 51 52 // Forward declaration. 53 struct BcId; 54 55 typedef struct BcVec { 56 char *v; 57 size_t len; 58 size_t cap; 59 size_t size; 60 BcVecFree dtor; 61 } BcVec; 62 63 void bc_vec_init(BcVec *restrict v, size_t esize, BcVecFree dtor); 64 void bc_vec_expand(BcVec *restrict v, size_t req); 65 void bc_vec_grow(BcVec *restrict v, size_t n); 66 67 void bc_vec_npop(BcVec *restrict v, size_t n); 68 void bc_vec_npopAt(BcVec *restrict v, size_t n, size_t idx); 69 70 void bc_vec_push(BcVec *restrict v, const void *data); 71 void bc_vec_npush(BcVec *restrict v, size_t n, const void *data); 72 void bc_vec_pushByte(BcVec *restrict v, uchar data); 73 void bc_vec_pushIndex(BcVec *restrict v, size_t idx); 74 void bc_vec_string(BcVec *restrict v, size_t len, const char *restrict str); 75 void bc_vec_concat(BcVec *restrict v, const char *restrict str); 76 void bc_vec_empty(BcVec *restrict v); 77 78 #if BC_ENABLE_HISTORY 79 void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data); 80 #endif // BC_ENABLE_HISTORY 81 82 void* bc_vec_item(const BcVec *restrict v, size_t idx); 83 void* bc_vec_item_rev(const BcVec *restrict v, size_t idx); 84 85 void bc_vec_clear(BcVec *restrict v); 86 87 void bc_vec_free(void *vec); 88 89 bool bc_map_insert(BcVec *restrict v, const char *name, 90 size_t idx, size_t *restrict i); 91 size_t bc_map_index(const BcVec *restrict v, const char *name); 92 93 #define bc_vec_pop(v) (bc_vec_npop((v), 1)) 94 #define bc_vec_popAll(v) (bc_vec_npop((v), (v)->len)) 95 #define bc_vec_top(v) (bc_vec_item_rev((v), 0)) 96 97 #ifndef NDEBUG 98 #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), bc_id_free)) 99 #else // NDEBUG 100 #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), NULL)) 101 #endif // NDEBUG 102 103 #endif // BC_VECTOR_H 104