Add code generation for doubly linked list
This commit is contained in:
8
codegen/dbl_list/snippets/list_empty
Normal file
8
codegen/dbl_list/snippets/list_empty
Normal file
@@ -0,0 +1,8 @@
|
||||
if (!list) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
u64 count = list->node_count;
|
||||
for (u64 i = 0; i < count; ++i) {{
|
||||
wapp_str8_list_pop_back(list);
|
||||
}}
|
13
codegen/dbl_list/snippets/list_get
Normal file
13
codegen/dbl_list/snippets/list_get
Normal file
@@ -0,0 +1,13 @@
|
||||
if (index >= list->node_count) {{
|
||||
return NULL;
|
||||
}}
|
||||
|
||||
{T}Node *output = NULL;
|
||||
{T}Node *current = list->first;
|
||||
for (u64 i = 1; i <= index; ++i) {{
|
||||
current = current->next;
|
||||
}}
|
||||
|
||||
output = current;
|
||||
|
||||
return output;
|
29
codegen/dbl_list/snippets/list_insert
Normal file
29
codegen/dbl_list/snippets/list_insert
Normal file
@@ -0,0 +1,29 @@
|
||||
if (!list || !node || !(node->string)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
if (index == 0) {{
|
||||
wapp_str8_list_push_front(list, node);
|
||||
return;
|
||||
}} else if (index == list->node_count) {{
|
||||
wapp_str8_list_push_back(list, node);
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}Node *dst_node = wapp_str8_list_get(list, index);
|
||||
if (!dst_node) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
list->total_size += node_list.total_size;
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{T}Node *prev = dst_node->prev;
|
||||
|
||||
dst_node->prev = node_list.last;
|
||||
prev->next = node_list.first;
|
||||
|
||||
node_list.first->prev = prev;
|
||||
node_list.last->next = dst_node;
|
21
codegen/dbl_list/snippets/list_pop_back
Normal file
21
codegen/dbl_list/snippets/list_pop_back
Normal file
@@ -0,0 +1,21 @@
|
||||
{T}Node *output = NULL;
|
||||
|
||||
if (!list || list->node_count == 0) {{
|
||||
goto RETURN_{Tupper}_LIST_POP_BACK;
|
||||
}}
|
||||
|
||||
output = list->last;
|
||||
|
||||
if (list->node_count == 1) {{
|
||||
*list = ({T}List){{0}};
|
||||
goto RETURN_{Tupper}_LIST_POP_BACK;
|
||||
}}
|
||||
|
||||
--(list->node_count);
|
||||
list->total_size -= output->string->size;
|
||||
list->last = output->prev;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_POP_BACK:
|
||||
return output;
|
21
codegen/dbl_list/snippets/list_pop_front
Normal file
21
codegen/dbl_list/snippets/list_pop_front
Normal file
@@ -0,0 +1,21 @@
|
||||
{T}Node *output = NULL;
|
||||
|
||||
if (!list || list->node_count == 0) {{
|
||||
goto RETURN_{Tupper}_LIST_POP_FRONT;
|
||||
}}
|
||||
|
||||
output = list->first;
|
||||
|
||||
if (list->node_count == 1) {{
|
||||
*list = ({T}List){{0}};
|
||||
goto RETURN_{Tupper}_LIST_POP_FRONT;
|
||||
}}
|
||||
|
||||
--(list->node_count);
|
||||
list->total_size -= output->string->size;
|
||||
list->first = output->next;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_POP_FRONT:
|
||||
return output;
|
21
codegen/dbl_list/snippets/list_push_back
Normal file
21
codegen/dbl_list/snippets/list_push_back
Normal file
@@ -0,0 +1,21 @@
|
||||
if (!list || !node || !(node->string)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
if (list->node_count == 0) {{
|
||||
*list = node_list;
|
||||
return;
|
||||
}}
|
||||
|
||||
list->total_size += node_list.total_size;
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{T}Node *last = list->last;
|
||||
if (last) {{
|
||||
last->next = node_list.first;
|
||||
}}
|
||||
|
||||
list->last = node_list.last;
|
||||
node_list.first->prev = last;
|
21
codegen/dbl_list/snippets/list_push_front
Normal file
21
codegen/dbl_list/snippets/list_push_front
Normal file
@@ -0,0 +1,21 @@
|
||||
if (!list || !node || !(node->string)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
if (list->node_count == 0) {{
|
||||
*list = node_list;
|
||||
return;
|
||||
}}
|
||||
|
||||
list->total_size += node_list.total_size;
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{T}Node *first = list->first;
|
||||
if (first) {{
|
||||
first->prev = node_list.last;
|
||||
}}
|
||||
|
||||
list->first = node_list.first;
|
||||
node_list.last->next = first;
|
28
codegen/dbl_list/snippets/list_remove
Normal file
28
codegen/dbl_list/snippets/list_remove
Normal file
@@ -0,0 +1,28 @@
|
||||
{T}Node *output = NULL;
|
||||
if (!list) {{
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
if (index == 0) {{
|
||||
output = wapp_str8_list_pop_front(list);
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}} else if (index == list->node_count) {{
|
||||
output = wapp_str8_list_pop_back(list);
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
output = wapp_str8_list_get(list, index);
|
||||
if (!output) {{
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
output->prev->next = output->next;
|
||||
output->next->prev = output->prev;
|
||||
|
||||
--(list->node_count);
|
||||
list->total_size -= output->string->size;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_REMOVE:
|
||||
return output;
|
15
codegen/dbl_list/snippets/node_to_list
Normal file
15
codegen/dbl_list/snippets/node_to_list
Normal file
@@ -0,0 +1,15 @@
|
||||
{T}List output = {{.first = node, .last = node, .total_size = node->string->size, .node_count = 1}};
|
||||
|
||||
while (output.first->prev != NULL) {{
|
||||
output.total_size += output.first->prev->string->size;
|
||||
output.first = output.first->prev;
|
||||
++(output.node_count);
|
||||
}}
|
||||
|
||||
while (output.last->next != NULL) {{
|
||||
output.total_size += output.last->next->string->size;
|
||||
output.last = output.last->next;
|
||||
++(output.node_count);
|
||||
}}
|
||||
|
||||
return output;
|
Reference in New Issue
Block a user