Remove codegen and implement dbl_list as generic container

This commit is contained in:
Abdelrahman Said
2025-04-16 00:07:11 +01:00
parent 0942643b4e
commit 6fb078a868
35 changed files with 552 additions and 1199 deletions

View File

@@ -0,0 +1,209 @@
#include "./dbl_list.h"
#include "../../common/aliases/aliases.h"
#include <stddef.h>
internal DBL_LIST(void) node_to_list(DBL_NODE(void) *node);
DBL_NODE(void) *_dbl_list_get(const DBL_LIST(void) *list, u64 index) {
if (index >= list->node_count) {
return NULL;
}
DBL_NODE(void) *output = NULL;
DBL_NODE(void) *current = list->first;
for (u64 i = 1; i <= index; ++i) {
current = current->next;
}
output = current;
return output;
}
void _dbl_list_push_front(DBL_LIST(void) *list, DBL_NODE(void) *node) {
if (!list || !node || !(node->item)) {
return;
}
DBL_LIST(void) node_list = node_to_list(node);
if (list->node_count == 0) {
*list = node_list;
return;
}
list->node_count += node_list.node_count;
DBL_NODE(void) *first = list->first;
if (first) {
first->prev = node_list.last;
}
list->first = node_list.first;
node_list.last->next = first;
}
void _dbl_list_push_back(DBL_LIST(void) *list, DBL_NODE(void) *node) {
if (!list || !node || !(node->item)) {
return;
}
DBL_LIST(void) node_list = node_to_list(node);
if (list->node_count == 0) {
*list = node_list;
return;
}
list->node_count += node_list.node_count;
DBL_NODE(void) *last = list->last;
if (last) {
last->next = node_list.first;
}
list->last = node_list.last;
node_list.first->prev = last;
}
void _dbl_list_insert(DBL_LIST(void) *list, DBL_NODE(void) *node, u64 index) {
if (!list || !node || !(node->item)) {
return;
}
if (index == 0) {
_dbl_list_push_front(list, node);
return;
} else if (index == list->node_count) {
_dbl_list_push_back(list, node);
return;
}
DBL_NODE(void) *dst_node = _dbl_list_get(list, index);
if (!dst_node) {
return;
}
DBL_LIST(void) node_list = node_to_list(node);
list->node_count += node_list.node_count;
DBL_NODE(void) *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;
}
DBL_NODE(void) *_dbl_list_pop_front(DBL_LIST(void) *list) {
DBL_NODE(void) *output = NULL;
if (!list || list->node_count == 0) {
goto RETURN_STR8_LIST_POP_FRONT;
}
output = list->first;
if (list->node_count == 1) {
*list = (DBL_LIST(void)){0};
goto RETURN_STR8_LIST_POP_FRONT;
}
--(list->node_count);
list->first = output->next;
output->prev = output->next = NULL;
RETURN_STR8_LIST_POP_FRONT:
return output;
}
DBL_NODE(void) *_dbl_list_pop_back(DBL_LIST(void) *list) {
DBL_NODE(void) *output = NULL;
if (!list || list->node_count == 0) {
goto RETURN_STR8_LIST_POP_BACK;
}
output = list->last;
if (list->node_count == 1) {
*list = (DBL_LIST(void)){0};
goto RETURN_STR8_LIST_POP_BACK;
}
--(list->node_count);
list->last = output->prev;
output->prev = output->next = NULL;
RETURN_STR8_LIST_POP_BACK:
return output;
}
DBL_NODE(void) *_dbl_list_remove(DBL_LIST(void) *list, u64 index) {
DBL_NODE(void) *output = NULL;
if (!list) {
goto RETURN_STR8_LIST_REMOVE;
}
if (index == 0) {
output = _dbl_list_pop_front(list);
goto RETURN_STR8_LIST_REMOVE;
} else if (index == list->node_count) {
output = _dbl_list_pop_back(list);
goto RETURN_STR8_LIST_REMOVE;
}
output = _dbl_list_get(list, index);
if (!output) {
goto RETURN_STR8_LIST_REMOVE;
}
output->prev->next = output->next;
output->next->prev = output->prev;
--(list->node_count);
output->prev = output->next = NULL;
RETURN_STR8_LIST_REMOVE:
return output;
}
void _dbl_list_empty(DBL_LIST(void) *list) {
if (!list) {
return;
}
u64 count = list->node_count;
for (u64 i = 0; i < count; ++i) {
_dbl_list_pop_back(list);
}
}
internal DBL_LIST(void) node_to_list(DBL_NODE(void) *node) {
DBL_LIST(void) output = {.first = node, .last = node, .node_count = 1};
while (output.first->prev != NULL) {
output.first = output.first->prev;
++(output.node_count);
}
while (output.last->next != NULL) {
output.last = output.last->next;
++(output.node_count);
}
return output;
}

