Add extra utilities for dbl list

This commit is contained in:
2026-01-11 21:26:17 +00:00
parent a4492cf8e8
commit 5a504c6791
2 changed files with 79 additions and 34 deletions

View File

@@ -133,69 +133,63 @@ void _dbl_list_insert(GenericList *list, GenericNode *node, u64 index, u64 item_
node_list.last->header.next = dst_node;
}
void *_dbl_list_pop_front(GenericList *list, u64 item_size) {
GenericNode *_dbl_list_pop_front(GenericList *list, u64 item_size) {
wapp_debug_assert(list != NULL, "`list` should not be NULL");
_dbl_list_validate(list, item_size);
void *output = NULL;
GenericNode *output = NULL;
if (list->node_count == 0) {
goto RETURN_LIST_POP_FRONT;
}
GenericNode *node = list->first;
output = list->first;
if (list->node_count == 1) {
*list = (GenericList){.magic = WAPP_DBL_LIST_MAGIC, .item_size = item_size};
goto LIST_POP_FRONT_RETRIEVE_NODE_ITEM;
goto RETURN_LIST_POP_FRONT;
}
--(list->node_count);
list->first = node->header.next;
list->first = output->header.next;
node->header.prev = node->header.next = NULL;
LIST_POP_FRONT_RETRIEVE_NODE_ITEM:
output = node->item;
output->header.prev = output->header.next = NULL;
RETURN_LIST_POP_FRONT:
return output;
}
void *_dbl_list_pop_back(GenericList *list, u64 item_size) {
GenericNode *_dbl_list_pop_back(GenericList *list, u64 item_size) {
wapp_debug_assert(list != NULL, "`list` should not be NULL");
_dbl_list_validate(list, item_size);
void *output = NULL;
GenericNode *output = NULL;
if (list->node_count == 0) {
goto RETURN_LIST_POP_BACK;
}
GenericNode *node = list->last;
output = list->last;
if (list->node_count == 1) {
*list = (GenericList){.magic = WAPP_DBL_LIST_MAGIC, .item_size = item_size};
goto LIST_POP_BACK_RETRIEVE_NODE_ITEM;
goto RETURN_LIST_POP_BACK;
}
--(list->node_count);
list->last = node->header.prev;
list->last = output->header.prev;
node->header.prev = node->header.next = NULL;
LIST_POP_BACK_RETRIEVE_NODE_ITEM:
output = node->item;
output->header.prev = output->header.next = NULL;
RETURN_LIST_POP_BACK:
return output;
}
void *_dbl_list_remove(GenericList *list, u64 index, u64 item_size) {
GenericNode *_dbl_list_remove(GenericList *list, u64 index, u64 item_size) {
wapp_debug_assert(list != NULL, "`list` should not be NULL");
_dbl_list_validate(list, item_size);
void *output = NULL;
GenericNode *output = NULL;
if (index == 0) {
output = _dbl_list_pop_front(list, item_size);
@@ -205,19 +199,17 @@ void *_dbl_list_remove(GenericList *list, u64 index, u64 item_size) {
goto RETURN_LIST_REMOVE;
}
GenericNode *node = _dbl_list_get(list, index, item_size);
if (!node) {
output = _dbl_list_get(list, index, item_size);
if (!output) {
goto RETURN_LIST_REMOVE;
}
node->header.prev->header.next = node->header.next;
node->header.next->header.prev = node->header.prev;
output->header.prev->header.next = output->header.next;
output->header.next->header.prev = output->header.prev;
--(list->node_count);
node->header.prev = node->header.next = NULL;
output = node->item;
output->header.prev = output->header.next = NULL;
RETURN_LIST_REMOVE:
return output;