-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
linked-list.spice
313 lines (267 loc) · 7.75 KB
/
linked-list.spice
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import "std/type/result";
import "std/type/error";
import "std/iterator/iterable";
import "std/iterator/iterator";
import "std/data/pair";
// Add generic type definitions
type T dyn;
type Numeric int|long|short;
/**
* Node of a LinkedList
*/
type Node<T> struct {
T value
heap Node<T>* next = nil<heap Node<T>*>
}
p Node.ctor(const T& value) {
this.value = value;
}
/**
* A linked list is a common, dynamically resizable data structure to store uniform data in order.
* It is characterized by the pointer for each item, pointing to the next one.
*
* E.g. for a LinkedList<int>:
* 1234 -> 4567 -> 7890 -> 4567 -> nil<int*>
* tail head
*
* Time complexity:
* Insert: O(1)
* Delete: O(1)
* Search: O(n)
*
* Beware that each add operation allocates memory and every remove operation frees memory.
*/
public type LinkedList<T> struct : IIterable<T> {
heap Node<T>* tail = nil<heap Node<T>*>
heap Node<T>* head = nil<heap Node<T>*>
unsigned long size = 0l
}
public p LinkedList.ctor() {}
public p LinkedList.pushBack(const T& value) {
// Create new node
heap Node<T>* newNode = this.createNode(value);
// Insert at head
if this.isEmpty() {
this.head = this.tail = newNode;
} else {
this.head.next = newNode; // Link the new node to the previous one
this.head = newNode; // Set the head to the new node
}
this.size++;
}
public p LinkedList.pushFront(const T& value) {
// Create new node
heap Node<T>* newNode = this.createNode(value);
// Insert at tail
if this.isEmpty() {
this.head = this.tail = newNode;
} else {
newNode.next = this.tail; // Link the next node to the new one
this.tail = newNode; // Set the tail to the new node
}
this.size++;
}
public p LinkedList.insertAt(unsigned long idx, const T& value) {
// Abort if the index is out of bounds
if idx < 0l || idx >= this.size { return; }
// Create new node
heap Node<T>* newNode = this.createNode(value);
if this.isEmpty() {
this.head = this.tail = newNode;
} else if idx == 0l {
newNode.next = this.tail; // Link the next node to the new one
this.tail = newNode; // Set the tail to the new node
} else {
heap Node<T>* curr = this.tail;
for unsigned long i = 0l; i < idx - 1l; i++ {
curr = curr.next;
}
newNode.next = curr.next; // Link the next node to the new one
curr.next = newNode; // Link the new node to the previous one
}
this.size++;
}
public p LinkedList.insertAt(unsigned int idx, const T& value) {
this.insertAt((unsigned long) idx, value);
}
public p LinkedList.remove(const T& valueToRemove) {
// Abort if the list is already empty
if this.isEmpty() { return; }
if this.tail.value == valueToRemove {
heap Node<T>* temp = this.tail;
this.tail = this.tail.next;
unsafe {
sDealloc((heap byte*) temp);
}
this.size--;
return;
}
heap Node<T>* curr = this.tail;
while curr.next != nil<heap Node<T>*> && curr.next.value != valueToRemove {
curr = curr.next;
}
if curr.next == nil<heap Node<T>*> { return; }
heap Node<T>* temp = curr.next;
curr.next = curr.next.next;
unsafe {
sDealloc((heap byte*) temp);
}
this.size--;
}
public p LinkedList.removeAt(unsigned long idx) {
// Abort if the index is out of bounds
if idx < 0l || idx >= this.size { return; }
if idx == 0l {
heap Node<T>* temp = this.tail;
this.tail = this.tail.next;
unsafe {
sDealloc((heap byte*) temp);
}
this.size--;
return;
}
heap Node<T>* curr = this.tail;
for unsigned long i = 0l; i < idx - 1l; i++ {
curr = curr.next;
}
heap Node<T>* temp = curr.next;
curr.next = curr.next.next;
unsafe {
sDealloc((heap byte*) temp);
}
if idx == this.size - 1l {
this.head = curr;
}
this.size--;
}
public p LinkedList.removeAt(unsigned int index) {
this.removeAt((unsigned long) index);
}
public p LinkedList.removeFront() {
this.removeAt(0l);
}
public p LinkedList.removeBack() {
this.removeAt(this.size - 1l);
}
public inline f<unsigned long> LinkedList.getSize() {
return this.size;
}
public inline f<bool> LinkedList.isEmpty() {
return this.size == 0l;
}
public f<T&> LinkedList.get(unsigned long idx) {
// Abort if the index is out of bounds
if idx < 0 || idx >= this.size { panic(Error("Access index out of bound")); }
heap Node<T>* curr = this.tail;
for unsigned long i = 0l; i < idx; i++ {
curr = curr.next;
}
return curr.value;
}
public f<T&> LinkedList.get(unsigned int idx) {
return this.get((unsigned long) idx);
}
public inline f<T&> LinkedList.getFront() {
if this.isEmpty() { panic(Error("Access index out of bounds")); }
return this.tail.value;
}
public inline f<T&> LinkedList.getBack() {
if this.isEmpty() { panic(Error("Access index out of bounds")); }
return this.head.value;
}
f<heap Node<T>*> LinkedList.createNode(const T& value) {
heap Node<T>* newNode;
unsafe {
Result<heap byte*> allocResult = sAlloc(sizeof(type Node<T>));
newNode = (heap Node<T>*) allocResult.unwrap();
}
newNode.value = value;
newNode.next = nil<heap Node<T>*>;
return newNode;
}
/**
* Iterator to iterate over a vector data structure
*/
public type LinkedListIterator<T> struct : IIterator<T> {
LinkedList<T>& list
unsigned long cursor
}
public p LinkedListIterator.ctor<T>(LinkedList<T>& list) {
this.list = list;
this.cursor = 0l;
}
/**
* Returns the current item of the vector
*
* @return Reference to the current item
*/
public inline f<T&> LinkedListIterator.get() {
return this.list.get(this.cursor);
}
/**
* Returns the current index and the current item of the vector
*
* @return Pair of current index and reference to current item
*/
public inline f<Pair<unsigned long, T&>> LinkedListIterator.getIdx() {
return Pair<unsigned long, T&>(this.cursor, this.list.get(this.cursor));
}
/**
* Check if the iterator is valid
*
* @return true or false
*/
public inline f<bool> LinkedListIterator.isValid() {
return this.cursor < this.list.getSize();
}
/**
* Returns the current item of the vector iterator and moves the cursor to the next item
*/
public inline p LinkedListIterator.next() {
if !this.isValid() { panic(Error("Calling next() on invalid iterator")); }
this.cursor++;
}
/**
* Advances the cursor by one
*
* @param it LinkedListIterator
*/
public inline p operator++<T>(LinkedListIterator<T>& it) {
if it.cursor >= it.list.getSize() { panic(Error("Iterator out of bounds")); }
it.cursor++;
}
/**
* Move the cursor back by one
*
* @param it LinkedListIterator
*/
public inline p operator--<T>(LinkedListIterator<T>& it) {
if it.cursor <= 0 { panic(Error("Iterator out of bounds")); }
it.cursor--;
}
/**
* Advances the cursor by the given offset
*
* @param it LinkedListIterator
* @param offset Offset
*/
public inline p operator+=<T, Numeric>(LinkedListIterator<T>& it, Numeric offset) {
if it.cursor + offset >= it.list.getSize() || it.cursor + offset < 0 { panic(Error("Iterator out of bounds")); }
it.cursor += offset;
}
/**
* Move the cursor back by the given offset
*
* @param it LinkedListIterator
* @param offset Offset
*/
public inline p operator-=<T, Numeric>(LinkedListIterator<T>& it, Numeric offset) {
if it.cursor - offset >= it.list.getSize() || it.cursor - offset < 0 { panic(Error("Iterator out of bounds")); }
it.cursor -= offset;
}
/**
* Retrieve an forward iterator for the linked list
*/
public f<LinkedListIterator<T>> LinkedList.getIterator() {
return LinkedListIterator<T>(*this);
}