feat(dynamic array): added dynamic array datastructure with init and destroy function
This commit is contained in:
parent
48f402708c
commit
6e83855216
5 changed files with 119 additions and 2 deletions
41
Include/Fledasty/Core/DynamicArray.h
Normal file
41
Include/Fledasty/Core/DynamicArray.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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.h
|
||||
* Description:
|
||||
* This file contains the Dynamic Array structure and the functions for modifying it.
|
||||
* 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
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#ifndef FLEDASTY_DYNAMIC_ARRAY
|
||||
#define FLEDASTY_DYNAMIC_ARRAY
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
typedef struct {
|
||||
size_t size, capacity;
|
||||
|
||||
size_t element_byte_size;
|
||||
unsigned char *buffer;
|
||||
} FledastyDynamicArray;
|
||||
|
||||
FledastyError fledasty_dynamic_array_initialize(FledastyDynamicArray *new_dynamic_array, void *values, const size_t values_size, const size_t element_byte_size);
|
||||
FledastyError fledasty_dynamic_array_destroy(FledastyDynamicArray *current_dynamic_array);
|
||||
|
||||
#endif
|
||||
71
Src/Core/DynamicArray.c
Normal file
71
Src/Core/DynamicArray.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* 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 <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values,
|
|||
}
|
||||
} else {
|
||||
new_queue->size = values_size;
|
||||
new_queue->capacity = values_size + (values_size / 2);
|
||||
new_queue->capacity = new_queue->size + new_queue->size;
|
||||
|
||||
new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size);
|
||||
if (new_queue->buffer == NULL) {
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values,
|
|||
}
|
||||
} else {
|
||||
new_stack->size = values_size;
|
||||
new_stack->capacity = values_size + (values_size / 2);
|
||||
new_stack->capacity = new_stack->size + new_stack->size;
|
||||
|
||||
new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size);
|
||||
if (new_stack->buffer == NULL) {
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
#include <stdio.h>
|
||||
#include <Fledasty/Core/Queue.h>
|
||||
#include <Fledasty/Core/Stack.h>
|
||||
#include <Fledasty/Core/DynamicArray.h>
|
||||
|
||||
int main() {
|
||||
FledastyQueue test_queue;
|
||||
|
|
@ -66,6 +67,10 @@ int main() {
|
|||
|
||||
fledasty_stack_destroy(&test_stack);
|
||||
|
||||
FledastyDynamicArray test_dynamic_array;
|
||||
fledasty_dynamic_array_initialize(&test_dynamic_array, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
||||
|
||||
fledasty_dynamic_array_destroy(&test_dynamic_array);
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue