perf(linked list): made same improvements to searching for value as in doubly linked list

This commit is contained in:
Mineplay 2025-05-22 17:58:01 -05:00
parent 31e9953bf7
commit b43b5c550d

View file

@ -199,15 +199,8 @@ FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *curre
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;
}
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, before_value, current_linked_list->element_byte_size)) {
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
@ -230,6 +223,13 @@ FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *curre
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_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value) {
@ -238,14 +238,8 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren
}
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;
}
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, after_value, current_linked_list->element_byte_size)) {
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
@ -270,6 +264,12 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren
current_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
current_node = current_node->next;
}
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index) {
@ -317,15 +317,8 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
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;
}
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) {
if (current_node == current_linked_list->end) {
current_linked_list->end = previous_node;
}
@ -346,6 +339,13 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
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_clear(FledastyLinkedList *current_linked_list) {
@ -368,10 +368,10 @@ FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list
}
}
current_linked_list->size = 0;
current_linked_list->start = NULL;
current_linked_list->end = NULL;
current_linked_list->size = 0;
return FLEDASTY_ERROR_NONE;
}