aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-03-18 12:57:53 -0700
committerChristian Cunningham <cc@localhost>2022-03-18 12:57:53 -0700
commit94f2b0b8f48f5715975446c637a078008fb7e941 (patch)
tree2fb1744748e3bd4c93b2dae65a6033763c17e737 /src/lib
parent7ea4a4e970ae4a72ac7b58e98d232662d580ed47 (diff)
Generalized Queue
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/queue.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lib/queue.c b/src/lib/queue.c
new file mode 100644
index 0000000..1fc35f6
--- /dev/null
+++ b/src/lib/queue.c
@@ -0,0 +1,55 @@
+#include <lib/queue.h>
+
+void push_to_queue(struct Entry* e, struct Queue* q)
+{
+ q->end.next->next = e;
+ q->end.next = e;
+ e->next = &q->end;
+}
+
+void prepend_to_queue(struct Entry* e, struct Queue* q)
+{
+ e->next = q->start.next;
+ q->start.next = e;
+ if (e->next->entry_type == END_ENTRY)
+ q->end.next = e;
+}
+
+struct Entry* pop_from_queue(struct Queue* q)
+{
+ if (q->start.next->entry_type == END_ENTRY)
+ return 0;
+ struct Entry* e = q->start.next;
+ q->start.next = e->next;
+ if (e->next->entry_type == END_ENTRY)
+ q->end.next = &q->start;
+ return e;
+}
+
+struct Entry* remove_next_from_queue(struct Entry* e)
+{
+ struct Entry* prev = e;
+ struct Entry* remove = e->next;
+ struct Entry* next = remove->next;
+ if (remove->entry_type != VALUE_ENTRY)
+ return 0;
+ prev->next = next;
+ if (next->entry_type == END_ENTRY)
+ next->next = prev;
+ return remove;
+}
+
+struct Entry* find_value(void* value, struct Queue* q)
+{
+ struct Entry* prev;
+ struct Entry* entry;
+ prev = &q->start;
+ entry = prev->next;
+ while (entry->entry_type != END_ENTRY) {
+ if (entry->value == value)
+ return prev;
+ prev = entry;
+ entry = prev->next;
+ }
+ return 0;
+}