refactor dynamic array API: rename builder functions, update memory management, and improve tests
This commit is contained in:
@@ -5,39 +5,36 @@
|
||||
#include "dynamic_array.h"
|
||||
|
||||
|
||||
Array *array_builder(const ArrayBuilderOptions *options) {
|
||||
Array *array_create(const ArrayCreateOptions *options) {
|
||||
const ArrayCreateOptions opt = options != NULL ? *options : DEFAULT_ARRAY_CREATE_OPTIONS;
|
||||
const auto array = (Array*) malloc(sizeof(Array));
|
||||
|
||||
int size = DEFAULT_ARRAY_SIZE;
|
||||
if (options != NULL && options->initial_size > 0) {
|
||||
size = options->initial_size;
|
||||
}
|
||||
array->capacity = size;
|
||||
array->capacity = opt.initial_size;
|
||||
array->value = (int*) calloc(array->capacity, sizeof(int));
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
void array_deconstructor(Array *array) {
|
||||
if (array == NULL) {
|
||||
return;
|
||||
}
|
||||
free(array->value);
|
||||
free(array);
|
||||
void array_deconstructor(Array **pp_array) {
|
||||
if (*pp_array == NULL) return;
|
||||
|
||||
free((*pp_array)->value);
|
||||
free(*pp_array);
|
||||
*pp_array = nullptr;
|
||||
}
|
||||
|
||||
void array_resize(Array *array, const int new_size) {
|
||||
int *new_ptr = (int *) realloc(array->value, new_size*sizeof(int));
|
||||
void array_resize(Array *p_array, const int new_size) {
|
||||
int *new_ptr = realloc(p_array->value, new_size*sizeof(int));
|
||||
if (new_ptr == NULL) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (new_size > array->capacity) {
|
||||
memset(new_ptr + array->capacity, 0, (new_size - array->capacity) * sizeof(int));
|
||||
if (new_size > p_array->capacity) {
|
||||
memset(new_ptr + p_array->capacity, 0, (new_size - p_array->capacity) * sizeof(int));
|
||||
}
|
||||
|
||||
array->value = new_ptr;
|
||||
array->capacity = new_size;
|
||||
p_array->value = new_ptr;
|
||||
p_array->capacity = new_size;
|
||||
}
|
||||
|
||||
int *array_get_value(const Array *array, const int index) {
|
||||
@@ -47,30 +44,10 @@ int *array_get_value(const Array *array, const int index) {
|
||||
return &array->value[index];
|
||||
}
|
||||
|
||||
void array_set_value(Array *array, const int index, const int value) {
|
||||
if (index > array->capacity) {
|
||||
const int new_size = (index - array->capacity) * 2 + array->capacity;
|
||||
array_resize(array, new_size);
|
||||
void array_set_value(Array *p_array, const int index, const int value) {
|
||||
if (index > p_array->capacity) {
|
||||
const int new_size = (index - p_array->capacity) * 2 + p_array->capacity;
|
||||
array_resize(p_array, new_size);
|
||||
}
|
||||
array->value[index] = value;
|
||||
p_array->value[index] = value;
|
||||
}
|
||||
//
|
||||
// int main() {
|
||||
// Array *a = array_builder();
|
||||
//
|
||||
// int index = 1000;
|
||||
// int expected_value = 99;
|
||||
//
|
||||
// array_set_value(a, index, expected_value);
|
||||
//
|
||||
// int *value = array_get_value(a, index);
|
||||
// if (value != NULL) {
|
||||
// printf("Found the value %d in the index %d of array", *value, index);
|
||||
// } else {
|
||||
// printf("Found invalid index for array");
|
||||
// }
|
||||
//
|
||||
// array_deconstructor(a);
|
||||
//
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user