@shd101wyy/yo 0.1.25 → 0.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/skills/yo-async-effects/SKILL.md +4 -4
- package/.github/skills/yo-async-effects/async-effects-recipes.md +40 -40
- package/.github/skills/yo-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +30 -26
- package/.github/skills/yo-project-workflow/SKILL.md +6 -3
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
- package/.github/skills/yo-syntax/SKILL.md +7 -6
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +78 -60
- package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
- package/README.md +10 -8
- package/out/cjs/index.cjs +583 -567
- package/out/cjs/yo-cli.cjs +664 -632
- package/out/cjs/yo-lsp.cjs +510 -485
- package/out/esm/index.mjs +538 -522
- package/out/types/src/codegen/codegen-c.d.ts +2 -2
- package/out/types/src/codegen/functions/collection.d.ts +2 -2
- package/out/types/src/codegen/functions/context.d.ts +3 -2
- package/out/types/src/codegen/types/collection.d.ts +2 -2
- package/out/types/src/codegen/utils/index.d.ts +3 -1
- package/out/types/src/doc/builder.d.ts +2 -2
- package/out/types/src/evaluator/calls/closure-type.d.ts +2 -2
- package/out/types/src/evaluator/calls/record-type.d.ts +11 -0
- package/out/types/src/evaluator/context.d.ts +8 -9
- package/out/types/src/evaluator/index.d.ts +3 -3
- package/out/types/src/evaluator/types/record.d.ts +14 -0
- package/out/types/src/evaluator/types/validation.d.ts +2 -2
- package/out/types/src/evaluator/values/anonymous-module.d.ts +5 -5
- package/out/types/src/evaluator/values/impl.d.ts +1 -1
- package/out/types/src/expr.d.ts +1 -4
- package/out/types/src/formatter.d.ts +11 -0
- package/out/types/src/function-value.d.ts +1 -1
- package/out/types/src/lsp/document-manager.d.ts +1 -1
- package/out/types/src/lsp/formatting.d.ts +2 -0
- package/out/types/src/module-manager.d.ts +3 -3
- package/out/types/src/tests/formatter.test.d.ts +1 -0
- package/out/types/src/types/creators.d.ts +3 -4
- package/out/types/src/types/definitions.d.ts +8 -19
- package/out/types/src/types/guards.d.ts +3 -3
- package/out/types/src/types/tags.d.ts +0 -1
- package/out/types/src/types/utils.d.ts +1 -1
- package/out/types/src/value-tag.d.ts +0 -1
- package/out/types/src/value.d.ts +6 -13
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/std/alg/hash.yo +13 -21
- package/std/allocator.yo +25 -40
- package/std/async.yo +3 -7
- package/std/build.yo +105 -151
- package/std/cli/arg_parser.yo +184 -169
- package/std/collections/array_list.yo +350 -314
- package/std/collections/btree_map.yo +142 -131
- package/std/collections/deque.yo +132 -128
- package/std/collections/hash_map.yo +542 -566
- package/std/collections/hash_set.yo +623 -687
- package/std/collections/linked_list.yo +275 -293
- package/std/collections/ordered_map.yo +113 -85
- package/std/collections/priority_queue.yo +73 -73
- package/std/crypto/md5.yo +191 -95
- package/std/crypto/random.yo +56 -64
- package/std/crypto/sha256.yo +151 -107
- package/std/encoding/base64.yo +87 -81
- package/std/encoding/hex.yo +43 -50
- package/std/encoding/html.yo +56 -81
- package/std/encoding/html_char_utils.yo +7 -13
- package/std/encoding/html_entities.yo +2248 -2253
- package/std/encoding/json.yo +316 -224
- package/std/encoding/punycode.yo +86 -116
- package/std/encoding/toml.yo +67 -66
- package/std/encoding/utf16.yo +37 -44
- package/std/env.yo +62 -91
- package/std/error.yo +12 -20
- package/std/fmt/display.yo +5 -9
- package/std/fmt/index.yo +8 -14
- package/std/fmt/to_string.yo +330 -315
- package/std/fmt/writer.yo +58 -87
- package/std/fs/dir.yo +83 -102
- package/std/fs/file.yo +147 -180
- package/std/fs/metadata.yo +45 -78
- package/std/fs/temp.yo +55 -65
- package/std/fs/types.yo +27 -40
- package/std/fs/walker.yo +53 -68
- package/std/gc.yo +5 -8
- package/std/glob.yo +30 -43
- package/std/http/client.yo +107 -120
- package/std/http/http.yo +106 -96
- package/std/http/index.yo +4 -6
- package/std/imm/list.yo +88 -93
- package/std/imm/map.yo +528 -464
- package/std/imm/set.yo +52 -57
- package/std/imm/sorted_map.yo +340 -286
- package/std/imm/sorted_set.yo +57 -63
- package/std/imm/string.yo +404 -345
- package/std/imm/vec.yo +173 -181
- package/std/io/reader.yo +3 -6
- package/std/io/writer.yo +4 -8
- package/std/libc/assert.yo +5 -9
- package/std/libc/ctype.yo +32 -22
- package/std/libc/dirent.yo +26 -25
- package/std/libc/errno.yo +164 -90
- package/std/libc/fcntl.yo +52 -45
- package/std/libc/float.yo +66 -44
- package/std/libc/limits.yo +42 -33
- package/std/libc/math.yo +53 -82
- package/std/libc/signal.yo +72 -47
- package/std/libc/stdatomic.yo +217 -188
- package/std/libc/stdint.yo +5 -29
- package/std/libc/stdio.yo +5 -29
- package/std/libc/stdlib.yo +32 -39
- package/std/libc/string.yo +5 -23
- package/std/libc/sys/stat.yo +58 -56
- package/std/libc/time.yo +5 -19
- package/std/libc/unistd.yo +5 -20
- package/std/libc/wctype.yo +6 -9
- package/std/libc/windows.yo +26 -30
- package/std/log.yo +41 -55
- package/std/net/addr.yo +102 -97
- package/std/net/dns.yo +27 -28
- package/std/net/errors.yo +50 -49
- package/std/net/tcp.yo +113 -124
- package/std/net/udp.yo +55 -66
- package/std/os/env.yo +35 -33
- package/std/os/signal.yo +15 -25
- package/std/path.yo +276 -311
- package/std/prelude.yo +6316 -4333
- package/std/process/command.yo +87 -103
- package/std/process/index.yo +12 -31
- package/std/regex/compiler.yo +196 -95
- package/std/regex/flags.yo +58 -39
- package/std/regex/index.yo +157 -173
- package/std/regex/match.yo +20 -31
- package/std/regex/node.yo +134 -152
- package/std/regex/parser.yo +283 -259
- package/std/regex/unicode.yo +172 -202
- package/std/regex/vm.yo +155 -171
- package/std/string/index.yo +5 -7
- package/std/string/rune.yo +45 -55
- package/std/string/string.yo +937 -964
- package/std/string/string_builder.yo +94 -104
- package/std/string/unicode.yo +46 -64
- package/std/sync/channel.yo +72 -73
- package/std/sync/cond.yo +31 -36
- package/std/sync/mutex.yo +30 -32
- package/std/sync/once.yo +13 -16
- package/std/sync/rwlock.yo +26 -31
- package/std/sync/waitgroup.yo +20 -25
- package/std/sys/advise.yo +16 -24
- package/std/sys/bufio/buf_reader.yo +77 -93
- package/std/sys/bufio/buf_writer.yo +52 -65
- package/std/sys/clock.yo +4 -9
- package/std/sys/constants.yo +77 -61
- package/std/sys/copy.yo +4 -10
- package/std/sys/dir.yo +26 -43
- package/std/sys/dns.yo +41 -61
- package/std/sys/errors.yo +95 -103
- package/std/sys/events.yo +45 -57
- package/std/sys/externs.yo +319 -267
- package/std/sys/fallocate.yo +7 -11
- package/std/sys/fcntl.yo +14 -22
- package/std/sys/file.yo +26 -40
- package/std/sys/future.yo +5 -8
- package/std/sys/iov.yo +12 -25
- package/std/sys/lock.yo +12 -13
- package/std/sys/mmap.yo +38 -43
- package/std/sys/path.yo +3 -8
- package/std/sys/perm.yo +7 -21
- package/std/sys/pipe.yo +5 -12
- package/std/sys/process.yo +23 -29
- package/std/sys/seek.yo +10 -12
- package/std/sys/signal.yo +7 -13
- package/std/sys/signals.yo +52 -35
- package/std/sys/socket.yo +63 -58
- package/std/sys/socketpair.yo +3 -6
- package/std/sys/sockinfo.yo +11 -20
- package/std/sys/statfs.yo +11 -34
- package/std/sys/statx.yo +25 -52
- package/std/sys/sysinfo.yo +15 -20
- package/std/sys/tcp.yo +62 -92
- package/std/sys/temp.yo +5 -9
- package/std/sys/time.yo +5 -15
- package/std/sys/timer.yo +6 -11
- package/std/sys/tty.yo +10 -18
- package/std/sys/udp.yo +22 -39
- package/std/sys/umask.yo +3 -6
- package/std/sys/unix.yo +33 -52
- package/std/testing/bench.yo +49 -52
- package/std/thread.yo +10 -15
- package/std/time/datetime.yo +105 -89
- package/std/time/duration.yo +43 -56
- package/std/time/instant.yo +13 -18
- package/std/time/sleep.yo +5 -9
- package/std/url/index.yo +184 -209
- package/std/worker.yo +6 -10
- package/out/types/src/evaluator/calls/module-type.d.ts +0 -11
- package/out/types/src/evaluator/types/module.d.ts +0 -19
|
@@ -1,200 +1,185 @@
|
|
|
1
1
|
//! Doubly-linked list with O(1) push/pop at both ends.
|
|
2
|
-
|
|
3
2
|
/// Error variants for LinkedList operations.
|
|
4
3
|
LinkedListError :: enum(
|
|
5
4
|
/// Index is out of bounds for the current length.
|
|
6
|
-
IndexOutOfBounds(index: usize, length: usize),
|
|
5
|
+
IndexOutOfBounds(index : usize, length : usize),
|
|
7
6
|
/// Attempted to access an element from an empty list.
|
|
8
7
|
EmptyList
|
|
9
8
|
);
|
|
10
|
-
|
|
11
9
|
/// Internal node for the linked list. Reference-counted via `object`.
|
|
12
|
-
Node :: (fn(comptime(T): Type) -> comptime(Type))
|
|
10
|
+
Node :: (fn(comptime(T) : Type) -> comptime(Type))(
|
|
13
11
|
object(
|
|
14
12
|
value : T,
|
|
15
13
|
next : Option(Self),
|
|
16
14
|
prev : Option(Self)
|
|
17
15
|
)
|
|
18
|
-
;
|
|
19
|
-
|
|
16
|
+
);
|
|
20
17
|
/// Doubly-linked list with O(1) push/pop at both ends.
|
|
21
|
-
LinkedList :: (fn(comptime(T): Type) -> comptime(Type))
|
|
18
|
+
LinkedList :: (fn(comptime(T) : Type) -> comptime(Type))(
|
|
22
19
|
object(
|
|
23
20
|
head : Option(Node(T)),
|
|
24
21
|
tail : Option(Node(T)),
|
|
25
22
|
length : usize
|
|
26
23
|
)
|
|
27
|
-
;
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
);
|
|
25
|
+
impl(
|
|
26
|
+
forall(T : Type),
|
|
27
|
+
LinkedList(T),
|
|
30
28
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
* Create a new empty LinkedList
|
|
30
|
+
*/
|
|
33
31
|
new : (fn() -> Self)(
|
|
34
32
|
Self(
|
|
35
|
-
head
|
|
36
|
-
tail
|
|
37
|
-
length: usize(0)
|
|
33
|
+
head :.None,
|
|
34
|
+
tail :.None,
|
|
35
|
+
length : usize(0)
|
|
38
36
|
)
|
|
39
37
|
),
|
|
40
|
-
|
|
41
38
|
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
len : (fn(self: Self) -> usize)(
|
|
39
|
+
* Get the number of elements in the list
|
|
40
|
+
*/
|
|
41
|
+
len : (fn(self : Self) -> usize)(
|
|
45
42
|
self.length
|
|
46
43
|
),
|
|
47
|
-
|
|
48
44
|
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
is_empty : (fn(self: Self) -> bool)(
|
|
52
|
-
|
|
45
|
+
* Check if the list is empty
|
|
46
|
+
*/
|
|
47
|
+
is_empty : (fn(self : Self) -> bool)(
|
|
48
|
+
self.length == usize(0)
|
|
53
49
|
),
|
|
54
|
-
|
|
55
50
|
/**
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
push_front : (fn(self: Self, value: T) -> unit)(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
.Some(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
self.length = (self.length + usize(1));
|
|
78
|
-
}
|
|
79
|
-
),
|
|
80
|
-
|
|
51
|
+
* Push an element to the front of the list
|
|
52
|
+
* O(1) time complexity
|
|
53
|
+
*/
|
|
54
|
+
push_front : (fn(self : Self, value : T) -> unit)({
|
|
55
|
+
new_node := Node(T)(
|
|
56
|
+
value : value,
|
|
57
|
+
next : self.head,
|
|
58
|
+
prev :.None
|
|
59
|
+
);
|
|
60
|
+
match(
|
|
61
|
+
self.head,
|
|
62
|
+
.None => {
|
|
63
|
+
self.tail =.Some(new_node);
|
|
64
|
+
},
|
|
65
|
+
.Some(old_head) => {
|
|
66
|
+
old_head.prev =.Some(new_node);
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
self.head =.Some(new_node);
|
|
70
|
+
self.length = (self.length + usize(1));
|
|
71
|
+
}),
|
|
81
72
|
/**
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
push_back : (fn(self: Self, value: T) -> unit)(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
.Some(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
self.length = (self.length + usize(1));
|
|
104
|
-
}
|
|
105
|
-
),
|
|
106
|
-
|
|
73
|
+
* Push an element to the back of the list
|
|
74
|
+
* O(1) time complexity
|
|
75
|
+
*/
|
|
76
|
+
push_back : (fn(self : Self, value : T) -> unit)({
|
|
77
|
+
new_node := Node(T)(
|
|
78
|
+
value : value,
|
|
79
|
+
next :.None,
|
|
80
|
+
prev : self.tail
|
|
81
|
+
);
|
|
82
|
+
match(
|
|
83
|
+
self.tail,
|
|
84
|
+
.None => {
|
|
85
|
+
self.head =.Some(new_node);
|
|
86
|
+
},
|
|
87
|
+
.Some(old_tail) => {
|
|
88
|
+
old_tail.next =.Some(new_node);
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
self.tail =.Some(new_node);
|
|
92
|
+
self.length = (self.length + usize(1));
|
|
93
|
+
}),
|
|
107
94
|
/**
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
pop_front : (fn(self: Self) -> Option(T))(
|
|
112
|
-
match(
|
|
113
|
-
.
|
|
95
|
+
* Remove and return the element from the front
|
|
96
|
+
* O(1) time complexity
|
|
97
|
+
*/
|
|
98
|
+
pop_front : (fn(self : Self) -> Option(T))(
|
|
99
|
+
match(
|
|
100
|
+
self.head,
|
|
101
|
+
.None =>.None,
|
|
114
102
|
.Some(old_head) => {
|
|
115
103
|
value := old_head.value;
|
|
116
104
|
new_head_opt := old_head.next;
|
|
117
|
-
|
|
118
|
-
|
|
105
|
+
match(
|
|
106
|
+
new_head_opt,
|
|
119
107
|
.None => {
|
|
120
|
-
self.head
|
|
121
|
-
self.tail
|
|
108
|
+
self.head =.None;
|
|
109
|
+
self.tail =.None;
|
|
122
110
|
},
|
|
123
111
|
.Some(new_head) => {
|
|
124
|
-
new_head.prev
|
|
125
|
-
self.head
|
|
112
|
+
new_head.prev =.None;
|
|
113
|
+
self.head =.Some(new_head);
|
|
126
114
|
}
|
|
127
115
|
);
|
|
128
|
-
|
|
129
116
|
self.length = (self.length - usize(1));
|
|
130
117
|
.Some(value)
|
|
131
118
|
}
|
|
132
119
|
)
|
|
133
120
|
),
|
|
134
|
-
|
|
135
121
|
/**
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
pop_back : (fn(self: Self) -> Option(T))(
|
|
140
|
-
match(
|
|
141
|
-
.
|
|
122
|
+
* Remove and return the element from the back
|
|
123
|
+
* O(1) time complexity
|
|
124
|
+
*/
|
|
125
|
+
pop_back : (fn(self : Self) -> Option(T))(
|
|
126
|
+
match(
|
|
127
|
+
self.tail,
|
|
128
|
+
.None =>.None,
|
|
142
129
|
.Some(old_tail) => {
|
|
143
130
|
value := old_tail.value;
|
|
144
131
|
new_tail_opt := old_tail.prev;
|
|
145
|
-
|
|
146
|
-
|
|
132
|
+
match(
|
|
133
|
+
new_tail_opt,
|
|
147
134
|
.None => {
|
|
148
|
-
self.head
|
|
149
|
-
self.tail
|
|
135
|
+
self.head =.None;
|
|
136
|
+
self.tail =.None;
|
|
150
137
|
},
|
|
151
138
|
.Some(new_tail) => {
|
|
152
|
-
new_tail.next
|
|
153
|
-
self.tail
|
|
139
|
+
new_tail.next =.None;
|
|
140
|
+
self.tail =.Some(new_tail);
|
|
154
141
|
}
|
|
155
142
|
);
|
|
156
|
-
|
|
157
143
|
self.length = (self.length - usize(1));
|
|
158
144
|
.Some(value)
|
|
159
145
|
}
|
|
160
146
|
)
|
|
161
147
|
),
|
|
162
|
-
|
|
163
148
|
/**
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
front : (fn(self: Self) -> Option(T))(
|
|
167
|
-
match(
|
|
168
|
-
.
|
|
169
|
-
.
|
|
149
|
+
* Get reference to the front element without removing it
|
|
150
|
+
*/
|
|
151
|
+
front : (fn(self : Self) -> Option(T))(
|
|
152
|
+
match(
|
|
153
|
+
self.head,
|
|
154
|
+
.None =>.None,
|
|
155
|
+
.Some(head_node) =>.Some(head_node.value)
|
|
170
156
|
)
|
|
171
157
|
),
|
|
172
|
-
|
|
173
158
|
/**
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
back : (fn(self: Self) -> Option(T))(
|
|
177
|
-
match(
|
|
178
|
-
.
|
|
179
|
-
.
|
|
159
|
+
* Get reference to the back element without removing it
|
|
160
|
+
*/
|
|
161
|
+
back : (fn(self : Self) -> Option(T))(
|
|
162
|
+
match(
|
|
163
|
+
self.tail,
|
|
164
|
+
.None =>.None,
|
|
165
|
+
.Some(tail_node) =>.Some(tail_node.value)
|
|
180
166
|
)
|
|
181
167
|
),
|
|
182
|
-
|
|
183
168
|
/**
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
get : (fn(self: Self, index: usize) -> Option(T))(
|
|
169
|
+
* Get element at a specific index
|
|
170
|
+
* O(n) time complexity
|
|
171
|
+
*/
|
|
172
|
+
get : (fn(self : Self, index : usize) -> Option(T))(
|
|
188
173
|
cond(
|
|
189
|
-
(index >= self.length)
|
|
174
|
+
(index >= self.length) =>.None,
|
|
190
175
|
true => {
|
|
191
176
|
current_opt := self.head;
|
|
192
177
|
i := usize(0);
|
|
193
|
-
|
|
194
178
|
while(i < index, i = (i + usize(1)), {
|
|
195
|
-
match(
|
|
179
|
+
match(
|
|
180
|
+
current_opt,
|
|
196
181
|
.None => {
|
|
197
|
-
return
|
|
182
|
+
return(.None);
|
|
198
183
|
},
|
|
199
184
|
.Some(current) => {
|
|
200
185
|
current_opt = current.next;
|
|
@@ -202,22 +187,21 @@ impl(forall(T : Type), LinkedList(T),
|
|
|
202
187
|
}
|
|
203
188
|
);
|
|
204
189
|
});
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
.None
|
|
208
|
-
.Some(node)
|
|
190
|
+
match(
|
|
191
|
+
current_opt,
|
|
192
|
+
.None =>.None,
|
|
193
|
+
.Some(node) =>.Some(node.value)
|
|
209
194
|
)
|
|
210
195
|
}
|
|
211
196
|
)
|
|
212
197
|
),
|
|
213
|
-
|
|
214
198
|
/**
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
insert : (fn(self: Self, index: usize, value: T) -> Result(unit, LinkedListError))(
|
|
199
|
+
* Insert element at a specific index
|
|
200
|
+
* O(n) time complexity
|
|
201
|
+
*/
|
|
202
|
+
insert : (fn(self : Self, index : usize, value : T) -> Result(unit, LinkedListError))(
|
|
219
203
|
cond(
|
|
220
|
-
(index > self.length)
|
|
204
|
+
(index > self.length) =>.Err(.IndexOutOfBounds(index : index, length : self.length)),
|
|
221
205
|
(index == usize(0)) => {
|
|
222
206
|
self.push_front(value);
|
|
223
207
|
.Ok(())
|
|
@@ -229,40 +213,37 @@ impl(forall(T : Type), LinkedList(T),
|
|
|
229
213
|
true => {
|
|
230
214
|
current_opt := self.head;
|
|
231
215
|
i := usize(0);
|
|
232
|
-
|
|
233
216
|
while(i < index, i = (i + usize(1)), {
|
|
234
|
-
match(
|
|
217
|
+
match(
|
|
218
|
+
current_opt,
|
|
235
219
|
.None => {
|
|
236
|
-
return
|
|
220
|
+
return(.Err(.IndexOutOfBounds(index : index, length : self.length)));
|
|
237
221
|
},
|
|
238
222
|
.Some(current) => {
|
|
239
223
|
current_opt = current.next;
|
|
240
224
|
}
|
|
241
225
|
);
|
|
242
226
|
});
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
.None
|
|
227
|
+
match(
|
|
228
|
+
current_opt,
|
|
229
|
+
.None =>.Err(.IndexOutOfBounds(index : index, length : self.length)),
|
|
246
230
|
.Some(current) => {
|
|
247
231
|
prev_opt := current.prev;
|
|
248
|
-
|
|
249
232
|
new_node := Node(T)(
|
|
250
|
-
value: value,
|
|
251
|
-
next
|
|
252
|
-
prev: prev_opt
|
|
233
|
+
value : value,
|
|
234
|
+
next :.Some(current),
|
|
235
|
+
prev : prev_opt
|
|
253
236
|
);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
match(prev_opt,
|
|
237
|
+
current.prev =.Some(new_node);
|
|
238
|
+
match(
|
|
239
|
+
prev_opt,
|
|
258
240
|
.None => {
|
|
259
|
-
self.head
|
|
241
|
+
self.head =.Some(new_node);
|
|
260
242
|
},
|
|
261
243
|
.Some(prev_node) => {
|
|
262
|
-
prev_node.next
|
|
244
|
+
prev_node.next =.Some(new_node);
|
|
263
245
|
}
|
|
264
246
|
);
|
|
265
|
-
|
|
266
247
|
self.length = (self.length + usize(1));
|
|
267
248
|
.Ok(())
|
|
268
249
|
}
|
|
@@ -270,47 +251,48 @@ impl(forall(T : Type), LinkedList(T),
|
|
|
270
251
|
}
|
|
271
252
|
)
|
|
272
253
|
),
|
|
273
|
-
|
|
274
254
|
/**
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
remove : (fn(self: Self, index: usize) -> Result(T, LinkedListError))(
|
|
255
|
+
* Remove element at a specific index
|
|
256
|
+
* O(n) time complexity
|
|
257
|
+
*/
|
|
258
|
+
remove : (fn(self : Self, index : usize) -> Result(T, LinkedListError))(
|
|
279
259
|
cond(
|
|
280
|
-
(index >= self.length)
|
|
260
|
+
(index >= self.length) =>.Err(.IndexOutOfBounds(index : index, length : self.length)),
|
|
281
261
|
(index == usize(0)) =>
|
|
282
|
-
match(
|
|
283
|
-
.
|
|
284
|
-
.
|
|
262
|
+
match(
|
|
263
|
+
self.pop_front(),
|
|
264
|
+
.Some(value) =>.Ok(value),
|
|
265
|
+
.None =>.Err(.EmptyList)
|
|
285
266
|
),
|
|
286
267
|
(index == (self.length - usize(1))) =>
|
|
287
|
-
match(
|
|
288
|
-
.
|
|
289
|
-
.
|
|
268
|
+
match(
|
|
269
|
+
self.pop_back(),
|
|
270
|
+
.Some(value) =>.Ok(value),
|
|
271
|
+
.None =>.Err(.EmptyList)
|
|
290
272
|
),
|
|
291
273
|
true => {
|
|
292
274
|
current_opt := self.head;
|
|
293
275
|
i := usize(0);
|
|
294
|
-
|
|
295
276
|
while(i < index, i = (i + usize(1)), {
|
|
296
|
-
match(
|
|
277
|
+
match(
|
|
278
|
+
current_opt,
|
|
297
279
|
.None => {
|
|
298
|
-
return
|
|
280
|
+
return(.Err(.IndexOutOfBounds(index : index, length : self.length)));
|
|
299
281
|
},
|
|
300
282
|
.Some(current) => {
|
|
301
283
|
current_opt = current.next;
|
|
302
284
|
}
|
|
303
285
|
);
|
|
304
286
|
});
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
.None
|
|
287
|
+
match(
|
|
288
|
+
current_opt,
|
|
289
|
+
.None =>.Err(.IndexOutOfBounds(index : index, length : self.length)),
|
|
308
290
|
.Some(node_to_remove) => {
|
|
309
291
|
value := node_to_remove.value;
|
|
310
292
|
prev_opt := node_to_remove.prev;
|
|
311
293
|
next_opt := node_to_remove.next;
|
|
312
|
-
|
|
313
|
-
|
|
294
|
+
match(
|
|
295
|
+
prev_opt,
|
|
314
296
|
.Some(prev_node) => {
|
|
315
297
|
prev_node.next = next_opt;
|
|
316
298
|
},
|
|
@@ -318,8 +300,8 @@ impl(forall(T : Type), LinkedList(T),
|
|
|
318
300
|
self.head = next_opt;
|
|
319
301
|
}
|
|
320
302
|
);
|
|
321
|
-
|
|
322
|
-
|
|
303
|
+
match(
|
|
304
|
+
next_opt,
|
|
323
305
|
.Some(next_node) => {
|
|
324
306
|
next_node.prev = prev_opt;
|
|
325
307
|
},
|
|
@@ -327,7 +309,6 @@ impl(forall(T : Type), LinkedList(T),
|
|
|
327
309
|
self.tail = prev_opt;
|
|
328
310
|
}
|
|
329
311
|
);
|
|
330
|
-
|
|
331
312
|
self.length = (self.length - usize(1));
|
|
332
313
|
.Ok(value)
|
|
333
314
|
}
|
|
@@ -335,154 +316,155 @@ impl(forall(T : Type), LinkedList(T),
|
|
|
335
316
|
}
|
|
336
317
|
)
|
|
337
318
|
),
|
|
338
|
-
|
|
339
319
|
/**
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
clear : (fn(self: Self) -> unit)(
|
|
343
|
-
{
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
348
|
-
),
|
|
349
|
-
|
|
320
|
+
* Clear all elements from the list
|
|
321
|
+
*/
|
|
322
|
+
clear : (fn(self : Self) -> unit)({
|
|
323
|
+
while(self.length > usize(0), {
|
|
324
|
+
_ := self.pop_front();
|
|
325
|
+
});
|
|
326
|
+
}),
|
|
350
327
|
/**
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
contains : (
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
),
|
|
384
|
-
|
|
328
|
+
* Check if the list contains a value
|
|
329
|
+
* O(n) time complexity
|
|
330
|
+
*/
|
|
331
|
+
contains : (
|
|
332
|
+
fn(
|
|
333
|
+
self : Self,
|
|
334
|
+
value : T,
|
|
335
|
+
where(T <: Eq(T))
|
|
336
|
+
) -> bool
|
|
337
|
+
)({
|
|
338
|
+
current_opt := self.head;
|
|
339
|
+
i := usize(0);
|
|
340
|
+
while(i < self.length, i = (i + usize(1)), {
|
|
341
|
+
match(
|
|
342
|
+
current_opt,
|
|
343
|
+
.None => {
|
|
344
|
+
return(false);
|
|
345
|
+
},
|
|
346
|
+
.Some(current) => {
|
|
347
|
+
cond(
|
|
348
|
+
(current.value == value) => {
|
|
349
|
+
return(true);
|
|
350
|
+
},
|
|
351
|
+
true => {
|
|
352
|
+
current_opt = current.next;
|
|
353
|
+
}
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
false
|
|
359
|
+
}),
|
|
385
360
|
/**
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
reverse : (fn(self: Self) -> unit)(
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
self.tail = temp_head;
|
|
411
|
-
}
|
|
412
|
-
)
|
|
361
|
+
* Reverse the list in place
|
|
362
|
+
* O(n) time complexity
|
|
363
|
+
*/
|
|
364
|
+
reverse : (fn(self : Self) -> unit)({
|
|
365
|
+
current_opt := self.head;
|
|
366
|
+
i := usize(0);
|
|
367
|
+
while(i < self.length, i = (i + usize(1)), {
|
|
368
|
+
match(
|
|
369
|
+
current_opt,
|
|
370
|
+
.None => {
|
|
371
|
+
return;
|
|
372
|
+
},
|
|
373
|
+
.Some(current) => {
|
|
374
|
+
temp := current.next;
|
|
375
|
+
current.next = current.prev;
|
|
376
|
+
current.prev = temp;
|
|
377
|
+
current_opt = temp;
|
|
378
|
+
}
|
|
379
|
+
);
|
|
380
|
+
});
|
|
381
|
+
temp_head := self.head;
|
|
382
|
+
self.head = self.tail;
|
|
383
|
+
self.tail = temp_head;
|
|
384
|
+
})
|
|
413
385
|
);
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
386
|
+
impl(
|
|
387
|
+
forall(T : Type),
|
|
388
|
+
LinkedList(T),
|
|
389
|
+
Dispose(
|
|
390
|
+
/**
|
|
417
391
|
* RAII destructor - automatically called when LinkedList goes out of scope
|
|
418
392
|
*/
|
|
419
|
-
|
|
420
|
-
|
|
393
|
+
dispose : (fn(self : Self) -> unit)(
|
|
394
|
+
self.clear()
|
|
395
|
+
)
|
|
421
396
|
)
|
|
422
|
-
)
|
|
423
|
-
|
|
397
|
+
);
|
|
424
398
|
/**
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
399
|
+
* Value iterator for LinkedList - yields elements by value (T)
|
|
400
|
+
* Traverses the linked list following node.next pointers.
|
|
401
|
+
*/
|
|
428
402
|
LinkedListIter :: (fn(comptime(T) : Type) -> comptime(Type))(
|
|
429
403
|
struct(
|
|
430
404
|
_current : Option(Node(T))
|
|
431
405
|
)
|
|
432
406
|
);
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
self._current
|
|
441
|
-
.
|
|
442
|
-
|
|
407
|
+
impl(
|
|
408
|
+
forall(T : Type),
|
|
409
|
+
LinkedListIter(T),
|
|
410
|
+
Iterator(
|
|
411
|
+
Item : T,
|
|
412
|
+
next : (fn(self : *(Self)) -> Option(T))(
|
|
413
|
+
match(
|
|
414
|
+
self._current,
|
|
415
|
+
.None =>.None,
|
|
416
|
+
.Some(node) => {
|
|
417
|
+
self._current = node.next;
|
|
418
|
+
.Some(node.value)
|
|
419
|
+
}
|
|
420
|
+
)
|
|
443
421
|
)
|
|
444
422
|
)
|
|
445
|
-
)
|
|
446
|
-
|
|
447
|
-
|
|
423
|
+
);
|
|
424
|
+
impl(
|
|
425
|
+
forall(T : Type),
|
|
426
|
+
LinkedList(T),
|
|
448
427
|
into_iter : (fn(self : Self) -> LinkedListIter(T))(
|
|
449
|
-
LinkedListIter(T)(_current: self.head)
|
|
428
|
+
LinkedListIter(T)(_current : self.head)
|
|
450
429
|
)
|
|
451
430
|
);
|
|
452
|
-
|
|
453
431
|
/**
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
432
|
+
* Pointer iterator for LinkedList - yields pointers to elements (*(T))
|
|
433
|
+
* Yields &(node.value) for each node. Pointers are valid as long as
|
|
434
|
+
* the list is not modified during iteration.
|
|
435
|
+
*/
|
|
458
436
|
LinkedListIterPtr :: (fn(comptime(T) : Type) -> comptime(Type))(
|
|
459
437
|
struct(
|
|
460
438
|
_current : Option(Node(T))
|
|
461
439
|
)
|
|
462
440
|
);
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
self._current
|
|
471
|
-
.
|
|
472
|
-
|
|
441
|
+
impl(
|
|
442
|
+
forall(T : Type),
|
|
443
|
+
LinkedListIterPtr(T),
|
|
444
|
+
Iterator(
|
|
445
|
+
Item : *(T),
|
|
446
|
+
next : (fn(self : *(Self)) -> Option(*(T)))(
|
|
447
|
+
match(
|
|
448
|
+
self._current,
|
|
449
|
+
.None =>.None,
|
|
450
|
+
.Some(node) => {
|
|
451
|
+
self._current = node.next;
|
|
452
|
+
.Some(&(node.value))
|
|
453
|
+
}
|
|
454
|
+
)
|
|
473
455
|
)
|
|
474
456
|
)
|
|
475
|
-
)
|
|
476
|
-
|
|
477
|
-
|
|
457
|
+
);
|
|
458
|
+
impl(
|
|
459
|
+
forall(T : Type),
|
|
460
|
+
LinkedList(T),
|
|
478
461
|
iter : (fn(self : *(Self)) -> LinkedListIterPtr(T))(
|
|
479
|
-
LinkedListIterPtr(T)(_current: self.*.head)
|
|
462
|
+
LinkedListIterPtr(T)(_current : self.*.head)
|
|
480
463
|
)
|
|
481
464
|
);
|
|
482
|
-
|
|
483
|
-
export
|
|
465
|
+
export(
|
|
484
466
|
LinkedList,
|
|
485
467
|
LinkedListError,
|
|
486
468
|
LinkedListIter,
|
|
487
469
|
LinkedListIterPtr
|
|
488
|
-
;
|
|
470
|
+
);
|