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) \
50 _vector_new(sizeof(type)); \
54 * @brief Copy constructor (shallow copy, ok for basic types).
57 Vector
* vector
///< "this" pointer.
61 * @brief Check if the vector is empty.
64 Vector
* vector
///< "this" pointer.
68 * @brief Return current size.
71 Vector
* vector
///< "this" pointer.
75 * @brief Reallocate internal array.
78 Vector
* vector
, ///< "this" pointer.
79 UInt newCapacity
///< New capacity of the vector (in number of elements).
83 * @brief Add data at the end.
86 Vector
* vector
, ///< "this" pointer.
87 void* data
///< Data to be added.
91 * @brief Add data at the end.
92 * @param vector "this" pointer.
93 * @param data Data to be added.
95 * Usage: void vector_push(Vector* vector, void data)
97 #define vector_push(vector, data) \
99 typeof((data)) tmp = data; \
100 _vector_push(vector,&tmp); \
104 * @brief Remove the last pushed element.
107 Vector
* vector
///< "this" pointer.
111 * @brief Get the element at given index.
114 Vector
* vector
, ///< "this" pointer.
115 UInt index
///< Index of the element to retrieve.
119 * @brief Get the element at given index.
120 * @param vector "this" pointer.
121 * @param index Index of the element to retrieve.
122 * @param data 'out' variable to contain the result.
124 * Usage: void vector_get(Vector* vector, size_t index, void data)
126 #define vector_get(vector, index, data) \
128 void* pData = _vector_get(vector,index); \
129 data = *((typeof(&data))pData); \
133 * @brief Set the element at given index.
136 Vector
* vector
, ///< "this" pointer.
137 UInt index
, ///< Index of the element to be modified.
138 void* data
///< Pointer to new data at given index.
142 * @brief Set the element at given index.
143 * @param vector "this" pointer.
144 * @param index Index of the element to be modified.
145 * @param data New data at given index.
147 * Usage: void vector_set(Vector* vector, size_t index, void data)
149 #define vector_set(vector, index, data) \
151 typeof((data)) tmp = data; \
152 _vector_set(vector,index,&tmp); \
156 * @brief Clear the entire vector.
159 Vector
* vector
///< "this" pointer.
163 * @brief Destroy the vector: clear it, and free 'vector' pointer.
166 Vector
* vector
///< "this" pointer.
174 * @brief Iterator on a generic vector.
176 typedef struct VectorIterator
{
177 Vector
* vector
; ///< Vector to be iterated.
178 void** current
; ///< Current vector element.
182 * @brief Obtain an iterator object, starting at vector beginning (index 0).
184 VectorIterator
* vector_get_iterator(
185 Vector
* vector
///< Pointer to the vector to iterate over.
189 * @brief (Re)set current position inside vector to beginning (0).
191 void vectorI_reset_begin(
192 VectorIterator
* vectorI
///< "this" pointer.
196 * @brief (Re)set current position inside vector to end (vector->size-1).
198 void vectorI_reset_end(
199 VectorIterator
* vectorI
///< "this" pointer.
203 * @brief Tell if there is some data at the current index.
205 bool vectorI_has_data(
206 VectorIterator
* vectorI
///< "this" pointer.
210 * @brief Get data contained at the current index.
213 VectorIterator
* vectorI
///< "this" pointer.
217 * @brief Get data contained at the current index.
218 * @param vectorI "this" pointer.
219 * @param data 'out' variable to contain the result.
221 * Usage: void vectorI_get(VectorIterator* vectorI, void data);
223 #define vectorI_get(vectorI, data) \
225 void* pData = _vectorI_get(vectorI); \
226 data = *((typeof(&data))pData); \
230 * @brief Set the element at current index.
233 VectorIterator
* vectorI
, ///< "this" pointer.
234 void* data
///< Data to be assigned.
238 * @brief Set the element at current index.
239 * @param vectorI "this" pointer.
240 * @param data Data to be assigned.
242 * Usage: void vectorI_set(VectorIterator* vectorI, void data)
244 #define vectorI_set(vectorI, data) \
246 typeof((data)) tmp = data; \
247 _vectorI_set(vectorI,&tmp); \
251 * @brief Move current iterator position forward (toward last index).
253 void vectorI_move_next(
254 VectorIterator
* vectorI
///< "this" pointer.
258 * @brief Move current iterator position backward (toward first index).
260 void vectorI_move_prev(
261 VectorIterator
* vectorI
///< "this" pointer.
265 * @brief Free memory allocated for the iterator.
267 void vectorI_destroy(
268 VectorIterator
* vectorI
///< "this" pointer.