-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
146 lines (123 loc) · 2.72 KB
/
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
package sync
type List[T comparable] struct {
Locking
values []T
}
// ----------------- Collection functions -----------------
func (s *List[T]) Add(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.Add(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.Add(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 -----------------
func (s *List[T]) Each(fn func(value T)) {
defer s.RLock()()
for _, v := range s.values {
fn(v)
}
}
// ----------------- other utility functions -----------------
// Values returns a slice containing all the values at the time of the call, this should be used
// sparingly and is only a con
func (s *List[T]) Values() []T {
defer s.RLock()()
return append([]T{}, s.values...)
}
func (s *List[T]) Clear() {
defer s.Lock()()
s.values = nil
}
func (s *List[T]) RemoveAll(values Iterator[T]) {
defer s.Lock()()
values.Each(func(value T) {
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 an 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)