View File

@@ -0,0 +1,72 @@
#ifndef DBL_LIST_H
#define DBL_LIST_H
#include "../../common/aliases/aliases.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
#endif // !__cplusplus
#define DBL_NODE(T) T##Node
#define DBL_LIST(T) T##List
#define CAST_NODE(NODE) ((DBL_NODE(void))(NODE))
#define CAST_LIST(LIST) ((DBL_LIST(void))(LIST))
#define CAST_NODE_PTR(NODE_PTR) ((DBL_NODE(void)*)(NODE_PTR))
#define CAST_LIST_PTR(LIST_PTR) ((DBL_LIST(void)*)(LIST_PTR))
#define DBL_LIST_DECL(T) typedef struct DBL_NODE(T) DBL_NODE(T); \
struct DBL_NODE(T) { \
T *item; \
DBL_NODE(T) *prev; \
DBL_NODE(T) *next; \
}; \
\
typedef struct DBL_LIST(T) DBL_LIST(T); \
struct DBL_LIST(T) { \
DBL_NODE(T) *first; \
DBL_NODE(T) *last; \
u64 node_count; \
}
DBL_LIST_DECL(void);
#define wapp_dbl_list_get(T, LIST_PTR, INDEX) \
(DBL_NODE(T)*)_dbl_list_get(CAST_LIST_PTR(LIST_PTR), INDEX)
#define wapp_dbl_list_push_front(T, LIST_PTR, NODE_PTR) \
_dbl_list_push_front(CAST_LIST_PTR(LIST_PTR), CAST_NODE_PTR(NODE_PTR))
#define wapp_dbl_list_push_back(T, LIST_PTR, NODE_PTR) \
_dbl_list_push_back(CAST_LIST_PTR(LIST_PTR), CAST_NODE_PTR(NODE_PTR))
#define wapp_dbl_list_insert(T, LIST_PTR, NODE_PTR, INDEX) \
_dbl_list_insert(CAST_LIST_PTR(LIST_PTR), CAST_NODE_PTR(NODE_PTR), INDEX)
#define wapp_dbl_list_pop_front(T, LIST_PTR) \
(DBL_NODE(T)*)_dbl_list_pop_front(CAST_LIST_PTR(LIST_PTR))
#define wapp_dbl_list_pop_back(T, LIST_PTR) \
(DBL_NODE(T)*)_dbl_list_pop_back(CAST_LIST_PTR(LIST_PTR))
#define wapp_dbl_list_remove(T, LIST_PTR, INDEX) \
(DBL_NODE(T)*)_dbl_list_remove(CAST_LIST_PTR(LIST_PTR), INDEX)
#define wapp_dbl_list_empty(T, LIST_PTR) \
_dbl_list_empty(CAST_LIST_PTR(LIST_PTR))
DBL_NODE(void) *_dbl_list_get(const DBL_LIST(void) *list, u64 index);
void _dbl_list_push_front(DBL_LIST(void) *list, DBL_NODE(void) *node);
void _dbl_list_push_back(DBL_LIST(void) *list, DBL_NODE(void) *node);
void _dbl_list_insert(DBL_LIST(void) *list, DBL_NODE(void) *node, u64 index);
DBL_NODE(void) *_dbl_list_pop_front(DBL_LIST(void) *list);
DBL_NODE(void) *_dbl_list_pop_back(DBL_LIST(void) *list);
DBL_NODE(void) *_dbl_list_remove(DBL_LIST(void) *list, u64 index);
void _dbl_list_empty(DBL_LIST(void) *list);
#ifdef __cplusplus
END_C_LINKAGE
#endif // !__cplusplus
#endif // !DBL_LIST_H

View File

@@ -0,0 +1,6 @@
#ifndef WAPP_CONTAINERS_C
#define WAPP_CONTAINERS_C
#include "dbl_list/dbl_list.c"
#endif // !WAPP_CONTAINERS_C

View File

@@ -0,0 +1,7 @@
#ifndef WAPP_CONTAINERS_H
#define WAPP_CONTAINERS_H
#include "dbl_list/dbl_list.h"
#include "../common/wapp_common.h"
#endif // !WAPP_CONTAINERS_H

View File

@@ -4,12 +4,13 @@
#include "../../mem/allocator/mem_allocator.h"
#include "../../mem/arena/mem_arena_allocator.h"
#include "../../strings/str8/str8.h"
#include "../../strings/str8/str8_list.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) {
u32 wapp_cpath_join_path(Str8 *dst, const DBL_LIST(Str8) *parts) {
if (!dst || !parts) {
return CPATH_JOIN_INVALID_ARGS;
}
@@ -21,29 +22,29 @@ u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) {
Str8 separator = wapp_str8_buf(4);
wapp_str8_push_back(&separator, PATH_SEP);
u64 required_capacity = parts->node_count * separator.size + parts->total_size;
u64 required_capacity = parts->node_count * separator.size + wapp_str8_list_total_size(parts);
if (dst->capacity < required_capacity) {
return CPATH_JOIN_INSUFFICIENT_DST_CAPACITY;
}
// Handle first node
const Str8Node *first_node = wapp_str8_list_get(parts, 0);
wapp_str8_copy_str8_capped(dst, first_node->string);
const DBL_NODE(Str8) *first_node = wapp_dbl_list_get(Str8, parts, 0);
wapp_str8_copy_str8_capped(dst, first_node->item);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
const Str8Node *node = first_node;
const DBL_NODE(Str8) *node = first_node;
u64 node_index = 1;
bool running = true;
while (running && node->next) {
node = node->next;
if (node->string->size == 0) {
if (node->item->size == 0) {
continue;
}
if (dst->size > 0) {
char dst_last = wapp_str8_get(dst, dst->size - 1);
char node_start = wapp_str8_get(node->string, 0);
char node_start = wapp_str8_get(node->item, 0);
bool add_path_sep = dst_last != PATH_SEP && node_start != PATH_SEP;
if (add_path_sep) {
@@ -51,7 +52,7 @@ u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) {
}
}
wapp_str8_concat_capped(dst, node->string);
wapp_str8_concat_capped(dst, node->item);
++node_index;
running = node_index < parts->node_count;
@@ -90,7 +91,7 @@ Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels) {
goto RETURN_DIRUP;
}
Str8List *parts = wapp_str8_split(&tmp_arena, path, &separator);
DBL_LIST(Str8) *parts = wapp_str8_split(&tmp_arena, path, &separator);
if (!parts) {
goto RETURN_DIRUP;
}
@@ -104,11 +105,11 @@ Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels) {
wapp_str8_push_back(output, absolute ? PATH_SEP : '.');
} else {
for (u64 i = 0; i < levels; ++i) {
wapp_str8_list_pop_back(parts);
wapp_dbl_list_pop_back(Str8, parts);
}
u64 alignment = sizeof(void *) * 2;
u64 alloc_size = parts->total_size + parts->node_count * separator.size;
u64 alloc_size = wapp_str8_list_total_size(parts) + parts->node_count * separator.size;
u64 modulo = alloc_size & (alignment - 1);
alloc_size += alignment - modulo;

View File

@@ -28,7 +28,7 @@ enum {
CPATH_JOIN_INSUFFICIENT_DST_CAPACITY,
};
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts);
u32 wapp_cpath_join_path(Str8 *dst, const DBL_LIST(Str8) *parts);
Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels);
#ifdef __cplusplus

