diff options
author | scratko <m@scratko.xyz> | 2024-04-23 19:03:35 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-05-25 21:12:30 +0300 |
commit | c1e5cffb43977f5a2f8d9623e40c01dab6d80c46 (patch) | |
tree | 31c9c9b6847c292b13a354d962b86b6f5e15bb26 /dynamic_array.c | |
download | shell-c1e5cffb43977f5a2f8d9623e40c01dab6d80c46.tar.gz shell-c1e5cffb43977f5a2f8d9623e40c01dab6d80c46.tar.bz2 shell-c1e5cffb43977f5a2f8d9623e40c01dab6d80c46.zip |
Shell-I releaseshell-I
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; +} |