-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
349 lines (298 loc) · 7.19 KB
/
set.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
package rice
import (
"sort"
"time"
)
type Numbers interface {
uint8 | uint16 | uint32 | uint64 | int8 | int16 | int32 | int64 | float32 | float64 | int | uint
}
// RemoveItem 移除 slice 中的一个元素
func RemoveItem[T any](items []T, index int) []T {
if index >= len(items) {
return []T{}
}
return append(items[:index], items[index+1:]...)
}
// RemoveItemNoOrder 移除 slice 中的一个元素,无序
func RemoveItemNoOrder[T any](items []T, index int) []T {
if index >= len(items) {
return []T{}
}
items[index] = items[len(items)-1]
return items[:len(items)-1]
}
// DeDuplicate slice 去重
func DeDuplicate[T comparable](items []T) []T {
m := make(map[T]struct{})
for _, v := range items {
m[v] = struct{}{}
}
s := make([]T, 0)
for k := range m {
s = append(s, k)
}
return s
}
// DeDuplicateInPlace slice 就地去重
func DeDuplicateInPlace[T Numbers](items []T) []T {
// if there are 0 or 1 items we return the slice itself.
if len(items) < 2 {
return items
}
// make the slice ascending sorted.
sort.SliceStable(items, func(i, j int) bool { return items[i] < items[j] })
uniqPointer := 0
for i := 1; i < len(items); i++ {
// compare a current item with the item under the unique pointer.
// if they are not the same, write the item next to the right of the unique pointer.
if items[uniqPointer] != items[i] {
uniqPointer++
items[uniqPointer] = items[i]
}
}
return items[:uniqPointer+1]
}
// Difference 取 items1 中有,而 items2 中没有的
func Difference[T comparable](items1, items2 []T) []T {
mb := make(map[T]struct{}, len(items2))
for _, x := range items2 {
mb[x] = struct{}{}
}
var diff []T
for _, x := range items1 {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}
// DifferenceBoth 取 slice1, slice2 的差集
func DifferenceBoth[T comparable](items1, items2 []T) []T {
var diff []T
// Loop two times, first to find items1 strings not in items2,
// second loop to find items2 strings not in items1
for i := 0; i < 2; i++ {
for _, s1 := range items1 {
found := false
for _, s2 := range items2 {
if s1 == s2 {
found = true
break
}
}
// String not found. We add it to return slice
if !found {
diff = append(diff, s1)
}
}
// Swap the slices, only if it was the first loop
if i == 0 {
items1, items2 = items2, items1
}
}
return diff
}
// Intersection 两个 slice 的交集
func Intersection[T comparable](s1, s2 []T) (inter []T) {
hash := make(map[T]bool)
for _, e := range s1 {
hash[e] = true
}
for _, e := range s2 {
// If elements present in the hashmap then append intersection list.
if hash[e] {
inter = append(inter, e)
}
}
//Remove dups from slice.
inter = removeDups(inter)
return
}
// Remove dups from slice.
func removeDups[T comparable](elements []T) (nodups []T) {
encountered := make(map[T]bool)
for _, element := range elements {
if !encountered[element] {
nodups = append(nodups, element)
encountered[element] = true
}
}
return
}
// IsTimeExistIntersection 两个时间段是否有交集 false 没有交集,true 有交集
func IsTimeExistIntersection(startTime, endTime time.Time, anotherStartTime, anotherEndTime time.Time) bool {
if anotherStartTime.After(endTime) || anotherEndTime.Before(startTime) {
return false
} else {
return true
}
}
// IsTimestampExistIntersection 两个时间段是否有交集 false 没有交集,true 有交集
func IsTimestampExistIntersection(startTime, endTime int64, anotherStartTime, anotherEndTime int64) bool {
if endTime < anotherStartTime || startTime > anotherEndTime {
return false
} else {
return true
}
}
// MaxNumber booleans, numbers, strings, pointers, channels, arrays
func MaxNumber[T Numbers](e ...T) T {
sort.Slice(e, func(i, j int) bool { return e[i] < e[j] })
return e[len(e)-1]
}
func MinNumber[T Numbers](e ...T) T {
sort.Slice(e, func(i, j int) bool { return e[i] < e[j] })
return e[0]
}
// NotIn e 不在 s 中吗? true 不在, false 在
func NotIn[T comparable](e T, items []T) bool {
for _, item := range items {
if e == item {
return false
}
}
return true
}
// In e 在 items 中吗? true 在,false 不在
func In[T comparable](e T, items []T) bool {
for _, item := range items {
if e == item {
return true
}
}
return false
}
// Reverse 反转 slice
func Reverse[T any](items []T) {
for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
items[i], items[j] = items[j], items[i]
}
}
// Pageination 切片分页
func Pageination[T any](page, pageSize int, s []T) []T {
if page <= 0 {
page = 1
}
if len(s) >= pageSize*(page-1) {
if pageSize*page <= len(s) {
s = s[pageSize*(page-1) : pageSize*page]
} else if page-1 == 0 {
s = s[:]
} else if pageSize*page > len(s) {
s = s[pageSize*(page-1):]
}
return s
} else {
return s
}
}
// Filter filter one slice
// func Filter[T any](objs []T, filter func(obj T) bool) []T {
// res := make([]T, 0, len(objs))
// for i := range objs {
// ok := filter(objs[i])
// if ok {
// res = append(res, objs[i])
// }
// }
// return res
// }
// Map one slice
// func Map[T any, K any](objs []T, mapper func(obj T) ([]K, bool)) []K {
// res := make([]K, 0, len(objs))
// for i := range objs {
// others, ok := mapper(objs[i])
// if ok {
// res = append(res, others...)
// }
// }
// return res
// }
// First make return first for slice
func First[T any](objs []T) (T, bool) {
if len(objs) > 0 {
return objs[0], true
}
return *new(T), false
}
type Iterator[T any] interface {
Next() bool
Value() T
}
type SliceIterator[T any] struct {
Elements []T
value T
index int
}
// NewSliceIterator Create an iterator over the slice xs
func NewSliceIterator[T any](xs []T) Iterator[T] {
return &SliceIterator[T]{
Elements: xs,
}
}
// Next Move to next value in collection
func (iter *SliceIterator[T]) Next() bool {
if iter.index < len(iter.Elements) {
iter.value = iter.Elements[iter.index]
iter.index += 1
return true
}
return false
}
// Value Get current element
func (iter *SliceIterator[T]) Value() T {
return iter.value
}
type mapIterator[T any] struct {
source Iterator[T]
mapper func(T) T
}
// Next advance to next element
func (iter *mapIterator[T]) Next() bool {
return iter.source.Next()
}
func (iter *mapIterator[T]) Value() T {
value := iter.source.Value()
return iter.mapper(value)
}
func Map[T any](iter Iterator[T], f func(T) T) Iterator[T] {
return &mapIterator[T]{
iter, f,
}
}
type filterIterator[T any] struct {
source Iterator[T]
pred func(T) bool
}
func (iter *filterIterator[T]) Next() bool {
for iter.source.Next() {
if iter.pred(iter.source.Value()) {
return true
}
}
return false
}
func (iter *filterIterator[T]) Value() T {
return iter.source.Value()
}
func Filter[T any](iter Iterator[T], pred func(T) bool) Iterator[T] {
return &filterIterator[T]{
iter, pred,
}
}
func Collect[T any](iter Iterator[T]) []T {
var xs []T
for iter.Next() {
xs = append(xs, iter.Value())
}
return xs
}
type Reducer[T, V any] func(accum T, value V) T
// Reduce values iterated over to a single value
func Reduce[T, V any](iter Iterator[V], f Reducer[T, V]) T {
var accum T
for iter.Next() {
accum = f(accum, iter.Value())
}
return accum
}