2dc91d316d88adf045d7ab9e2785e0ea4f375f72
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 (shallow copy, ok for basic types).
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 Get the element at given index.
112 Vector
* vector
, ///< "this" pointer.
113 UInt index
///< Index of the element to retrieve.
117 * @brief Get the element at given index.
118 * @param vector "this" pointer.
119 * @param index Index of the element to retrieve.
120 * @param data 'out' variable to contain the result.
122 * Usage: void vector_get(Vector* vector, size_t index, void data)
124 #define vector_get(vector, index, data) \
126 void* pData = _vector_get(vector,index); \
127 data = *((typeof(&data))pData); \
131 * @brief Set the element at given index.
134 Vector
* vector
, ///< "this" pointer.
135 UInt index
, ///< Index of the element to be modified.
136 void* data
///< Pointer to new data at given index.
140 * @brief Set the element at given index.
141 * @param vector "this" pointer.
142 * @param index Index of the element to be modified.
143 * @param data New data at given index.
145 * Usage: void vector_set(Vector* vector, size_t index, void data)
147 #define vector_set(vector, index, data) \
149 typeof(data) tmp = data; \
150 _vector_set(vector, index, &tmp); \
154 * @brief Clear the entire vector.
157 Vector
* vector
///< "this" pointer.
161 * @brief Destroy the vector: clear it, and free 'vector' pointer.
164 Vector
* vector
///< "this" pointer.
172 * @brief Iterator on a generic vector.
174 typedef struct VectorIterator
{
175 Vector
* vector
; ///< Vector to be iterated.
176 void* current
; ///< Current vector element.
180 * @brief Obtain an iterator object, starting at vector beginning (index 0).
182 VectorIterator
* vector_get_iterator(
183 Vector
* vector
///< Pointer to the vector to iterate over.
187 * @brief (Re)set current position inside vector to beginning (0).
189 void vectorI_reset_begin(
190 VectorIterator
* vectorI
///< "this" pointer.
194 * @brief (Re)set current position inside vector to end (vector->size-1).
196 void vectorI_reset_end(
197 VectorIterator
* vectorI
///< "this" pointer.
201 * @brief Tell if there is some data at the current index.
203 bool vectorI_has_data(
204 VectorIterator
* vectorI
///< "this" pointer.
208 * @brief Get data contained at the current index.
211 VectorIterator
* vectorI
///< "this" pointer.
215 * @brief Get data contained at the current index.
216 * @param vectorI "this" pointer.
217 * @param data 'out' variable to contain the result.
219 * Usage: void vectorI_get(VectorIterator* vectorI, void data);
221 #define vectorI_get(vectorI, data) \
223 void* pData = _vectorI_get(vectorI); \
224 data = *((typeof(&data))pData); \
228 * @brief Set the element at current index.
231 VectorIterator
* vectorI
, ///< "this" pointer.
232 void* data
///< Data to be assigned.
236 * @brief Set the element at current index.
237 * @param vectorI "this" pointer.
238 * @param data Data to be assigned.
240 * Usage: void vectorI_set(VectorIterator* vectorI, void data)
242 #define vectorI_set(vectorI, data) \
244 typeof(data) tmp = data; \
245 _vectorI_set(vectorI, &tmp); \
249 * @brief Move current iterator position forward (toward last index).
251 void vectorI_move_next(
252 VectorIterator
* vectorI
///< "this" pointer.
256 * @brief Move current iterator position backward (toward first index).
258 void vectorI_move_prev(
259 VectorIterator
* vectorI
///< "this" pointer.
263 * @brief Free memory allocated for the iterator.
265 void vectorI_destroy(
266 VectorIterator
* vectorI
///< "this" pointer.