diff --git a/.gitignore b/.gitignore index 2d82e85..7ab93c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -Build \ No newline at end of file +Build +compile_commands.json +.cache \ No newline at end of file diff --git a/Include/Fledasty/Core/DynamicArray.h b/Include/Fledasty/Core/DynamicArray.h index b6f087e..ab03b99 100644 --- a/Include/Fledasty/Core/DynamicArray.h +++ b/Include/Fledasty/Core/DynamicArray.h @@ -21,13 +21,13 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ - #ifndef FLEDASTY_DYNAMIC_ARRAY - #define FLEDASTY_DYNAMIC_ARRAY +#ifndef FLEDASTY_DYNAMIC_ARRAY +#define FLEDASTY_DYNAMIC_ARRAY - #include +#include #include - #include "../Utils/Error.h" +#include "../Utils/Error.h" typedef struct { size_t size, capacity; @@ -53,4 +53,4 @@ FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_ 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 \ No newline at end of file +#endif diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h new file mode 100644 index 0000000..82c2fc6 --- /dev/null +++ b/Include/Fledasty/Core/LinkedList.h @@ -0,0 +1,57 @@ +/* + * 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: LinkedList.c + * Description: + * This file contains the functions for modifying the Linked List. 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_LINKED_LIST +#define FLEDASTY_LINKED_LIST + +#include +#include + +#include "../Utils/Error.h" + +typedef struct FledastyLinkedListNode{ + void *value; + struct FledastyLinkedListNode *next; +} FledastyLinkedListNode; + +typedef struct { + size_t size, element_byte_size; + FledastyLinkedListNode *start, *end; +} FledastyLinkedList; + +FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size); +FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_list); + +FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_list, void *value); + +FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_linked_list, const size_t index, void *value); +FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value); +FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value); + +FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index); +FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_linked_list, void *value); + +bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value); +static inline bool fledasty_linked_list_is_empty(const FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; } + +#endif diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index d4919a6..2b88630 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -45,4 +45,4 @@ void *fledasty_queue_pop(FledastyQueue *current_queue); static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; } -#endif \ No newline at end of file +#endif diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index da54a65..ca5349d 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -44,4 +44,4 @@ void *fledasty_stack_pop(FledastyStack *current_stack); static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; } -#endif \ No newline at end of file +#endif diff --git a/Src/Core/DynamicArray.c b/Src/Core/DynamicArray.c index 79dc033..16c6197 100644 --- a/Src/Core/DynamicArray.c +++ b/Src/Core/DynamicArray.c @@ -192,7 +192,7 @@ FledastyError fledasty_dynamic_array_remove_at_index(FledastyDynamicArray *curre return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; } - void *index_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size); + unsigned char *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; @@ -232,4 +232,4 @@ bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynami } return false; -} \ No newline at end of file +} diff --git a/Src/Core/LinkedList.c b/Src/Core/LinkedList.c new file mode 100644 index 0000000..5b415e1 --- /dev/null +++ b/Src/Core/LinkedList.c @@ -0,0 +1,290 @@ +/* + * 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: LinkedList.c + * Description: + * This file contains the functions for modifying the Linked List. 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/LinkedList.h" + +#include +#include +#include + +FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size) { + if (new_linked_list == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + new_linked_list->element_byte_size = element_byte_size; + if (values == NULL || values_size == 0) { + new_linked_list->size = 0; + new_linked_list->start = NULL; + new_linked_list->end = NULL; + } else { + new_linked_list->size = values_size; + + new_linked_list->start = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + new_linked_list->start->value = hallocy_malloc(new_linked_list->element_byte_size); + hallocy_copy_memory(new_linked_list->start->value, values, new_linked_list->element_byte_size); + new_linked_list->start->next = NULL; + + FledastyLinkedListNode *last_node = new_linked_list->start; + for (size_t index = new_linked_list->element_byte_size; index < new_linked_list->size * new_linked_list->element_byte_size; index += new_linked_list->element_byte_size) { + FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + new_node->value = hallocy_malloc(new_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_linked_list->element_byte_size); + + new_node->next = NULL; + last_node->next = new_node; + last_node = new_node; + } + + new_linked_list->end = last_node; + } + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_list) { + if (current_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_linked_list->start; + while (current_node != NULL) { + previous_node = current_node; + current_node = current_node->next; + + HallocyError result = hallocy_free(previous_node->value); + if (result == HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + result = hallocy_free(previous_node); + if (result == HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_list, void *value) { + if (current_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + new_node->next = NULL; + + current_linked_list->end->next = new_node; + current_linked_list->end = new_node; + current_linked_list->size += 1; + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_linked_list, const size_t index, void *value) { + if (current_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (index > current_linked_list->size) { + return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + } + + FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + + if (index == 0) { + new_node->next = current_linked_list->start; + current_linked_list->start = new_node; + } else if (index == current_linked_list->size) { + current_linked_list->end->next = new_node; + current_linked_list->end = new_node; + } else { + FledastyLinkedListNode *current_node = current_linked_list->start; + for (size_t node = 0; node < index - 1; node += 1) { + current_node = current_node->next; + } + + new_node->next = current_node->next; + current_node->next = new_node; + } + + current_linked_list->size += 1; + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value) { + if (current_linked_list == NULL || before_value == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_linked_list->start; + while (current_node != NULL && !hallocy_compare_memory(current_node->value, before_value, current_linked_list->element_byte_size)) { + previous_node = current_node; + current_node = current_node->next; + } + + if (current_node == NULL) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + + if (previous_node == NULL) { + new_node->next = current_linked_list->start; + current_linked_list->start = new_node; + } else { + new_node->next = current_node; + previous_node->next = new_node; + } + + current_linked_list->size += 1; + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value) { + if (current_linked_list == NULL || after_value == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *current_node = current_linked_list->start; + while (current_node != NULL && !hallocy_compare_memory(current_node->value, after_value, current_linked_list->element_byte_size)) { + current_node = current_node->next; + } + + if (current_node == NULL) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + + if (current_node == current_linked_list->end) { + new_node->next = NULL; + + current_linked_list->end->next = new_node; + current_linked_list->end = new_node; + } else { + new_node->next = current_node->next; + current_node->next = new_node; + } + + current_linked_list->size += 1; + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index) { + if (current_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (index >= current_linked_list->size) { + return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + } + + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_linked_list->start; + for (size_t node = 0; node < index; node += 1) { + previous_node = current_node; + current_node = current_node->next; + } + + if (current_node->next == NULL) { + current_linked_list->end = previous_node; + } + + if (previous_node == NULL) { + current_linked_list->start = current_node->next; + } else { + previous_node->next = current_node->next; + } + + hallocy_free(current_node->value); + hallocy_free(current_node); + + current_linked_list->size -= 1; + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_linked_list, void *value) { + if (current_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_linked_list->start; + while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) { + previous_node = current_node; + current_node = current_node->next; + } + + if (current_node == NULL) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + if (current_node->next == NULL) { + current_linked_list->end = previous_node; + } + + if (previous_node == NULL) { + current_linked_list->start = current_node->next; + } else { + previous_node->next = current_node->next; + } + + hallocy_free(current_node->next); + hallocy_free(current_node); + + + current_linked_list->size -= 1; + return FLEDASTY_ERROR_NONE; +} + +bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value) { + if (current_linked_list == NULL || value == NULL) { + return false; + } + + FledastyLinkedListNode *current_node = current_linked_list->start; + while (current_node != NULL) { + if (hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) { + return true; + } + + current_node = current_node->next; + } + + return false; +} diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index 9b1be7c..4206615 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -124,4 +124,4 @@ void *fledasty_queue_pop(FledastyQueue *current_queue) { } return value_address; -} \ No newline at end of file +} diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index 8988e58..fd13ec9 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -106,4 +106,4 @@ void *fledasty_stack_pop(FledastyStack *current_stack) { current_stack->size -= 1; return value_address; -} \ No newline at end of file +} diff --git a/Tests/Main.c b/Tests/Main.c index c9bb3fe..4a7805e 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -23,6 +23,7 @@ #include #include #include +#include int main() { FledastyQueue test_queue; @@ -101,6 +102,41 @@ int main() { } fledasty_dynamic_array_destroy(&test_dynamic_array); + + FledastyLinkedList test_linked_list; + fledasty_linked_list_initialize(&test_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); + + for (int i = 0; i < 10; i += 1) { + fledasty_linked_list_append(&test_linked_list, &i); + } + + insert_value = 35; + fledasty_linked_list_insert_at_index(&test_linked_list, 1, &insert_value); + insert_value = 28; + insert_at_value = 35; + fledasty_linked_list_insert_before_value(&test_linked_list, &insert_at_value, &insert_value); + insert_value = 90; + fledasty_linked_list_insert_after_value(&test_linked_list, &insert_at_value, &insert_value); + + fledasty_linked_list_remove_at_index(&test_linked_list, 2); + remove_value = 0; + fledasty_linked_list_remove_value(&test_linked_list, &remove_value); + + FledastyLinkedListNode *test_linked_list_node = test_linked_list.start; + for (int i = 0; i < test_linked_list.size; i += 1) { + printf("Linked list get: %d\n", *(int*)test_linked_list_node->value); + test_linked_list_node = test_linked_list_node->next; + } + + if (fledasty_linked_list_has_value(&test_linked_list, &insert_value)) { + printf("Linked list contains %d\n", insert_value); + } + + if (fledasty_linked_list_is_empty(&test_linked_list)) { + printf("Linked list is empty\n"); + } + + fledasty_linked_list_destroy(&test_linked_list); printf("Done\n"); return 0; } \ No newline at end of file