forked from eteran/c-vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvector_utils.h
43 lines (39 loc) · 1.57 KB
/
cvector_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef CVECTOR_UTILS_H_
#define CVECTOR_UTILS_H_
/**
* @brief cvector_for_each_in - for header to iterate over vector each element's address
* @param it - iterator of type pointer to vector element
* @param vec - the vector
* @return void
*/
#define cvector_for_each_in(it, vec) \
for (it = cvector_begin(vec); it < cvector_end(vec); it++)
/**
* @brief cvector_for_each - call function func on each element of the vector
* @param vec - the vector
* @param func - function to be called on each element that takes each element as argument
* @return void
*/
#define cvector_for_each(vec, func) \
do { \
if ((vec) && (func) != NULL) { \
size_t i; \
for (i = 0; i < cvector_size(vec); i++) { \
func((vec)[i]); \
} \
} \
} while (0)
/**
* @brief cvector_free_each_and_free - calls `free_func` on each element
* contained in the vector and then destroys the vector itself
* @param vec - the vector
* @param free_func - function used to free each element in the vector with
* one parameter which is the element to be freed)
* @return void
*/
#define cvector_free_each_and_free(vec, free_func) \
do { \
cvector_for_each((vec), (free_func)); \
cvector_free(vec); \
} while (0)
#endif /* CVECTOR_UTILS_H_ */