diff options
author | Katharina Fey <kookie@spacekookie.de> | 2019-06-09 09:07:48 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2019-06-09 09:12:52 +0200 |
commit | d0cca976be6fb90f3724911c8a124bce56b3c5f9 (patch) | |
tree | 96c92dc695f81b39227fb5f52e4c2ac469fea06b /array.c | |
parent | 7df71001ef1a9ea271ae3c409a367d6c2dd628b7 (diff) |
Restructuring the main API and project
This commit rewrites pretty much the entire library. It is now much
smaller and more maintainable (split over multiple files). It will now
also support more features (that aren't implemented yet).
Adding two examples to show how to use the new API. Also changing the
name of the library everywhere.
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 120 |
1 files changed, 120 insertions, 0 deletions
@@ -0,0 +1,120 @@ +#include "bowl.h" +#include "array.h" +#include "utils.h" + +#include <malloc.h> + + +err_t array_malloc(struct bowl *self, size_t size) +{ + CHECK(self, INVALID_STATE) + CHECK((size > 0), INVALID_PARAMS) + + struct bowl_arr *arr = malloc(sizeof(struct bowl_arr)); + CHECK(arr, MALLOC_FAILED) + arr->size = 2; + arr->used = 0; + + struct bowl **ptr = calloc(sizeof(struct bowl *), arr->size); + CHECK(ptr, MALLOC_FAILED) + arr->ptr = ptr; + + self->_pl.array = arr; + return OK; +} + +err_t array_insert(struct bowl *self, struct bowl *new) +{ + CHECK(self, INVALID_PARAMS) + CHECK(new, INVALID_PARAMS) + + struct bowl_arr *arr = self->_pl.array; + CHECK(arr, INVALID_STATE) + + err_t e = _array_rescale((void ***) &arr->ptr, &arr->size, arr->used); + if(e) return e; + + arr->ptr[arr->used++] = new; + return OK; +} + +err_t array_insert_key(struct bowl *self, size_t idx, struct bowl *new) +{ + CHECK(self, INVALID_PARAMS) + CHECK(new, INVALID_PARAMS) + + struct bowl_arr *arr = self->_pl.array; + CHECK(arr, INVALID_STATE) + CHECK((idx < arr->used), INVALID_PARAMS) + + err_t e = _array_rescale((void ***) &arr->ptr, &arr->size, arr->used); + if(e) return e; + + for(int i = idx + 1; idx < arr->used; i++) + arr->ptr[i] = arr->ptr[i - 1]; + + arr->ptr[idx] = new; + arr->used++; + + return OK; +} + +err_t array_swap_key(struct bowl *self, size_t idx, struct bowl *new, struct bowl **old) +{ + CHECK(self, INVALID_PARAMS) + CHECK(new, INVALID_PARAMS) + + struct bowl_arr *arr = self->_pl.array; + CHECK(arr, INVALID_STATE) + + (*old) = NULL; // Explicitly set to NULL if no such key + CHECK((idx < arr->used), INVALID_PARAMS) + + (*old) = arr->ptr[idx]; + arr->ptr[idx] = new; + + return OK; +} + +err_t array_remove(struct bowl *self, struct bowl *to_remove) +{ + CHECK(self, INVALID_PARAMS) + CHECK(to_remove, INVALID_PARAMS) + + struct bowl_arr *arr = self->_pl.array; + CHECK(arr, INVALID_STATE) + + size_t idx; + err_t e = _array_search((void **) arr->ptr, arr->size, &idx, to_remove); + if(e) return e; + + e = _array_remove((void **) arr->ptr, idx, arr->size, NULL); + return e; +} + +err_t array_remove_key(struct bowl *self, size_t idx, struct bowl **out) +{ + CHECK(self, INVALID_PARAMS); + struct bowl_arr *arr = self->_pl.array; + CHECK(arr, INVALID_STATE) + CHECK((idx < arr->used), INVALID_PARAMS) + + err_t e = _array_remove((void **) arr->ptr, idx, arr->size, (void **) out); + return e; +} + +err_t array_free(struct bowl *self) +{ + CHECK(self, INVALID_PARAMS) + struct bowl_arr *arr = self->_pl.array; + CHECK(arr, INVALID_STATE) + + for(int i = 0; i < arr->used; i++) { + err_t e = bowl_free(arr->ptr[i]); + if(e) break; + } + + free(arr->ptr); + free(arr); + return OK; +}
\ No newline at end of file |