-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashrange.go
60 lines (48 loc) · 1.83 KB
/
hashrange.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
package zrange
import "sort"
// HashRange contains a minimum and maximum Geohash integer range value.
type HashRange struct {
Min, Max uint64
}
// HashRanges is a list of ranges containing minimum and maximum Geohash integers,
// used for performing range queries.
type HashRanges []HashRange
// SortMinAsc sorts a list of hash ranges in ascending order by their items'
// Min fields. SetDefaults, and typically FindNeighborsWithRadius, should be called
// before use. RadialRange should be used instead of calling this method directly,
// unless more customized behavior is desired.
func (hashRangeList HashRanges) SortMinAsc() HashRanges {
sort.Sort(hashRangesMinAscSorter(hashRangeList))
return hashRangeList
}
// CombineRanges merges each overlapping range.
// The input list of hash ranges are expected to be sorted in ascending order by
// their items' Min fields. SetDefaults, and typically FindNeighborsWithRadius
// and SortMinAsc, should be called before use. RadialRange should be used instead
// of calling this method directly, unless more customized behavior is desired.
func (hashRangeList HashRanges) CombineRanges() HashRanges {
combinedList := hashRangeList[:0]
for i := 0; i < len(hashRangeList)-1; i++ {
hashRange := hashRangeList[i]
nextHashRange := hashRangeList[i+1]
if hashRange.Max == nextHashRange.Min {
hashRange.Max = nextHashRange.Max
}
if hashRange.Max == nextHashRange.Max {
hashRangeList[i+1].Min = hashRange.Min
continue
}
combinedList = append(combinedList, hashRange)
}
return append(combinedList, hashRangeList[len(hashRangeList)-1])
}
type hashRangesMinAscSorter HashRanges
func (s hashRangesMinAscSorter) Len() int {
return len(s)
}
func (s hashRangesMinAscSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s hashRangesMinAscSorter) Less(i, j int) bool {
return s[i].Min < s[j].Min
}