319 lines
43 KiB
C
319 lines
43 KiB
C
/*
|
|
* 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.h
|
|
* Description:
|
|
* This file contains the Linked List 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_LINKED_LIST
|
|
#define FLEDASTY_LINKED_LIST
|
|
|
|
#include <stddef.h>
|
|
#include <stdbool.h>
|
|
#include <Hallocy/Core/Allocator.h>
|
|
#include <Hallocy/Core/Memory.h>
|
|
#include <Hallocy/Utils/Error.h>
|
|
|
|
#include "../Utils/Error.h"
|
|
|
|
#define FLEDASTY_LINKED_LIST_DEFINE(type, name) \
|
|
typedef struct FledastyLinkedListNode_##name { \
|
|
type value; \
|
|
struct FledastyLinkedListNode_##name *next; \
|
|
} FledastyLinkedListNode_##name; \
|
|
\
|
|
typedef struct { \
|
|
size_t size; \
|
|
FledastyLinkedListNode_##name *start, *end; \
|
|
} FledastyLinkedList_##name; \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_free(FledastyLinkedList_##name *current_linked_list); \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_append(FledastyLinkedList_##name *current_linked_list, type value); \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_insert_at_index(FledastyLinkedList_##name *current_linked_list, const size_t index, type value); \
|
|
FledastyError fledasty_linked_list_##name##_insert_before_value(FledastyLinkedList_##name *current_linked_list, type before_value, type value); \
|
|
FledastyError fledasty_linked_list_##name##_insert_after_value(FledastyLinkedList_##name *current_linked_list, type after_value, type value); \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_remove_at_index(FledastyLinkedList_##name *current_linked_list, const size_t index); \
|
|
FledastyError fledasty_linked_list_##name##_remove_value(FledastyLinkedList_##name *current_linked_list, type value); \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_clear(FledastyLinkedList_##name *current_linked_list); \
|
|
\
|
|
bool fledasty_linked_list_##name##_has_value(const FledastyLinkedList_##name *current_linked_list, type value); \
|
|
static inline bool fledasty_linked_list_##name##_is_empty(const FledastyLinkedList_##name *current_linked_list) { return current_linked_list == NULL || current_linked_list->size == 0; }
|
|
|
|
#define FLEDASTY_LINKED_LIST_IMPLEMENT(type, name, compare_function) \
|
|
FledastyError fledasty_linked_list_##name##_free(FledastyLinkedList_##name *current_linked_list) { \
|
|
if (current_linked_list == NULL) { \
|
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *previous_node = NULL; \
|
|
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
|
|
while (current_node != NULL) { \
|
|
previous_node = current_node; \
|
|
current_node = current_node->next; \
|
|
\
|
|
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
} \
|
|
\
|
|
return FLEDASTY_ERROR_NONE; \
|
|
} \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_append(FledastyLinkedList_##name *current_linked_list, type value) { \
|
|
if (current_linked_list == NULL) { \
|
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
|
|
if (new_node == NULL) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
\
|
|
new_node->value = value; \
|
|
new_node->next = NULL; \
|
|
\
|
|
if (current_linked_list->start == NULL) { \
|
|
current_linked_list->start = new_node; \
|
|
current_linked_list->end = new_node; \
|
|
} else { \
|
|
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_##name##_insert_at_index(FledastyLinkedList_##name *current_linked_list, const size_t index, type value) { \
|
|
if (current_linked_list == NULL) { \
|
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
|
} \
|
|
\
|
|
if (index > current_linked_list->size) { \
|
|
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
|
|
if (new_node == NULL) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
\
|
|
new_node->value = value; \
|
|
if (index == 0) { \
|
|
new_node->next = current_linked_list->start; \
|
|
current_linked_list->start = new_node; \
|
|
} else if (index == current_linked_list->size) { \
|
|
new_node->next = NULL; \
|
|
current_linked_list->end->next = new_node; \
|
|
current_linked_list->end = new_node; \
|
|
} else { \
|
|
FledastyLinkedListNode_##name *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_##name##_insert_before_value(FledastyLinkedList_##name *current_linked_list, type before_value, type value) { \
|
|
if (current_linked_list == NULL) { \
|
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *previous_node = NULL; \
|
|
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
|
|
while (current_node != NULL) { \
|
|
if (compare_function(current_node->value, before_value)) { \
|
|
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
|
|
if (new_node == NULL) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
\
|
|
new_node->value = value; \
|
|
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; \
|
|
} \
|
|
\
|
|
previous_node = current_node; \
|
|
current_node = current_node->next; \
|
|
} \
|
|
\
|
|
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
|
} \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_insert_after_value(FledastyLinkedList_##name *current_linked_list, type after_value, type value) { \
|
|
if (current_linked_list == NULL) { \
|
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
|
|
while (current_node != NULL) { \
|
|
if (compare_function(current_node->value, after_value)) { \
|
|
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
|
|
if (new_node == NULL) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
\
|
|
new_node->value = value; \
|
|
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; \
|
|
} \
|
|
\
|
|
current_node = current_node->next; \
|
|
} \
|
|
\
|
|
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
|
} \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_remove_at_index(FledastyLinkedList_##name *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_##name *previous_node = NULL; \
|
|
FledastyLinkedListNode_##name *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 == current_linked_list->end) { \
|
|
current_linked_list->end = previous_node; \
|
|
} \
|
|
\
|
|
if (previous_node == NULL) { \
|
|
current_linked_list->start = current_node->next; \
|
|
} else { \
|
|
previous_node->next = current_node->next; \
|
|
} \
|
|
\
|
|
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
\
|
|
current_linked_list->size -= 1; \
|
|
return FLEDASTY_ERROR_NONE; \
|
|
} \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_remove_value(FledastyLinkedList_##name *current_linked_list, type value) { \
|
|
if (current_linked_list == NULL) { \
|
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *previous_node = NULL; \
|
|
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
|
|
while (current_node != NULL) { \
|
|
if (compare_function(current_node->value, value)) { \
|
|
if (current_node == current_linked_list->end) { \
|
|
current_linked_list->end = previous_node; \
|
|
} \
|
|
\
|
|
if (previous_node == NULL) { \
|
|
current_linked_list->start = current_node->next; \
|
|
} else { \
|
|
previous_node->next = current_node->next; \
|
|
} \
|
|
\
|
|
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
\
|
|
current_linked_list->size -= 1; \
|
|
return FLEDASTY_ERROR_NONE; \
|
|
} \
|
|
\
|
|
previous_node = current_node; \
|
|
current_node = current_node->next; \
|
|
} \
|
|
\
|
|
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
|
} \
|
|
\
|
|
FledastyError fledasty_linked_list_##name##_clear(FledastyLinkedList_##name *current_linked_list) { \
|
|
if (current_linked_list == NULL) { \
|
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *previous_node = NULL; \
|
|
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
|
|
while (current_node != NULL) { \
|
|
previous_node = current_node; \
|
|
current_node = current_node->next; \
|
|
\
|
|
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { \
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
|
} \
|
|
} \
|
|
\
|
|
current_linked_list->start = NULL; \
|
|
current_linked_list->end = NULL; \
|
|
\
|
|
current_linked_list->size = 0; \
|
|
return FLEDASTY_ERROR_NONE; \
|
|
} \
|
|
\
|
|
bool fledasty_linked_list_##name##_has_value(const FledastyLinkedList_##name *current_linked_list, type value) { \
|
|
if (current_linked_list == NULL) { \
|
|
return false; \
|
|
} \
|
|
\
|
|
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
|
|
while (current_node != NULL) { \
|
|
if (compare_function(current_node->value, value)) { \
|
|
return true; \
|
|
} \
|
|
\
|
|
current_node = current_node->next; \
|
|
} \
|
|
\
|
|
return false; \
|
|
}
|
|
|
|
#endif
|