1*5a6e8488SAndroid Build Coastguard Worker /* 2*5a6e8488SAndroid Build Coastguard Worker * ***************************************************************************** 3*5a6e8488SAndroid Build Coastguard Worker * 4*5a6e8488SAndroid Build Coastguard Worker * SPDX-License-Identifier: BSD-2-Clause 5*5a6e8488SAndroid Build Coastguard Worker * 6*5a6e8488SAndroid Build Coastguard Worker * Copyright (c) 2018-2024 Gavin D. Howard and contributors. 7*5a6e8488SAndroid Build Coastguard Worker * 8*5a6e8488SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 9*5a6e8488SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met: 10*5a6e8488SAndroid Build Coastguard Worker * 11*5a6e8488SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright notice, this 12*5a6e8488SAndroid Build Coastguard Worker * list of conditions and the following disclaimer. 13*5a6e8488SAndroid Build Coastguard Worker * 14*5a6e8488SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright notice, 15*5a6e8488SAndroid Build Coastguard Worker * this list of conditions and the following disclaimer in the documentation 16*5a6e8488SAndroid Build Coastguard Worker * and/or other materials provided with the distribution. 17*5a6e8488SAndroid Build Coastguard Worker * 18*5a6e8488SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*5a6e8488SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*5a6e8488SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*5a6e8488SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*5a6e8488SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*5a6e8488SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*5a6e8488SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*5a6e8488SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*5a6e8488SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*5a6e8488SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*5a6e8488SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE. 29*5a6e8488SAndroid Build Coastguard Worker * 30*5a6e8488SAndroid Build Coastguard Worker * ***************************************************************************** 31*5a6e8488SAndroid Build Coastguard Worker * 32*5a6e8488SAndroid Build Coastguard Worker * Definitions for bc vectors (resizable arrays). 33*5a6e8488SAndroid Build Coastguard Worker * 34*5a6e8488SAndroid Build Coastguard Worker */ 35*5a6e8488SAndroid Build Coastguard Worker 36*5a6e8488SAndroid Build Coastguard Worker #ifndef BC_VECTOR_H 37*5a6e8488SAndroid Build Coastguard Worker #define BC_VECTOR_H 38*5a6e8488SAndroid Build Coastguard Worker 39*5a6e8488SAndroid Build Coastguard Worker #include <stdbool.h> 40*5a6e8488SAndroid Build Coastguard Worker #include <stddef.h> 41*5a6e8488SAndroid Build Coastguard Worker #include <stdint.h> 42*5a6e8488SAndroid Build Coastguard Worker 43*5a6e8488SAndroid Build Coastguard Worker #include <status.h> 44*5a6e8488SAndroid Build Coastguard Worker 45*5a6e8488SAndroid Build Coastguard Worker /// An invalid index for a map to mark when an item does not exist. 46*5a6e8488SAndroid Build Coastguard Worker #define BC_VEC_INVALID_IDX (SIZE_MAX) 47*5a6e8488SAndroid Build Coastguard Worker 48*5a6e8488SAndroid Build Coastguard Worker /// The starting capacity for vectors. This is based on the minimum allocation 49*5a6e8488SAndroid Build Coastguard Worker /// for 64-bit systems. 50*5a6e8488SAndroid Build Coastguard Worker #define BC_VEC_START_CAP (UINTMAX_C(1) << 5) 51*5a6e8488SAndroid Build Coastguard Worker 52*5a6e8488SAndroid Build Coastguard Worker /// An alias. 53*5a6e8488SAndroid Build Coastguard Worker typedef unsigned char uchar; 54*5a6e8488SAndroid Build Coastguard Worker 55*5a6e8488SAndroid Build Coastguard Worker /** 56*5a6e8488SAndroid Build Coastguard Worker * A destructor. Frees the object that @a ptr points to. This is used by vectors 57*5a6e8488SAndroid Build Coastguard Worker * to free the memory they own. 58*5a6e8488SAndroid Build Coastguard Worker * @param ptr Pointer to the data to free. 59*5a6e8488SAndroid Build Coastguard Worker */ 60*5a6e8488SAndroid Build Coastguard Worker typedef void (*BcVecFree)(void* ptr); 61*5a6e8488SAndroid Build Coastguard Worker 62*5a6e8488SAndroid Build Coastguard Worker #if BC_LONG_BIT >= 64 63*5a6e8488SAndroid Build Coastguard Worker 64*5a6e8488SAndroid Build Coastguard Worker /// An integer to shrink the size of a vector by using these instead of size_t. 65*5a6e8488SAndroid Build Coastguard Worker typedef uint32_t BcSize; 66*5a6e8488SAndroid Build Coastguard Worker 67*5a6e8488SAndroid Build Coastguard Worker #else // BC_LONG_BIT >= 64 68*5a6e8488SAndroid Build Coastguard Worker 69*5a6e8488SAndroid Build Coastguard Worker /// An integer to shrink the size of a vector by using these instead of size_t. 70*5a6e8488SAndroid Build Coastguard Worker typedef uint16_t BcSize; 71*5a6e8488SAndroid Build Coastguard Worker 72*5a6e8488SAndroid Build Coastguard Worker #endif // BC_LONG_BIT >= 64 73*5a6e8488SAndroid Build Coastguard Worker 74*5a6e8488SAndroid Build Coastguard Worker /// An enum of all of the destructors. We use an enum to save space. 75*5a6e8488SAndroid Build Coastguard Worker typedef enum BcDtorType 76*5a6e8488SAndroid Build Coastguard Worker { 77*5a6e8488SAndroid Build Coastguard Worker /// No destructor needed. 78*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_NONE, 79*5a6e8488SAndroid Build Coastguard Worker 80*5a6e8488SAndroid Build Coastguard Worker /// Vector destructor. 81*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_VEC, 82*5a6e8488SAndroid Build Coastguard Worker 83*5a6e8488SAndroid Build Coastguard Worker /// BcNum destructor. 84*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_NUM, 85*5a6e8488SAndroid Build Coastguard Worker 86*5a6e8488SAndroid Build Coastguard Worker #if !BC_ENABLE_LIBRARY 87*5a6e8488SAndroid Build Coastguard Worker 88*5a6e8488SAndroid Build Coastguard Worker #if BC_DEBUG 89*5a6e8488SAndroid Build Coastguard Worker 90*5a6e8488SAndroid Build Coastguard Worker /// BcFunc destructor. 91*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_FUNC, 92*5a6e8488SAndroid Build Coastguard Worker 93*5a6e8488SAndroid Build Coastguard Worker #endif // BC_DEBUG 94*5a6e8488SAndroid Build Coastguard Worker 95*5a6e8488SAndroid Build Coastguard Worker /// BcSlab destructor. 96*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_SLAB, 97*5a6e8488SAndroid Build Coastguard Worker 98*5a6e8488SAndroid Build Coastguard Worker /// BcConst destructor. 99*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_CONST, 100*5a6e8488SAndroid Build Coastguard Worker 101*5a6e8488SAndroid Build Coastguard Worker /// BcResult destructor. 102*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_RESULT, 103*5a6e8488SAndroid Build Coastguard Worker 104*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_HISTORY 105*5a6e8488SAndroid Build Coastguard Worker 106*5a6e8488SAndroid Build Coastguard Worker /// String destructor for history, which is *special*. 107*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_HISTORY_STRING, 108*5a6e8488SAndroid Build Coastguard Worker 109*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_HISTORY 110*5a6e8488SAndroid Build Coastguard Worker #else // !BC_ENABLE_LIBRARY 111*5a6e8488SAndroid Build Coastguard Worker 112*5a6e8488SAndroid Build Coastguard Worker /// Destructor for bcl numbers. 113*5a6e8488SAndroid Build Coastguard Worker BC_DTOR_BCL_NUM, 114*5a6e8488SAndroid Build Coastguard Worker 115*5a6e8488SAndroid Build Coastguard Worker #endif // !BC_ENABLE_LIBRARY 116*5a6e8488SAndroid Build Coastguard Worker 117*5a6e8488SAndroid Build Coastguard Worker } BcDtorType; 118*5a6e8488SAndroid Build Coastguard Worker 119*5a6e8488SAndroid Build Coastguard Worker /// The actual vector struct. 120*5a6e8488SAndroid Build Coastguard Worker typedef struct BcVec 121*5a6e8488SAndroid Build Coastguard Worker { 122*5a6e8488SAndroid Build Coastguard Worker /// The vector array itself. This uses a char* because it is compatible with 123*5a6e8488SAndroid Build Coastguard Worker /// pointers of all other types, and I can do pointer arithmetic on it. 124*5a6e8488SAndroid Build Coastguard Worker char* restrict v; 125*5a6e8488SAndroid Build Coastguard Worker 126*5a6e8488SAndroid Build Coastguard Worker /// The length of the vector, which is how many items actually exist. 127*5a6e8488SAndroid Build Coastguard Worker size_t len; 128*5a6e8488SAndroid Build Coastguard Worker 129*5a6e8488SAndroid Build Coastguard Worker /// The capacity of the vector, which is how many items can fit in the 130*5a6e8488SAndroid Build Coastguard Worker /// current allocation. 131*5a6e8488SAndroid Build Coastguard Worker size_t cap; 132*5a6e8488SAndroid Build Coastguard Worker 133*5a6e8488SAndroid Build Coastguard Worker /// The size of the items in the vector, as returned by sizeof(). 134*5a6e8488SAndroid Build Coastguard Worker BcSize size; 135*5a6e8488SAndroid Build Coastguard Worker 136*5a6e8488SAndroid Build Coastguard Worker /// The destructor as a BcDtorType enum. 137*5a6e8488SAndroid Build Coastguard Worker BcSize dtor; 138*5a6e8488SAndroid Build Coastguard Worker 139*5a6e8488SAndroid Build Coastguard Worker } BcVec; 140*5a6e8488SAndroid Build Coastguard Worker 141*5a6e8488SAndroid Build Coastguard Worker /** 142*5a6e8488SAndroid Build Coastguard Worker * Initializes a vector. 143*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to initialize. 144*5a6e8488SAndroid Build Coastguard Worker * @param esize The size of the elements, as returned by sizeof(). 145*5a6e8488SAndroid Build Coastguard Worker * @param dtor The destructor of the elements, as a BcDtorType enum. 146*5a6e8488SAndroid Build Coastguard Worker */ 147*5a6e8488SAndroid Build Coastguard Worker void 148*5a6e8488SAndroid Build Coastguard Worker bc_vec_init(BcVec* restrict v, size_t esize, BcDtorType dtor); 149*5a6e8488SAndroid Build Coastguard Worker 150*5a6e8488SAndroid Build Coastguard Worker /** 151*5a6e8488SAndroid Build Coastguard Worker * Expands the vector to have a capacity of @a req items, if it doesn't have 152*5a6e8488SAndroid Build Coastguard Worker * enough already. 153*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to expand. 154*5a6e8488SAndroid Build Coastguard Worker * @param req The requested capacity. 155*5a6e8488SAndroid Build Coastguard Worker */ 156*5a6e8488SAndroid Build Coastguard Worker void 157*5a6e8488SAndroid Build Coastguard Worker bc_vec_expand(BcVec* restrict v, size_t req); 158*5a6e8488SAndroid Build Coastguard Worker 159*5a6e8488SAndroid Build Coastguard Worker /** 160*5a6e8488SAndroid Build Coastguard Worker * Grow a vector by at least @a n elements. 161*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to grow. 162*5a6e8488SAndroid Build Coastguard Worker * @param n The number of elements to grow the vector by. 163*5a6e8488SAndroid Build Coastguard Worker */ 164*5a6e8488SAndroid Build Coastguard Worker void 165*5a6e8488SAndroid Build Coastguard Worker bc_vec_grow(BcVec* restrict v, size_t n); 166*5a6e8488SAndroid Build Coastguard Worker 167*5a6e8488SAndroid Build Coastguard Worker /** 168*5a6e8488SAndroid Build Coastguard Worker * Pops @a n items off the back of the vector. The vector must have at least 169*5a6e8488SAndroid Build Coastguard Worker * @a n elements. 170*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to pop off of. 171*5a6e8488SAndroid Build Coastguard Worker * @param n The number of elements to pop off. 172*5a6e8488SAndroid Build Coastguard Worker */ 173*5a6e8488SAndroid Build Coastguard Worker void 174*5a6e8488SAndroid Build Coastguard Worker bc_vec_npop(BcVec* restrict v, size_t n); 175*5a6e8488SAndroid Build Coastguard Worker 176*5a6e8488SAndroid Build Coastguard Worker /** 177*5a6e8488SAndroid Build Coastguard Worker * Pops @a n items, starting at index @a idx, off the vector. The vector must 178*5a6e8488SAndroid Build Coastguard Worker * have at least @a n elements after the @a idx index. Any remaining elements at 179*5a6e8488SAndroid Build Coastguard Worker * the end are moved up to fill the hole. 180*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to pop off of. 181*5a6e8488SAndroid Build Coastguard Worker * @param n The number of elements to pop off. 182*5a6e8488SAndroid Build Coastguard Worker * @param idx The index to start popping at. 183*5a6e8488SAndroid Build Coastguard Worker */ 184*5a6e8488SAndroid Build Coastguard Worker void 185*5a6e8488SAndroid Build Coastguard Worker bc_vec_npopAt(BcVec* restrict v, size_t n, size_t idx); 186*5a6e8488SAndroid Build Coastguard Worker 187*5a6e8488SAndroid Build Coastguard Worker /** 188*5a6e8488SAndroid Build Coastguard Worker * Pushes one item on the back of the vector. It does a memcpy(), but it assumes 189*5a6e8488SAndroid Build Coastguard Worker * that the vector takes ownership of the data. 190*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to push onto. 191*5a6e8488SAndroid Build Coastguard Worker * @param data A pointer to the data to push. 192*5a6e8488SAndroid Build Coastguard Worker */ 193*5a6e8488SAndroid Build Coastguard Worker void 194*5a6e8488SAndroid Build Coastguard Worker bc_vec_push(BcVec* restrict v, const void* data); 195*5a6e8488SAndroid Build Coastguard Worker 196*5a6e8488SAndroid Build Coastguard Worker /** 197*5a6e8488SAndroid Build Coastguard Worker * Pushes @a n items on the back of the vector. It does a memcpy(), but it 198*5a6e8488SAndroid Build Coastguard Worker * assumes that the vector takes ownership of the data. 199*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to push onto. 200*5a6e8488SAndroid Build Coastguard Worker * @param data A pointer to the elements of data to push. 201*5a6e8488SAndroid Build Coastguard Worker */ 202*5a6e8488SAndroid Build Coastguard Worker void 203*5a6e8488SAndroid Build Coastguard Worker bc_vec_npush(BcVec* restrict v, size_t n, const void* data); 204*5a6e8488SAndroid Build Coastguard Worker 205*5a6e8488SAndroid Build Coastguard Worker /** 206*5a6e8488SAndroid Build Coastguard Worker * Push an empty element and return a pointer to it. This is done as an 207*5a6e8488SAndroid Build Coastguard Worker * optimization where initializing an item needs a pointer anyway. It removes an 208*5a6e8488SAndroid Build Coastguard Worker * extra memcpy(). 209*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to push onto. 210*5a6e8488SAndroid Build Coastguard Worker * @return A pointer to the newly-pushed element. 211*5a6e8488SAndroid Build Coastguard Worker */ 212*5a6e8488SAndroid Build Coastguard Worker void* 213*5a6e8488SAndroid Build Coastguard Worker bc_vec_pushEmpty(BcVec* restrict v); 214*5a6e8488SAndroid Build Coastguard Worker 215*5a6e8488SAndroid Build Coastguard Worker /** 216*5a6e8488SAndroid Build Coastguard Worker * Pushes a byte onto a bytecode vector. This is a convenience function for the 217*5a6e8488SAndroid Build Coastguard Worker * parsers pushing instructions. The vector must be a bytecode vector. 218*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to push onto. 219*5a6e8488SAndroid Build Coastguard Worker * @param data The byte to push. 220*5a6e8488SAndroid Build Coastguard Worker */ 221*5a6e8488SAndroid Build Coastguard Worker void 222*5a6e8488SAndroid Build Coastguard Worker bc_vec_pushByte(BcVec* restrict v, uchar data); 223*5a6e8488SAndroid Build Coastguard Worker 224*5a6e8488SAndroid Build Coastguard Worker /** 225*5a6e8488SAndroid Build Coastguard Worker * Pushes and index onto a bytecode vector. The vector must be a bytecode 226*5a6e8488SAndroid Build Coastguard Worker * vector. For more info about why and how this is done, see the development 227*5a6e8488SAndroid Build Coastguard Worker * manual (manuals/development#bytecode-indices). 228*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to push onto. 229*5a6e8488SAndroid Build Coastguard Worker * @param idx The index to push. 230*5a6e8488SAndroid Build Coastguard Worker */ 231*5a6e8488SAndroid Build Coastguard Worker void 232*5a6e8488SAndroid Build Coastguard Worker bc_vec_pushIndex(BcVec* restrict v, size_t idx); 233*5a6e8488SAndroid Build Coastguard Worker 234*5a6e8488SAndroid Build Coastguard Worker /** 235*5a6e8488SAndroid Build Coastguard Worker * Push an item onto the vector at a certain index. The index must be valid 236*5a6e8488SAndroid Build Coastguard Worker * (either exists or is equal to the length of the vector). The elements at that 237*5a6e8488SAndroid Build Coastguard Worker * index and after are moved back one element and kept in the same order. This 238*5a6e8488SAndroid Build Coastguard Worker * is how the map vectors are kept sorted. 239*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to push onto. 240*5a6e8488SAndroid Build Coastguard Worker * @param data A pointer to the data to push. 241*5a6e8488SAndroid Build Coastguard Worker * @param idx The index to push at. 242*5a6e8488SAndroid Build Coastguard Worker */ 243*5a6e8488SAndroid Build Coastguard Worker void 244*5a6e8488SAndroid Build Coastguard Worker bc_vec_pushAt(BcVec* restrict v, const void* data, size_t idx); 245*5a6e8488SAndroid Build Coastguard Worker 246*5a6e8488SAndroid Build Coastguard Worker /** 247*5a6e8488SAndroid Build Coastguard Worker * Empties the vector and sets it to the string. The vector must be a valid 248*5a6e8488SAndroid Build Coastguard Worker * vector and must have chars as its elements. 249*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to set to the string. 250*5a6e8488SAndroid Build Coastguard Worker * @param len The length of the string. This can be less than the actual length 251*5a6e8488SAndroid Build Coastguard Worker * of the string, but must never be more. 252*5a6e8488SAndroid Build Coastguard Worker * @param str The string to push. 253*5a6e8488SAndroid Build Coastguard Worker */ 254*5a6e8488SAndroid Build Coastguard Worker void 255*5a6e8488SAndroid Build Coastguard Worker bc_vec_string(BcVec* restrict v, size_t len, const char* restrict str); 256*5a6e8488SAndroid Build Coastguard Worker 257*5a6e8488SAndroid Build Coastguard Worker /** 258*5a6e8488SAndroid Build Coastguard Worker * Appends the string to the end of the vector, which must be holding a string 259*5a6e8488SAndroid Build Coastguard Worker * (nul byte-terminated) already. 260*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to append to. 261*5a6e8488SAndroid Build Coastguard Worker * @param str The string to append (by copying). 262*5a6e8488SAndroid Build Coastguard Worker */ 263*5a6e8488SAndroid Build Coastguard Worker void 264*5a6e8488SAndroid Build Coastguard Worker bc_vec_concat(BcVec* restrict v, const char* restrict str); 265*5a6e8488SAndroid Build Coastguard Worker 266*5a6e8488SAndroid Build Coastguard Worker /** 267*5a6e8488SAndroid Build Coastguard Worker * Empties a vector and pushes a nul-byte at the first index. The vector must be 268*5a6e8488SAndroid Build Coastguard Worker * a char vector. 269*5a6e8488SAndroid Build Coastguard Worker */ 270*5a6e8488SAndroid Build Coastguard Worker void 271*5a6e8488SAndroid Build Coastguard Worker bc_vec_empty(BcVec* restrict v); 272*5a6e8488SAndroid Build Coastguard Worker 273*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_HISTORY 274*5a6e8488SAndroid Build Coastguard Worker 275*5a6e8488SAndroid Build Coastguard Worker /** 276*5a6e8488SAndroid Build Coastguard Worker * Replaces an item at a particular index. No elements are moved. The index must 277*5a6e8488SAndroid Build Coastguard Worker * exist. 278*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to replace an item on. 279*5a6e8488SAndroid Build Coastguard Worker * @param idx The index of the item to replace. 280*5a6e8488SAndroid Build Coastguard Worker * @param data The data to replace the item with. 281*5a6e8488SAndroid Build Coastguard Worker */ 282*5a6e8488SAndroid Build Coastguard Worker void 283*5a6e8488SAndroid Build Coastguard Worker bc_vec_replaceAt(BcVec* restrict v, size_t idx, const void* data); 284*5a6e8488SAndroid Build Coastguard Worker 285*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_HISTORY 286*5a6e8488SAndroid Build Coastguard Worker 287*5a6e8488SAndroid Build Coastguard Worker /** 288*5a6e8488SAndroid Build Coastguard Worker * Returns a pointer to the item in the vector at the index. This is the key 289*5a6e8488SAndroid Build Coastguard Worker * function for vectors. The index must exist. 290*5a6e8488SAndroid Build Coastguard Worker * @param v The vector. 291*5a6e8488SAndroid Build Coastguard Worker * @param idx The index to the item to get a pointer to. 292*5a6e8488SAndroid Build Coastguard Worker * @return A pointer to the item at @a idx. 293*5a6e8488SAndroid Build Coastguard Worker */ 294*5a6e8488SAndroid Build Coastguard Worker void* 295*5a6e8488SAndroid Build Coastguard Worker bc_vec_item(const BcVec* restrict v, size_t idx); 296*5a6e8488SAndroid Build Coastguard Worker 297*5a6e8488SAndroid Build Coastguard Worker /** 298*5a6e8488SAndroid Build Coastguard Worker * Returns a pointer to the item in the vector at the index, reversed. This is 299*5a6e8488SAndroid Build Coastguard Worker * another key function for vectors. The index must exist. 300*5a6e8488SAndroid Build Coastguard Worker * @param v The vector. 301*5a6e8488SAndroid Build Coastguard Worker * @param idx The index to the item to get a pointer to. 302*5a6e8488SAndroid Build Coastguard Worker * @return A pointer to the item at len - @a idx - 1. 303*5a6e8488SAndroid Build Coastguard Worker */ 304*5a6e8488SAndroid Build Coastguard Worker void* 305*5a6e8488SAndroid Build Coastguard Worker bc_vec_item_rev(const BcVec* restrict v, size_t idx); 306*5a6e8488SAndroid Build Coastguard Worker 307*5a6e8488SAndroid Build Coastguard Worker /** 308*5a6e8488SAndroid Build Coastguard Worker * Zeros a vector. The vector must not be allocated. 309*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to clear. 310*5a6e8488SAndroid Build Coastguard Worker */ 311*5a6e8488SAndroid Build Coastguard Worker void 312*5a6e8488SAndroid Build Coastguard Worker bc_vec_clear(BcVec* restrict v); 313*5a6e8488SAndroid Build Coastguard Worker 314*5a6e8488SAndroid Build Coastguard Worker /** 315*5a6e8488SAndroid Build Coastguard Worker * Frees a vector and its elements. This is a destructor. 316*5a6e8488SAndroid Build Coastguard Worker * @param vec A vector as a void pointer. 317*5a6e8488SAndroid Build Coastguard Worker */ 318*5a6e8488SAndroid Build Coastguard Worker void 319*5a6e8488SAndroid Build Coastguard Worker bc_vec_free(void* vec); 320*5a6e8488SAndroid Build Coastguard Worker 321*5a6e8488SAndroid Build Coastguard Worker /** 322*5a6e8488SAndroid Build Coastguard Worker * Attempts to insert an ID into a map and returns true if it succeeded, false 323*5a6e8488SAndroid Build Coastguard Worker * if the item already exists. 324*5a6e8488SAndroid Build Coastguard Worker * @param v The map vector to insert into. 325*5a6e8488SAndroid Build Coastguard Worker * @param name The name of the item to insert. This name is assumed to be owned 326*5a6e8488SAndroid Build Coastguard Worker * by another entity. 327*5a6e8488SAndroid Build Coastguard Worker * @param idx The index of the partner array where the actual item is. 328*5a6e8488SAndroid Build Coastguard Worker * @param i A pointer to an index that will be set to the index of the item 329*5a6e8488SAndroid Build Coastguard Worker * in the map. 330*5a6e8488SAndroid Build Coastguard Worker * @return True if the item was inserted, false if the item already exists. 331*5a6e8488SAndroid Build Coastguard Worker */ 332*5a6e8488SAndroid Build Coastguard Worker bool 333*5a6e8488SAndroid Build Coastguard Worker bc_map_insert(BcVec* restrict v, const char* name, size_t idx, 334*5a6e8488SAndroid Build Coastguard Worker size_t* restrict i); 335*5a6e8488SAndroid Build Coastguard Worker 336*5a6e8488SAndroid Build Coastguard Worker /** 337*5a6e8488SAndroid Build Coastguard Worker * Returns the index of the item with @a name in the map, or BC_VEC_INVALID_IDX 338*5a6e8488SAndroid Build Coastguard Worker * if it doesn't exist. 339*5a6e8488SAndroid Build Coastguard Worker * @param v The map vector. 340*5a6e8488SAndroid Build Coastguard Worker * @param name The name of the item to find. 341*5a6e8488SAndroid Build Coastguard Worker * @return The index in the map of the item with @a name, or 342*5a6e8488SAndroid Build Coastguard Worker * BC_VEC_INVALID_IDX if the item does not exist. 343*5a6e8488SAndroid Build Coastguard Worker */ 344*5a6e8488SAndroid Build Coastguard Worker size_t 345*5a6e8488SAndroid Build Coastguard Worker bc_map_index(const BcVec* restrict v, const char* name); 346*5a6e8488SAndroid Build Coastguard Worker 347*5a6e8488SAndroid Build Coastguard Worker #if DC_ENABLED 348*5a6e8488SAndroid Build Coastguard Worker 349*5a6e8488SAndroid Build Coastguard Worker /** 350*5a6e8488SAndroid Build Coastguard Worker * Returns the name of the item at index @a idx in the map. 351*5a6e8488SAndroid Build Coastguard Worker * @param v The map vector. 352*5a6e8488SAndroid Build Coastguard Worker * @param idx The index. 353*5a6e8488SAndroid Build Coastguard Worker * @return The name of the item at @a idx. 354*5a6e8488SAndroid Build Coastguard Worker */ 355*5a6e8488SAndroid Build Coastguard Worker const char* 356*5a6e8488SAndroid Build Coastguard Worker bc_map_name(const BcVec* restrict v, size_t idx); 357*5a6e8488SAndroid Build Coastguard Worker 358*5a6e8488SAndroid Build Coastguard Worker #endif // DC_ENABLED 359*5a6e8488SAndroid Build Coastguard Worker 360*5a6e8488SAndroid Build Coastguard Worker /** 361*5a6e8488SAndroid Build Coastguard Worker * Pops one item off of the vector. 362*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to pop one item off of. 363*5a6e8488SAndroid Build Coastguard Worker */ 364*5a6e8488SAndroid Build Coastguard Worker #define bc_vec_pop(v) (bc_vec_npop((v), 1)) 365*5a6e8488SAndroid Build Coastguard Worker 366*5a6e8488SAndroid Build Coastguard Worker /** 367*5a6e8488SAndroid Build Coastguard Worker * Pops all items off of the vector. 368*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to pop all items off of. 369*5a6e8488SAndroid Build Coastguard Worker */ 370*5a6e8488SAndroid Build Coastguard Worker #define bc_vec_popAll(v) (bc_vec_npop((v), (v)->len)) 371*5a6e8488SAndroid Build Coastguard Worker 372*5a6e8488SAndroid Build Coastguard Worker /** 373*5a6e8488SAndroid Build Coastguard Worker * Return a pointer to the last item in the vector, or first if it's being 374*5a6e8488SAndroid Build Coastguard Worker * treated as a stack. 375*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to get the top of stack of. 376*5a6e8488SAndroid Build Coastguard Worker */ 377*5a6e8488SAndroid Build Coastguard Worker #define bc_vec_top(v) (bc_vec_item_rev((v), 0)) 378*5a6e8488SAndroid Build Coastguard Worker 379*5a6e8488SAndroid Build Coastguard Worker /** 380*5a6e8488SAndroid Build Coastguard Worker * Initializes a vector to serve as a map. 381*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to initialize. 382*5a6e8488SAndroid Build Coastguard Worker */ 383*5a6e8488SAndroid Build Coastguard Worker #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), BC_DTOR_NONE)) 384*5a6e8488SAndroid Build Coastguard Worker 385*5a6e8488SAndroid Build Coastguard Worker /// A reference to the array of destructors. 386*5a6e8488SAndroid Build Coastguard Worker extern const BcVecFree bc_vec_dtors[]; 387*5a6e8488SAndroid Build Coastguard Worker 388*5a6e8488SAndroid Build Coastguard Worker #if !BC_ENABLE_LIBRARY 389*5a6e8488SAndroid Build Coastguard Worker 390*5a6e8488SAndroid Build Coastguard Worker /// The allocated size of slabs. 391*5a6e8488SAndroid Build Coastguard Worker #define BC_SLAB_SIZE (4096) 392*5a6e8488SAndroid Build Coastguard Worker 393*5a6e8488SAndroid Build Coastguard Worker /// A slab for allocating strings. 394*5a6e8488SAndroid Build Coastguard Worker typedef struct BcSlab 395*5a6e8488SAndroid Build Coastguard Worker { 396*5a6e8488SAndroid Build Coastguard Worker /// The actual allocation. 397*5a6e8488SAndroid Build Coastguard Worker char* s; 398*5a6e8488SAndroid Build Coastguard Worker 399*5a6e8488SAndroid Build Coastguard Worker /// How many bytes of the slab are taken. 400*5a6e8488SAndroid Build Coastguard Worker size_t len; 401*5a6e8488SAndroid Build Coastguard Worker 402*5a6e8488SAndroid Build Coastguard Worker } BcSlab; 403*5a6e8488SAndroid Build Coastguard Worker 404*5a6e8488SAndroid Build Coastguard Worker /** 405*5a6e8488SAndroid Build Coastguard Worker * Frees a slab. This is a destructor. 406*5a6e8488SAndroid Build Coastguard Worker * @param slab The slab as a void pointer. 407*5a6e8488SAndroid Build Coastguard Worker */ 408*5a6e8488SAndroid Build Coastguard Worker void 409*5a6e8488SAndroid Build Coastguard Worker bc_slab_free(void* slab); 410*5a6e8488SAndroid Build Coastguard Worker 411*5a6e8488SAndroid Build Coastguard Worker /** 412*5a6e8488SAndroid Build Coastguard Worker * Initializes a slab vector. 413*5a6e8488SAndroid Build Coastguard Worker * @param v The vector to initialize. 414*5a6e8488SAndroid Build Coastguard Worker */ 415*5a6e8488SAndroid Build Coastguard Worker void 416*5a6e8488SAndroid Build Coastguard Worker bc_slabvec_init(BcVec* restrict v); 417*5a6e8488SAndroid Build Coastguard Worker 418*5a6e8488SAndroid Build Coastguard Worker /** 419*5a6e8488SAndroid Build Coastguard Worker * Duplicates the string using slabs in the slab vector. 420*5a6e8488SAndroid Build Coastguard Worker * @param v The slab vector. 421*5a6e8488SAndroid Build Coastguard Worker * @param str The string to duplicate. 422*5a6e8488SAndroid Build Coastguard Worker * @return A pointer to the duplicated string, owned by the slab vector. 423*5a6e8488SAndroid Build Coastguard Worker */ 424*5a6e8488SAndroid Build Coastguard Worker char* 425*5a6e8488SAndroid Build Coastguard Worker bc_slabvec_strdup(BcVec* restrict v, const char* str); 426*5a6e8488SAndroid Build Coastguard Worker 427*5a6e8488SAndroid Build Coastguard Worker /** 428*5a6e8488SAndroid Build Coastguard Worker * Clears a slab vector. This deallocates all but the first slab and clears the 429*5a6e8488SAndroid Build Coastguard Worker * first slab. 430*5a6e8488SAndroid Build Coastguard Worker * @param v The slab vector to clear. 431*5a6e8488SAndroid Build Coastguard Worker */ 432*5a6e8488SAndroid Build Coastguard Worker void 433*5a6e8488SAndroid Build Coastguard Worker bc_slabvec_clear(BcVec* restrict v); 434*5a6e8488SAndroid Build Coastguard Worker 435*5a6e8488SAndroid Build Coastguard Worker #if BC_DEBUG_CODE 436*5a6e8488SAndroid Build Coastguard Worker 437*5a6e8488SAndroid Build Coastguard Worker /** 438*5a6e8488SAndroid Build Coastguard Worker * Prints all of the items in a slab vector, in order. 439*5a6e8488SAndroid Build Coastguard Worker * @param v The vector whose items will be printed. 440*5a6e8488SAndroid Build Coastguard Worker */ 441*5a6e8488SAndroid Build Coastguard Worker void 442*5a6e8488SAndroid Build Coastguard Worker bc_slabvec_print(BcVec* v, const char* func); 443*5a6e8488SAndroid Build Coastguard Worker 444*5a6e8488SAndroid Build Coastguard Worker #endif // BC_DEBUG_CODE 445*5a6e8488SAndroid Build Coastguard Worker 446*5a6e8488SAndroid Build Coastguard Worker /// A convenience macro for freeing a vector of slabs. 447*5a6e8488SAndroid Build Coastguard Worker #define bc_slabvec_free bc_vec_free 448*5a6e8488SAndroid Build Coastguard Worker 449*5a6e8488SAndroid Build Coastguard Worker #ifndef _WIN32 450*5a6e8488SAndroid Build Coastguard Worker 451*5a6e8488SAndroid Build Coastguard Worker /** 452*5a6e8488SAndroid Build Coastguard Worker * A macro to get rid of a warning on Windows. 453*5a6e8488SAndroid Build Coastguard Worker * @param d The destination string. 454*5a6e8488SAndroid Build Coastguard Worker * @param l The length of the destination string. This has to be big enough to 455*5a6e8488SAndroid Build Coastguard Worker * contain @a s. 456*5a6e8488SAndroid Build Coastguard Worker * @param s The source string. 457*5a6e8488SAndroid Build Coastguard Worker */ 458*5a6e8488SAndroid Build Coastguard Worker #define bc_strcpy(d, l, s) strcpy(d, s) 459*5a6e8488SAndroid Build Coastguard Worker 460*5a6e8488SAndroid Build Coastguard Worker #else // _WIN32 461*5a6e8488SAndroid Build Coastguard Worker 462*5a6e8488SAndroid Build Coastguard Worker /** 463*5a6e8488SAndroid Build Coastguard Worker * A macro to get rid of a warning on Windows. 464*5a6e8488SAndroid Build Coastguard Worker * @param d The destination string. 465*5a6e8488SAndroid Build Coastguard Worker * @param l The length of the destination string. This has to be big enough to 466*5a6e8488SAndroid Build Coastguard Worker * contain @a s. 467*5a6e8488SAndroid Build Coastguard Worker * @param s The source string. 468*5a6e8488SAndroid Build Coastguard Worker */ 469*5a6e8488SAndroid Build Coastguard Worker #define bc_strcpy(d, l, s) strcpy_s(d, l, s) 470*5a6e8488SAndroid Build Coastguard Worker 471*5a6e8488SAndroid Build Coastguard Worker #endif // _WIN32 472*5a6e8488SAndroid Build Coastguard Worker 473*5a6e8488SAndroid Build Coastguard Worker #endif // !BC_ENABLE_LIBRARY 474*5a6e8488SAndroid Build Coastguard Worker 475*5a6e8488SAndroid Build Coastguard Worker #endif // BC_VECTOR_H 476