addition/refactor: Made ADT design and added grow/shrink
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--.
This commit is contained in:
@@ -5,15 +5,10 @@
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
size_t capacity;
|
||||
size_t len;
|
||||
size_t elem_size;
|
||||
} ArrayList;
|
||||
typedef struct ArrayList ArrayList;
|
||||
|
||||
typedef struct {
|
||||
ArrayList array;
|
||||
ArrayList *array;
|
||||
size_t offset;
|
||||
} ArrayListSlice;
|
||||
|
||||
@@ -27,21 +22,13 @@ typedef enum {
|
||||
ARRLIST_INVALID_ELEM_SIZE,
|
||||
} ArrayListErr;
|
||||
|
||||
typedef struct {
|
||||
bool is_valid;
|
||||
union {
|
||||
ArrayListErr err;
|
||||
ArrayList array;
|
||||
};
|
||||
} ArrayListResult;
|
||||
|
||||
ArrayListResult arraylist_init(size_t capacity, size_t elem_size);
|
||||
ArrayList *arraylist_init(size_t capacity, size_t elem_size);
|
||||
ArrayListErr arraylist_destroy(ArrayList *arr);
|
||||
ArrayListErr arraylist_clear(ArrayList *arr);
|
||||
|
||||
size_t arraylist_size(ArrayList *arr);
|
||||
size_t arraylist_capacity(ArrayList *arr);
|
||||
bool arraylist_is_empty(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);
|
||||
@@ -52,7 +39,7 @@ 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(ArrayList *arr, size_t index, 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);
|
||||
|
||||
Reference in New Issue
Block a user