Add extra utilities for dbl list
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -36,7 +36,7 @@ typedef struct {
|
||||
GenericNode *last;
|
||||
} GenericList;
|
||||
|
||||
// NOTE (Abdelrahman): Typedefs for readability
|
||||
// NOTE (Abdelrahman): GenericList typedefs for readability
|
||||
typedef GenericList VoidPtrList;
|
||||
typedef GenericList C8List;
|
||||
typedef GenericList C16List;
|
||||
@@ -57,6 +57,27 @@ typedef GenericList UptrList;
|
||||
typedef GenericList IptrList;
|
||||
typedef GenericList Str8List;
|
||||
|
||||
// NOTE (Abdelrahman): GenericNode typedefs for readability
|
||||
typedef GenericNode VoidPtrNode;
|
||||
typedef GenericNode C8Node;
|
||||
typedef GenericNode C16Node;
|
||||
typedef GenericNode C32Node;
|
||||
typedef GenericNode U8Node;
|
||||
typedef GenericNode U16Node;
|
||||
typedef GenericNode U32Node;
|
||||
typedef GenericNode U64Node;
|
||||
typedef GenericNode B8Node;
|
||||
typedef GenericNode I8Node;
|
||||
typedef GenericNode I16Node;
|
||||
typedef GenericNode I32Node;
|
||||
typedef GenericNode I64Node;
|
||||
typedef GenericNode F32Node;
|
||||
typedef GenericNode F64Node;
|
||||
typedef GenericNode F128Node;
|
||||
typedef GenericNode UptrNode;
|
||||
typedef GenericNode IptrNode;
|
||||
typedef GenericNode Str8Node;
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
#define wapp_dbl_list(TYPE) \
|
||||
GenericList{WAPP_DBL_LIST_MAGIC, 0, sizeof(TYPE), nullptr, nullptr}
|
||||
@@ -82,6 +103,8 @@ typedef GenericList Str8List;
|
||||
(_dbl_list_alloc(ALLOCATOR, sizeof(TYPE)))
|
||||
#define wapp_dbl_list_get(TYPE, LIST_PTR, ITEM_INDEX) \
|
||||
((TYPE *)(_dbl_list_get(LIST_PTR, ITEM_INDEX, sizeof(TYPE))->item))
|
||||
#define wapp_dbl_list_get_node(TYPE, LIST_PTR, ITEM_INDEX) \
|
||||
(_dbl_list_get(LIST_PTR, ITEM_INDEX, sizeof(TYPE)))
|
||||
#define wapp_dbl_list_push_front(TYPE, LIST_PTR, ITEM_PTR) \
|
||||
(_dbl_list_push_front(LIST_PTR, _dbl_list_node(TYPE, ITEM_PTR), sizeof(TYPE)))
|
||||
#define wapp_dbl_list_push_back(TYPE, LIST_PTR, ITEM_PTR) \
|
||||
@@ -99,11 +122,41 @@ typedef GenericList Str8List;
|
||||
(_dbl_list_insert(LIST_PTR, _dbl_list_node_alloc(ALLOCATOR, ITEM_PTR, sizeof(TYPE)), \
|
||||
ITEM_INDEX, sizeof(TYPE)))
|
||||
#define wapp_dbl_list_pop_front(TYPE, LIST_PTR) \
|
||||
((TYPE *)_dbl_list_pop_front(LIST_PTR, sizeof(TYPE)))
|
||||
((TYPE *)( \
|
||||
(LIST_PTR == NULL || (LIST_PTR)->node_count == 0) ? \
|
||||
NULL : \
|
||||
_dbl_list_pop_front(LIST_PTR, sizeof(TYPE))->item \
|
||||
))
|
||||
#define wapp_dbl_list_pop_back(TYPE, LIST_PTR) \
|
||||
((TYPE *)_dbl_list_pop_back(LIST_PTR, sizeof(TYPE)))
|
||||
((TYPE *)( \
|
||||
(LIST_PTR == NULL || (LIST_PTR)->node_count == 0) ? \
|
||||
NULL : \
|
||||
_dbl_list_pop_back(LIST_PTR, sizeof(TYPE))->item \
|
||||
))
|
||||
#define wapp_dbl_list_remove(TYPE, LIST_PTR, ITEM_INDEX) \
|
||||
((TYPE *)_dbl_list_remove(LIST_PTR, ITEM_INDEX, sizeof(TYPE)))
|
||||
((TYPE *)( \
|
||||
(LIST_PTR == NULL || (LIST_PTR)->node_count == 0 || ITEM_INDEX >= (LIST_PTR)->node_count) ? \
|
||||
NULL : \
|
||||
_dbl_list_remove(LIST_PTR, ITEM_INDEX, sizeof(TYPE))->item \
|
||||
))
|
||||
#define wapp_dbl_list_pop_front_node(TYPE, LIST_PTR) \
|
||||
((TYPE *)( \
|
||||
(LIST_PTR == NULL || (LIST_PTR)->node_count == 0) ? \
|
||||
NULL : \
|
||||
_dbl_list_pop_front(LIST_PTR, sizeof(TYPE)) \
|
||||
))
|
||||
#define wapp_dbl_list_pop_back_node(TYPE, LIST_PTR) \
|
||||
((TYPE *)( \
|
||||
(LIST_PTR == NULL || (LIST_PTR)->node_count == 0) ? \
|
||||
NULL : \
|
||||
_dbl_list_pop_back(LIST_PTR, sizeof(TYPE)) \
|
||||
))
|
||||
#define wapp_dbl_list_remove_node(TYPE, LIST_PTR, ITEM_INDEX) \
|
||||
((TYPE *)( \
|
||||
(LIST_PTR == NULL || (LIST_PTR)->node_count == 0 || ITEM_INDEX >= (LIST_PTR)->node_count) ? \
|
||||
NULL : \
|
||||
_dbl_list_remove(LIST_PTR, ITEM_INDEX, sizeof(TYPE)) \
|
||||
))
|
||||
#define wapp_dbl_list_empty(TYPE, LIST_PTR) \
|
||||
(_dbl_list_empty(LIST_PTR, sizeof(TYPE)))
|
||||
|
||||
@@ -113,9 +166,9 @@ GenericNode *_dbl_list_get(const GenericList *list, u64 index, u64 item_size);
|
||||
void _dbl_list_push_front(GenericList *list, GenericNode *node, u64 item_size);
|
||||
void _dbl_list_push_back(GenericList *list, GenericNode *node, u64 item_size);
|
||||
void _dbl_list_insert(GenericList *list, GenericNode *node, u64 index, u64 item_size);
|
||||
void *_dbl_list_pop_front(GenericList *list, u64 item_size);
|
||||
void *_dbl_list_pop_back(GenericList *list, u64 item_size);
|
||||
void *_dbl_list_remove(GenericList *list, u64 index, u64 item_size);
|
||||
GenericNode *_dbl_list_pop_front(GenericList *list, u64 item_size);
|
||||
GenericNode *_dbl_list_pop_back(GenericList *list, u64 item_size);
|
||||
GenericNode *_dbl_list_remove(GenericList *list, u64 index, u64 item_size);
|
||||
void _dbl_list_empty(GenericList *list, u64 item_size);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
|
||||
Reference in New Issue
Block a user