LibCDS
vector.h
Go to the documentation of this file.
1 /**
2  * @file vector.h The dynamically growable array.
3  */
4 
5 #ifndef _VECTOR_H_
6 #define _VECTOR_H_
7 
8 #include "../util.h"
9 
10 /** VectorData is the data type for the container private information. */
11 typedef struct _VectorData VectorData;
12 
13 /** The implementation for dynamically growable array. */
14 typedef struct _Vector {
15  /** The container private information */
17 
18  /** Push an item to the tail of the vector.
19  @see VectorPushBack */
20  int32_t (*push_back) (struct _Vector*, Item);
21 
22  /** Insert an item to the designated index of the vector.
23  @see VectorInsert */
24  int32_t (*insert) (struct _Vector*, Item, int32_t);
25 
26  /** Pop an item from the tail of the vector.
27  @see VectorPopBack */
28  int32_t (*pop_back) (struct _Vector*);
29 
30  /** Delete an item from the designated index of the vector.
31  @see VectorDelete */
32  int32_t (*delete) (struct _Vector*, int32_t);
33 
34  /** Set an item at the designated index of the vector.
35  @see VectorSet */
36  int32_t (*set) (struct _Vector*, Item, int32_t);
37 
38  /** Get an item from the designated index of the vector.
39  @see VectorGet */
40  int32_t (*get) (struct _Vector*, Item*, int32_t);
41 
42  /** Change the container capacity.
43  @see VectorResize */
44  int32_t (*resize) (struct _Vector*, int32_t);
45 
46  /** Return the number of stored items.
47  @see VectorSize */
48  int32_t (*size) (struct _Vector*);
49 
50  /** Return the container capacity.
51  @see VectorCapacity */
52  int32_t (*capacity) (struct _Vector*);
53 
54  /** Sort the items via the designated item comparison method.
55  @see VectorSort */
56  int32_t (*sort) (struct _Vector*, int32_t (*) (const void*, const void*));
57 
58  /** Iterate through the vector till the tail end.
59  @see VectorIterate */
60  int32_t (*iterate) (struct _Vector*, bool, Item*);
61 
62  /** Reversely iterate through the vector till the head end.
63  @see VectorReverseIterate */
64  int32_t (*reverse_iterate) (struct _Vector*, bool, Item*);
65 
66  /** Set the custom item resource clean method.
67  @see VectorSetDestroy */
68  int32_t (*set_destroy) (struct _Vector*, void (*) (Item));
69 } Vector;
70 
71 
72 /*===========================================================================*
73  * Definition for the exported member operations *
74  *===========================================================================*/
75 /**
76  * @brief The constructor for Vector.
77  *
78  * @param ppObj The double pointer to the to be constructed vector
79  * @param iCap The designated initial capacity
80  *
81  * @retval SUCC
82  * @retval ERR_NOMEM Insufficient memory for vector construction
83  *
84  * @note Specify iCap to 0 for default initial capacity.
85  */
86 int32_t VectorInit(Vector **ppObj, int32_t iCap);
87 
88 /**
89  * @brief The destructor for Vector.
90  *
91  * If the custom resource clean method is set, it also runs the clean method for
92  * all the items.
93  *
94  * @param ppObj The double pointer to the to be destructed vector
95  */
96 void VectorDeinit(Vector **ppObj);
97 
98 /**
99  * @brief Push an item to the tail of the vector.
100  *
101  * This function pushes an item to the tail of the vector with the corresponding
102  * vector size extension. If the storage is full, the space reallocation is
103  * automatically triggered to store the newly pushed item.
104  *
105  * @param self The pointer to the Vector structure
106  * @param item The designated item
107  *
108  * @retval SUCC
109  * @retval ERR_NOINIT Uninitialized container
110  * @retval ERR_NOMEM Insufficient memory for vector extension
111  */
112 int32_t VectorPushBack(Vector *self, Item item);
113 
114 /**
115  * @brief Insert an item to the designated index of the vector.
116  *
117  * This function inserts an item to the designated index of the vector and shifts
118  * the trailing items one position to the tail. Naturally, the vector size is extended.
119  * And if the storage is full, the space reallocation is automatically triggered
120  * to store the newly inserted item.
121  *
122  * @param self The pointer to the Vector structure
123  * @param item The designated item
124  * @param iIdx The designated index
125  *
126  * @retval SUCC
127  * @retval ERR_NOINIT Uninitialized container
128  * @retval ERR_IDX Illegal index
129  *
130  * @note The designated index should be equal to or smaller than the vector
131  * size and should not be negative. If the index is equal to the vector size,
132  * the effect is equivalent to push_back().
133  */
134 int32_t VectorInsert(Vector *self, Item item, int32_t iIdx);
135 
136 /**
137  * @brief Pop an item from the tail of the vector.
138  *
139  * This function removes an item from the tail of the vector. If the custom
140  * resource clean method is set, it also runs the clean method for the removed
141  * item. Naturally, the vector size is shrunk.
142  *
143  * @param self The pointer to the Vector structure
144  *
145  * @retval SUCC
146  * @retval ERR_NOINIT Uninitialized container
147  * @retval ERR_IDX Empty vector
148  */
149 int32_t VectorPopBack(Vector *self);
150 
151 /**
152  * @brief Delete an item from the designated index of the vector.
153  *
154  * This function removes an item from the designated index of the vector and shifts
155  * the trailing items one position to the head. If the custom resource clean
156  * method is set, it also runs the clean method for the removed item. Naturally,
157  * the vector size is shrunk.
158  *
159  * @param self The pointer to the Vector structure
160  * @param iIdx The designated index
161  *
162  * @retval SUCC
163  * @retval ERR_NOINIT Uninitialized container
164  * @retval ERR_IDX Illegal index
165  *
166  * @note The designated index should be smaller than the vector size and
167  * should not be negative. If the index is equal to the vector size minus one,
168  * the operation is equivalent to pop_back().
169  */
170 int32_t VectorDelete(Vector *self, int32_t iIdx);
171 
172 /**
173  * @brief Set an item at the designated index of the vector.
174  *
175  * This function replaces an item at the designated index of the vector. If the
176  * custom resource clean method is set, it also runs the clean method for the
177  * replaced item.
178  *
179  * @param self The pointer to the Vector structure
180  * @param item The designated item
181  * @param iIdx The designated index
182  *
183  * @retval SUCC
184  * @retval ERR_NOINIT Uninitialized container
185  * @retval ERR_IDX Illegal index
186  *
187  * @note The designated index should be smaller than the vector size and should
188  * not be negative.
189  */
190 int32_t VectorSet(Vector *self, Item item, int32_t iIdx);
191 
192 /**
193  * @brief Get an item from the designated index of the vector.
194  *
195  * This function retrieves an item from the designated index of the vector. If
196  * the index is illegal, the error code is returned and the second parameter is
197  * also updated with NULL.
198  *
199  * @param self The pointer to the Vector structure
200  * @param pItem The pointer to the returned item
201  * @param iIdx The designated index
202  *
203  * @retval SUCC
204  * @retval ERR_NOINIT Uninitialized container
205  * @retval ERR_IDX Illegal index
206  * @retval ERR_GET Invalid parameter to store returned item
207  *
208  * @note The designated index should be smaller than the vector size and should
209  * not be negative.
210  */
211 int32_t VectorGet(Vector *self, Item *pItem, int32_t iIdx);
212 
213 /**
214  * @brief Change the container capacity.
215  *
216  * This function resizes the storage capacity. If the new capacity is smaller
217  * than the old size. The trailing items will be removed. If the custom resource
218  * clean method is set, it also runs the clean method for the removed items.
219  *
220  * @param self The pointer to the Vector structure
221  * @param iCap The designated capacity
222  *
223  * @retval SUCC
224  * @retval ERR_NOINIT Uninitialized container
225  * @retval ERR_NOMEM Insufficient memory for vector extension
226  *
227  * @note The designated capacity should greater than zero.
228  */
229 int32_t VectorResize(Vector *self, int32_t iCap);
230 
231 /**
232  * @brief Return the number of stored items.
233  *
234  * @param self The pointer to the Vector structure
235  *
236  * @return The number of stored items
237  * @retval ERR_NOINIT Uninitialized container
238  */
239 int32_t VectorSize(Vector *self);
240 
241 /**
242  * @brief Return the container capacity.
243  *
244  * @param self The pointer to the Vector structure
245  *
246  * @return The container capacity
247  * @retval ERR_NOINIT Uninitialized container
248  */
249 int32_t VectorCapacity(Vector *self);
250 
251 /**
252  * @brief Sort the items via the designated item comparison method.
253  *
254  * @param self The pointer to the Vector structure
255  * @param pFunc The function pointer to the custom method
256  *
257  * @retval SUCC
258  * @retval ERR_NOINIT Uninitialized container
259  */
260 int32_t VectorSort(Vector *self, int32_t (*pFunc) (const void*, const void*));
261 
262 /**
263  * @brief Iterate through the vector till the tail end.
264  *
265  * Before iterating through the vector, it is necessary to pass bReset := true and
266  * pItem := NULL for iterator initialization.
267  * After initialization, you can pass bReset := false and pItem := the relevant
268  * pointer to get the returned item at each iteration.
269  *
270  * @param self The pointer to the Vector structure
271  * @param bReset The knob to reset the iteration
272  * @param pItem The pointer to the returned item
273  *
274  * @retval SUCC
275  * @retval END At the tail end of the vector
276  * @retval ERR_NOINIT Uninitialized container
277  * @retval ERR_GET Invalid parameter to store returned item
278  */
279 int32_t VectorIterate(Vector *self, bool bReset, Item *pItem);
280 
281 /**
282  * @brief Reversely iterate through the vector till the head end.
283  *
284  * Before reversely iterating through the vector, it is necessary to pass
285  * bReset := true and pItem := NULL for iterator initialization.
286  * After initialization, you can pass bReset := false and pItem := the relevant
287  * pointer to get the returned item at each iteration.
288  *
289  * @param self The pointer to the Vector structure
290  * @param bReset The knob to reset the iteration
291  * @param pItem The pointer to the returned item
292  *
293  * @retval SUCC
294  * @retval END At the tail end of the vector
295  * @retval ERR_NOINIT Uninitialized container
296  * @retval ERR_GET Invalid parameter to store returned item
297  */
298 int32_t VectorReverseIterate(Vector *self, bool bReset, Item *pItem);
299 
300 /**
301  * @brief Set the custom item resource clean method.
302  *
303  * @param self The pointer to the Vector structure
304  * @param pFunc The function pointer to the custom method
305  *
306  * @retval SUCC
307  * @retval ERR_NOINIT Uninitialized container
308  */
309 int32_t VectorSetDestroy(Vector *self, void (*pFunc) (Item));
310 
311 #endif
int32_t VectorInsert(Vector *self, Item item, int32_t iIdx)
Insert an item to the designated index of the vector.
void VectorDeinit(Vector **ppObj)
The destructor for Vector.
int32_t VectorDelete(Vector *self, int32_t iIdx)
Delete an item from the designated index of the vector.
int32_t VectorSetDestroy(Vector *self, void(*pFunc)(Item))
Set the custom item resource clean method.
int32_t VectorIterate(Vector *self, bool bReset, Item *pItem)
Iterate through the vector till the tail end.
int32_t VectorSort(Vector *self, int32_t(*pFunc)(const void *, const void *))
Sort the items via the designated item comparison method.
int32_t VectorPushBack(Vector *self, Item item)
Push an item to the tail of the vector.
struct _VectorData VectorData
VectorData is the data type for the container private information.
Definition: vector.h:11
int32_t VectorPopBack(Vector *self)
Pop an item from the tail of the vector.
The implementation for dynamically growable array.
Definition: vector.h:14
int32_t VectorInit(Vector **ppObj, int32_t iCap)
The constructor for Vector.
int32_t VectorSize(Vector *self)
Return the number of stored items.
int32_t VectorResize(Vector *self, int32_t iCap)
Change the container capacity.
int32_t VectorReverseIterate(Vector *self, bool bReset, Item *pItem)
Reversely iterate through the vector till the head end.
int32_t VectorGet(Vector *self, Item *pItem, int32_t iIdx)
Get an item from the designated index of the vector.
VectorData * pData
The container private information.
Definition: vector.h:16
int32_t VectorSet(Vector *self, Item item, int32_t iIdx)
Set an item at the designated index of the vector.
int32_t VectorCapacity(Vector *self)
Return the container capacity.