perf(doubly linked list): made small improvements in performance when searching for values in a function

This commit is contained in:
Mineplay 2025-05-22 17:54:22 -05:00
parent 18c7f70fca
commit 31e9953bf7

View file

@ -26,7 +26,7 @@
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
#include <stdio.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;
@ -223,43 +223,43 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, before_value, current_doubly_linked_list->element_byte_size)) {
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;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value) {
@ -268,43 +268,43 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, after_value, current_doubly_linked_list->element_byte_size)) {
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;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index) {
@ -329,19 +329,16 @@ FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedLi
}
}
if (current_node->previous == NULL) {
if (current_node == current_doubly_linked_list->start) {
current_doubly_linked_list->start = current_node->next;
current_doubly_linked_list->start->previous = NULL;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
}
} else {
current_node->previous->next = current_node->next;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
} else {
current_node->next->previous = current_node->previous;
}
}
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) {
@ -362,39 +359,36 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) {
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;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_node->previous == NULL) {
current_doubly_linked_list->start = current_node->next;
current_doubly_linked_list->start->previous = NULL;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
}
} else {
current_node->previous->next = current_node->next;
if (current_node->next == NULL) {
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;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list) {
@ -417,10 +411,10 @@ FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *curren
}
}
current_doubly_linked_list->size = 0;
current_doubly_linked_list->start = NULL;
current_doubly_linked_list->end = NULL;
current_doubly_linked_list->size = 0;
return FLEDASTY_ERROR_NONE;
}