Implement HashTable + fix some extra blank spaces, remove Bool type (using bool ...
[cgds.git] / src / Vector.h
CommitLineData
a7868768
BA
1/**
2 * @file Vector.h
3 */
4
5#ifndef CGDS_VECTOR_H
6#define CGDS_VECTOR_H
7
8#include <stdlib.h>
9#include <string.h>
10#include "cgds/safe_alloc.h"
11#include "cgds/types.h"
12
13//*************
14// Vector logic
15//*************
16
17/**
18 * @brief Generic resizable array.
19 */
20typedef 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.
25} Vector;
26
27/**
28 * @brief Initialize an empty vector.
29 */
30void _vector_init(
31 Vector* vector, ///< "this" pointer.
32 size_t dataSize ///< Size in bytes of a vector element.
33);
34
35/**
36 * @brief Return an allocated and initialized vector.
37 */
38Vector* _vector_new(
39 size_t dataSize ///< Size in bytes of a vector element.
40);
41
42/**
43 * @brief Return an allocated and initialized vector.
44 * @param type Type of a vector element (int, char*, ...).
1ff641f9 45 *
a7868768
BA
46 * Usage: Vector* vector_new(<Type> type)
47 */
48#define vector_new(type) \
49 _vector_new(sizeof(type))
50
51/**
1ff641f9 52 * @brief Copy constructor (shallow copy, ok for basic types).
a7868768
BA
53 */
54Vector* vector_copy(
55 Vector* vector ///< "this" pointer.
56);
57
58/**
59 * @brief Check if the vector is empty.
60 */
1ff641f9 61bool vector_empty(
a7868768
BA
62 Vector* vector ///< "this" pointer.
63);
64
65/**
66 * @brief Return current size.
67 */
68UInt vector_size(
69 Vector* vector ///< "this" pointer.
70);
71
72/**
73 * @brief Reallocate internal array.
74 */
75void _vector_realloc(
76 Vector* vector, ///< "this" pointer.
77 UInt newCapacity ///< New capacity of the vector (in number of elements).
78);
79
80/**
81 * @brief Add data at the end.
82 */
83void _vector_push(
84 Vector* vector, ///< "this" pointer.
85 void* data ///< Data to be added.
86);
87
88/**
89 * @brief Add data at the end.
90 * @param vector "this" pointer.
91 * @param data Data to be added.
1ff641f9 92 *
a7868768
BA
93 * Usage: void vector_push(Vector* vector, void data)
94 */
95#define vector_push(vector, data) \
96{ \
97 typeof((data)) tmp = data; \
98 _vector_push(vector,&tmp); \
99}
100
101/**
102 * @brief Remove the last pushed element.
103 */
104void vector_pop(
105 Vector* vector ///< "this" pointer.
106);
107
108/**
109 * @brief Get the element at given index.
110 */
111void* _vector_get(
112 Vector* vector, ///< "this" pointer.
113 UInt index ///< Index of the element to retrieve.
114);
115
116/**
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.
1ff641f9 121 *
a7868768
BA
122 * Usage: void vector_get(Vector* vector, size_t index, void data)
123 */
124#define vector_get(vector, index, data) \
125{ \
126 void* pData = _vector_get(vector,index); \
127 data = *((typeof(&data))pData); \
128}
129
130/**
131 * @brief Set the element at given index.
132 */
133void _vector_set(
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.
137);
138
139/**
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.
1ff641f9 144 *
a7868768
BA
145 * Usage: void vector_set(Vector* vector, size_t index, void data)
146 */
147#define vector_set(vector, index, data) \
148{ \
149 typeof((data)) tmp = data; \
150 _vector_set(vector,index,&tmp); \
151}
152
153/**
154 * @brief Clear the entire vector.
155 */
156void vector_clear(
157 Vector* vector ///< "this" pointer.
158);
159
160/**
161 * @brief Destroy the vector: clear it, and free 'vector' pointer.
162 */
163void vector_destroy(
164 Vector* vector ///< "this" pointer.
165);
166
167//***************
168// Iterator logic
169//***************
170
171/**
172 * @brief Iterator on a generic vector.
173 */
174typedef struct VectorIterator {
175 Vector* vector; ///< Vector to be iterated.
176 void** current; ///< Current vector element.
177} VectorIterator;
178
179/**
180 * @brief Obtain an iterator object, starting at vector beginning (index 0).
181 */
182VectorIterator* vector_get_iterator(
183 Vector* vector ///< Pointer to the vector to iterate over.
184);
185
186/**
187 * @brief (Re)set current position inside vector to beginning (0).
188 */
189void vectorI_reset_begin(
190 VectorIterator* vectorI ///< "this" pointer.
191);
192
193/**
194 * @brief (Re)set current position inside vector to end (vector->size-1).
195 */
196void vectorI_reset_end(
197 VectorIterator* vectorI ///< "this" pointer.
198);
199
200/**
201 * @brief Tell if there is some data at the current index.
202 */
1ff641f9 203bool vectorI_has_data(
a7868768
BA
204 VectorIterator* vectorI ///< "this" pointer.
205);
206
207/**
208 * @brief Get data contained at the current index.
209 */
210void* _vectorI_get(
211 VectorIterator* vectorI ///< "this" pointer.
212);
213
214/**
215 * @brief Get data contained at the current index.
216 * @param vectorI "this" pointer.
217 * @param data 'out' variable to contain the result.
1ff641f9 218 *
a7868768
BA
219 * Usage: void vectorI_get(VectorIterator* vectorI, void data);
220 */
221#define vectorI_get(vectorI, data) \
222{ \
223 void* pData = _vectorI_get(vectorI); \
224 data = *((typeof(&data))pData); \
225}
226
227/**
228 * @brief Set the element at current index.
229 */
230void _vectorI_set(
231 VectorIterator* vectorI, ///< "this" pointer.
232 void* data ///< Data to be assigned.
233);
234
235/**
236 * @brief Set the element at current index.
237 * @param vectorI "this" pointer.
238 * @param data Data to be assigned.
1ff641f9 239 *
a7868768
BA
240 * Usage: void vectorI_set(VectorIterator* vectorI, void data)
241 */
242#define vectorI_set(vectorI, data) \
243{ \
244 typeof((data)) tmp = data; \
245 _vectorI_set(vectorI,&tmp); \
246}
247
248/**
249 * @brief Move current iterator position forward (toward last index).
250 */
251void vectorI_move_next(
252 VectorIterator* vectorI ///< "this" pointer.
253);
254
255/**
256 * @brief Move current iterator position backward (toward first index).
257 */
258void vectorI_move_prev(
259 VectorIterator* vectorI ///< "this" pointer.
260);
261
262/**
263 * @brief Free memory allocated for the iterator.
264 */
265void vectorI_destroy(
266 VectorIterator* vectorI ///< "this" pointer.
267);
268
269#endif