perf(doubly linked list): changed doubly linked list to be defined using macros to improve performance
This commit is contained in:
parent
9b2c601a82
commit
00dac4ff9c
3 changed files with 337 additions and 482 deletions
|
|
@ -27,34 +27,328 @@
|
|||
|
||||
#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"
|
||||
|
||||
typedef struct FledastyDoublyLinkedListNode {
|
||||
void *value;
|
||||
struct FledastyDoublyLinkedListNode *previous, *next;
|
||||
} FledastyDoublyLinkedListNode;
|
||||
#define FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(type, name) \
|
||||
typedef struct FledastyDoublyLinkedListNode_##name { \
|
||||
type value; \
|
||||
struct FledastyDoublyLinkedListNode_##name *previous, *next; \
|
||||
} FledastyDoublyLinkedListNode_##name; \
|
||||
\
|
||||
typedef struct { \
|
||||
size_t size; \
|
||||
FledastyDoublyLinkedListNode_##name *start, *end; \
|
||||
} FledastyDoublyLinkedList_##name; \
|
||||
\
|
||||
FledastyError fledasty_doubly_list_##name##_free(FledastyDoublyLinkedList_##name *current_doubly_linked_list); \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_append(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value); \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_insert_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index, type value); \
|
||||
FledastyError fledasty_doubly_linked_list_##name##_insert_before_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type before_value, type value); \
|
||||
FledastyError fledasty_doubly_linked_list_##name##_insert_after_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type after_value, type value); \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_remove_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index); \
|
||||
FledastyError fledasty_doubly_linked_list_##name##_remove_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value); \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_clear(FledastyDoublyLinkedList_##name *current_doubly_linked_list); \
|
||||
\
|
||||
bool fledasty_doubly_linked_list_##name##_has_value(const FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value); \
|
||||
static inline bool fledasty_doubly_linked_list_##name##_is_empty(const FledastyDoublyLinkedList_##name *current_doubly_linked_list) { return current_doubly_linked_list == NULL || current_doubly_linked_list->size == 0; }
|
||||
|
||||
typedef struct {
|
||||
size_t size, element_byte_size;
|
||||
FledastyDoublyLinkedListNode *start, *end;
|
||||
} FledastyDoublyLinkedList;
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size);
|
||||
FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_doubly_linked_list);
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value);
|
||||
FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value);
|
||||
FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value);
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index);
|
||||
FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list);
|
||||
|
||||
bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
|
||||
static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list == NULL || current_doubly_linked_list->size == 0; }
|
||||
#define FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(type, name, compare_function) \
|
||||
FledastyError fledasty_doubly_list_##name##_free(FledastyDoublyLinkedList_##name *current_doubly_linked_list) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *previous_node = NULL; \
|
||||
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_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_doubly_linked_list_##name##_append(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
|
||||
if (new_node == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
new_node->value = value; \
|
||||
\
|
||||
new_node->previous = current_doubly_linked_list->end; \
|
||||
new_node->next = NULL; \
|
||||
\
|
||||
if (current_doubly_linked_list->start == NULL) { \
|
||||
current_doubly_linked_list->start = new_node; \
|
||||
} else { \
|
||||
current_doubly_linked_list->end->next = new_node; \
|
||||
} \
|
||||
\
|
||||
current_doubly_linked_list->end = new_node; \
|
||||
current_doubly_linked_list->size += 1; \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_insert_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index, type value) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
if (index > current_doubly_linked_list->size) { \
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
|
||||
if (new_node == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
new_node->value = value; \
|
||||
if (index == 0) { \
|
||||
new_node->previous = NULL; \
|
||||
new_node->next = current_doubly_linked_list->start; \
|
||||
\
|
||||
current_doubly_linked_list->start->previous = new_node; \
|
||||
current_doubly_linked_list->start = new_node; \
|
||||
} else if (index == current_doubly_linked_list->size) { \
|
||||
new_node->previous = current_doubly_linked_list->end; \
|
||||
new_node->next = NULL; \
|
||||
\
|
||||
current_doubly_linked_list->end->next = new_node; \
|
||||
current_doubly_linked_list->end = new_node; \
|
||||
} else { \
|
||||
FledastyDoublyLinkedListNode_##name *current_node = NULL; \
|
||||
if (index < current_doubly_linked_list->size / 2) { \
|
||||
current_node = current_doubly_linked_list->start; \
|
||||
for (size_t node = 0; node < index - 1; node += 1) { \
|
||||
current_node = current_node->next; \
|
||||
} \
|
||||
} else { \
|
||||
current_node = current_doubly_linked_list->end; \
|
||||
for (size_t node = current_doubly_linked_list->size - 1; node > index - 1; node -= 1) { \
|
||||
current_node = current_node->previous; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
new_node->previous = current_node; \
|
||||
new_node->next = current_node->next; \
|
||||
\
|
||||
current_node->next->previous = new_node; \
|
||||
current_node->next = new_node; \
|
||||
} \
|
||||
\
|
||||
current_doubly_linked_list->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_insert_before_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type before_value, type value) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
|
||||
while (current_node != NULL) { \
|
||||
if (compare_function(current_node->value, before_value)) { \
|
||||
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
|
||||
if (new_node == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
new_node->value = value; \
|
||||
if (current_node->previous == NULL) { \
|
||||
new_node->previous = NULL; \
|
||||
new_node->next = current_doubly_linked_list->start; \
|
||||
\
|
||||
current_doubly_linked_list->start->previous = new_node; \
|
||||
current_doubly_linked_list->start = new_node; \
|
||||
} else { \
|
||||
new_node->previous = current_node->previous; \
|
||||
new_node->next = current_node; \
|
||||
\
|
||||
current_node->previous->next = new_node; \
|
||||
current_node->previous = new_node; \
|
||||
} \
|
||||
\
|
||||
current_doubly_linked_list->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
current_node = current_node->next; \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_insert_after_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type after_value, type value) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
|
||||
while (current_node != NULL) { \
|
||||
if (compare_function(current_node->value, after_value)) { \
|
||||
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
|
||||
if (new_node == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
new_node->value = value; \
|
||||
if (current_node == current_doubly_linked_list->end) { \
|
||||
new_node->next = NULL; \
|
||||
new_node->previous = current_doubly_linked_list->end; \
|
||||
\
|
||||
current_doubly_linked_list->end->next = new_node; \
|
||||
current_doubly_linked_list->end = new_node; \
|
||||
} else { \
|
||||
new_node->previous = current_node; \
|
||||
new_node->next = current_node->next; \
|
||||
\
|
||||
current_node->next->previous = new_node; \
|
||||
current_node->next = new_node; \
|
||||
} \
|
||||
\
|
||||
current_doubly_linked_list->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
current_node = current_node->next; \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_remove_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
if (index >= current_doubly_linked_list->size) { \
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *current_node = NULL; \
|
||||
if (index < current_doubly_linked_list->size / 2) { \
|
||||
current_node = current_doubly_linked_list->start; \
|
||||
for (size_t node = 0; node < index; node += 1) { \
|
||||
current_node = current_node->next; \
|
||||
} \
|
||||
} else { \
|
||||
current_node = current_doubly_linked_list->end; \
|
||||
for (size_t node = current_doubly_linked_list->size - 1; node > index; node -= 1) { \
|
||||
current_node = current_node->previous; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
if (current_node == current_doubly_linked_list->start) { \
|
||||
current_doubly_linked_list->start = current_node->next; \
|
||||
} else { \
|
||||
current_node->previous->next = current_node->next; \
|
||||
} \
|
||||
\
|
||||
if (current_node == current_doubly_linked_list->end) { \
|
||||
current_doubly_linked_list->end = current_node->previous; \
|
||||
} else { \
|
||||
current_node->next->previous = current_node->previous; \
|
||||
} \
|
||||
\
|
||||
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_doubly_linked_list->size -= 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_remove_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
|
||||
while (current_node != NULL) { \
|
||||
if (compare_function(current_node->value, value)) { \
|
||||
if (current_node == current_doubly_linked_list->start) { \
|
||||
current_doubly_linked_list->start = current_node->next; \
|
||||
} else { \
|
||||
current_node->previous->next = current_node->next; \
|
||||
} \
|
||||
\
|
||||
if (current_node == current_doubly_linked_list->end) { \
|
||||
current_doubly_linked_list->end = current_node->previous; \
|
||||
} else { \
|
||||
current_node->next->previous = current_node->previous; \
|
||||
} \
|
||||
\
|
||||
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_doubly_linked_list->size -= 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
current_node = current_node->next; \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_doubly_linked_list_##name##_clear(FledastyDoublyLinkedList_##name *current_doubly_linked_list) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *previous_node = NULL; \
|
||||
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_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_doubly_linked_list->start = NULL; \
|
||||
current_doubly_linked_list->end = NULL; \
|
||||
\
|
||||
current_doubly_linked_list->size = 0; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
bool fledasty_doubly_linked_list_##name##_has_value(const FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value) { \
|
||||
if (current_doubly_linked_list == NULL) { \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
|
||||
while (current_node != NULL) { \
|
||||
if (compare_function(current_node->value, value)) { \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
current_node = current_node->next; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,436 +0,0 @@
|
|||
/*
|
||||
* 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: DoublyLinkedList.c
|
||||
* Description:
|
||||
* This file contains the functions for modifying the Doubly 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/DoublyLinkedList.h"
|
||||
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size) {
|
||||
if (new_doubly_linked_list == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
new_doubly_linked_list->element_byte_size = element_byte_size;
|
||||
if (values == NULL || values_size == 0) {
|
||||
new_doubly_linked_list->size = 0;
|
||||
new_doubly_linked_list->start = NULL;
|
||||
new_doubly_linked_list->end = NULL;
|
||||
} else {
|
||||
new_doubly_linked_list->size = values_size;
|
||||
|
||||
new_doubly_linked_list->start = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
|
||||
if (new_doubly_linked_list->start == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
new_doubly_linked_list->start->value = hallocy_malloc(new_doubly_linked_list->element_byte_size);
|
||||
if (new_doubly_linked_list->start->value == NULL) {
|
||||
hallocy_free(new_doubly_linked_list->start);
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(new_doubly_linked_list->start->value, values, new_doubly_linked_list->element_byte_size);
|
||||
|
||||
new_doubly_linked_list->start->previous = NULL;
|
||||
new_doubly_linked_list->start->next = NULL;
|
||||
|
||||
FledastyDoublyLinkedListNode *last_node = new_doubly_linked_list->start;
|
||||
for (size_t index = new_doubly_linked_list->element_byte_size; index < new_doubly_linked_list->size * new_doubly_linked_list->element_byte_size; index += new_doubly_linked_list->element_byte_size) {
|
||||
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
|
||||
if (new_node == NULL) {
|
||||
FledastyDoublyLinkedListNode *current_node = new_doubly_linked_list->start;
|
||||
while (current_node->next != NULL) {
|
||||
current_node = current_node->next;
|
||||
|
||||
hallocy_free(current_node->previous->value);
|
||||
hallocy_free(current_node->previous);
|
||||
}
|
||||
|
||||
hallocy_free(current_node->value);
|
||||
hallocy_free(current_node);
|
||||
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
new_node->value = hallocy_malloc(new_doubly_linked_list->element_byte_size);
|
||||
if (new_node->value == NULL) {
|
||||
hallocy_free(new_node);
|
||||
|
||||
FledastyDoublyLinkedListNode *current_node = new_doubly_linked_list->start;
|
||||
while (current_node->next != NULL) {
|
||||
current_node = current_node->next;
|
||||
|
||||
hallocy_free(current_node->previous->value);
|
||||
hallocy_free(current_node);
|
||||
}
|
||||
|
||||
hallocy_free(current_node->value);
|
||||
hallocy_free(current_node);
|
||||
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_doubly_linked_list->element_byte_size);
|
||||
|
||||
new_node->previous = last_node;
|
||||
new_node->next = NULL;
|
||||
|
||||
last_node->next = new_node;
|
||||
last_node = new_node;
|
||||
}
|
||||
|
||||
new_doubly_linked_list->end = last_node;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_doubly_linked_list) {
|
||||
if (current_doubly_linked_list == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *previous_node = NULL;
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
previous_node = current_node;
|
||||
current_node = current_node->next;
|
||||
|
||||
if (hallocy_free(previous_node->value) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
|
||||
if (current_doubly_linked_list == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
|
||||
if (new_node == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
|
||||
if (new_node->value == NULL) {
|
||||
hallocy_free(new_node);
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
|
||||
|
||||
new_node->previous = current_doubly_linked_list->end;
|
||||
new_node->next = NULL;
|
||||
|
||||
current_doubly_linked_list->end->next = new_node;
|
||||
current_doubly_linked_list->end = new_node;
|
||||
current_doubly_linked_list->size += 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value) {
|
||||
if (current_doubly_linked_list == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (index > current_doubly_linked_list->size) {
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
|
||||
if (new_node == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
|
||||
if (new_node->value) {
|
||||
hallocy_free(new_node);
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
|
||||
|
||||
if (index == 0) {
|
||||
new_node->previous = NULL;
|
||||
new_node->next = current_doubly_linked_list->start;
|
||||
|
||||
current_doubly_linked_list->start->previous = new_node;
|
||||
current_doubly_linked_list->start = new_node;
|
||||
} else if (index == current_doubly_linked_list->size) {
|
||||
new_node->previous = current_doubly_linked_list->end;
|
||||
new_node->next = NULL;
|
||||
|
||||
current_doubly_linked_list->end->next = new_node;
|
||||
current_doubly_linked_list->end = new_node;
|
||||
} else {
|
||||
FledastyDoublyLinkedListNode *current_node = NULL;
|
||||
if (index < current_doubly_linked_list->size / 2) {
|
||||
current_node = current_doubly_linked_list->start;
|
||||
for (size_t node = 0; node < index - 1; node += 1) {
|
||||
current_node = current_node->next;
|
||||
}
|
||||
} else {
|
||||
current_node = current_doubly_linked_list->end;
|
||||
for (size_t node = current_doubly_linked_list->size - 1; node > index - 1; node -= 1) {
|
||||
current_node = current_node->previous;
|
||||
}
|
||||
}
|
||||
|
||||
new_node->previous = current_node;
|
||||
new_node->next = current_node->next;
|
||||
|
||||
current_node->next->previous = new_node;
|
||||
current_node->next = new_node;
|
||||
}
|
||||
|
||||
current_doubly_linked_list->size += 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value) {
|
||||
if (current_doubly_linked_list == NULL || before_value == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
if (hallocy_compare_memory(current_node->value, before_value, current_doubly_linked_list->element_byte_size)) {
|
||||
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
|
||||
if (new_node == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
|
||||
if (new_node->value == NULL) {
|
||||
hallocy_free(new_node);
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
|
||||
|
||||
if (current_node->previous == NULL) {
|
||||
new_node->previous = NULL;
|
||||
new_node->next = current_doubly_linked_list->start;
|
||||
|
||||
current_doubly_linked_list->start->previous = new_node;
|
||||
current_doubly_linked_list->start = new_node;
|
||||
} else {
|
||||
new_node->previous = current_node->previous;
|
||||
new_node->next = current_node;
|
||||
|
||||
current_node->previous->next = new_node;
|
||||
current_node->previous = new_node;
|
||||
}
|
||||
|
||||
current_doubly_linked_list->size += 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
current_node = current_node->next;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value) {
|
||||
if (current_doubly_linked_list == NULL || after_value == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
if (hallocy_compare_memory(current_node->value, after_value, current_doubly_linked_list->element_byte_size)) {
|
||||
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
|
||||
if (new_node == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
|
||||
if (new_node->value == NULL) {
|
||||
hallocy_free(new_node);
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
|
||||
|
||||
if (current_node == current_doubly_linked_list->end) {
|
||||
new_node->next = NULL;
|
||||
new_node->previous = current_doubly_linked_list->end;
|
||||
|
||||
current_doubly_linked_list->end->next = new_node;
|
||||
current_doubly_linked_list->end = new_node;
|
||||
} else {
|
||||
new_node->previous = current_node;
|
||||
new_node->next = current_node->next;
|
||||
|
||||
current_node->next->previous = new_node;
|
||||
current_node->next = new_node;
|
||||
}
|
||||
|
||||
current_doubly_linked_list->size += 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
current_node = current_node->next;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index) {
|
||||
if (current_doubly_linked_list == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (index >= current_doubly_linked_list->size) {
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *current_node = NULL;
|
||||
if (index < current_doubly_linked_list->size / 2) {
|
||||
current_node = current_doubly_linked_list->start;
|
||||
for (size_t node = 0; node < index; node += 1) {
|
||||
current_node = current_node->next;
|
||||
}
|
||||
} else {
|
||||
current_node = current_doubly_linked_list->end;
|
||||
for (size_t node = current_doubly_linked_list->size - 1; node > index; node -= 1) {
|
||||
current_node = current_node->previous;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_node == current_doubly_linked_list->start) {
|
||||
current_doubly_linked_list->start = current_node->next;
|
||||
} else {
|
||||
current_node->previous->next = current_node->next;
|
||||
}
|
||||
|
||||
if (current_node == current_doubly_linked_list->end) {
|
||||
current_doubly_linked_list->end = current_node->previous;
|
||||
} else {
|
||||
current_node->next->previous = current_node->previous;
|
||||
}
|
||||
|
||||
if (hallocy_free(current_node->value) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
current_doubly_linked_list->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
|
||||
if (current_doubly_linked_list == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
if (hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) {
|
||||
if (current_node == current_doubly_linked_list->start) {
|
||||
current_doubly_linked_list->start = current_node->next;
|
||||
} else {
|
||||
current_node->previous->next = current_node->next;
|
||||
}
|
||||
|
||||
if (current_node == current_doubly_linked_list->end) {
|
||||
current_doubly_linked_list->end = current_node->previous;
|
||||
} else {
|
||||
current_node->next->previous = current_node->previous;
|
||||
}
|
||||
|
||||
if (hallocy_free(current_node->value) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
current_doubly_linked_list->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
current_node = current_node->next;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list) {
|
||||
if (current_doubly_linked_list == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *previous_node = NULL;
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
previous_node = current_node;
|
||||
current_node = current_node->next;
|
||||
|
||||
if (hallocy_free(previous_node->value) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
current_doubly_linked_list->start = NULL;
|
||||
current_doubly_linked_list->end = NULL;
|
||||
|
||||
current_doubly_linked_list->size = 0;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
|
||||
if (current_doubly_linked_list == NULL || value == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
if (hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
current_node = current_node->next;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
39
Tests/Main.c
39
Tests/Main.c
|
|
@ -48,6 +48,9 @@ FLEDASTY_HASH_TABLE_IMPLEMENT(int, int, int_int, compare_integers)
|
|||
FLEDASTY_LINKED_LIST_DEFINE(int, int)
|
||||
FLEDASTY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
|
||||
|
||||
FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int)
|
||||
FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
|
||||
|
||||
int main() {
|
||||
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
|
|
@ -149,46 +152,40 @@ int main() {
|
|||
|
||||
fledasty_linked_list_int_free(&test_linked_list);
|
||||
|
||||
FledastyDoublyLinkedList test_doubly_linked_list;
|
||||
fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
||||
FledastyDoublyLinkedList_int test_doubly_linked_list = { 0, NULL , NULL };
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i);
|
||||
fledasty_doubly_linked_list_int_append(&test_doubly_linked_list, i);
|
||||
}
|
||||
|
||||
int insert_value = 35;
|
||||
fledasty_doubly_linked_list_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, &insert_value);
|
||||
insert_value = 28;
|
||||
int insert_at_value = 35;
|
||||
fledasty_doubly_linked_list_insert_before_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
|
||||
insert_value = 90;
|
||||
fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
|
||||
fledasty_doubly_linked_list_int_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, 35);
|
||||
fledasty_doubly_linked_list_int_insert_before_value(&test_doubly_linked_list, 35, 28);
|
||||
fledasty_doubly_linked_list_int_insert_after_value(&test_doubly_linked_list, 35, 90);
|
||||
|
||||
fledasty_doubly_linked_list_remove_at_index(&test_doubly_linked_list, 2);
|
||||
int remove_value = 0;
|
||||
fledasty_doubly_linked_list_remove_value(&test_doubly_linked_list, &remove_value);
|
||||
fledasty_doubly_linked_list_int_remove_at_index(&test_doubly_linked_list, 2);
|
||||
fledasty_doubly_linked_list_int_remove_value(&test_doubly_linked_list, 0);
|
||||
|
||||
FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start;
|
||||
FledastyDoublyLinkedListNode_int *test_doubly_linked_list_node = test_doubly_linked_list.start;
|
||||
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
|
||||
printf("Doubly linked list get: %d\n", *(int*)test_doubly_linked_list_node->value);
|
||||
printf("Doubly linked list get: %d\n", test_doubly_linked_list_node->value);
|
||||
test_doubly_linked_list_node = test_doubly_linked_list_node->next;
|
||||
}
|
||||
|
||||
test_doubly_linked_list_node = test_doubly_linked_list.end;
|
||||
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
|
||||
printf("Doubly linked list get backwards: %d\n", *(int*)test_doubly_linked_list_node->value);
|
||||
printf("Doubly linked list get backwards: %d\n", test_doubly_linked_list_node->value);
|
||||
test_doubly_linked_list_node = test_doubly_linked_list_node->previous;
|
||||
}
|
||||
|
||||
if (fledasty_doubly_linked_list_has_value(&test_doubly_linked_list, &insert_value)) {
|
||||
printf("Doubly linked list contains %d\n", insert_value);
|
||||
if (fledasty_doubly_linked_list_int_has_value(&test_doubly_linked_list, 35)) {
|
||||
printf("Doubly linked list contains 35\n");
|
||||
}
|
||||
|
||||
fledasty_doubly_linked_list_clear(&test_doubly_linked_list);
|
||||
if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) {
|
||||
fledasty_doubly_linked_list_int_clear(&test_doubly_linked_list);
|
||||
if (fledasty_doubly_linked_list_int_is_empty(&test_doubly_linked_list)) {
|
||||
printf("Doubly linked list is empty\n");
|
||||
}
|
||||
|
||||
fledasty_doubly_list_destroy(&test_doubly_linked_list);
|
||||
fledasty_doubly_list_int_free(&test_doubly_linked_list);
|
||||
|
||||
FledastyHashTable_int_int test_hash_table = { .size = 0, .capacity = 0, .Table = NULL, .hash_function = (size_t(*)(const int))integer_hash_function };
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue