-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
379 lines (304 loc) · 7.07 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
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
// Package list provides a built-in implementation of a generic list
package list
import (
"sort"
"sync"
)
// T0 is a generic type variable placeholder of a key type. It will not appear in the generated code
type T0 string
// T0Predicate is a predicate function on T0 type
type T0Predicate func(T0) bool
// T0Visitor is a visitor function used to visit items of the list
type T0Visitor func(i int, val T0) bool
// T0LessFunc is a comparator function used to sort the list
type T0LessFunc func(first, second T0) bool
// T0List exposes a contract of a list of T0 elements
type T0List interface {
// IFilter is similar to Filter but returns a channel instead of a T0List instance
IFilter(f T0Predicate) <-chan T0
// Iter return a channel that is filled with this list elements
Iter() <-chan T0
// Each visits each element in the map. It stops iterations if visitor func returns false
Each(visitor T0Visitor) T0List
// Get returns i-th element from this list
Get(i int) T0
// Filter returns a new list filled with items that are yielded true on given predicate
Filter(f T0Predicate) T0List
// Any return true if there is at least one item on which predicate returns true
Any(f T0Predicate) bool
// All return true if a given predicate returns true for all of the item
All(f T0Predicate) bool
FindFirst(f T0Predicate, defaultVal T0) T0
FindLast(f T0Predicate, defaultVal T0) T0
// Len returns th length of the list
Len() int
// Swap swaps given items
Swap(i, j int)
// Sort sorts the list
Sort(byFunc T0LessFunc) T0List
// Clone creates a shallow copy of this list
Clone() T0List
// Set set the value of i-th element
Set(i int, val T0)
// Append appends given items to the end of the list preserving the order
Append(items ...T0)
// Prepend prepends given items to the beginning of the list preserving the order
Prepend(items ...T0)
// Pop removes last item from the end of the list and returns it. It returns defaultVal in case of empty list
Pop(defaultVal T0) T0
}
type baseT0List struct {
list []T0
}
// NewT0List creates an empty list of T0 elements
func NewT0List() T0List {
return NewT0ListFromSlice([]T0{}...)
}
// NewT0ListFromSlice creates a list of T0 element initialised from a given slice
func NewT0ListFromSlice(items ...T0) T0List {
l := make([]T0, len(items))
copy(l, items)
return &baseT0List{list: l}
}
// NewSyncronizedT0List creates a concurrent safe instance of a T0List
func NewSyncronizedT0List(items ...T0) T0List {
l := make([]T0, len(items))
copy(l, items)
inner := &baseT0List{list: l}
return &syncT0List{inner: inner}
}
func (l *baseT0List) Filter(f T0Predicate) T0List {
result := &baseT0List{list: []T0{}}
for _, v := range l.list {
if f(v) {
result.list = append(result.list, v)
}
}
return result
}
func (l *baseT0List) IFilter(f T0Predicate) <-chan T0 {
results := make(chan T0)
go func() {
for _, v := range l.list {
if f(v) {
results <- v
}
}
close(results)
}()
return results
}
func (l *baseT0List) Iter() <-chan T0 {
results := make(chan T0)
go func() {
for _, v := range l.list {
results <- v
}
close(results)
}()
return results
}
func (l *baseT0List) Each(visitor T0Visitor) T0List {
for i, v := range l.list {
if !visitor(i, v) {
return l
}
}
return l
}
func (l *baseT0List) Get(i int) T0 {
return l.list[i]
}
func (l *baseT0List) Any(f T0Predicate) bool {
for _, v := range l.list {
if f(v) {
return true
}
}
return false
}
func (l *baseT0List) All(f T0Predicate) bool {
for _, v := range l.list {
if !f(v) {
return false
}
}
return true
}
func (l *baseT0List) FindFirst(f T0Predicate, defaultVal T0) T0 {
for _, v := range l.list {
if f(v) {
return v
}
}
return defaultVal
}
func (l *baseT0List) FindLast(f T0Predicate, defaultVal T0) T0 {
found := defaultVal
for _, v := range l.list {
if f(v) {
found = v
}
}
return found
}
// For sort
func (l *baseT0List) Len() int {
return len(l.list)
}
func (l *baseT0List) Swap(i, j int) {
l.list[i], l.list[j] = l.list[j], l.list[i]
}
func (l *baseT0List) Sort(byFunc T0LessFunc) T0List {
sorter := &byFuncT0Sorter{l, byFunc}
sort.Sort(sorter)
return l
}
func (l *baseT0List) Clone() T0List {
copied := make([]T0, len(l.list))
copy(copied, l.list)
r := NewT0ListFromSlice(copied...)
return r
}
// Mutators
func (l *baseT0List) Set(i int, val T0) {
l.list[i] = val
}
func (l *baseT0List) Append(items ...T0) {
l.list = append(l.list, items...)
}
func (l *baseT0List) Prepend(items ...T0) {
l.list = append(items, l.list...)
}
func (l *baseT0List) Pop(defaultVal T0) T0 {
if len(l.list) < 1 {
return defaultVal
}
result := l.list[len(l.list)-1]
l.list = l.list[:len(l.list)-1]
return result
}
// PopRight
type byFuncT0Sorter struct {
*baseT0List
lessFn T0LessFunc
}
func (s *byFuncT0Sorter) Less(i, j int) bool {
return s.lessFn(s.list[i], s.list[j])
}
type syncT0List struct {
inner *baseT0List
mutex sync.RWMutex
}
func (l *syncT0List) Filter(f T0Predicate) T0List {
l.mutex.RLock()
r := l.inner.Filter(f)
l.mutex.RUnlock()
return r
}
func (l *syncT0List) IFilter(f T0Predicate) <-chan T0 {
results := make(chan T0)
go func() {
l.mutex.RLock()
for _, v := range l.inner.list {
if f(v) {
results <- v
}
}
l.mutex.RUnlock()
close(results)
}()
return results
}
func (l *syncT0List) Iter() <-chan T0 {
results := make(chan T0)
go func() {
l.mutex.RLock()
for _, v := range l.inner.list {
results <- v
}
l.mutex.RUnlock()
close(results)
}()
return results
}
func (l *syncT0List) Each(visitor T0Visitor) T0List {
l.mutex.RLock()
l.inner.Each(visitor)
l.mutex.RUnlock()
return l
}
func (l *syncT0List) Get(i int) T0 {
l.mutex.RLock()
v := l.inner.Get(i)
l.mutex.RUnlock()
return v
}
func (l *syncT0List) Any(f T0Predicate) bool {
l.mutex.RLock()
r := l.inner.Any(f)
l.mutex.RUnlock()
return r
}
func (l *syncT0List) All(f T0Predicate) bool {
l.mutex.RLock()
r := l.inner.All(f)
l.mutex.RUnlock()
return r
}
func (l *syncT0List) FindFirst(f T0Predicate, defaultVal T0) T0 {
l.mutex.RLock()
r := l.inner.FindFirst(f, defaultVal)
l.mutex.RUnlock()
return r
}
func (l *syncT0List) FindLast(f T0Predicate, defaultVal T0) T0 {
l.mutex.RLock()
r := l.inner.FindLast(f, defaultVal)
l.mutex.RUnlock()
return r
}
func (l *syncT0List) Len() int {
l.mutex.RLock()
r := l.inner.Len()
l.mutex.RUnlock()
return r
}
func (l *syncT0List) Swap(i int, j int) {
l.mutex.Lock()
l.inner.Swap(i, j)
l.mutex.Unlock()
}
func (l *syncT0List) Sort(byFunc T0LessFunc) T0List {
l.mutex.Lock()
l.inner.Sort(byFunc)
l.mutex.Unlock()
return l
}
func (l *syncT0List) Clone() T0List {
l.mutex.RLock()
r := l.inner.Clone()
l.mutex.RUnlock()
return r
}
func (l *syncT0List) Set(i int, val T0) {
l.mutex.Lock()
l.inner.Set(i, val)
l.mutex.Unlock()
}
func (l *syncT0List) Append(items ...T0) {
l.mutex.Lock()
l.inner.Append(items...)
l.mutex.Unlock()
}
func (l *syncT0List) Prepend(items ...T0) {
l.mutex.Lock()
l.inner.Prepend(items...)
l.mutex.Unlock()
}
func (l *syncT0List) Pop(defaultVal T0) T0 {
l.mutex.Lock()
r := l.inner.Pop(defaultVal)
l.mutex.Unlock()
return r
}