1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
Dynamic Arrays
==============
Dynamically resizing arrays (a C equivalent to std::vector).
.. code:: cpp
#include <util/darray.h>
.. struct:: darray
The base dynamic array structure.
.. :c:macro:: DARRAY(type)
Macro for a dynamic array based upon an actual type. Use this with
da_* macros.
.. member:: void *darray.array
The array pointer.
.. member:: size_t darray.num
The number of items within the array.
.. member:: size_t darray.capacity
The capacity of the array.
Dynamic Array Macros
--------------------
These macro functions are used with variables created with the
**DARRAY** type macro. When using these functions, do not use the
dynamic array value with a reference (&) operator. For example:
.. code:: cpp
/* creates an array of integers: 0..9 */
DARRAY(int) array_of_integers;
da_init(array_of_integers);
for (size_t i = 0; i < 10; i++)
da_push_back(array_of_integers, &i);
[...]
/* free when complete */
da_free(array_of_integers);
.. function:: void da_init(da)
Initializes a dynamic array.
:param da: The dynamic array
---------------------
.. function:: void da_free(da)
Frees a dynamic array.
:param da: The dynamic array
---------------------
.. function:: void *da_end(da)
Gets a pointer to the last value.
:param da: The dynamic array
:return: The last value of a dynamic array, or *NULL* if empty.
---------------------
.. function:: void da_reserve(da, size_t capacity)
Reserves a specific amount of buffer space for the dynamic array.
:param da: The dynamic array
:param capacity: New capacity of the dynamic array
---------------------
.. function:: void da_resize(da, size_t new_size)
Resizes the dynamic array with zeroed values.
:param da: The dynamic array
:param size: New size of the dynamic array
---------------------
.. function:: void da_copy(da_dst, da_src)
Makes a copy of a dynamic array.
:param da_dst: The dynamic array to copy to
:param da_src: The dynamic array to copy from
---------------------
.. function:: void da_copy_array(da, const void *src_array, size_t size)
Makes a copy of an array pointer.
:param da: The dynamic array
:param src_array: The array pointer to make a copy from
:param size: New size of the dynamic array
---------------------
.. function:: void da_move(da_dst, da_src)
Moves one dynamic array variable to another without allocating new
data. *da_dst* is freed before moving, *da_dst* is set to *da_src*,
then *da_src* is then zeroed.
:param da_dst: Destination variable
:param da_src: Source variable
---------------------
.. function:: size_t da_find(da, const void *item_data, size_t starting_idx)
Finds a value based upon its data. If the value cannot be found, the
return value will be DARRAY_INVALID (-1).
:param da: The dynamic array
:param item_data: The item data to find
:param starting_idx: The index to start from or 0 to search the
entire array
---------------------
.. function:: void da_push_back(da, const void *data)
Pushes data to the back of the array.
:param da: The dynamic array
:param data: Pointer to the new data to push
---------------------
.. function:: void *da_push_back_new(da)
Pushes a zeroed value to the back of the array, and returns a pointer
to it.
:param da: The dynamic array
:return: Pointer to the new value
---------------------
.. function:: void da_push_back_array(da, const void *src_array, size_t item_count)
Pushes an array of values to the back of the array.
:param da: The dynamic array
:param src_array: Pointer of the array of values
:param item_count: Number of items to push back
---------------------
.. function:: void da_insert(da, size_t idx, const void *data)
Inserts a value at a given index.
:param da: The dynamic array:
:param idx: Index where the new item will be inserted
:param data: Pointer to the item data to insert
---------------------
.. function:: void *da_insert_new(da, size_t idx)
Inserts a new zeroed value at a specific index, and returns a pointer
to it.
:param da: The dynamic array
:param idx: Index to insert at
:return: Pointer to the new value
---------------------
.. function:: void da_insert_da(da_dst, size_t idx, da_src)
Inserts a dynamic array in to another dynamic array at a specific
index.
:param da_dst: Destination dynamic array being inserted in to
:param idx: Index to insert the data at
:param da_src: The dynamic array to insert
---------------------
.. function:: void da_erase(da, size_t idx)
Erases an item at a specific index.
:param da: The dynamic array
:param idx: The index of the value to remove
---------------------
.. function:: void da_erase_item(da, const void *item_data)
Erases an item that matches the value specified
:param da: The dynamic array
:param item_data: Pointer to the data to remove
---------------------
.. function:: void da_erase_range(da, size_t start_idx, size_t end_idx)
Erases a range of values.
:param da: The dynamic array
:param start_idx: The starting index
:param end_idx: The ending index
---------------------
.. function:: void da_pop_back(da)
Removes one item from the end of a dynamic array.
:param da: The dynamic array
---------------------
.. function:: void da_join(da_dst, da_src)
Pushes *da_src* to the end of *da_dst* and frees *da_src*.
:param da_dst: The destination dynamic array
:param da_src: The source dynamic array
---------------------
.. function:: void da_split(da_dst1, da_dst2, da_src, size_t split_idx)
Creates two dynamic arrays by splitting another dynamic array at a
specific index. If the destination arrays are not freed, they will
be freed before getting their new values. The array being split will
not be freed.
:param da_dst1: Dynamic array that will get the lower half
:param da_dst2: Dynamic array that will get the upper half
:param da_src: Dynamic array to split
:param split_idx: Index to split *da_src* at
---------------------
.. function:: void da_move_item(da, size_t src_idx, size_t dst_idx)
Moves an item from one index to another, moving data between if
necessary.
:param da: The dynamic array
:param src_idx: The index of the item to move
:param dst_idx: The new index of where the item will be moved to
---------------------
.. function:: void da_swap(da, size_t idx1, size_t idx2)
Swaps two values at the given indices.
:param da: The dynamic array
:param idx1: Index of the first item to swap
:param idx2: Index of the second item to swap
|