diff options
Diffstat (limited to 'dynamic_array.c')
-rw-r--r-- | dynamic_array.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/dynamic_array.c b/dynamic_array.c new file mode 100644 index 0000000..5a24cb8 --- /dev/null +++ b/dynamic_array.c @@ -0,0 +1,43 @@ +#include "dynamic_array.h" +#include <stdlib.h> + +void dynarr_create_array(struct dynamic_array *array) +{ + array->arr = calloc(initial_size, 1); + array->last_element_index = -1; + array->allocation_size = initial_size; +} + +static void dynarr_allocate_memory(struct dynamic_array *array) +{ + char *new_arr = calloc(array->allocation_size * 2, 1); + dynarr_copy_array(array, new_arr); + free(array->arr); + array->arr = new_arr; + array->allocation_size *= 2; +} + +void dynarr_push_back(struct dynamic_array *array, int letter) +{ + if(array->last_element_index+1 == array->allocation_size) + dynarr_allocate_memory(array); + ++array->last_element_index; + array->arr[array->last_element_index] = letter; +} +void dynarr_copy_array(struct dynamic_array *array, char *new_arr) +{ + int i; + for(i = 0; i <= array->last_element_index; ++i) + new_arr[i] = array->arr[i]; +} + +void dynarr_drop_word(struct dynamic_array *array) +{ + array->last_element_index = -1; +} + +void dynarr_clear(struct dynamic_array *array) +{ + free(array->arr); + array->arr = NULL; +} |