summaryrefslogtreecommitdiffstats
path: root/src/list.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2021-12-20 16:25:43 +0100
committerLouis Burda <quent.burda@gmail.com>2021-12-20 16:25:43 +0100
commitf07580d31d1148c4a1811c36b09ca0ad50d9576b (patch)
tree05a8fbb44af81f3b5937df80c5af3305ec3ed3c9 /src/list.c
parentfaee8e9c6db45c6adacfc5113d55927f92e8bf29 (diff)
downloadtmus-f07580d31d1148c4a1811c36b09ca0ad50d9576b.tar.gz
tmus-f07580d31d1148c4a1811c36b09ca0ad50d9576b.zip
Restructured repository and added automatic make dependency generation
Diffstat (limited to 'src/list.c')
-rw-r--r--src/list.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/list.c b/src/list.c
new file mode 100644
index 0000000..29ddcf1
--- /dev/null
+++ b/src/list.c
@@ -0,0 +1,108 @@
+#include "list.h"
+#include "util.h"
+
+int
+list_empty(struct link *head)
+{
+ return head->next == NULL;
+}
+
+int
+list_len(struct link *head)
+{
+ struct link *iter;
+ int len;
+
+ ASSERT(head != NULL);
+
+ len = 0;
+ for (iter = head->next; iter; iter = iter->next)
+ len += 1;
+
+ return len;
+}
+
+int
+list_ffind(struct link *head, struct link *link)
+{
+ struct link *iter;
+
+ ASSERT(head != NULL);
+
+ for (iter = head->next; iter && iter != link; iter = iter->next);
+
+ return (iter == link);
+}
+
+struct link *
+link_back(struct link *link)
+{
+ ASSERT(link != NULL);
+
+ for (; link->next; link = link->next);
+
+ return link;
+}
+
+void
+link_prepend(struct link *cur, struct link *link)
+{
+ ASSERT(cur != NULL && link != NULL);
+
+ link->prev = cur->prev;
+ link->next = cur;
+
+ if (link->prev)
+ link->prev->next = link;
+ if (link->next)
+ link->next->prev = link;
+}
+
+void
+link_append(struct link *cur, struct link *link)
+{
+ ASSERT(cur != NULL && link != NULL);
+
+ link->prev = cur;
+ link->next = cur->next;
+
+ if (link->prev)
+ link->prev->next = link;
+ if (link->next)
+ link->next->prev = link;
+}
+
+struct link *
+link_pop(struct link *link)
+{
+ ASSERT(link != NULL);
+
+ if (link->prev)
+ link->prev->next = link->next;
+ if (link->next)
+ link->next->prev = link->prev;
+
+ return link;
+}
+
+struct link *
+link_iter(struct link *link, int n)
+{
+ int i;
+
+ for (i = 0; i < n; i++) {
+ if (!link) return NULL;
+ link = link->next;
+ }
+
+ return link;
+}
+
+void
+list_push_back(struct link *cur, struct link *link)
+{
+ struct link *back;
+
+ back = link_back(cur);
+ link_append(back, link);
+}