-
Notifications
You must be signed in to change notification settings - Fork 5
/
linked_list.go
294 lines (237 loc) · 5.75 KB
/
linked_list.go
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
package linkedlist
import (
"fmt"
"iter"
"github.com/joetifa2003/mm-go/allocator"
)
var popEmptyMsg = "cannot pop empty linked list"
type linkedListNode[T any] struct {
value T
next *linkedListNode[T]
prev *linkedListNode[T]
}
// LinkedList a doubly-linked list.
// Note: can be a lot slower than Vector but sometimes faster in specific use cases
type LinkedList[T any] struct {
alloc allocator.Allocator
head *linkedListNode[T]
tail *linkedListNode[T]
length int
}
// New creates a new linked list.
func New[T any](alloc allocator.Allocator) *LinkedList[T] {
linkedList := allocator.Alloc[LinkedList[T]](alloc)
linkedList.alloc = alloc
return linkedList
}
func (ll *LinkedList[T]) init(value T) {
ll.head = allocator.Alloc[linkedListNode[T]](ll.alloc)
ll.head.value = value
ll.tail = ll.head
ll.length++
}
func (ll *LinkedList[T]) popLast() T {
value := ll.tail.value
allocator.Free(ll.alloc, ll.tail)
ll.tail = nil
ll.head = nil
ll.length--
return value
}
// PushBack pushes value T to the back of the linked list.
func (ll *LinkedList[T]) PushBack(value T) {
// initialize the linked list
if ll.head == nil && ll.tail == nil {
ll.init(value)
return
}
newNode := allocator.Alloc[linkedListNode[T]](ll.alloc)
newNode.value = value
newNode.prev = ll.tail
ll.tail.next = newNode
ll.tail = newNode
ll.length++
}
// PushFront pushes value T to the back of the linked list.
func (ll *LinkedList[T]) PushFront(value T) {
// initialize the linked list
if ll.head == nil && ll.tail == nil {
ll.init(value)
return
}
newNode := allocator.Alloc[linkedListNode[T]](ll.alloc)
newNode.value = value
newNode.next = ll.head
ll.head.prev = newNode
ll.head = newNode
ll.length++
}
// PopBack pops and returns value T from the back of the linked list.
func (ll *LinkedList[T]) PopBack() T {
if ll.length == 0 {
panic(popEmptyMsg)
}
if ll.length == 1 {
return ll.popLast()
}
value := ll.tail.value
newTail := ll.tail.prev
newTail.next = nil
allocator.Free(ll.alloc, ll.tail)
ll.tail = newTail
ll.length--
return value
}
// PopFront pops and returns value T from the front of the linked list.
func (ll *LinkedList[T]) PopFront() T {
if ll.length == 0 {
panic(popEmptyMsg)
}
if ll.length == 1 {
return ll.popLast()
}
value := ll.head.value
newHead := ll.head.next
newHead.prev = nil
allocator.Free(ll.alloc, ll.head)
ll.head = newHead
ll.length--
return value
}
// ForEach iterates through the linked list.
func (ll *LinkedList[T]) ForEach(f func(idx int, value T)) {
idx := 0
currentNode := ll.head
for currentNode != nil {
f(idx, currentNode.value)
currentNode = currentNode.next
idx++
}
}
// Iter returns an iterator over the linked list values.
func (ll *LinkedList[T]) Iter() iter.Seq2[int, T] {
return func(yield func(int, T) bool) {
idx := 0
currentNode := ll.head
for currentNode != nil {
if !yield(idx, currentNode.value) {
return
}
currentNode = currentNode.next
idx++
}
}
}
func (ll *LinkedList[T]) nodeAt(idx int) *linkedListNode[T] {
if idx >= ll.length {
panic(fmt.Sprintf("cannot index %d in a linked list with length %d", idx, ll.length))
}
i := 0
currentNode := ll.head
for i != idx {
currentNode = currentNode.next
i++
}
return currentNode
}
// At gets value T at idx.
func (ll *LinkedList[T]) At(idx int) T {
return ll.nodeAt(idx).value
}
// AtPtr gets a pointer to value T at idx.
func (ll *LinkedList[T]) AtPtr(idx int) *T {
return &ll.nodeAt(idx).value
}
// RemoveAt removes value T at specified index and returns it.
func (ll *LinkedList[T]) RemoveAt(idx int) T {
node := ll.nodeAt(idx)
if node.prev == nil {
return ll.PopFront()
}
if node.next == nil {
return ll.PopBack()
}
value := node.value
nextNode := node.next
prevNode := node.prev
nextNode.prev = prevNode
prevNode.next = nextNode
ll.length--
allocator.Free(ll.alloc, node)
return value
}
// Remove removes the first value T that pass the test implemented by the provided function.
// if the test succeeded it will return the value and true
func (ll *LinkedList[T]) Remove(f func(idx int, value T) bool) (value T, ok bool) {
i := 0
currentNode := ll.head
for currentNode != nil {
nextNode := currentNode.next
if f(i, currentNode.value) {
return ll.RemoveAt(i), true
}
currentNode = nextNode
i++
}
ok = false
return
}
// RemoveAll removes all values of T that pass the test implemented by the provided function.
func (ll *LinkedList[T]) RemoveAll(f func(idx int, value T) bool) []T {
res := []T{}
i := 0
currentNode := ll.head
for currentNode != nil {
nextNode := currentNode.next
if f(i, currentNode.value) {
res = append(res, ll.RemoveAt(i))
i--
}
currentNode = nextNode
i++
}
return res
}
// FindIndex returns the first index of value T that pass the test implemented by the provided function.
func (ll *LinkedList[T]) FindIndex(f func(value T) bool) (idx int, ok bool) {
i := 0
currentNode := ll.head
for currentNode != nil {
nextNode := currentNode.next
if f(currentNode.value) {
return i, true
}
currentNode = nextNode
i++
}
return 0, false
}
// FindIndex returns all indexes of value T that pass the test implemented by the provided function.
func (ll *LinkedList[T]) FindIndexes(f func(value T) bool) []int {
res := []int{}
i := 0
currentNode := ll.head
for currentNode != nil {
nextNode := currentNode.next
if f(currentNode.value) {
res = append(res, i)
}
currentNode = nextNode
i++
}
return res
}
// Len gets linked list length.
func (ll *LinkedList[T]) Len() int {
return ll.length
}
// Free frees the linked list.
func (ll *LinkedList[T]) Free() {
currentNode := ll.head
for currentNode != nil {
nextNode := currentNode.next
allocator.Free(ll.alloc, currentNode)
currentNode = nextNode
}
allocator.Free(ll.alloc, ll)
}