-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.go
119 lines (105 loc) · 2.08 KB
/
float.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
package utils
func NewPtrFloat32(f float32) *float32 {
return &f
}
func NewPtrFloat64(f float64) *float64 {
return &f
}
type SliceOfFloat32 []float32
type SliceOfFloat64 []float64
func (sof SliceOfFloat32) Unique() []float32 {
mof := map[float32]bool{}
for _, f := range sof {
mof[f] = false
}
uniqueSof := []float32{}
for _, f := range sof {
if added, ok := mof[f]; ok && !added {
uniqueSof = append(uniqueSof, f)
mof[f] = true
}
}
return uniqueSof
}
func (sof SliceOfFloat64) Unique() []float64 {
mof := map[float64]bool{}
for _, f := range sof {
mof[f] = false
}
uniqueSof := []float64{}
for _, f := range sof {
if added, ok := mof[f]; ok && !added {
uniqueSof = append(uniqueSof, f)
mof[f] = true
}
}
return uniqueSof
}
func (sof SliceOfFloat32) InSlice(t float32) bool {
for _, f := range sof {
if t == f {
return true
}
}
return false
}
func (sof SliceOfFloat64) InSlice(t float64) bool {
for _, f := range sof {
if t == f {
return true
}
}
return false
}
type SliceOfPtrFloat32 []*float32
type SliceOfPtrFloat64 []*float64
func (sopf SliceOfPtrFloat32) Unique() []*float32 {
mof := map[float32]bool{}
for _, pf := range sopf {
if pf == nil {
continue
}
mof[*pf] = false
}
uniqueSopf := []*float32{}
for _, pf := range sopf {
if added, ok := mof[*pf]; ok && !added {
uniqueSopf = append(uniqueSopf, NewPtrFloat32(*pf))
mof[*pf] = true
}
}
return uniqueSopf
}
func (sopf SliceOfPtrFloat64) Unique() []*float64 {
mof := map[float64]bool{}
for _, pf := range sopf {
if pf == nil {
continue
}
mof[*pf] = false
}
uniqueSopf := []*float64{}
for _, pf := range sopf {
if added, ok := mof[*pf]; ok && !added {
uniqueSopf = append(uniqueSopf, NewPtrFloat64(*pf))
mof[*pf] = true
}
}
return uniqueSopf
}
func (sopf SliceOfPtrFloat32) InSlice(t float32) bool {
for _, pf := range sopf {
if pf != nil && t == *pf {
return true
}
}
return false
}
func (sopf SliceOfPtrFloat64) InSlice(t float64) bool {
for _, pf := range sopf {
if pf != nil && t == *pf {
return true
}
}
return false
}