2017-04-04 21:44:13 +02:00
|
|
|
/* vector.h - A simple vector implementation in c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _VECTOR_H_
|
|
|
|
#define _VECTOR_H_
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
struct vector {
|
2017-12-20 20:36:18 +01:00
|
|
|
unsigned size;
|
|
|
|
unsigned cap;
|
2017-04-04 21:44:13 +02:00
|
|
|
void **data;
|
|
|
|
};
|
2017-05-04 22:16:00 +02:00
|
|
|
|
2017-04-04 21:44:13 +02:00
|
|
|
void vec_init(struct vector *v);
|
|
|
|
void vec_push_back(struct vector *v, void *p);
|
2018-07-06 01:32:16 +02:00
|
|
|
void vec_push_back_all(struct vector *v, void *p, ...);
|
2017-05-26 16:59:17 +02:00
|
|
|
void *vec_pop_back(struct vector *v);
|
2017-04-04 21:44:13 +02:00
|
|
|
void vec_sort(struct vector *v, int (*compar)(const void *, const void *));
|
|
|
|
void vec_destroy(struct vector *v);
|
2017-04-06 00:12:29 +02:00
|
|
|
void vec_deep_destroy(struct vector *v);
|
2018-07-06 01:32:16 +02:00
|
|
|
void vec_dup(struct vector *v, struct vector *vv);
|
2017-05-04 22:16:00 +02:00
|
|
|
|
2017-04-04 21:44:13 +02:00
|
|
|
#define vec_size(v) (v)->size
|
|
|
|
#define vec_cap(v) (v)->cap
|
|
|
|
#define vec_entry(v) (v)->data
|
|
|
|
/* Usage: vec_for_each(vector *v, void *e) */
|
|
|
|
#define vec_for_each(v, e) \
|
2017-04-30 19:58:52 +02:00
|
|
|
e = v ? (v)->data[0] : NULL; \
|
2017-12-20 20:36:18 +01:00
|
|
|
for (int _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_])
|
2017-04-04 21:44:13 +02:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
#define vec_for_each_r(v, e) \
|
2017-12-24 20:01:33 +01:00
|
|
|
e = (v && (v)->size > 0) ? (v)->data[(v)->size - 1] : NULL; \
|
2017-12-20 20:36:18 +01:00
|
|
|
for (int _ = ((int) (v)->size) - 1; v && _ >= 0; --_, e = (v)->data[_])
|
2017-04-06 00:12:29 +02:00
|
|
|
|
2017-09-07 13:22:30 +02:00
|
|
|
#define vec_cur(v) vec_entry(v)[_]
|
|
|
|
|
2017-08-20 15:36:32 +02:00
|
|
|
#endif
|