-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbounds.go
54 lines (45 loc) · 1.05 KB
/
bounds.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
package sortedmap
import "sort"
func (sm *SortedMap) setBoundIdx(boundVal interface{}) int {
return sort.Search(len(sm.sorted), func(i int) bool {
return sm.lessFn(boundVal, sm.idx[sm.sorted[i]])
})
}
func (sm *SortedMap) boundsIdxSearch(lowerBound, upperBound interface{}) []int {
smLen := len(sm.sorted)
if smLen == 0 {
return nil
}
if lowerBound != nil && upperBound != nil {
if sm.lessFn(upperBound, lowerBound) {
return nil
}
}
lowerBoundIdx := 0
if lowerBound != nil {
lowerBoundIdx = sm.setBoundIdx(lowerBound)
if lowerBoundIdx == smLen {
lowerBoundIdx--
}
if lowerBoundIdx >= 0 && sm.lessFn(sm.idx[sm.sorted[lowerBoundIdx]], lowerBound) {
lowerBoundIdx++
}
}
upperBoundIdx := smLen - 1
if upperBound != nil {
upperBoundIdx = sm.setBoundIdx(upperBound)
if upperBoundIdx == smLen {
upperBoundIdx--
}
if upperBoundIdx >= 0 && sm.lessFn(upperBound, sm.idx[sm.sorted[upperBoundIdx]]) {
upperBoundIdx--
}
}
if lowerBoundIdx > upperBoundIdx {
return nil
}
return []int{
lowerBoundIdx,
upperBoundIdx,
}
}