diff options
Diffstat (limited to 'source/std.elna')
| -rw-r--r-- | source/std.elna | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/source/std.elna b/source/std.elna index c028b93..98fb35d 100644 --- a/source/std.elna +++ b/source/std.elna @@ -3,12 +3,74 @@ obtain one at https://mozilla.org/MPL/2.0/. *) module +import sys.posix.stdlib + type StdString* = []const Word8 + (* + Stores a nullable pointer. + + An empty StdOptional can created by just using the default initializer. + std_optional_make should be used to create an StdOptional holding a pointer. + + @param T Pointer target type. + *) StdOptional#[T]* = record has_value: Bool; value: ^T end + (* + Initializes a sinlgy-linked list. + + @param T List element type. + *) + StdSListNode#[T]* = record + next: ^StdSListNode#[T]; + value: ^T + end + (* + Singly-linked list. + + @param T List element type. + *) + StdSList#[T]* = record + head: StdOptional#[StdSListNode#[T]]; + tail: ^StdSListNode#[T] + end + +(* + Inserts a new element into the list. + + @param list List. + @param element Element to insert. +*) +proc std_slist_append#[T]*(list: ^StdSList#[T]; element: ^T) +var + node := malloc#[StdSListNode#[T]](#size(StdSListNode#[T])) +begin + node^.value := element; + + if list^.head.has_value then + node^.next := list^.head.value; + + list^.tail^.next := node; + list^.tail := node + else + node^.next := node; + + list^.head := std_optional_make(node); + list^.tail := node + end +return + +(* + Tells whether the list is empty. + + @param list List to exemine. + @return Whether the provided list is empty. +*) +proc std_slist_empty#[T]*(list: ^StdSList#[T]) -> Bool +return ~list^.head.has_value (* Constructs an optional holding the pointer value. |
