/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ----------------------------------------------------------------------------- * File: DynamicArray.c * Description: * This file contains the functions for modifying the Dynamic Array. It includes * functions to append, Insert before, Insert after, Insert at index, * Get, Remove element, Remove at index, Check if has element and Check if is empty. * * Author: Mineplay * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/DynamicArray.h" #include #include #include FledastyError fledasty_dynamic_array_initialize(FledastyDynamicArray *new_dynamic_array, void *values, const size_t values_size, const size_t element_byte_size) { if (new_dynamic_array == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } new_dynamic_array->element_byte_size = element_byte_size; if (values == NULL || values_size == 0) { new_dynamic_array->size = 0; new_dynamic_array->capacity = 10; new_dynamic_array->buffer = (unsigned char*)hallocy_malloc(new_dynamic_array->capacity * element_byte_size); if (new_dynamic_array->buffer == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } else { new_dynamic_array->size = values_size; new_dynamic_array->capacity = new_dynamic_array->size + new_dynamic_array->size; new_dynamic_array->buffer = (unsigned char*)hallocy_malloc(new_dynamic_array->capacity * element_byte_size); if (new_dynamic_array->buffer == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } hallocy_copy_memory(new_dynamic_array->buffer, values, values_size * element_byte_size); } return FLEDASTY_ERROR_NONE; } FledastyError fledasty_dynamic_array_destroy(FledastyDynamicArray *current_dynamic_array) { if (current_dynamic_array == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } HallocyError result = hallocy_free(current_dynamic_array->buffer); if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } current_dynamic_array->buffer = NULL; return FLEDASTY_ERROR_NONE; }