forked from chen3feng/stl4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
156 lines (144 loc) · 2.83 KB
/
lookup.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
package stl4go
// Max return the larger value between `a` and `b`.
//
// Complexity: O(1).
func Max[T Ordered](a, b T) T {
if a > b {
return a
}
return b
}
// Min return the smaller value between `a` and `b`.
//
// Complexity: O(1).
func Min[T Ordered](a, b T) T {
if a < b {
return a
}
return b
}
// MaxN return the maximum value in the sequence `a`.
//
// Complexity: O(len(a)).
func MaxN[T Ordered](a ...T) T {
if len(a) == 0 {
panic("can't call MaxN() with empty arguments list")
}
v := a[0]
for i := 0; i < len(a); i++ {
if a[i] > v {
v = a[i]
}
}
return v
}
// MinN return the minimum value in the sequence `a`.
//
// Complexity: O(len(a)).
func MinN[T Ordered](a ...T) T {
if len(a) == 0 {
panic("can't call MaxN() with empty arguments list")
}
v := a[0]
for i := 0; i < len(a); i++ {
if a[i] < v {
v = a[i]
}
}
return v
}
// MinMax returns both min and max between a and b.
//
// Complexity: O(1).
func MinMax[T Ordered](a, b T) (min, max T) {
if a < b {
return a, b
}
return b, a
}
// MinMaxN returns both min and max in slice a.
//
// Complexity: O(len(a))
func MinMaxN[T Ordered](a ...T) (min, max T) {
if len(a) == 0 {
panic("can't call MaxN() with empty arguments list")
}
min = a[0]
max = a[0]
for i := 0; i < len(a); i++ {
if a[i] < min {
min = a[i]
}
if a[i] > max {
max = a[i]
}
}
return
}
// Find find the first value x in the given slice a linearly.
// return (index, true) if found,
// return (_, false) if not found.
//
// Complexity: O(len(a)).
func Find[T comparable](a []T, x T) (index int, ok bool) {
for i, v := range a {
if v == x {
return i, true
}
}
return -1, false
}
// FindIf find the first value x satisfying function cond in the given slice a linearly.
// return (index, true) if found,
// return (_, false) if not found.
//
// Complexity: O(len(a)).
func FindIf[T any](a []T, cond func(T) bool) (index int, ok bool) {
for i, v := range a {
if cond(v) {
return i, true
}
}
return -1, false
}
// Index find the value x in the given slice a linearly.
//
// Return index if found, -1 if not found.
//
// Complexity: O(len(a)).
func Index[T comparable](a []T, x T) int {
for i, v := range a {
if v == x {
return i
}
}
return -1
}
// AllOf return true if pred(e) returns true for all emements e in a.
//
// Complexity: O(len(a)).
func AllOf[T any](a []T, pred func(T) bool) bool {
for _, v := range a {
if !pred(v) {
return false
}
}
return true
}
// AnyOf return true if pred(e) returns true for any emements e in a.
//
// Complexity: O(len(a)).
func AnyOf[T any](a []T, pred func(T) bool) bool {
for _, v := range a {
if pred(v) {
return true
}
}
return false
}
// NoneOf return true pred(e) returns true for none emements e in a.
//
// Complexity: O(len(a)).
func NoneOf[T any](a []T, pred func(T) bool) bool {
return !AnyOf(a, pred)
}