View File

@@ -18,7 +18,7 @@
internal inline CMDResult execute_command(Str8RO *cmd, CMDOutHandling out_handling, Str8 *out_buf);
internal inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf);
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd) {
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const DBL_LIST(Str8) *cmd) {
if (!cmd) {
return CMD_NO_EXIT(SHELL_ERR_INVALID_ARGS);
}

View File

@@ -14,7 +14,7 @@ BEGIN_C_LINKAGE
#define CMD_NO_EXIT(ERR) ((CMDResult){.exited = false, .exit_code = EXIT_FAILURE, .error = ERR})
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd);
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const DBL_LIST(Str8) *cmd);
external CMDError get_output_status(FILE *fp, i32 *status_out);

View File

@@ -1,5 +1,7 @@
#include "str8.h"
#include "str8_list.h"
#include "../../../common/aliases/aliases.h"
#include "../../../containers/dbl_list/dbl_list.h"
#include "../../mem/allocator/mem_allocator.h"
#include <stdarg.h>
#include <stddef.h>
@@ -300,19 +302,19 @@ i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) {
return -1;
}
Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
DBL_LIST(Str8) *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
if (!allocator || !str || !delimiter) {
return NULL;
}
Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List));
DBL_LIST(Str8) *output = wapp_mem_allocator_alloc(allocator, sizeof(DBL_LIST(Str8)));
if (delimiter->size > str->size) {
Str8 *full = wapp_str8_alloc_str8(allocator, str);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8)));
if (node) {
node->string = full;
wapp_str8_list_push_back(output, node);
node->item = full;
wapp_dbl_list_push_back(Str8, output, node);
}
goto RETURN_STR8_SPLIT;
@@ -330,10 +332,10 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8
}
before_str = wapp_str8_alloc_substr(allocator, str, start, start + end);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8)));
if (node) {
node->string = before_str;
wapp_str8_list_push_back(output, node);
node->item = before_str;
wapp_dbl_list_push_back(Str8, output, node);
}
wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8));
@@ -344,30 +346,30 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8
}
// Ensure the last part of the string after the delimiter is added to the list
rest = wapp_str8_alloc_substr(allocator, str, start, str->size);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
rest = wapp_str8_alloc_substr(allocator, str, start, str->size);
DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8)));
if (node) {
node->string = rest;
wapp_str8_list_push_back(output, node);
node->item = rest;
wapp_dbl_list_push_back(Str8, output, node);
}
RETURN_STR8_SPLIT:
return output;
}
Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
DBL_LIST(Str8) *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
if (!allocator || !str || !delimiter) {
return NULL;
}
Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List));
DBL_LIST(Str8) *output = wapp_mem_allocator_alloc(allocator, sizeof(DBL_LIST(Str8)));
if (delimiter->size > str->size) {
Str8 *full = wapp_str8_alloc_str8(allocator, str);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8)));
if (node) {
node->string = full;
wapp_str8_list_push_back(output, node);
node->item = full;
wapp_dbl_list_push_back(Str8, output, node);
}
goto RETURN_STR8_SPLIT;
@@ -384,10 +386,10 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str
}
after_str = wapp_str8_alloc_substr(allocator, rest, end + delimiter->size, str->size);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8)));
if (node) {
node->string = after_str;
wapp_str8_list_push_front(output, node);
node->item = after_str;
wapp_dbl_list_push_front(Str8, output, node);
}
wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8));
@@ -397,32 +399,32 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str
}
rest = wapp_str8_alloc_substr(allocator, str, 0, rest->size);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
DBL_NODE(Str8) *node = wapp_mem_allocator_alloc(allocator, sizeof(DBL_NODE(Str8)));
if (node) {
node->string = rest;
wapp_str8_list_push_front(output, node);
node->item = rest;
wapp_dbl_list_push_front(Str8, output, node);
}
RETURN_STR8_SPLIT:
return output;
}
Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter) {
Str8 *wapp_str8_join(const Allocator *allocator, const DBL_LIST(Str8) *list, Str8RO *delimiter) {
if (!allocator || !list || !delimiter) {
return NULL;
}
u64 capacity = list->total_size + (delimiter->size * (list->node_count - 1));
u64 capacity = wapp_str8_list_total_size(list) + (delimiter->size * (list->node_count - 1));
Str8 *output = wapp_str8_alloc_buf(allocator, capacity * 2);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
Str8Node *node;
DBL_NODE(Str8) *node;
u64 node_index = 0;
bool running = true;
while (running) {
node = wapp_str8_list_get(list, node_index);
wapp_str8_concat_capped(output, node->string);
node = wapp_dbl_list_get(Str8, list, node_index);
wapp_str8_concat_capped(output, node->item);
if (node_index + 1 < list->node_count) {
wapp_str8_concat_capped(output, delimiter);
}

View File

@@ -3,6 +3,7 @@
#include "./str8_list.h"
#include "../../../common/aliases/aliases.h"
#include "../../../containers/dbl_list/dbl_list.h"
#include "../../mem/allocator/mem_allocator.h"
#include <string.h>
#include <stdbool.h>
@@ -84,15 +85,15 @@ i64 wapp_str8_rfind(Str8RO *str, Str8RO substr);
*/
#define wapp_str8_split(ALLOCATOR, STR, DELIMITER) wapp_str8_split_with_max(ALLOCATOR, STR, DELIMITER, -1)
#define wapp_str8_rsplit(ALLOCATOR, STR, DELIMITER) wapp_str8_rsplit_with_max(ALLOCATOR, STR, DELIMITER, -1)
Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits);
Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits);
Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter);
DBL_LIST(Str8) *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits);
DBL_LIST(Str8) *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits);
Str8 *wapp_str8_join(const Allocator *allocator, const DBL_LIST(Str8) *list, Str8RO *delimiter);
/**
* Str8 list utilities
*/
#define wapp_str8_node_from_cstr(STRING) ((Str8Node){.string = &wapp_str8_lit(STRING)})
#define wapp_str8_node_from_str8(STRING) ((Str8Node){.string = &(STRING)})
#define wapp_str8_node_from_cstr(STRING) ((DBL_NODE(Str8)){.item = &wapp_str8_lit(STRING)})
#define wapp_str8_node_from_str8(STRING) ((DBL_NODE(Str8)){.item = &(STRING)})
#ifdef __cplusplus
END_C_LINKAGE

