Last active
March 31, 2020 14:48
-
-
Save Nimblz/4beeed75a8491722303732abec369e53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Table3d = {} | |
function Table3d.new(defaultValue) | |
local self = setmetatable({},{__index = Table3d}) | |
self.defaultValue = defaultValue | |
self._table = {} | |
return self | |
end | |
function Table3d:get(x, y, z) | |
local t = self._table | |
local default = self.defaultValue | |
if not t[x] then return default end | |
if not t[x][y] then return default end | |
return t[x][y][z] or default | |
end | |
function Table3d:set(x, y, z, value) | |
local t = self._table | |
t[x] = t[x] or {} | |
t[x][y] = t[x][y] or {} | |
t[x][y][z] = value | |
self:prune(x,y) | |
end | |
function Table3d:prune(x,y) | |
local t = self._table | |
if next(t[x][y]) == nil then | |
t[x][y] = nil | |
end | |
if next(t[x]) == nil then | |
t[x] = nil | |
end | |
end | |
return Table3d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment