-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.go
125 lines (107 loc) · 2.56 KB
/
lists.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
package core
import (
"container/list"
)
// ListContains checks if a container/list contains an element
func ListContains[T comparable](l *list.List, val T) bool {
return ListContainsFn(l, val, func(va, vb T) bool {
return va == vb
})
}
// ListContainsFn checks if a container/list contains an element
// that satisfies a given function
func ListContainsFn[T any](l *list.List, val T, eq func(T, T) bool) bool {
var found bool
if l != nil && eq != nil {
ListForEach(l, func(v T) bool {
found = eq(val, v)
return found
})
}
return found
}
// ListForEach calls a function for each value until told to stop
func ListForEach[T any](l *list.List, fn func(v T) bool) {
if l == nil || fn == nil {
return
}
ListForEachElement(l, func(e *list.Element) bool {
if v, ok := e.Value.(T); ok {
return fn(v)
}
return false
})
}
// ListForEachElement calls a function for each element until told to stop
func ListForEachElement(l *list.List, fn func(*list.Element) bool) {
if l == nil || fn == nil {
return
}
e, next := listIterStep(l.Front())
for e != nil {
if fn(e) {
break
}
e, next = listIterStep(next)
}
}
func listIterStep(ref *list.Element) (e *list.Element, next *list.Element) {
if ref != nil {
next = ref.Next()
}
return ref, next
}
// ListForEachBackward calls a function for each value until told to stop
func ListForEachBackward[T any](l *list.List, fn func(v T) bool) {
if l == nil || fn == nil {
return
}
ListForEachBackwardElement(l, func(e *list.Element) bool {
if v, ok := e.Value.(T); ok {
return fn(v)
}
return false
})
}
// ListForEachBackwardElement calls a function for each element until told to stop
func ListForEachBackwardElement(l *list.List, fn func(*list.Element) bool) {
if l == nil || fn == nil {
return
}
e, prev := listIterBackwardStep(l.Back())
for e != nil {
if fn(e) {
break
}
e, prev = listIterBackwardStep(prev)
}
}
func listIterBackwardStep(ref *list.Element) (e *list.Element, prev *list.Element) {
if ref != nil {
prev = ref.Prev()
}
return ref, prev
}
// ListCopy makes a shallow copy of a list
func ListCopy[T any](src *list.List) *list.List {
return ListCopyFn[T](src, nil)
}
// ListCopyFn makes a copy of a list using the given helper
func ListCopyFn[T any](src *list.List, fn func(v T) (T, bool)) *list.List {
if src == nil || src.Len() == 0 {
return list.New()
}
if fn == nil {
fn = func(v T) (T, bool) {
return v, true
}
}
out := list.New()
ListForEach(src, func(v0 T) bool {
if v1, ok := fn(v0); ok {
out.PushBack(v1)
}
return false
})
return out
}