10 #include "cgds/safe_alloc.h"
11 #include "cgds/types.h"
18 * @brief Generic resizable array.
20 typedef struct Vector
{
21 void** datas
; ///< Data array of fixed length (reallocated if needed).
22 size_t dataSize
; ///< Size in bytes of a vector element.
23 UInt size
; ///< Count elements in the vector.
24 UInt capacity
; ///< Current maximal capacity; always larger than size.
28 * @brief Initialize an empty vector.
31 Vector
* vector
, ///< "this" pointer.
32 size_t dataSize
///< Size in bytes of a vector element.
36 * @brief Return an allocated and initialized vector.
39 size_t dataSize
///< Size in bytes of a vector element.
43 * @brief Return an allocated and initialized vector.
44 * @param type Type of a vector element (int, char*, ...).
46 * Usage: Vector* vector_new(<Type> type)
48 #define vector_new(type) \
49 _vector_new(sizeof(type))
52 * @brief Copy constructor (works well if items do not have allocated sub-pointers).
55 Vector
* vector
///< "this" pointer.
59 * @brief Check if the vector is empty.
62 Vector
* vector
///< "this" pointer.
66 * @brief Return current size.
69 Vector
* vector
///< "this" pointer.
73 * @brief Reallocate internal array.
76 Vector
* vector
, ///< "this" pointer.
77 UInt newCapacity
///< New capacity of the vector (in number of elements).
81 * @brief Add data at the end.
84 Vector
* vector
, ///< "this" pointer.
85 void* data
///< Data to be added.
89 * @brief Add data at the end.
90 * @param vector "this" pointer.
91 * @param data Data to be added.
93 * Usage: void vector_push(Vector* vector, void data)
95 #define vector_push(vector, data) \
97 typeof((data)) tmp = data; \
98 _vector_push(vector,&tmp); \
102 * @brief Remove the last pushed element.
105 Vector
* vector
///< "this" pointer.
109 * @brief Remove the first element.
111 void vector_pop_first(
112 Vector
* vector
///< "this" pointer.
116 * @brief Get the element at given index.
119 Vector
* vector
, ///< "this" pointer.
120 UInt index
///< Index of the element to retrieve.
124 * @brief Get the element at given index.
125 * @param vector "this" pointer.
126 * @param index Index of the element to retrieve.
127 * @param data 'out' variable to contain the result.
129 * Usage: void vector_get(Vector* vector, size_t index, void data)
131 #define vector_get(vector, index, data) \
133 void* pData = _vector_get(vector,index); \
134 data = *((typeof(&data))pData); \
138 * @brief Set the element at given index.
141 Vector
* vector
, ///< "this" pointer.
142 UInt index
, ///< Index of the element to be modified.
143 void* data
///< Pointer to new data at given index.
147 * @brief Set the element at given index.
148 * @param vector "this" pointer.
149 * @param index Index of the element to be modified.
150 * @param data New data at given index.
152 * Usage: void vector_set(Vector* vector, size_t index, void data)
154 #define vector_set(vector, index, data) \
156 typeof((data)) tmp = data; \
157 _vector_set(vector,index,&tmp); \
161 * @brief Clear the entire vector.
164 Vector
* vector
///< "this" pointer.
168 * @brief Destroy the vector: clear it, and free 'vector' pointer.
171 Vector
* vector
///< "this" pointer.
179 * @brief Iterator on a generic vector.
181 typedef struct VectorIterator
{
182 Vector
* vector
; ///< Vector to be iterated.
183 void** current
; ///< Current vector element.
187 * @brief Obtain an iterator object, starting at vector beginning (index 0).
189 VectorIterator
* vector_get_iterator(
190 Vector
* vector
///< Pointer to the vector to iterate over.
194 * @brief (Re)set current position inside vector to beginning (0).
196 void vectorI_reset_begin(
197 VectorIterator
* vectorI
///< "this" pointer.
201 * @brief (Re)set current position inside vector to end (vector->size-1).
203 void vectorI_reset_end(
204 VectorIterator
* vectorI
///< "this" pointer.
208 * @brief Tell if there is some data at the current index.
210 Bool
vectorI_has_data(
211 VectorIterator
* vectorI
///< "this" pointer.
215 * @brief Get data contained at the current index.
218 VectorIterator
* vectorI
///< "this" pointer.
222 * @brief Get data contained at the current index.
223 * @param vectorI "this" pointer.
224 * @param data 'out' variable to contain the result.
226 * Usage: void vectorI_get(VectorIterator* vectorI, void data);
228 #define vectorI_get(vectorI, data) \
230 void* pData = _vectorI_get(vectorI); \
231 data = *((typeof(&data))pData); \
235 * @brief Set the element at current index.
238 VectorIterator
* vectorI
, ///< "this" pointer.
239 void* data
///< Data to be assigned.
243 * @brief Set the element at current index.
244 * @param vectorI "this" pointer.
245 * @param data Data to be assigned.
247 * Usage: void vectorI_set(VectorIterator* vectorI, void data)
249 #define vectorI_set(vectorI, data) \
251 typeof((data)) tmp = data; \
252 _vectorI_set(vectorI,&tmp); \
256 * @brief Move current iterator position forward (toward last index).
258 void vectorI_move_next(
259 VectorIterator
* vectorI
///< "this" pointer.
263 * @brief Move current iterator position backward (toward first index).
265 void vectorI_move_prev(
266 VectorIterator
* vectorI
///< "this" pointer.
270 * @brief Free memory allocated for the iterator.
272 void vectorI_destroy(
273 VectorIterator
* vectorI
///< "this" pointer.