aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/lists.pk
blob: 1e8dee5fb5b04756c60a8f93d4bf29796b255eae (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
## std.lists: Dynamic arrays.
## This module is imported by default.
# reference: https://doc.rust-lang.org/nomicon/vec/vec.html

use std.pointers

## The list type.
type list[T] = struct
  data: unique[T] # hrm
  capacity: int
  length: int

## Initialize and return an empty list with inner type T.
pub func init[T](): list[T] =
  ... # malloc? idk
  let (data, )
  { data: , capacity: 0, length: 0 }

## Gets the length of a list.
pub func len[T](self: list[T]): uint =
  self.length

pub func empty[T](self: list[T]): bool =
  self.length == 0

## Gets the internal capacity of a list.
func cap[T](self: list[T]): uint =
  self.capacity

## Expands the capacity of a list.
func grow[T](self: mut list[T]) =
  ...

## Pushes a new element to the end of a list.
pub func push[T](self: mut list[T], val: owned T) =
  if self.length == self.capacity:
    self.grow()
  unsafe: # todo: do we want unsafe blocks, as they are in rust?
    self.data.raw_set(self.length, val) # fixme
    # unsafe { ptr::write(self.ptr().add(self.length), val); }
    self.length += 1

## Takes ownership of and pushes all the values of a list into another list.
pub func push[T](self: mut list[T], val: owned list[T]) =
  ...

## Removes & returns an element from the end of a list, if it exists.
pub func pop[T](self: mut list[T]): T? =
  if self.length == 0:
    None
  unsafe:
    self.length -= 1
    Some(self.data.raw_get[T](self.length)) # fixme
    # unsafe { Some(ptr::read(self.ptr().add(self.length))) }

## Returns a reference to an element of a list, if in range.
pub func get[T](self: list[T], i: uint): lent T? =
  ...

## Sets the element of a list to a value. todo: when is `val` owned?
## todo: how do we deal with having the wrong offset?
pub func set[T](self: mut list[T], i: uint, val: owned T) =
  ...

## Inserts a value at a location and shifts elements of the list accordingly.
pub func insert[T](self: mut list[T], i: uint, val: T) =
  assert i <= self.length, "index out of bounds"
  if self.cap == self.len:
    self.grow()
  ...
  # unsafe {
  #     ptr::copy(
  #         self.ptr().add(index),
  #         self.ptr().add(index + 1),
  #         self.len - index,
  #     );
  #     ptr::write(self.ptr().add(index), elem);
  #     self.len += 1;
  # }

## Removes a value at a location and shifts elements of the list accordingly.
pub func remove[T](self: mut list[T], i: uint): T =
  assert index < self.length, "index out of bounds"
  unsafe:
    self.length -= 1
    result = ...
  # unsafe {
  #     self.len -= 1;
  #     let result = ptr::read(self.ptr().add(index));
  #     ptr::copy(
  #         self.ptr().add(index + 1),
  #         self.ptr().add(index),
  #         self.len - index,
  #     );
  #     result
  # }

## Gets the last element of a list, if it exists.
pub func last[T](self: list[T]): lent T? =
  self.get(self.len - 1)

# todo: many questions. owned? how does it not free?
pub func iter[T](self: owned list[T]): ListIter[T] =
  ...

# todo: iteration...
# todo: destructors...
# todo: syntax for inlining + other pragmas... as a macro, perhaps?
# todo: slices

type slice[T] = struct
  data: ptr[T] # hrm...
  length: uint