-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorton.go
243 lines (200 loc) · 5.14 KB
/
morton.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Morton implements Z-Order Curve encoding and decoding for N-dimensions, using lookup tables and magic bits respectively.
In order to supply for N-dimensions, this library generates the magic bits used in decoding. While this library does supply for N-dimensions, because this type of ordering uses bit interleaving for encoding it is limited by the width of the uint64 type divided by the number of dimensions (i.e., uint64/3 for 3 dimensions).
*/
package morton
import (
"errors"
"fmt"
"sort"
)
type Table struct {
Index uint8
Length uint32
Encode []Bit
}
func (t Table) String() string {
var bits string
for _, b := range t.Encode {
bits = fmt.Sprintf("%v%v\n", bits, b)
}
return fmt.Sprintf("Index: %v\nLength: %v\n%v", t.Index, t.Length, bits)
}
// Sortable Table slice type to satisfy the sort package interface
type ByTable []Table
func (t ByTable) Len() int {
return len(t)
}
func (t ByTable) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
func (t ByTable) Less(i, j int) bool {
return t[i].Index < t[j].Index
}
type Bit struct {
Index uint32
Value uint64
}
func (b Bit) String() string {
return fmt.Sprintf("[%v]%08b", b.Index, b.Value)
}
// Sortable Table slice type to satisfy the sort package interface
type ByBit []Bit
func (b ByBit) Len() int {
return len(b)
}
func (b ByBit) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b ByBit) Less(i, j int) bool {
return b[i].Index < b[j].Index
}
type Morton struct {
Dimensions uint8
Tables []Table
Magic []uint64
}
// Convenience function
func New(dimensions uint8, size uint32) *Morton {
m := new(Morton)
m.Create(dimensions, size)
return m
}
func (m *Morton) Create(dimensions uint8, size uint32) {
done := make(chan struct{})
mch := make(chan []uint64)
go func() {
m.CreateTables(dimensions, size)
done <- struct{}{}
}()
go func() {
mch <- MakeMagic(dimensions)
m.Magic = MakeMagic(dimensions)
}()
m.Magic = <-mch
close(mch)
<-done
close(done)
}
func (m *Morton) CreateTables(dimensions uint8, length uint32) {
ch := make(chan Table)
m.Dimensions = dimensions
for i := uint8(0); i < dimensions; i++ {
go func(i uint8) {
ch <- CreateTable(i, dimensions, length)
}(i)
}
for i := uint8(0); i < dimensions; i++ {
t := <-ch
m.Tables = append(m.Tables, t)
}
close(ch)
sort.Sort(ByTable(m.Tables))
}
func MakeMagic(dimensions uint8) []uint64 {
// Generate nth and ith bits variables
d := uint64(dimensions)
limit := 64/d + 1
nth := []uint64{0, 0, 0, 0, 0, 0}
for i := uint64(0); i < limit; i++ {
switch {
case i <= 32:
//32
nth[0] |= 1 << (i * d)
fallthrough
case i <= 16:
//16
nth[1] |= 3 << (i * (d << 1))
fallthrough
case i <= 8:
//8
nth[2] |= 0xf << (i * (d << 2))
fallthrough
case i <= 4:
//4
nth[3] |= 0xff << (i * (d << 3))
fallthrough
case i <= 2:
//2
nth[4] |= 0xffff << (i * (d << 4))
fallthrough
case i <= 1:
//1
nth[5] |= 0xffffff << (i * (d << 5))
}
}
return nth
}
func (m *Morton) Decode(code uint64) (result []uint32) {
if m.Dimensions == 0 {
return
}
d := uint64(m.Dimensions)
r := make([]uint64, d)
// Process each dimension
for i := uint64(0); i < d; i++ {
r[i] = (code >> i) & m.Magic[0]
for j := uint64(0); int(j) < len(m.Magic)-1; j++ {
r[i] = (r[i] ^ (r[i] >> ((d - 1) * (1 << j)))) & m.Magic[j+1]
}
result = append(result, uint32(r[i]))
}
return
}
func (m *Morton) Encode(vector []uint32) (result uint64, err error) {
length := len(m.Tables)
if length == 0 {
err = errors.New("No lookup tables. Please generate them via CreateTables().")
return
}
if len(vector) > length {
err = errors.New("Input vector slice length exceeds the number of lookup tables. Please regenerate them via CreateTables()")
return
}
//sort.Sort(sort.Reverse(ByUint32Index(vector)))
for k, v := range vector {
if v > uint32(len(m.Tables[k].Encode)-1) {
err = errors.New(fmt.Sprint("Input vector component, ", k, " length exceeds the corresponding lookup table's size. Please regenerate them via CreateTables() and specify the appropriate table length"))
return
}
result |= m.Tables[k].Encode[v].Value
}
return
}
func CreateTable(index, dimensions uint8, length uint32) Table {
t := Table{Index: index, Length: length}
bch := make(chan Bit)
// Build interleave queue
for i := uint32(0); i < length; i++ {
go func(i uint32) {
bch <- InterleaveBits(i, uint32(index), uint32(dimensions-1))
}(i)
}
// Pull from interleave queue
for i := uint32(0); i < length; i++ {
ib := <-bch
t.Encode = append(t.Encode, ib)
}
close(bch)
sort.Sort(ByBit(t.Encode))
return t
}
// Interleave bits of a uint32.
func InterleaveBits(value, offset, spread uint32) Bit {
ib := Bit{value, 0}
// Determine the minimum number of single shifts required. There's likely a better, and more efficient, way to do this.
n := value
limit := uint64(0)
for i := uint32(0); n != 0; i++ {
n = n >> 1
limit++
}
// Offset value for interleaving and reconcile types
v, o, s := uint64(value), uint64(offset), uint64(spread)
for i := uint64(0); i < limit; i++ {
// Interleave bits, bit by bit.
ib.Value |= (v & (1 << i)) << (i * s)
}
ib.Value = ib.Value << o
return ib
}