From 830345192956aabac0f9eb67b1b8b24a1a7078d8 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 24 Oct 2023 08:48:00 -0400 Subject: [PATCH] Add List class --- src/hulk/list.d | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/hulk/list.d diff --git a/src/hulk/list.d b/src/hulk/list.d new file mode 100644 index 0000000..f9f9879 --- /dev/null +++ b/src/hulk/list.d @@ -0,0 +1,64 @@ +/** + * Linked list functionality. + */ +module hulk.list; + +import hulk.hurl.a1; + +/** + * Linked list structure. + */ +struct List(T) +{ + /** + * Linked list item. + */ + T * item; + + /** + * Next list entry. + */ + List!T * next; + + /** + * Add an item to this linked list. + */ + public void add(T * item) + { + List!T * last_entry = last_entry(); + List!T * new_entry = A1.allocate!(List!T)(); + last_entry.item = item; + last_entry.next = new_entry; + } + + /** + * Allow foreach iteration across a List. + */ + public int opApply(scope int delegate(ref T) dg) + { + List!T * entry = &this; + while (entry.next != null) + { + int result = dg(*entry.item); + if (result != 0) + { + return result; + } + entry = entry.next; + } + return 0; + } + + /** + * Get the last linked list entry. + */ + private @property List!T * last_entry() + { + List!T * result = &this; + while (result.next != null) + { + result = result.next; + } + return result; + } +}