-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinders.go
129 lines (110 loc) · 3.02 KB
/
finders.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
package option
import "sort"
func FindElemIntArrayGreaterThan(inputNum int) Finder {
return func(value interface{}) (result bool, elem interface{}) {
if arrayB, ok := value.([]int); ok == true {
result, elem = findIntGreaterThan(arrayB, inputNum)
}
return result, elem
}
}
func FindElemIntArrayLesserThan(inputNum int) Finder {
return func(value interface{}) (result bool, elem interface{}) {
if arrayB, ok := value.([]int); ok == true {
result, elem = findIntLesserThan(arrayB, inputNum)
}
return result, elem
}
}
func FindElemIntArray(inputNum int) Finder {
return func(value interface{}) (result bool, elem interface{}) {
if arrayB, ok := value.([]int); ok == true {
result, elem = findInt(arrayB, inputNum)
}
return result, elem
}
}
func FindElemFloat64Array(inputNum float64) Finder {
return func(value interface{}) (result bool, elem interface{}) {
if arrayB, ok := value.([]float64); ok == true {
result, elem = findFloat64(arrayB, inputNum)
}
return result, elem
}
}
func FindElemStringArray(inputNum string) Finder {
return func(value interface{}) (result bool, elem interface{}) {
if arrayB, ok := value.([]string); ok == true {
result, elem = findString(arrayB, inputNum)
}
return result, elem
}
}
func findInt(value []int, inputNum int) (result bool, elem interface{}) {
if !sort.IntsAreSorted(value) {
sort.Ints(value)
}
i := sort.Search(len(value), func(i int) bool { return value[i] >= inputNum })
if i >= len(value) || value[i] != inputNum {
result = false
elem = &None{}
} else {
result = true
elem = value[i]
}
return result, elem
}
func findIntGreaterThan(value []int, inputNum int) (bool, interface{}) {
if !sort.IntsAreSorted(value) {
sort.Ints(value)
}
i := sort.Search(len(value), func(i int) bool { return value[i] > inputNum })
if i >= len(value) {
return false, &None{}
} else {
elem := make([]int, len(value)-i)
copy(elem, value[i:len(value)])
return true, elem
}
}
func findIntLesserThan(value []int, inputNum int) (bool, interface{}) {
if !sort.IntsAreSorted(value) {
sort.Ints(value)
}
i := sort.Search(len(value), func(i int) bool { return value[i] >= inputNum })
if i >= len(value) {
return false, &None{}
} else {
elem := make([]int, i)
copy(elem, value[0:i])
return true, elem
}
}
func findFloat64(value []float64, inputNum float64) (result bool, elem interface{}) {
if !sort.Float64sAreSorted(value) {
sort.Float64s(value)
}
i := sort.Search(len(value), func(i int) bool { return value[i] >= inputNum })
if i >= len(value) || value[i] != inputNum {
result = false
elem = &None{}
} else {
result = true
elem = value[i]
}
return result, elem
}
func findString(value []string, inputNum string) (result bool, elem interface{}) {
if !sort.StringsAreSorted(value) {
sort.Strings(value)
}
i := sort.Search(len(value), func(i int) bool { return value[i] >= inputNum })
if i >= len(value) || value[i] != inputNum {
result = false
elem = &None{}
} else {
result = true
elem = value[i]
}
return result, elem
}