wm: glendy

Download patch

ref: 38cb6954edfac11880bf40c2b87d3e05f649b082
parent: d4e2dd4c6f49c9c0e45a29e7bacdfb37d01b3d93
author: mkf <mkf@cloud9p.org>
date: Tue Jun 4 14:56:45 EDT 2024

util: add quene and doubly linked lists

--- a/util.c
+++ b/util.c
@@ -1,4 +1,6 @@
 #include "port.h"
+
+#include "util.h"
 #include "engine.h"
 
 int
@@ -113,4 +115,102 @@
 		sysfatal("malloc: %r");
 	
 	return p;	
+}
+/* list */
+int
+lladd(List *last, void *data)
+{
+	List *temp;
+
+	dprint("lladd(%p, %p)\n", last, data);
+	
+	if(last->next != nil)
+		return 0;
+
+	dprint("lladd(): adding entry\n");
+	
+	temp = llnew();
+	
+	last->next = temp;
+	temp->data = data;
+	
+	return 1;
+}
+
+void
+llappend(List *first, void *data)
+{
+	List *temp, *last;
+
+	dprint("lladd(%p, %p)\n", first, data);
+
+	/* workaround for first entry */	
+	if(first->data == nil)
+		temp = first;
+	else
+		temp = llnew();
+	temp->data = data;
+	
+	last = first->prev;
+	if(last != nil)
+		last->next = temp;
+	
+	first->prev = temp;
+}
+
+List*
+llnew(void)
+{
+	List *l;
+	l = (List*)emalloc(sizeof(List));
+	l->next = nil;
+	l->data = nil;
+	l->prev = nil;
+	
+	return l;
+}
+
+/* returns nth item from list  */
+void*
+lookup(List *l, int n)
+{
+	List *tmp;
+	
+	dprint("lookup(%p, %d)\n", l, n);
+	/* cycles thru list if n > list size */
+	for(tmp = l ; --n > 0 ; tmp = tmp->next)
+		;
+	
+	return tmp->data;
+}
+
+/* quene */
+void
+qadd(Quene *q, void *data)
+{
+	q->len++;
+	llappend(q->l, data);
+}
+
+void
+qnext(Quene *q)
+{
+	List *oldfirst, *last;
+	if(q->len == 0)
+		sysfatal("qnext(): q->len == 0");
+	
+	q->len--;
+	
+	/* note the last node */
+	last = q->l->prev;
+	
+	/* note the first */
+	oldfirst = q->l;
+	
+	/* delete oldfirst */
+	q->l = q->l->next;
+	free(oldfirst);
+	
+	/* update last */
+	last->next = q->l;
 }
--- a/util.h
+++ b/util.h
@@ -1,6 +1,30 @@
+typedef struct List List;
+struct List
+{
+	void *data;
+	List *next;
+	List *prev;
+};
+
+typedef struct
+{
+	int len;
+	List *l;
+}Quene;
+
 int isnum(char *s, unsigned int n);
 int strtodir(char *s);
 char* dirtostr(int dir);
 Point parseput(char *x, char *y);
 int dprint(char *fmt, ...);
-void* emalloc(unsigned long n);
\ No newline at end of file
+void* emalloc(unsigned long n);
+
+/* ll */
+int lladd(List *tail, void *data);
+List* llnew(void);
+void llappend(List *first, void *data);
+void* lookup(List *l, int n);
+
+/* quene */
+void qadd(Quene *q, void *data);
+void qnext(Quene *q);