/
horizontal.go
66 lines (60 loc) · 1.96 KB
/
horizontal.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
package vec32
import (
"github.com/duncanpierce/vector/constraintsExt"
"golang.org/x/exp/constraints"
)
/*
IsDuplicate checks elements of vector a for values which are equal to one another. If several elements have the same value,
exactly one is chosen as the original. Elements other than the original are deemed to be duplicates and have the corresponding bit set
in the boolean vector result.
It is not defined which value is chosen as the original. Your code should assume it is chosen at random.
*/
func IsDuplicate[Element constraintsExt.Number](m Bool, a [Length]Element) (r Bool) {
seen := map[Element]bool{}
m.ForTrue(func(i int) {
r.Set(i, seen[a[i]])
seen[a[i]] = true
})
return
}
/*
ElementSum adds each element selected by the Bool mask and returns their sum. If no elements are selected by the Bool mask, the result is 0.
*/
func ElementSum[Element constraintsExt.Number](m Bool, a [Length]Element) (r Element) {
m.ForTrue(func(i int) {
r += a[i]
})
return
}
/*
ElementProduct multiplies each element selected by the Bool mask and returns their product. If no elements are selected by the Bool mask, the result is 1.
*/
func ElementProduct[Element constraintsExt.Number](m Bool, a [Length]Element) (r Element) {
r = 1
m.ForTrue(func(i int) {
r *= a[i]
})
return
}
/*
ElementMin returns the minimum of the elements selected by the Bool mask. If no elements are selected by the Bool mask, the result ok is false.
*/
func ElementMin[Element constraints.Ordered](m Bool, a [Length]Element) (min Element, ok bool) {
return ReduceElements(m, a, func(x, y Element) Element {
if x < y {
return x
}
return y
})
}
/*
ElementMax returns the maximum of the elements selected by the Bool mask. If no elements are selected by the Bool mask, the result ok is false.
*/
func ElementMax[Element constraints.Ordered](m Bool, a [Length]Element) (min Element, ok bool) {
return ReduceElements(m, a, func(x, y Element) Element {
if x > y {
return x
}
return y
})
}