f6-dynamic-array #18

Merged
Mineplay merged 10 commits from f6-dynamic-array into main 2025-04-25 10:21:54 -05:00
8 changed files with 338 additions and 8 deletions

View file

@ -0,0 +1,56 @@
/*
* 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 <stdbool.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);
FledastyError fledasty_dynamic_array_append(FledastyDynamicArray *current_dynamic_array, void *value);
FledastyError fledasty_dynamic_array_insert_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index, void *value);
FledastyError fledasty_dynamic_array_insert_before_value(FledastyDynamicArray *current_dynamic_array, void *before_value, void *value);
FledastyError fledasty_dynamic_array_insert_after_value(FledastyDynamicArray *current_dynamic_array, void *after_value, void *value);
void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_array, const size_t index);
FledastyError fledasty_dynamic_array_remove_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index);
FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_dynamic_array, void *value);
bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value);
inline static bool fledasty_dynamic_array_is_empty(const FledastyDynamicArray *current_dynamic_array) { return current_dynamic_array->size == 0; }
#endif

View file

@ -24,6 +24,7 @@
#define FLEDASTY_QUEUE
#include <stddef.h>
#include <stdbool.h>
#include "../Utils/Error.h"
@ -42,6 +43,6 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
void *fledasty_queue_peek(const FledastyQueue *current_queue);
void *fledasty_queue_pop(FledastyQueue *current_queue);
static inline size_t fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
#endif

View file

@ -24,6 +24,7 @@
#define FLEDASTY_STACK
#include <stddef.h>
#include <stdbool.h>
#include "../Utils/Error.h"
@ -41,6 +42,6 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value);
void *fledasty_stack_peek(const FledastyStack *current_stack);
void *fledasty_stack_pop(FledastyStack *current_stack);
static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
#endif

View file

@ -27,6 +27,8 @@ typedef enum {
FLEDASTY_ERROR_NONE = 0,
FLEDASTY_ERROR_FAILED_ALLOCATION = 1,
FLEDASTY_ERROR_INVALID_POINTER = 2,
FLEDASTY_ERROR_INDEX_OUT_OF_RANGE = 3,
FLEDASTY_ERROR_VALUE_NOT_FOUND = 4,
} FledastyError;
#endif

235
Src/Core/DynamicArray.c Normal file
View file

@ -0,0 +1,235 @@
/*
* 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;
}
FledastyError fledasty_dynamic_array_append(FledastyDynamicArray *current_dynamic_array, void *value) {
if (current_dynamic_array == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (current_dynamic_array->size == current_dynamic_array->capacity) {
current_dynamic_array->capacity += current_dynamic_array->capacity;
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
}
hallocy_copy_memory(current_dynamic_array->buffer + (current_dynamic_array->size * current_dynamic_array->element_byte_size), value, current_dynamic_array->element_byte_size);
current_dynamic_array->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_dynamic_array_insert_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index, void *value) {
if (current_dynamic_array == NULL || value == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
if (index >= current_dynamic_array->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
if (current_dynamic_array->size == current_dynamic_array->capacity) {
current_dynamic_array->capacity += current_dynamic_array->capacity;
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
if (current_dynamic_array->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
unsigned char *insert_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
hallocy_move_memory(insert_pointer + current_dynamic_array->element_byte_size, insert_pointer, (current_dynamic_array->size - index) * current_dynamic_array->element_byte_size);
hallocy_copy_memory(insert_pointer, value, current_dynamic_array->element_byte_size);
current_dynamic_array->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_dynamic_array_insert_before_value(FledastyDynamicArray *current_dynamic_array, void *before_value, void *value) {
if (current_dynamic_array == NULL || before_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
size_t byte_index = 0;
size_t byte_size = current_dynamic_array->size * current_dynamic_array->element_byte_size;
while (byte_index < byte_size && !hallocy_compare_memory(current_dynamic_array->buffer + byte_index, before_value, current_dynamic_array->element_byte_size)) {
byte_index += current_dynamic_array->element_byte_size;
}
if (byte_index == byte_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_dynamic_array->size == current_dynamic_array->capacity) {
current_dynamic_array->capacity += current_dynamic_array->capacity;
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
if (current_dynamic_array->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_move_memory(current_dynamic_array->buffer + (byte_index + current_dynamic_array->element_byte_size), current_dynamic_array->buffer + byte_index, byte_size - byte_index);
hallocy_copy_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size);
current_dynamic_array->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_dynamic_array_insert_after_value(FledastyDynamicArray *current_dynamic_array, void *after_value, void *value) {
if (current_dynamic_array == NULL || after_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
size_t byte_index = 0;
size_t byte_size = current_dynamic_array->size * current_dynamic_array->element_byte_size;
while (byte_index < byte_size && !hallocy_compare_memory(current_dynamic_array->buffer + byte_index, after_value, current_dynamic_array->element_byte_size)) {
byte_index += current_dynamic_array->element_byte_size;
}
if (byte_index == byte_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_dynamic_array->size == current_dynamic_array->capacity) {
current_dynamic_array->capacity += current_dynamic_array->capacity;
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
if (current_dynamic_array->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
byte_index += current_dynamic_array->element_byte_size;
hallocy_move_memory(current_dynamic_array->buffer + (byte_index + current_dynamic_array->element_byte_size), current_dynamic_array->buffer + byte_index, byte_size - byte_index);
hallocy_copy_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size);
current_dynamic_array->size += 1;
return FLEDASTY_ERROR_NONE;
}
void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_array, const size_t index) {
if (current_dynamic_array == NULL || index >= current_dynamic_array->size) {
return NULL;
}
return current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
}
FledastyError fledasty_dynamic_array_remove_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index) {
if (current_dynamic_array == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (index >= current_dynamic_array->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
void *index_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
hallocy_copy_memory(index_pointer, index_pointer + current_dynamic_array->element_byte_size, (current_dynamic_array->size + index) * current_dynamic_array->element_byte_size);
current_dynamic_array->size -= 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_dynamic_array, void *value) {
if (current_dynamic_array == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
size_t byte_index = 0;
size_t byte_size = current_dynamic_array->size * current_dynamic_array->element_byte_size;
while (byte_index < byte_size && !hallocy_compare_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size)) {
byte_index += current_dynamic_array->element_byte_size;
}
if (byte_index == byte_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
hallocy_copy_memory(current_dynamic_array->buffer + byte_index, current_dynamic_array->buffer + (byte_index + current_dynamic_array->element_byte_size), byte_size - (byte_index + current_dynamic_array->element_byte_size));
current_dynamic_array->size -= 1;
return FLEDASTY_ERROR_NONE;
}
bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value) {
if (current_dynamic_array == NULL) {
return false;
}
for (size_t byte_index = 0; byte_index < current_dynamic_array->size * current_dynamic_array->element_byte_size; byte_index += current_dynamic_array->element_byte_size) {
if (hallocy_compare_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size)) {
return true;
}
}
return false;
}

View file

@ -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) {

View file

@ -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) {

View file

@ -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;
@ -32,11 +33,11 @@ int main() {
}
int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue);
printf("Peeked: %d\n", *peeked_queue_data);
printf("Queue peeked: %d\n", *peeked_queue_data);
for (int i = test_queue.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
printf("Popped: %d\n", *popped_data);
printf("Queue popped: %d\n", *popped_data);
}
if (fledasty_queue_is_empty(&test_queue)) {
@ -53,11 +54,11 @@ int main() {
}
int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack);
printf("Peeked: %d\n", *peeked_stack_data);
printf("Stack peeked: %d\n", *peeked_stack_data);
for (int i = test_stack.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_stack_pop(&test_stack);
printf("Popped: %d\n", *popped_data);
printf("Stack popped: %d\n", *popped_data);
}
if (fledasty_stack_is_empty(&test_stack)) {
@ -66,6 +67,40 @@ 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));
for (int i = 0; i < 10; i += 1) {
fledasty_dynamic_array_append(&test_dynamic_array, &i);
}
int insert_value = 18;
fledasty_dynamic_array_insert_at_index(&test_dynamic_array, 1, &insert_value);
insert_value = 35;
int insert_at_value = 11;
fledasty_dynamic_array_insert_before_value(&test_dynamic_array, &insert_at_value, &insert_value);
insert_value = 90;
insert_at_value = 35;
fledasty_dynamic_array_insert_after_value(&test_dynamic_array, &insert_at_value, &insert_value);
int remove_value = 15;
fledasty_dynamic_array_remove_value(&test_dynamic_array, &remove_value);
fledasty_dynamic_array_remove_at_index(&test_dynamic_array, test_dynamic_array.size - 2);
for (int i = 0; i < test_dynamic_array.size; i += 1) {
int *dynamic_array_data = (int*)fledasty_dynamic_array_get(&test_dynamic_array, i);
printf("Dynamic array get: %d\n", *dynamic_array_data);
}
if (fledasty_dynamic_array_has_value(&test_dynamic_array, &insert_value)) {
printf("Dynamic array contains %d\n", insert_value);
}
if (fledasty_dynamic_array_is_empty(&test_dynamic_array)) {
printf("Dynamic array is empty\n");
}
fledasty_dynamic_array_destroy(&test_dynamic_array);
printf("Done\n");
return 0;
}