forked from qedus/osmpbf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
binary_entity_map.go
173 lines (150 loc) · 4.1 KB
/
binary_entity_map.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny
// EDIT: This file was manually updated
// TODO: Use generics once Go supports them
package gosmonaut
import (
"math"
"sort"
)
// binaryNodeEntityMap uses a binary search table for storing
// entites. It is well suited in this case since we only have to sort it once
// between the reads and writes. Also we avoid the memory overhead of storing
// the IDs twice and instead just read them from the struct. The (fake-)generic
// solution performs much better than it would using the OSMEntity interface.
type binaryNodeEntityMap struct {
buckets [][]rawNode
n uint64
}
func newBinaryNodeEntityMap(n int) *binaryNodeEntityMap {
// Calculate the number of buckets, exponent defines max number of lookups
nb := n / int(math.Pow(2, 20))
if nb < 1 {
nb = 1
}
// Calculate the bucket sizes. Leave a small array overhead since the
// distribution is not completely even.
var bs int
if nb == 1 {
bs = n
} else {
bs = int(float64(n/nb) * 1.05)
}
// Create the buckets
buckets := make([][]rawNode, nb)
for i := 0; i < nb; i++ {
buckets[i] = make([]rawNode, 0, bs)
}
return &binaryNodeEntityMap{
buckets: buckets,
n: uint64(nb),
}
}
func (m *binaryNodeEntityMap) hash(id int64) uint64 {
return uint64(id) % m.n
}
// Must not be called after calling prepare()
func (m *binaryNodeEntityMap) add(e rawNode) {
h := m.hash(e.id)
m.buckets[h] = append(m.buckets[h], e)
}
// Must be called between the last write and the first read
func (m *binaryNodeEntityMap) prepare() {
// Sort buckets
for _, b := range m.buckets {
sort.Slice(b, func(i, j int) bool {
return b[i].id < b[j].id
})
}
}
// Must not be called before calling prepare()
func (m *binaryNodeEntityMap) get(id int64) (rawNode, bool) {
h := m.hash(id)
bucket := m.buckets[h]
// Binary search (we can't use sort.Search as we use int64)
lo := 0
hi := len(bucket) - 1
for lo <= hi {
mid := (lo + hi) / 2
midID := bucket[mid].id
if midID < id {
lo = mid + 1
} else if midID > id {
hi = mid - 1
} else {
return bucket[mid], true
}
}
return rawNode{}, false
}
// binaryWayEntityMap uses a binary search table for storing
// entites. It is well suited in this case since we only have to sort it once
// between the reads and writes. Also we avoid the memory overhead of storing
// the IDs twice and instead just read them from the struct. The (fake-)generic
// solution performs much better than it would using the OSMEntity interface.
type binaryWayEntityMap struct {
buckets [][]rawWay
n uint64
}
func newBinaryWayEntityMap(n int) *binaryWayEntityMap {
// Calculate the number of buckets, exponent defines max number of lookups
nb := n / int(math.Pow(2, 20))
if nb < 1 {
nb = 1
}
// Calculate the bucket sizes. Leave a small array overhead since the
// distribution is not completely even.
var bs int
if nb == 1 {
bs = n
} else {
bs = int(float64(n/nb) * 1.05)
}
// Create the buckets
buckets := make([][]rawWay, nb)
for i := 0; i < nb; i++ {
buckets[i] = make([]rawWay, 0, bs)
}
return &binaryWayEntityMap{
buckets: buckets,
n: uint64(nb),
}
}
func (m *binaryWayEntityMap) hash(id int64) uint64 {
return uint64(id) % m.n
}
// Must not be called after calling prepare()
func (m *binaryWayEntityMap) add(e rawWay) {
h := m.hash(e.id)
m.buckets[h] = append(m.buckets[h], e)
}
// Must be called between the last write and the first read
func (m *binaryWayEntityMap) prepare() {
// Sort buckets
for _, b := range m.buckets {
sort.Slice(b, func(i, j int) bool {
return b[i].id < b[j].id
})
}
}
// Must not be called before calling prepare()
func (m *binaryWayEntityMap) get(id int64) (rawWay, bool) {
h := m.hash(id)
bucket := m.buckets[h]
// Binary search (we can't use sort.Search as we use int64)
lo := 0
hi := len(bucket) - 1
for lo <= hi {
mid := (lo + hi) / 2
midID := bucket[mid].id
if midID < id {
lo = mid + 1
} else if midID > id {
hi = mid - 1
} else {
return bucket[mid], true
}
}
return rawWay{}, false
}