Simplify dbl list API (#11)

Reviewed-on: #11
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
This commit was merged in pull request #11.
This commit is contained in:
2026-01-11 18:22:54 +00:00
committed by Abdelrahman Said
parent b88cb71aa8
commit cff418b9e9
13 changed files with 455 additions and 533 deletions

View File

@@ -25,15 +25,16 @@ DBL_LIST_ALLOC_RETURN:
return list;
}
GenericNode *_dbl_list_node_alloc(const Allocator *allocator, u64 item_size) {
GenericNode *_dbl_list_node_alloc(const Allocator *allocator, void *item, u64 item_size) {
wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL");
GenericNode *node = wapp_mem_allocator_alloc(allocator, sizeof(GenericNode));
if (!node) { goto DBL_LIST_NODE_ALLOC_RETURN; }
memset((void *)node, 0, sizeof(GenericNode));
node->magic = WAPP_DBL_NODE_MAGIC;
node->item_size = item_size;
node->item = item;
node->header.magic = WAPP_DBL_NODE_MAGIC;
node->header.item_size = item_size;
DBL_LIST_NODE_ALLOC_RETURN:
return node;
@@ -47,7 +48,7 @@ GenericNode *_dbl_list_get(const GenericList *list, u64 index, u64 item_size) {
GenericNode *output = NULL;
GenericNode *current = list->first;
for (u64 i = 1; i <= index; ++i) {
current = current->next;
current = current->header.next;
}
output = current;
@@ -71,11 +72,11 @@ void _dbl_list_push_front(GenericList *list, GenericNode *node, u64 item_size) {
GenericNode *first = list->first;
if (first) {
first->prev = node_list.last;
first->header.prev = node_list.last;
}
list->first = node_list.first;
node_list.last->next = first;
node_list.last->header.next = first;
}
void _dbl_list_push_back(GenericList *list, GenericNode *node, u64 item_size) {
@@ -94,11 +95,11 @@ void _dbl_list_push_back(GenericList *list, GenericNode *node, u64 item_size) {
GenericNode *last = list->last;
if (last) {
last->next = node_list.first;
last->header.next = node_list.first;
}
list->last = node_list.last;
node_list.first->prev = last;
node_list.first->header.prev = last;
}
void _dbl_list_insert(GenericList *list, GenericNode *node, u64 index, u64 item_size) {
@@ -123,72 +124,78 @@ void _dbl_list_insert(GenericList *list, GenericNode *node, u64 index, u64 item_
list->node_count += node_list.node_count;
GenericNode *prev = dst_node->prev;
GenericNode *prev = dst_node->header.prev;
dst_node->prev = node_list.last;
prev->next = node_list.first;
dst_node->header.prev = node_list.last;
prev->header.next = node_list.first;
node_list.first->prev = prev;
node_list.last->next = dst_node;
node_list.first->header.prev = prev;
node_list.last->header.next = dst_node;
}
GenericNode *_dbl_list_pop_front(GenericList *list, u64 item_size) {
void *_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);
GenericNode *output = NULL;
void *output = NULL;
if (list->node_count == 0) {
goto RETURN_LIST_POP_FRONT;
}
output = list->first;
GenericNode *node = list->first;
if (list->node_count == 1) {
*list = (GenericList){.magic = WAPP_DBL_LIST_MAGIC, .item_size = item_size};
goto RETURN_LIST_POP_FRONT;
goto LIST_POP_FRONT_RETRIEVE_NODE_ITEM;
}
--(list->node_count);
list->first = output->next;
list->first = node->header.next;
output->prev = output->next = NULL;
node->header.prev = node->header.next = NULL;
LIST_POP_FRONT_RETRIEVE_NODE_ITEM:
output = node->item;
RETURN_LIST_POP_FRONT:
return output;
}
GenericNode *_dbl_list_pop_back(GenericList *list, u64 item_size) {
void *_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);
GenericNode *output = NULL;
void *output = NULL;
if (list->node_count == 0) {
goto RETURN_LIST_POP_BACK;
}
output = list->last;
GenericNode *node = list->last;
if (list->node_count == 1) {
*list = (GenericList){.magic = WAPP_DBL_LIST_MAGIC, .item_size = item_size};
goto RETURN_LIST_POP_BACK;
goto LIST_POP_BACK_RETRIEVE_NODE_ITEM;
}
--(list->node_count);
list->last = output->prev;
list->last = node->header.prev;
output->prev = output->next = NULL;
node->header.prev = node->header.next = NULL;
LIST_POP_BACK_RETRIEVE_NODE_ITEM:
output = node->item;
RETURN_LIST_POP_BACK:
return output;
}
GenericNode *_dbl_list_remove(GenericList *list, u64 index, u64 item_size) {
void *_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);
GenericNode *output = NULL;
void *output = NULL;
if (index == 0) {
output = _dbl_list_pop_front(list, item_size);
@@ -198,17 +205,19 @@ GenericNode *_dbl_list_remove(GenericList *list, u64 index, u64 item_size) {
goto RETURN_LIST_REMOVE;
}
output = _dbl_list_get(list, index, item_size);
if (!output) {
GenericNode *node = _dbl_list_get(list, index, item_size);
if (!node) {
goto RETURN_LIST_REMOVE;
}
output->prev->next = output->next;
output->next->prev = output->prev;
node->header.prev->header.next = node->header.next;
node->header.next->header.prev = node->header.prev;
--(list->node_count);
output->prev = output->next = NULL;
node->header.prev = node->header.next = NULL;
output = node->item;
RETURN_LIST_REMOVE:
return output;
@@ -233,13 +242,13 @@ wapp_intern GenericList _node_to_list(GenericNode *node, u64 item_size) {
.item_size = item_size,
};
while (output.first->prev != NULL) {
output.first = output.first->prev;
while (output.first->header.prev != NULL) {
output.first = output.first->header.prev;
++(output.node_count);
}
while (output.last->next != NULL) {
output.last = output.last->next;
while (output.last->header.next != NULL) {
output.last = output.last->header.next;
++(output.node_count);
}
@@ -252,7 +261,7 @@ wapp_intern inline void _dbl_list_validate(const GenericList *list, u64 item_siz
}
wapp_intern inline void _dbl_list_node_validate(const GenericList *list, const GenericNode *node, u64 item_size) {
wapp_runtime_assert(node->magic == WAPP_DBL_NODE_MAGIC, "`node` isn't a valid wapp node type");
wapp_runtime_assert(list->item_size == node->item_size, "Mismatched `list` and `node` types");
wapp_runtime_assert(node->item_size == item_size, "Invalid item provided");
wapp_runtime_assert(node->header.magic == WAPP_DBL_NODE_MAGIC, "`node` isn't a valid wapp node type");
wapp_runtime_assert(list->item_size == node->header.item_size, "Mismatched `list` and `node` types");
wapp_runtime_assert(node->header.item_size == item_size, "Invalid item provided");
}