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,37 +199,37 @@ 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)) {
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;
}
new_node->value = hallocy_malloc(current_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_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;
}
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));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(current_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_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;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value) {
@ -238,38 +238,38 @@ 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)) {
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;
}
new_node->value = hallocy_malloc(current_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_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;
}
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(current_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_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;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index) {
@ -317,35 +317,35 @@ 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)) {
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;
}
if (previous_node == NULL) {
current_linked_list->start = current_node->next;
} else {
previous_node->next = current_node->next;
}
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_linked_list->size -= 1;
return FLEDASTY_ERROR_NONE;
}
previous_node = current_node;
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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->value) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
current_linked_list->size -= 1;
return FLEDASTY_ERROR_NONE;
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;
}