xref: /freebsd/contrib/bc/include/vector.h (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2020 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 
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);
68 
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);
76 
77 #if BC_ENABLE_HISTORY
78 void bc_vec_replaceAt(BcVec *restrict v, size_t idx, const void *data);
79 #endif // BC_ENABLE_HISTORY
80 
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);
83 
84 void bc_vec_clear(BcVec *restrict v);
85 
86 void bc_vec_free(void *vec);
87 
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);
91 
92 #define bc_vec_pop(v) (bc_vec_npop((v), 1))
93 #define bc_vec_top(v) (bc_vec_item_rev((v), 0))
94 
95 #ifndef NDEBUG
96 #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), bc_id_free))
97 #else // NDEBUG
98 #define bc_map_init(v) (bc_vec_init((v), sizeof(BcId), NULL))
99 #endif // NDEBUG
100 
101 #endif // BC_VECTOR_H
102