/
linkedlist.go
419 lines (347 loc) · 8.73 KB
/
linkedlist.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package collection
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
)
// LinkedList encapsulates a list where each entry is aware of only the next entry in the list.
type LinkedList[T any] struct {
first *llNode[T]
last *llNode[T]
length uint
key sync.RWMutex
}
type llNode[T any] struct {
payload T
next *llNode[T]
prev *llNode[T]
}
// Comparator is a function which evaluates two values to determine their relation to one another.
// - Zero is returned when `a` and `b` are equal.
// - Positive numbers are returned when `a` is greater than `b`.
// - Negative numbers are returned when `a` is less than `b`.
type Comparator[T any] func(a, b T) (int, error)
// A collection of errors that may be thrown by functions in this file.
var (
ErrUnexpectedType = errors.New("value was of an unexpected type")
)
// NewLinkedList instantiates a new LinkedList with the entries provided.
func NewLinkedList[T any](entries ...T) *LinkedList[T] {
list := &LinkedList[T]{}
for _, entry := range entries {
list.AddBack(entry)
}
return list
}
// AddBack creates an entry in the LinkedList that is logically at the back of the list.
func (list *LinkedList[T]) AddBack(entry T) {
list.key.Lock()
defer list.key.Unlock()
toAppend := &llNode[T]{
payload: entry,
}
list.addNodeBack(toAppend)
}
func (list *LinkedList[T]) addNodeBack(node *llNode[T]) {
list.length++
node.prev = list.last
if list.first == nil {
list.first = node
list.last = node
return
}
list.last.next = node
list.last = node
}
// AddFront creates an entry in the LinkedList that is logically at the front of the list.
func (list *LinkedList[T]) AddFront(entry T) {
toAppend := &llNode[T]{
payload: entry,
}
list.key.Lock()
defer list.key.Unlock()
list.addNodeFront(toAppend)
}
func (list *LinkedList[T]) addNodeFront(node *llNode[T]) {
list.length++
node.next = list.first
if list.first == nil {
list.last = node
} else {
list.first.prev = node
}
list.first = node
}
// Enumerate creates a new instance of Enumerable which can be executed on.
func (list *LinkedList[T]) Enumerate(ctx context.Context) Enumerator[T] {
retval := make(chan T)
go func() {
list.key.RLock()
defer list.key.RUnlock()
defer close(retval)
current := list.first
for current != nil {
select {
case retval <- current.payload:
break
case <-ctx.Done():
return
}
current = current.next
}
}()
return retval
}
// Get finds the value from the LinkedList.
// pos is expressed as a zero-based index begining from the 'front' of the list.
func (list *LinkedList[T]) Get(pos uint) (T, bool) {
list.key.RLock()
defer list.key.RUnlock()
node, ok := get(list.first, pos)
if ok {
return node.payload, true
}
return *new(T), false
}
// IsEmpty tests the list to determine if it is populate or not.
func (list *LinkedList[T]) IsEmpty() bool {
list.key.RLock()
defer list.key.RUnlock()
return list.first == nil
}
// Length returns the number of elements present in the LinkedList.
func (list *LinkedList[T]) Length() uint {
list.key.RLock()
defer list.key.RUnlock()
return list.length
}
// PeekBack returns the entry logicall stored at the back of the list without removing it.
func (list *LinkedList[T]) PeekBack() (T, bool) {
list.key.RLock()
defer list.key.RUnlock()
if list.last == nil {
return *new(T), false
}
return list.last.payload, true
}
// PeekFront returns the entry logically stored at the front of this list without removing it.
func (list *LinkedList[T]) PeekFront() (T, bool) {
list.key.RLock()
defer list.key.RUnlock()
if list.first == nil {
return *new(T), false
}
return list.first.payload, true
}
// RemoveFront returns the entry logically stored at the front of this list and removes it.
func (list *LinkedList[T]) RemoveFront() (T, bool) {
list.key.Lock()
defer list.key.Unlock()
if list.first == nil {
return *new(T), false
}
retval := list.first.payload
list.first = list.first.next
list.length--
if 0 == list.length {
list.last = nil
}
return retval, true
}
// RemoveBack returns the entry logically stored at the back of this list and removes it.
func (list *LinkedList[T]) RemoveBack() (T, bool) {
list.key.Lock()
defer list.key.Unlock()
if list.last == nil {
return *new(T), false
}
retval := list.last.payload
list.length--
if list.length == 0 {
list.first = nil
} else {
list.last = list.last.prev
list.last.next = nil
}
return retval, true
}
// removeNode
func (list *LinkedList[T]) removeNode(target *llNode[T]) {
if target == nil {
return
}
if target.next != nil {
target.next.prev = target.prev
}
if target.prev != nil {
target.prev.next = target.next
}
list.length--
if list.length == 0 {
list.first = nil
list.last = nil
return
}
if list.first == target {
list.first = target.next
}
if list.last == target {
list.last = target.prev
}
}
// Sort rearranges the positions of the entries in this list so that they are
// ascending.
func (list *LinkedList[T]) Sort(comparator Comparator[T]) error {
list.key.Lock()
defer list.key.Unlock()
var err error
list.first, err = mergeSort(list.first, comparator)
if err != nil {
return err
}
list.last = findLast(list.first)
return err
}
// String prints upto the first fifteen elements of the list in string format.
func (list *LinkedList[T]) String() string {
list.key.RLock()
defer list.key.RUnlock()
builder := bytes.NewBufferString("[")
current := list.first
for i := 0; i < 15 && current != nil; i++ {
builder.WriteString(fmt.Sprintf("%v ", current.payload))
current = current.next
}
if current == nil || current.next == nil {
builder.Truncate(builder.Len() - 1)
} else {
builder.WriteString("...")
}
builder.WriteRune(']')
return builder.String()
}
// Swap switches the positions in which two values are stored in this list.
// x and y represent the indexes of the items that should be swapped.
func (list *LinkedList[T]) Swap(x, y uint) error {
list.key.Lock()
defer list.key.Unlock()
var xNode, yNode *llNode[T]
if temp, ok := get(list.first, x); ok {
xNode = temp
} else {
return fmt.Errorf("index out of bounds 'x', wanted less than %d got %d", list.length, x)
}
if temp, ok := get(list.first, y); ok {
yNode = temp
} else {
return fmt.Errorf("index out of bounds 'y', wanted less than %d got %d", list.length, y)
}
temp := xNode.payload
xNode.payload = yNode.payload
yNode.payload = temp
return nil
}
// ToSlice converts the contents of the LinkedList into a slice.
func (list *LinkedList[T]) ToSlice() []T {
return list.Enumerate(context.Background()).ToSlice()
}
func findLast[T any](head *llNode[T]) *llNode[T] {
if head == nil {
return nil
}
current := head
for current.next != nil {
current = current.next
}
return current
}
func get[T any](head *llNode[T], pos uint) (*llNode[T], bool) {
for i := uint(0); i < pos; i++ {
if head == nil {
return nil, false
}
head = head.next
}
return head, true
}
// merge takes two sorted lists and merges them into one sorted list.
// Behavior is undefined when you pass a non-sorted list as `left` or `right`
func merge[T any](left, right *llNode[T], comparator Comparator[T]) (first *llNode[T], err error) {
curLeft := left
curRight := right
var last *llNode[T]
appendResults := func(updated *llNode[T]) {
if last == nil {
last = updated
} else {
last.next = updated
last = last.next
}
if first == nil {
first = last
}
}
for curLeft != nil && curRight != nil {
var res int
if res, err = comparator(curLeft.payload, curRight.payload); nil != err {
break // Don't return, stitch the remaining elements back on.
} else if res < 0 {
appendResults(curLeft)
curLeft = curLeft.next
} else {
appendResults(curRight)
curRight = curRight.next
}
}
if curLeft != nil {
appendResults(curLeft)
}
if curRight != nil {
appendResults(curRight)
}
return
}
func mergeSort[T any](head *llNode[T], comparator Comparator[T]) (*llNode[T], error) {
if head == nil {
return nil, nil
}
left, right := split(head)
repair := func(left, right *llNode[T]) *llNode[T] {
lastLeft := findLast(left)
lastLeft.next = right
return left
}
var err error
if left != nil && left.next != nil {
left, err = mergeSort(left, comparator)
if err != nil {
return repair(left, right), err
}
}
if right != nil && right.next != nil {
right, err = mergeSort(right, comparator)
if err != nil {
return repair(left, right), err
}
}
return merge(left, right, comparator)
}
// split breaks a list in half.
func split[T any](head *llNode[T]) (left, right *llNode[T]) {
left = head
if head == nil || head.next == nil {
return
}
right = head
sprinter := head
prev := head
for sprinter != nil && sprinter.next != nil {
prev = right
right = right.next
sprinter = sprinter.next.next
}
prev.next = nil
return
}