aboutsummaryrefslogtreecommitdiff
path: root/lib/list
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-01-15 01:13:51 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-01-15 01:13:51 +0100
commit3f79b7bd553a52fca7a098f5195b406ff9970491 (patch)
tree99d0399141e7894219fe7a0deed1ccadb226b9c9 /lib/list
parent919450dc7d965c4067287e1ece3ceafdde8ff5a9 (diff)
add list and static library builder
Diffstat (limited to 'lib/list')
-rw-r--r--lib/list/list.c98
-rw-r--r--lib/list/list.h26
2 files changed, 124 insertions, 0 deletions
diff --git a/lib/list/list.c b/lib/list/list.c
new file mode 100644
index 0000000..753fdb1
--- /dev/null
+++ b/lib/list/list.c
@@ -0,0 +1,98 @@
+#include "list.h"
+#include "../malloc/malloc.h"
+
+
+list_node_t *__new_list_node(void *value);
+
+
+list_t *new_list()
+{
+ list_t *list = malloc(sizeof(list_t));
+ list->first = 0;
+ list->last = 0;
+ list->size = 0;
+ return list;
+}
+
+
+list_t *from_array(void **array, u64 size)
+{
+ int i;
+ list_t *list = new_list();
+ list->size = size;
+
+ for (i = 0; i < size; ++i)
+ list_append(list, array[i]);
+
+ return list;
+}
+
+
+void list_append(list_t *list, void *value)
+{
+ list_node_t *node = __new_list_node(value);
+
+ if (list->last == 0) {
+ list->first = node;
+ list->last = list->first;
+ } else {
+ list->last->next = node;
+ node->previous = list->last;
+ list->last = node;
+ }
+
+ ++list->size;
+}
+
+
+void list_prepend(list_t *list, void *value)
+{
+ list_node_t *node = __new_list_node(value);
+
+ if (list->last == 0) {
+ list->first = node;
+ list->last = list->first;
+ } else {
+ list->first->previous = node;
+ node->next = list->first;
+ list->first = node;
+ }
+
+ ++list->size;
+}
+
+
+void *pop_first(list_t *list)
+{
+ void *value = list->first->value;
+ list_node_t *node = list->first;
+
+ list->first = node->next;
+ free(node);
+
+ --list->size;
+ return value;
+}
+
+
+void *pop_last(list_t *list)
+{
+ void *value = list->last->value;
+ list_node_t *node = list->last;
+
+ list->last = node->previous;
+ free(node);
+
+ --list->size;
+ return value;
+}
+
+
+list_node_t *__new_list_node(void *value)
+{
+ list_node_t *node = malloc(sizeof(list_node_t));
+ node->value = value;
+ node->next = 0;
+ node->previous = 0;
+ return node;
+}
diff --git a/lib/list/list.h b/lib/list/list.h
new file mode 100644
index 0000000..9acecac
--- /dev/null
+++ b/lib/list/list.h
@@ -0,0 +1,26 @@
+#ifndef LIST_H
+#define LIST_H
+
+#include "../sys/sizes.h"
+
+typedef struct list_node_t__ {
+ struct list_node_t__ *next;
+ struct list_node_t__ *previous;
+ void *value;
+} list_node_t;
+
+typedef struct {
+ list_node_t *first;
+ list_node_t *last;
+ u64 size;
+} list_t;
+
+list_t *new_list();
+list_t *from_array(void **array, u64 size);
+void list_append(list_t *list, void *value);
+void list_prepend(list_t *list, void *value);
+void *pop_first(list_t *list);
+void *pop_last(list_t *list);
+void free_list();
+
+#endif