local table = _G.table or {}; table.copy = function(t) local new = {}; if not t or type(t) ~= 'table' or next(t) == nil then return new end for k, v in next, t, nil do if type(v) == 'table' then new[k] = table.copy(v) else new[k] = v end end return new; end table.size = function(t, iterate) if not t or type(t) ~= 'table' or next(t) == nil then return 0 end if iterate then return #t end local q = 0 for _ in next, t, nil do q = q + 1 end return q end table.find = function(tbl, value) if not tbl or type(tbl) ~= 'table' or next(tbl) == nil then return nil end for key, val in next, tbl, nil do if val == value then return key, val end end return nil end table.ifind = function(tbl, value) if not tbl or type(tbl) ~= 'table' or next(tbl) == nil then return nil end for key, val in next, tbl, nil do if key == value then return key, val end end return nil end table.flatten = function (t, prefix) if not t or type(t) ~= 'table' or next(t) == nil then return {} end local flat = {} prefix = prefix or '' for k,v in next, t, nil do local key = prefix .. k if type(v) == 'table' then local sub = table.flatten(v, key..'.'); for sk, sv in next, sub, nil do flat[sk] = sv end else flat[key] = v end end return flat end table.compare = function(t1, t2) if (t1 == t2) then return true end if (type(t1) ~= 'table' or type(t2) ~= 'table') then return false end for k in pairs(t2) do if (t1[k] == nil) then return false end end for k, v in pairs(t1) do if (type(v) == 'table') then if (not table.compare(v, t2[k])) then return false end else if (v ~= t2[k]) then return false end end end return true end table.merge = function (base, override) local result = {} if type(base) == 'table' then for k, v in pairs(base) do result[k] = v end end if type(override) == 'table' then for k, v in pairs(override) do result[k] = v end end return result end table.deepmerge = function (from, to, options) options = type(options) == "table" and options or { allowNew = true } from = type(from) == "table" and from or {} to = type(to) == "table" and to or {} local result = from for key, value in pairs(to) do local from_type = type(from[key]) local to_type = type(value) if (from_type == "nil" and not options.allowNew) and -- It's okay to merge previous nonexistent values if 'allowNew' is specified from_type ~= to_type then error(string.format("'table.deepmerge' failed to merge incompatible types from '%s' to '%s' on key '%s'", from_type, to_type, key)) end if to_type == "table" then result[key] = table.deepmerge(from[key], value) else result[key] = to[key] end end return result end table.element = function (t, elemType, _aux) local elem = _aux or {} for k, v in pairs(t) do if (type(v) == "table") then table.element(v, elemType, elem) else if (type(v) == "userdata") then if elemType then if (getElementType(v) == elemType) then table.insert(elem, v) end else table.insert(elem, v) end end end end return elem end table.random = function(t) local tSize = table.size(t) if tSize == 0 then return nil end local random = math.random(1, tSize) local i = 0 for key, value in next, t, nil do i = i + 1 if i == random then return key, value end end end table.map = function (tab, depth, func, ...) for key, value in pairs(tab) do if (type(value) == "table" and depth ~= 0) then tab[key] = table.map(value, depth - 1, func, ...) else tab[key] = func(key, value, ...) end end return tab end table.flip = function (theTable) assert(type(theTable) == "table", "Bad argument @ 'table.flip' [Expected table at argument 1, got "..(type(theTable)).."]") local newTable = {} for i = 1, #theTable do newTable[i] = theTable[#theTable-(i-1)] end return newTable end table.addChangeHandler = function (tablename, func) local tablename, func = tablename, func local parts = {} local name local t = _G local i = 0 for word in tablename:gmatch("[^%.]+") do i = i + 1 parts[i] = word end for i = 1, #parts-1 do name = not name and parts[i] or name..'.'..parts[i] if t[parts[i]] then t = t[parts[i]] else return error('The table ["'..name..'"] does not exist' ) end end local last = parts[#parts] if not t[last] then return error('The table ["'..(name..'.'..last)..'"] does not exist') end local old = t[last] t[last] = setmetatable({}, { __index = old, __newindex = function(self, key, value) local oldValue = rawget(old, key) rawset(old, key, value) if func then func(tablename, old, key, oldValue, value) end end, }) return t[last] end addTableChangeHandler = function(...) return table.addChangeHandler(...) end pairsByKeys = function(t) local a = {} for n in pairs(t) do table.insert(a, n) end table.sort(a, f) local i = 0 local iter = function() i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end return table;