So, added private functions made to grow and shrink the array in a multiple of 2, 2 as a factro is fine, at least that is Rust does it that way. Also changed the design to ADT, forgot i liked that, probably will be changing that to apply also to my Arena implementation, i will see if fits --probably--.
49 lines
1.4 KiB
C
49 lines
1.4 KiB
C
#ifndef ARRAYLIST_H
|
|
#define ARRAYLIST_H
|
|
|
|
#include <inttypes.h>
|
|
#include <stddef.h>
|
|
#include <stdbool.h>
|
|
|
|
typedef struct ArrayList ArrayList;
|
|
|
|
typedef struct {
|
|
ArrayList *array;
|
|
size_t offset;
|
|
} ArrayListSlice;
|
|
|
|
typedef enum {
|
|
ARRLIST_OK = 0,
|
|
ARRLIST_OUT_OF_BOUNDS,
|
|
ARRLIST_BAD_ALLOC,
|
|
ARRLIST_EMPTY,
|
|
ARRLIST_NULL_ARG,
|
|
ARRLIST_INVALID_CAPACITY,
|
|
ARRLIST_INVALID_ELEM_SIZE,
|
|
} ArrayListErr;
|
|
|
|
ArrayList *arraylist_init(size_t capacity, size_t elem_size);
|
|
ArrayListErr arraylist_destroy(ArrayList *arr);
|
|
ArrayListErr arraylist_clear(ArrayList *arr);
|
|
|
|
size_t arraylist_size(const ArrayList *arr);
|
|
size_t arraylist_capacity(const ArrayList *arr);
|
|
bool arraylist_is_empty(const ArrayList *arr);
|
|
|
|
ArrayListErr arraylist_push_back(ArrayList *arr, void *data);
|
|
ArrayListErr arraylist_insert(ArrayList*arr, size_t index, void *data);
|
|
ArrayListErr arraylist_push_front(ArrayList* arr, void *data);
|
|
|
|
// Here out can be null for not having anything writen
|
|
ArrayListErr arraylist_pop_back(ArrayList *arr, void *out);
|
|
ArrayListErr arraylist_remove_at(ArrayList *arr, size_t index, void *out);
|
|
ArrayListErr arraylist_pop_front(ArrayList *arr, void *out);
|
|
|
|
ArrayListErr arraylist_get(const ArrayList *arr, size_t index, void *out);
|
|
ArrayListErr arraylist_set(ArrayList *arr, size_t index, void *data);
|
|
|
|
ArrayListErr arraylist_resize(ArrayList *arr, size_t new_capacity);
|
|
ArrayListErr arraylist_reserve(ArrayList *arr, size_t size_to_reserve);
|
|
|
|
#endif
|