Skip to content

Instantly share code, notes, and snippets.

@Nimblz
Last active Mar 31, 2020
Embed
What would you like to do?
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