aboutsummaryrefslogtreecommitdiff
path: root/source/std.elna
blob: 98fb35df9e0acc16a0abc44b07d2f11ecc19c92e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(* This Source Code Form is subject to the terms of the Mozilla Public License,
  v. 2.0. If a copy of the MPL was not distributed with this file, You can
  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.

  @param value Initial optional value.
  @return Constructed optional.
*)
proc std_optional_make#[T]*(value: ^T) -> StdOptional#[T]
return { StdOptional#[T]; has_value: true, value: value }

end.