-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap2D.lua
More file actions
60 lines (52 loc) · 1.26 KB
/
map2D.lua
File metadata and controls
60 lines (52 loc) · 1.26 KB
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
local serpent = require 'serpent'
return function(defaultValue)
local t = {}
--[[
Cantor pairing function
\pi(k_1,k_2) := \frac{1}{2}(k_1 + k_2)(k_1 + k_2 + 1)+k_2.
--]]
local function primitiveCantorPair(k1, k2)
return 0.5 * (k1 + k2) * ((k1 + k2) + 1) + k2
end
local function generalizedCantorPair(arg)
if #arg == 2 then
return primitiveCantorPair(unpack(arg))
elseif #arg > 2 then
return primitiveCantorPair(generalizedCantorPair(arg), table.remove(arg))
else
error('Cantor pairing function need at least 2 arguments!')
end
end
local function cantorPair(...)
return generalizedCantorPair({...})
end
local load = function(data)
r, t = serpent.load(data)
return r
end
local save = function()
return serpent.dump(t)
end
setmetatable(t, {
__index = function(_, k)
if type(k)=="table" then
local i = rawget(t, cantorPair(k[1] or 1, k[2] or 1))
return i or defaultValue
elseif type(k)=="string" then
if k=="load" then
return load
elseif k=="save" then
return save
end
end
end,
__newindex = function(_, k, v)
if type(k)=="table" then
rawset(t, cantorPair(k[1] or 1, k[2] or 1), v)
else
rawset(t, k, v)
end
end,
})
return t
end