-
Notifications
You must be signed in to change notification settings - Fork 0
/
generics.go
164 lines (129 loc) · 2.39 KB
/
generics.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
package gotk
import (
"golang.org/x/exp/constraints"
)
func VectorIndex[T constraints.Ordered](list []T, v T) int {
for i := range list {
if list[i] == v {
return i
}
}
return -1
}
func EqualVector[T constraints.Ordered](arr1, arr2 []T) (ok bool) {
if len(arr1) != len(arr2) {
return false
}
for i := range arr1 {
if arr1[i] != arr2[i] {
return false
}
}
return true
}
func UniqVector[T constraints.Ordered](arr []T) (list []T) {
n := len(arr)
list = make([]T, 0, n)
if len(arr) == 0 {
return list
}
mp := make(map[T]bool, n)
for _, v := range arr {
if !mp[v] {
list = append(list, v)
mp[v] = true
}
}
return list
}
func First[T any](v []T) *T {
if len(v) == 0 {
return nil
}
return &v[0]
}
func Last[T any](v []T) *T {
var n = len(v)
if n == 0 {
return nil
}
return &v[n-1]
}
func SliceGet[T any](slice []T, index int) (val T, exists bool) {
if index > len(slice)-1 {
return val, false
}
return slice[index], true
}
func Slice2Map[K comparable, T, V any](items []T, getKey func(*T) K, getValue func(*T) V) (
mp map[K]V) {
mp = make(map[K]V, len(items))
for i := range items {
mp[getKey(&items[i])] = getValue(&items[i])
}
return mp
}
func JoinSlices[K comparable, T, V any](items []T, exts map[K]V,
getKey func(*T) K, setValue func(*T, V)) (n int) {
var (
v V
ok bool
)
if len(exts) == 0 {
return 0
}
for i := range items {
if v, ok = exts[getKey(&items[i])]; ok {
n++
setValue(&items[i], v)
}
}
return n
}
func PickOne[T any](items []T) (item T) {
if len(items) == 0 {
return item
}
return items[Rand.IntN(len(items))]
}
func PickSome[T any](items []T, num int) (ans []T) {
if len(items) <= num {
return items
}
ans = make([]T, num)
if num == 0 {
return ans
}
slice := Rand.Perm(len(items))
for i := 0; i < num; i++ {
ans[i] = items[slice[i]]
}
return ans
}
func PickSomeIndex[T any](items []T, num int) (ans []int) {
if len(items) <= num {
ans = make([]int, len(items))
for i := range items {
ans[i] = i
}
return ans
}
ans = make([]int, num)
if num == 0 {
return ans
}
slice := Rand.Perm(len(items))
for i := 0; i < num; i++ {
ans[i] = slice[i]
}
return ans
}
func NewSliceWith[T any](item T, caps ...int) (slice []T) {
if len(caps) > 0 { // caps[0] > 0
slice = make([]T, 0, caps[0])
} else {
slice = make([]T, 0, 1)
}
slice = append(slice, item)
return slice
}