Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Nimblz
Last active March 31, 2020 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nimblz/4beeed75a8491722303732abec369e53 to your computer and use it in GitHub Desktop.
Save Nimblz/4beeed75a8491722303732abec369e53 to your computer and use it in GitHub Desktop.
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