Code:
local table_IsEmpty = table.IsEmpty
local table_Count = table.Count
local Right = string.Right
local type = type
------------------------------------------------------------------------------------------------
-- Multi Dimentional Tables
------------------------------------------------------------------------------------------------
------------------------------------------------
-- Helper functions
------------------------------------------------
local function checkZero( zero, value, id )
if (type(zero) == "table" and type(value) == "table") then
if (#zero == 0) then return true end
for k,v in pairs( zero ) do
if (value[id .. k] != v) then
return true
end
end
elseif (type(zero) == type(value)) then
return (value != zero)
end
return false
end
local function upperfirst( word )
return word:Left(1):upper() .. word:Right(-2):lower()
end
local function findVar( tbl, index )
for k,v in pairs( wire_expression_types ) do
local id = v[1]
if (tbl[id..index]) then return true, id..index
end
end
return false
end
------------------------------------------------
-- Type
------------------------------------------------
registerType("mtable", "xmt", {},
function(self, input)
local ret = {}
local c = 0
for k,v in pairs(input) do c = c + 1 ret[k] = v end
self.prf = self.prf + c / 3
return ret
end,
nil,
function(retval)
if type(retval) ~= "table" then error("Return value is not a table, but a "..type(retval).."!",0) end
end,
function(v)
return type(v) ~= "table"
end
)
------------------------------------------------
-- General functions
------------------------------------------------
__e2setcost(1)
e2function mtable mtable( ... )
local tbl = {...}
if (#tbl == 0) then return {} end
local ret = {}
for k,v in ipairs( tbl ) do
self.prf = self.prf + 5
ret[typeids[k]..tostring(k)] = v
end
return ret
end
e2function mtable operator=(mtable lhs, mtable rhs)
--return rhs
local lookup = self.data.lookup
-- remove old lookup entry
if lookup[rhs] then lookup[rhs][lhs] = nil end
-- add new lookup entry
local lookup_entry = lookup[rhs]
if not lookup_entry then
lookup_entry = {}
lookup[rhs] = lookup_entry
end
lookup_entry[lhs] = true
self.vars[lhs] = rhs
self.vclk[lhs] = true
return rhs
end
e2function number operator_is( mtable op )
return table_IsEmpty( op ) and 1 or 0
end
------------------------------------------------
-- Remove
------------------------------------------------
__e2setcost(20)
e2function void mtable:remove( string index )
local bool, index = findVar( this, index )
if (bool) then
this[index] = nil
end
end
------------------------------------------------
-- Getters, setters and remove
------------------------------------------------
__e2setcost(5)
registerCallback( "postinit", function()
local getf, setf
for k,v in pairs( wire_expression_types ) do
local name = k
local id = v[1]
local def = v[2]
function getf(self, args)
local op1, op2 = args[2], args[3]
local rv1, rv2, rv3 = op1[1](self, op1), op2[1](self, op2)
if (!rv1) then return def end
if (!rv2) then return def end
if (type(rv2) == "number") then rv2 = tostring(rv2) end
if (!rv1[id..rv2]) then return def end
return rv1[id..rv2]
end
function setf(self, args)
local op1, op2, op3 = args[2], args[3], args[4]
local rv1, rv2, rv3 = op1[1](self, op1), op2[1](self, op2), op3[1](self, op3)
if (!rv1) then return def end
if (!rv2) then return def end
if (type(rv2) == "number") then rv2 = tostring(rv2) end
if (!rv3) then return def end
if (!checkZero( def, rv3, id )) then return end
rv1[id..rv2] = rv3
self.vclk[rv1] = true
end
registerOperator("idx", id.."=xmts", id, getf)
registerOperator("idx", id.."=xmts"..id, id, setf)
registerOperator("idx", id.."=xmtn", id, getf)
registerOperator("idx", id.."=xmtn"..id, id, setf)
name = upperfirst( name )
if (name == "Normal") then name = "Number" end
registerFunction("remove"..name,"xmt:s",id,function(self,args)
local op1, op2 = args[2], args[3]
local rv1, rv2 = op1[1](self, op1), op2[1](self, op2)
local bool, index = findVar( rv1, rv2 )
if (bool) then
local temp = rv1[index]
rv1[index] = nil
self.vclk[rv1] = true
return temp
end
end)
end
end)
------------------------------------------------
-- Debug
------------------------------------------------
__e2setcost(1)
e2function void printTable( mtable tbl )
if (!tbl) then return end
self.prf = self.prf + table_Count(tbl) / 2
for k,v in pairs( tbl ) do
self.player:ChatPrint( k .. " = " .. tostring(v) )
end
end
__e2setcost(nil) Example usage: Code:
T = mtable( 1,2,3, "hello", vec(5,6,7), mtable("blargh","rofl",1231) )
print("6,3:",T[6,mtable][3,number])
printTable(T) prints:
Bookmarks