-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.go
More file actions
160 lines (135 loc) · 3.17 KB
/
Copy pathlist.go
File metadata and controls
160 lines (135 loc) · 3.17 KB
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
package sync
import "iter"
type List[T comparable] struct {
Locking
values []T
}
// ----------------- Collection functions -----------------
func (s *List[T]) Append(value T) {
defer s.Lock()()
s.values = append(s.values, value)
}
func (s *List[T]) Remove(value T) {
defer s.Lock()()
idx := s.indexOf(value)
if idx >= 0 {
_, _ = s.removeIndex(idx)
}
}
func (s *List[T]) Contains(value T) bool {
defer s.RLock()()
return s.indexOf(value) >= 0
}
func (s *List[T]) Len() int {
defer s.RLock()()
return len(s.values)
}
// ----------------- Queue functions -----------------
func (s *List[T]) Enqueue(value T) {
s.Append(value)
}
func (s *List[T]) Dequeue() (value T, ok bool) {
defer s.Lock()()
if len(s.values) == 0 {
return value, false
}
value = (s.values)[0]
_, _ = s.removeIndex(0)
return value, true
}
// ----------------- Stack functions -----------------
func (s *List[T]) Push(value T) {
s.Append(value)
}
func (s *List[T]) Pop() (value T, ok bool) {
defer s.Lock()()
last := len(s.values) - 1
if last >= 0 {
v := (s.values)[last]
s.values = (s.values)[0:last]
return v, true
}
return value, false
}
func (s *List[T]) Peek() (value T, ok bool) {
defer s.RLock()()
last := len(s.values) - 1
if last >= 0 {
return (s.values)[last], true
}
return value, false
}
// ----------------- Iterator functions -----------------
// Seq is an iter.Seq compatible iterator function with a read lock, as such it is not possible to
// modify this list during the loop -- use Values() to obtain a copy for those purposes
func (s *List[T]) Seq(fn func(value T) bool) {
defer s.RLock()()
for _, v := range s.values {
if !fn(v) {
return
}
}
}
// ----------------- other utility functions -----------------
// Values returns a slice containing all the values at the time of the call, this should be used
// sparingly as it is only a snapshot of the current values
func (s *List[T]) Values() []T {
defer s.RLock()()
return s.copyValues()
}
// copyValues creates a copy of the values and returns it, without any locking
func (s *List[T]) copyValues() []T {
out := make([]T, len(s.values))
copy(out, s.values)
return out
}
// Clear removes all values
func (s *List[T]) Clear() {
defer s.Lock()()
s.values = nil
}
func (s *List[T]) RemoveAll(values iter.Seq[T]) {
defer s.Lock()()
for value := range values {
s.Remove(value)
}
}
func (s *List[T]) Update(updater func(values []T) []T) {
defer s.Lock()()
s.values = updater(s.values)
}
// removeIndex removes the index from the list, returns the value and true if a value was removed
func (s *List[T]) removeIndex(index int) (value T, ok bool) {
last := len(s.values) - 1
if index < 0 || index > last {
return value, false
}
value = (s.values)[index]
switch index {
case 0:
if last == 0 {
s.values = nil
} else {
s.values = (s.values)[1:]
}
case last:
s.values = (s.values)[:last]
default:
s.values = append((s.values)[0:index], (s.values)[index+1:]...)
}
return value, true
}
func (s *List[T]) indexOf(value T) (index int) {
for i, v := range s.values {
if value == v {
return i
}
}
return -1
}
var _ interface {
Lockable
Collection[int]
Queue[int]
Stack[int]
} = (*List[int])(nil)