-
Notifications
You must be signed in to change notification settings - Fork 2
/
hilbert.lua
213 lines (166 loc) · 4.49 KB
/
hilbert.lua
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
--- Hilbert curve-type space filling operations.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
-- Standard library imports --
local remove = table.remove
-- Modules --
local operators = require("bitwise_ops.operators")
-- Forward references --
local band
local bor
local lshift
local rshift
-- Imports --
if operators.HasBitLib() then -- Bit library available
band = operators.band
bor = operators.bor
lshift = operators.lshift
rshift = operators.rshift
else -- Otherwise, make equivalents for Hilbert curve purposes
function band (x, n)
return x % (n + 1)
end
function bor (a, b, c)
return a + b + (c or 0)
end
lshift = operators.lshift
local floor = math.floor
local lastn, power
function rshift (x, n)
if n ~= lastn then
lastn, power = n, lshift(1, n)
end
return floor(x / power)
end
end
-- Exports --
local M = {}
--
--
--
--
local function AuxHilbert (order, dir, rot, step)
order = order - 1
if order >= 0 then
dir = dir + rot
AuxHilbert(order, dir, -rot, step)
step(dir)
dir = dir - rot
AuxHilbert(order, dir, rot, step)
step(dir)
AuxHilbert(order, dir, rot, step)
dir = dir - rot
step(dir)
AuxHilbert(order, dir, -rot, step)
end
end
-- --
local Step = {}
-- --
local Ways = { "right", "down", "left", "up" }
--- DOCME
-- [LINK](http://www.hackersdelight.org/HDcode/hilbert/hilgen2.c.txt)
-- @uint order
-- @callable func
function M.ForEach (order, func)
local x, y, s
local step = remove(Step) or function(dir, arg)
if arg == false then
func = nil
elseif arg then
x, y, s, func = -1, 0, 0, arg
else
dir = band(dir, 3)
if dir == 0 or dir == 2 then
x = x + 1 - dir
else
y = y + 2 - dir
end
func(s, x, y, Ways[dir + 1])
s = s + 1
end
end
step(nil, func)
step(0)
AuxHilbert(order, 0, 1, step)
step(nil, false)
Step[#Step + 1] = step
end
--
local function MaskedShift (x, n, mask)
return band(rshift(x, n), mask)
end
--- DOCME
-- [LINK](http://www.hackersdelight.org/HDcode/hilbert/hil_xy_from_s.c.txt)
-- @uint order
-- @uint s
-- @treturn uint X
-- @treturn uint Y
function M.GetXY (order, s)
local x, y, state = 0, 0, 0
for i = 2 * order - 2, 0, -2 do
local row = bor(4 * state, MaskedShift(s, i, 3))
x = bor(2 * x, MaskedShift(0x936C, row, 1))
y = bor(2 * y, MaskedShift(0x39C6, row, 1))
state = MaskedShift(0x3E6B94C1, 2 * row, 3)
end
return x, y
end
--- DOCME
-- [LINK](http://www.hackersdelight.org/HDcode/hilbert/hil_inc_xy.c.txt)
-- @uint order
-- @uint[opt=0] x
-- @uint[opt=0] y
-- @treturn uint X
-- @treturn uint Y
function M.GetXY_Incremental (order, x, y)
x, y = x or 0, y or 0
local state, dx, dy = 0
for i = order - 1, 0, -1 do
local row = bor(4 * state, 2 * MaskedShift(x, i, 1), MaskedShift(y, i, 1))
local r2 = 2 * row
if MaskedShift(0xBDDB, row, 1) ~= 0 then
dx = MaskedShift(0x16451659, r2, 3) - 1
dy = MaskedShift(0x51166516, r2, 3) - 1
end
state = MaskedShift(0x8FE65831, r2, 3)
end
return x + (dx or -(lshift(1, order) - 1)), y + (dy or 0)
end
--- DOCME
-- [LINK](http://www.hackersdelight.org/HDcode/hilbert/hil_s_from_xy.c.txt)
-- @uint order
-- @uint x
-- @uint y
-- @treturn uint S
function M.GetS (order, x, y)
local s, state = 0, 0
for i = order - 1, 0, -1 do
local row = bor(4 * state, 2 * MaskedShift(x, i, 1), MaskedShift(y, i, 1))
local r2 = 2 * row
s = bor(4 * s, MaskedShift(0x361E9CB4, r2, 3))
state = MaskedShift(0x8FE65831, r2, 3)
end
return s
end
return M