-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfloat32_slice.go
103 lines (93 loc) · 1.64 KB
/
xfloat32_slice.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
package xfloat32Slice
import (
. "github.com/go-xe2/x/type/slice/comm"
"github.com/go-xe2/x/type/t"
"sort"
)
func Contain(arr []float32, item float32) bool {
for _, v := range arr {
if v == item {
return true
}
}
return false
}
func ForEach(arr []float32, fn FloatArrForEachFunc) {
if fn == nil {
return
}
for k, v := range arr {
fn(k, v)
}
}
func Map(arr []float32, fn FloatArrMapFunc) {
if fn == nil {
return
}
for k, v := range arr {
nv := fn(k, v)
arr[k] = nv
}
}
func Find(arr []float32, fn FloatArrSearchFunc) float32 {
if fn == nil {
return -1
}
for k, v := range arr {
if fn(k, v) {
return v
}
}
return -1
}
func FindIndex(arr []float32, fn FloatArrSearchFunc) int {
if fn == nil {
return -1
}
for k, v := range arr {
if fn(k, v) {
return k
}
}
return -1
}
func Sort(arr []float32, comparer ...ArrSortCompareFunc) {
var defComparer = func(aIndex int, bIndex int) int {
n1 := arr[aIndex]
n2 := arr[bIndex]
return int(n1 - n2)
}
if len(comparer) > 0 && comparer[0] != nil {
defComparer = comparer[0]
}
sort.Slice(arr, func(i, j int) bool {
return defComparer(i, j) < 0
})
}
func Join(arr []float32, sep string) string {
var result = ""
for _, v := range arr {
if result != "" {
result += sep
}
result += t.String(v)
}
return result
}
func String(this []float32) string {
var result = ""
for _, v := range this {
if result != "" {
result += ","
}
result += t.String(v)
}
return "[" + result + "]"
}
func AsInterface(this []float32) []interface{} {
var result = make([]interface{}, 0)
for _, v := range this {
result = append(result, v)
}
return result
}