2022-08-30 17:54:56 -05:00
|
|
|
// vi:ft=c
|
2022-08-29 17:27:03 -05:00
|
|
|
#ifndef VEC_H
|
|
|
|
#define VEC_H
|
2022-08-30 12:08:36 -05:00
|
|
|
#include <unistd.h>
|
2022-08-29 17:27:03 -05:00
|
|
|
|
|
|
|
#define vec_of(type) vec_new(sizeof(type))
|
|
|
|
|
|
|
|
typedef struct {
|
2022-08-30 12:08:36 -05:00
|
|
|
void *data;
|
2022-08-29 17:27:03 -05:00
|
|
|
size_t cap;
|
|
|
|
size_t len;
|
|
|
|
size_t stride;
|
|
|
|
} Vec;
|
|
|
|
|
|
|
|
// Create a new vector
|
|
|
|
Vec vec_new(size_t data_size);
|
|
|
|
// Create a new vector with an initial capacity
|
|
|
|
Vec vec_cap(size_t data_size, size_t initial_capacity);
|
|
|
|
// Push an element into the vector
|
2022-08-30 12:08:36 -05:00
|
|
|
void vec_push(Vec *v, void *data);
|
2022-08-29 17:27:03 -05:00
|
|
|
// Pop an element into the vector, and put it in data if it is not null
|
2022-08-30 12:08:36 -05:00
|
|
|
void vec_pop(Vec *v, void *data);
|
2022-08-29 17:27:03 -05:00
|
|
|
// Get a pointer to the element at an index, returns NULL if there is no such element
|
2022-08-30 12:08:36 -05:00
|
|
|
void *vec_get(Vec *v, size_t index);
|
2022-08-29 17:27:03 -05:00
|
|
|
// Insert an element at any index in the vector (except past the end)
|
2022-08-30 12:08:36 -05:00
|
|
|
void vec_insert(Vec *v, void *data, size_t index);
|
2022-08-29 17:27:03 -05:00
|
|
|
// Remove an element from the vector, and put it in data if it is not NULL
|
2022-08-30 12:08:36 -05:00
|
|
|
void vec_remove(Vec *v, size_t index, void *data);
|
2022-08-29 17:27:03 -05:00
|
|
|
// Clear the vector
|
2022-08-30 12:08:36 -05:00
|
|
|
void vec_clear(Vec *v);
|
2022-08-29 17:27:03 -05:00
|
|
|
// Extend the vector with the content of data
|
2022-08-30 12:08:36 -05:00
|
|
|
void vec_extend(Vec *v, void *data, size_t len);
|
2022-08-29 17:27:03 -05:00
|
|
|
// Free the vector
|
|
|
|
void vec_free(Vec v);
|
|
|
|
|
|
|
|
#endif
|