View File

@@ -1,224 +1,18 @@
/**
* THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN
*/
#include "./str8_list.h"
#include "../../../common/aliases/aliases.h"
#include "./str8.h"
#include <stddef.h>
#include "../../../common/aliases/aliases.h"
#include "../../../containers/dbl_list/dbl_list.h"
internal Str8List str8_node_to_list(Str8Node *node);
u64 wapp_str8_list_total_size(const DBL_LIST(Str8) *list) {
if (!list) {
return 0;
}
Str8Node *wapp_str8_list_get(const Str8List *list, u64 index) {
if (index >= list->node_count) {
return NULL;
}
Str8Node *output = NULL;
Str8Node *current = list->first;
for (u64 i = 1; i <= index; ++i) {
current = current->next;
}
output = current;
return output;
u64 output = 0;
for (u64 i = 0; i < list->node_count; ++i) {
DBL_NODE(Str8) *node = wapp_dbl_list_get(Str8, list, i);
output += node->item->size;
}
return output;
}
void wapp_str8_list_push_front(Str8List *list, Str8Node *node) {
if (!list || !node || !(node->string)) {
return;
}
Str8List node_list = str8_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;
Str8Node *first = list->first;
if (first) {
first->prev = node_list.last;
}
list->first = node_list.first;
node_list.last->next = first;
}
void wapp_str8_list_push_back(Str8List *list, Str8Node *node) {
if (!list || !node || !(node->string)) {
return;
}
Str8List node_list = str8_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;
Str8Node *last = list->last;
if (last) {
last->next = node_list.first;
}
list->last = node_list.last;
node_list.first->prev = last;
}
void wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index) {
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;
}
Str8Node *dst_node = wapp_str8_list_get(list, index);
if (!dst_node) {
return;
}
Str8List node_list = str8_node_to_list(node);
list->total_size += node_list.total_size;
list->node_count += node_list.node_count;
Str8Node *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;
}
Str8Node *wapp_str8_list_pop_front(Str8List *list) {
Str8Node *output = NULL;
if (!list || list->node_count == 0) {
goto RETURN_STR8_LIST_POP_FRONT;
}
output = list->first;
if (list->node_count == 1) {
*list = (Str8List){0};
goto RETURN_STR8_LIST_POP_FRONT;
}
--(list->node_count);
list->total_size -= output->string->size;
list->first = output->next;
output->prev = output->next = NULL;
RETURN_STR8_LIST_POP_FRONT:
return output;
}
Str8Node *wapp_str8_list_pop_back(Str8List *list) {
Str8Node *output = NULL;
if (!list || list->node_count == 0) {
goto RETURN_STR8_LIST_POP_BACK;
}
output = list->last;
if (list->node_count == 1) {
*list = (Str8List){0};
goto RETURN_STR8_LIST_POP_BACK;
}
--(list->node_count);
list->total_size -= output->string->size;
list->last = output->prev;
output->prev = output->next = NULL;
RETURN_STR8_LIST_POP_BACK:
return output;
}
Str8Node *wapp_str8_list_remove(Str8List *list, u64 index) {
Str8Node *output = NULL;
if (!list) {
goto RETURN_STR8_LIST_REMOVE;
}
if (index == 0) {
output = wapp_str8_list_pop_front(list);
goto RETURN_STR8_LIST_REMOVE;
} else if (index == list->node_count) {
output = wapp_str8_list_pop_back(list);
goto RETURN_STR8_LIST_REMOVE;
}
output = wapp_str8_list_get(list, index);
if (!output) {
goto RETURN_STR8_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_STR8_LIST_REMOVE:
return output;
}
void wapp_str8_list_empty(Str8List *list) {
if (!list) {
return;
}
u64 count = list->node_count;
for (u64 i = 0; i < count; ++i) {
wapp_str8_list_pop_back(list);
}
}
internal Str8List str8_node_to_list(Str8Node *node) {
Str8List 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;
}

View File

@@ -6,6 +6,7 @@
#define STR8_LIST_H
#include "../../../common/aliases/aliases.h"
#include "../../../containers/dbl_list/dbl_list.h"
#ifdef __cplusplus
BEGIN_C_LINKAGE
@@ -13,29 +14,9 @@ BEGIN_C_LINKAGE
typedef struct str8 Str8;
typedef struct Str8Node Str8Node;
struct Str8Node {
Str8 *string;
Str8Node *prev;
Str8Node *next;
};
DBL_LIST_DECL(Str8);
typedef struct Str8List Str8List;
struct Str8List {
Str8Node *first;
Str8Node *last;
u64 total_size;
u64 node_count;
};
Str8Node *wapp_str8_list_get(const Str8List *list, u64 index);
void wapp_str8_list_push_front(Str8List *list, Str8Node *node);
void wapp_str8_list_push_back(Str8List *list, Str8Node *node);
void wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index);
Str8Node *wapp_str8_list_pop_front(Str8List *list);
Str8Node *wapp_str8_list_pop_back(Str8List *list);
Str8Node *wapp_str8_list_remove(Str8List *list, u64 index);
void wapp_str8_list_empty(Str8List *list);
u64 wapp_str8_list_total_size(const DBL_LIST(Str8) *list);
#ifdef __cplusplus
END_C_LINKAGE

View File

@@ -2,6 +2,7 @@
#define WAPP_C
#include "wapp.h"
#include "containers/wapp_containers.c"
#include "core/wapp_core.c"
#include "prng/wapp_prng.c"
#include "uuid/uuid.c"

View File

@@ -2,6 +2,7 @@
#define WAPP_H
#include "common/wapp_common.h"
#include "containers/wapp_containers.h"
#include "core/wapp_core.h"
#include "prng/wapp_prng.h"
#include "uuid/wapp_uuid.h"