forked from OneOfOne/genh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lslice.go
182 lines (155 loc) · 3.21 KB
/
lslice.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
package genh
import (
"encoding/json"
"sync"
)
type LSlice[T any] struct {
mux sync.RWMutex
v []T
}
func (ls *LSlice[T]) Update(fn func(v []T) []T) {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = fn(ls.v)
}
func (ls *LSlice[T]) Append(vs ...T) {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = append(ls.v, vs...)
}
func (ls *LSlice[T]) Set(i int, v T) {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v[i] = v
}
func (ls *LSlice[T]) Insert(i int, vs ...T) {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = Insert(ls.v, i, vs...)
}
func (ls *LSlice[T]) Delete(i, j int) {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = Delete(ls.v, i, j)
}
func (ls *LSlice[T]) Filter(fn func(T) bool, inplace bool) *LSlice[T] {
if inplace {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = Filter(ls.v, fn, true)
return ls
}
ls.mux.RLock()
defer ls.mux.RUnlock()
return &LSlice[T]{v: Filter(ls.v, fn, false)}
}
func (ls *LSlice[T]) Map(fn func(T) T, inplace bool) *LSlice[T] {
if inplace {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = SliceMapSameType(ls.v, fn, true)
return ls
}
ls.mux.RLock()
defer ls.mux.RUnlock()
return &LSlice[T]{v: SliceMapSameType(ls.v, fn, false)}
}
func (ls *LSlice[T]) Swap(i int, v T) (old T) {
ls.mux.Lock()
defer ls.mux.Unlock()
old, ls.v[i] = ls.v[i], v
return
}
func (ls *LSlice[T]) SetSlice(v []T) {
ls.mux.Lock()
ls.v = v
ls.mux.Unlock()
}
func (ls *LSlice[T]) Sort(lessFn func(a, b T) bool) {
ls.mux.Lock()
defer ls.mux.Unlock()
SortFunc(ls.v, lessFn)
}
func (ls *LSlice[T]) Grow(sz int) {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = Grow(ls.v, sz)
}
func (ls *LSlice[T]) ClipTo(len_, cap_ int) {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = ls.v[:len_:cap_]
}
func (ls *LSlice[T]) Clip() {
ls.mux.Lock()
defer ls.mux.Unlock()
ls.v = Clip(ls.v)
}
func (ls *LSlice[T]) Len() int {
ls.mux.RLock()
defer ls.mux.RUnlock()
return len(ls.v)
}
func (ls *LSlice[T]) Cap() int {
ls.mux.RLock()
defer ls.mux.RUnlock()
return cap(ls.v)
}
func (ls *LSlice[T]) Get(i int) T {
ls.mux.RLock()
defer ls.mux.RUnlock()
return ls.v[i]
}
func (ls *LSlice[T]) ForEach(fn func(i int, v T) bool) {
ls.mux.RLock()
defer ls.mux.RUnlock()
for i, v := range ls.v {
if !fn(i, v) {
return
}
}
}
func (ls *LSlice[T]) Search(cmpFn func(v T) int) (v T, found bool) {
var i int
ls.mux.RLock()
defer ls.mux.RUnlock()
if i, found = BinarySearchFunc(ls.v, cmpFn); found {
v = ls.v[i]
}
return
}
func (ls *LSlice[T]) Clone() []T {
ls.mux.RLock()
defer ls.mux.RUnlock()
return SliceClone(ls.v)
}
func (ls *LSlice[T]) LClone() *LSlice[T] {
ls.mux.RLock()
defer ls.mux.RUnlock()
return &LSlice[T]{v: SliceClone(ls.v)}
}
func (ls *LSlice[T]) Raw() []T {
ls.mux.RLock()
defer ls.mux.RUnlock()
return ls.v
}
func (ls *LSlice[T]) MarshalJSON() ([]byte, error) {
ls.mux.RLock()
defer ls.mux.RUnlock()
return json.Marshal(ls.v)
}
func (ls *LSlice[T]) UnmarshalJSON(p []byte) error {
ls.mux.Lock()
defer ls.mux.Unlock()
return json.Unmarshal(p, &ls.v)
}
func (ls *LSlice[T]) MarshalBinary() ([]byte, error) {
ls.mux.RLock()
defer ls.mux.RUnlock()
return MarshalMsgpack(ls.v)
}
func (ls *LSlice[T]) UnmarshalBinary(p []byte) error {
ls.mux.Lock()
defer ls.mux.Unlock()
return UnmarshalMsgpack(p, &ls.v)
}