cu
vector.h
Go to the documentation of this file.
1 #ifndef CU_VECTOR_H_
2 #define CU_VECTOR_H_
3 
4 #include <stdlib.h>
5 #include <stdbool.h>
6 
14 typedef struct {
16  void **data;
18  size_t memsize;
20  size_t count;
21 } vector_t;
22 
23 
34 #define VECTOR_INIT(N) {.data = calloc(N, sizeof(void *)), .memsize = N, \
35  .count = 0 }
36 
48 #define VECTOR_DEL(V, FN) vector_clear(&V, FN); free(V.data);
49 
58 extern vector_t *vector_new();
59 
60 
71 extern vector_t *vector_with_cap(size_t n);
72 
73 
91 extern void vector_insert(vector_t *v, size_t index, void *data);
92 
93 
108 extern void vector_push(vector_t *v, void *data);
109 
115 extern size_t vector_len(const vector_t *v);
116 
122 extern bool vector_is_empty(const vector_t *v);
123 
140 extern void vector_pop(vector_t *v, void (*df)(void *));
141 
142 
159 extern void vector_remove(vector_t *v, size_t index, void (*df)(void *));
160 
161 
176 extern void *vector_get(const vector_t *v, size_t index);
177 
178 
192 extern void vector_foreach(const vector_t *v, void (*f)(void *));
193 
194 
205 extern void vector_del(vector_t *v);
206 
207 
222 extern void vector_clear(vector_t *v, void (*df)(void *));
223 
224 
237 extern void vector_clear_del(vector_t *v, void (*df)(void *));
238 
239 
250 extern bool vector_contains(const vector_t *v,
251  const void *key,
252  int (*pred)(const void *, const void *));
253 
254 
262 extern void *vector_find(const vector_t *v,
263  const void *key,
264  bool (*pred)(const void *, const void *));
265 
266 
272 extern void vector_sort(vector_t *v, int (*pred)(const void *, const void *));
273 
274 
275 #endif /*CUVECTOR_H_ */
void * vector_get(const vector_t *v, size_t index)
Retrieve the data at given index.
size_t memsize
Definition: vector.h:18
void vector_remove(vector_t *v, size_t index, void(*df)(void *))
Delete a element from the vector.
size_t count
Definition: vector.h:20
void vector_del(vector_t *v)
Delete the vector and free allocated memory. The elements of the vector will not be freed...
void vector_push(vector_t *v, void *data)
Appends a element to the vector.
vector dynamically growing array
Definition: vector.h:14
void vector_clear_del(vector_t *v, void(*df)(void *))
Remove all elements and free the vector_t.
size_t vector_len(const vector_t *v)
void vector_clear(vector_t *v, void(*df)(void *))
Remove all elements from the vector, in case that df is NULL every entry is set to 0...
void * vector_find(const vector_t *v, const void *key, bool(*pred)(const void *, const void *))
Find an element in given vector.
vector_t * vector_with_cap(size_t n)
Creates a empty vector of the size n.
void vector_pop(vector_t *v, void(*df)(void *))
Remove the last element from the vector.
vector_t * vector_new()
Creates a new empty vector of the size 8.
void vector_foreach(const vector_t *v, void(*f)(void *))
Apply the funtion f to every element in the vector.
void vector_insert(vector_t *v, size_t index, void *data)
Insert an element at given position, should the index be greater than the current vector size then th...
bool vector_contains(const vector_t *v, const void *key, int(*pred)(const void *, const void *))
Check whether a element is in the vector. for an int created on the stack: return memcmp(a...
void vector_sort(vector_t *v, int(*pred)(const void *, const void *))
Sort the vector with quicksort.
void ** data
Definition: vector.h:16
bool vector_is_empty(const vector_t *v)