2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
|
|
|
extra table routines
|
|
|
|
]]
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--apply prototype to module if it isn't the global table
|
2020-03-15 10:22:22 +00:00
|
|
|
--so it works "as if" it was the global table api
|
|
|
|
--upgraded with these routines
|
2020-04-07 03:49:10 +00:00
|
|
|
|
|
|
|
local tablex = setmetatable({}, {
|
|
|
|
__index = table,
|
|
|
|
})
|
2020-03-15 09:28:50 +00:00
|
|
|
|
|
|
|
--alias
|
2020-04-07 03:49:10 +00:00
|
|
|
tablex.join = tablex.concat
|
2020-03-15 09:28:50 +00:00
|
|
|
|
2020-04-10 04:14:15 +00:00
|
|
|
--return the front element of a table
|
|
|
|
function tablex.front(t)
|
|
|
|
return t[1]
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--return the back element of a table
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.back(t)
|
2020-01-29 03:26:28 +00:00
|
|
|
return t[#t]
|
|
|
|
end
|
|
|
|
|
|
|
|
--remove the back element of a table and return it
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.pop(t)
|
2020-01-29 03:26:28 +00:00
|
|
|
return table.remove(t)
|
|
|
|
end
|
|
|
|
|
|
|
|
--insert to the back of a table
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.push(t, v)
|
2020-01-29 03:26:28 +00:00
|
|
|
return table.insert(t, v)
|
|
|
|
end
|
|
|
|
|
|
|
|
--remove the front element of a table and return it
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.shift(t)
|
2020-01-29 03:26:28 +00:00
|
|
|
return table.remove(t, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
--insert to the front of a table
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unshift(t, v)
|
2020-01-29 03:26:28 +00:00
|
|
|
return table.insert(t, 1, v)
|
|
|
|
end
|
|
|
|
|
|
|
|
--find the index in a sequential table that a resides at
|
|
|
|
--or nil if nothing was found
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.index_of(t, a)
|
2020-01-29 03:26:28 +00:00
|
|
|
if a == nil then return nil end
|
|
|
|
for i,b in ipairs(t) do
|
|
|
|
if a == b then
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2020-04-10 04:14:15 +00:00
|
|
|
--find the key in a keyed table that a resides at
|
|
|
|
--or nil if nothing was found
|
|
|
|
function tablex.key_of(t, a)
|
|
|
|
if a == nil then return nil end
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if a == v then
|
|
|
|
return k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--remove the first instance of value from a table (linear search)
|
|
|
|
--returns true if the value was removed, else false
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.remove_value(t, a)
|
|
|
|
local i = tablex.index_of(t, a)
|
2020-01-29 03:26:28 +00:00
|
|
|
if i then
|
|
|
|
table.remove(t, i)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--add a value to a table if it doesn't already exist (linear search)
|
|
|
|
--returns true if the value was added, else false
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.add_value(t, a)
|
|
|
|
local i = tablex.index_of(t, a)
|
2020-01-29 03:26:28 +00:00
|
|
|
if not i then
|
|
|
|
table.insert(t, a)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-04-10 04:14:15 +00:00
|
|
|
--note: keyed versions of the above aren't required; you can't double
|
|
|
|
--up values under keys
|
|
|
|
|
2020-04-08 10:51:16 +00:00
|
|
|
--helper for optionally passed random; defaults to love.math.random if present, otherwise math.random
|
|
|
|
local _global_random = math.random
|
|
|
|
if love and love.math and love.math.random then
|
|
|
|
_global_random = love.math.random
|
|
|
|
end
|
2020-01-29 03:26:28 +00:00
|
|
|
local function _random(min, max, r)
|
|
|
|
return r and r:random(min, max)
|
|
|
|
or _global_random(min, max)
|
|
|
|
end
|
|
|
|
|
|
|
|
--pick a random value from a table (or nil if it's empty)
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.pick_random(t, r)
|
2020-01-29 03:26:28 +00:00
|
|
|
if #t == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return t[_random(1, #t, r)]
|
|
|
|
end
|
|
|
|
|
|
|
|
--shuffle the order of a table
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.shuffle(t, r)
|
2020-01-29 03:26:28 +00:00
|
|
|
for i = 1, #t do
|
|
|
|
local j = _random(1, #t, r)
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
end
|
2020-02-05 10:16:23 +00:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
--reverse the order of a table
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.reverse(t)
|
2020-02-05 10:16:23 +00:00
|
|
|
for i = 1, #t / 2 do
|
|
|
|
local j = #t - i + 1
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
end
|
|
|
|
return t
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--collect all keys of a table into a sequential table
|
|
|
|
--(useful if you need to iterate non-changing keys often and want an nyi tradeoff;
|
|
|
|
-- this call will be slow but then following iterations can use ipairs)
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.keys(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
local r = {}
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
table.insert(r, k)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2020-03-15 09:28:50 +00:00
|
|
|
return r
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--collect all values of a keyed table into a sequential table
|
|
|
|
--(shallow copy if it's already sequential)
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.values(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
local r = {}
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
table.insert(r, v)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2020-03-15 09:28:50 +00:00
|
|
|
return r
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2020-03-12 10:15:18 +00:00
|
|
|
|
2020-04-08 10:50:51 +00:00
|
|
|
--append sequence t2 into t1, modifying t1
|
|
|
|
function tablex.append_inplace(t1, t2)
|
|
|
|
for i,v in ipairs(t2) do
|
|
|
|
table.insert(t1, v)
|
|
|
|
end
|
|
|
|
return t1
|
|
|
|
end
|
|
|
|
|
|
|
|
--return a new sequence with the elements of both t1 and t2
|
|
|
|
function tablex.append(t1, t2)
|
|
|
|
local r = {}
|
|
|
|
tablex.append_inplace(r, t1)
|
|
|
|
tablex.append_inplace(r, t2)
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2020-04-08 11:18:52 +00:00
|
|
|
--return a copy of a sequence with all duplicates removed
|
|
|
|
-- causes a little "extra" gc churn of one table to track the duplicates internally
|
|
|
|
function tablex.dedupe(t)
|
|
|
|
local seen = {}
|
|
|
|
local r = {}
|
|
|
|
for i,v in ipairs(t) do
|
|
|
|
if not seen[v] then
|
|
|
|
seen[v] = true
|
|
|
|
table.insert(r, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--(might already exist depending on luajit)
|
2020-04-08 10:50:51 +00:00
|
|
|
if table.clear then
|
|
|
|
--import from global if it exists
|
|
|
|
tablex.clear = table.clear
|
|
|
|
else
|
|
|
|
--remove all values from a table
|
|
|
|
--useful when multiple references are being held
|
|
|
|
--so you cannot just create a new table
|
|
|
|
function tablex.clear(t)
|
|
|
|
assert(type(t) == "table", "table.clear - argument 't' must be a table")
|
|
|
|
local k = next(t)
|
|
|
|
while k ~= nil do
|
|
|
|
t[k] = nil
|
|
|
|
k = next(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--note:
|
|
|
|
-- copies and overlays are currently not satisfactory
|
|
|
|
--
|
|
|
|
-- i feel that copy especially tries to do too much and
|
|
|
|
-- probably they should be split into separate functions
|
|
|
|
-- to be both more explicit and performant, ie
|
|
|
|
--
|
|
|
|
-- shallow_copy, deep_copy, shallow_overlay, deep_overlay
|
|
|
|
--
|
|
|
|
-- input is welcome on this :)
|
|
|
|
|
2020-03-12 10:15:18 +00:00
|
|
|
--copy a table
|
|
|
|
-- deep_or_into is either:
|
|
|
|
-- a boolean value, used as deep flag directly
|
|
|
|
-- or a table to copy into, which implies a deep copy
|
|
|
|
-- if deep specified:
|
|
|
|
-- calls copy method of member directly if it exists
|
|
|
|
-- and recurses into all "normal" table children
|
|
|
|
-- if into specified, copies into that table
|
|
|
|
-- but doesn't clear anything out
|
|
|
|
-- (useful for deep overlays and avoiding garbage)
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.copy(t, deep_or_into)
|
2020-03-12 10:15:18 +00:00
|
|
|
assert(type(t) == "table", "table.copy - argument 't' must be a table")
|
|
|
|
local is_bool = type(deep_or_into) == "boolean"
|
2020-04-07 03:49:10 +00:00
|
|
|
local istablex = type(deep_or_into) == "table"
|
2020-03-12 10:15:18 +00:00
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
local deep = (is_bool and deep_or_into) or istablex
|
|
|
|
local into = istablex and deep_or_into or {}
|
2020-03-12 10:15:18 +00:00
|
|
|
for k,v in pairs(t) do
|
|
|
|
if deep and type(v) == "table" then
|
|
|
|
if type(v.copy) == "function" then
|
|
|
|
v = v:copy()
|
|
|
|
else
|
2020-04-07 03:49:10 +00:00
|
|
|
v = tablex.copy(v, deep)
|
2020-03-12 10:15:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
into[k] = v
|
|
|
|
end
|
|
|
|
return into
|
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--overlay one table directly onto another, shallow only
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.overlay(to, from)
|
2020-03-15 09:28:50 +00:00
|
|
|
assert(type(to) == "table", "table.overlay - argument 'to' must be a table")
|
|
|
|
assert(type(from) == "table", "table.overlay - argument 'from' must be a table")
|
|
|
|
for k,v in pairs(from) do
|
|
|
|
to[k] = v
|
|
|
|
end
|
|
|
|
return to
|
|
|
|
end
|
|
|
|
|
2020-04-08 10:50:51 +00:00
|
|
|
--turn a table into a vaguely easy to read string
|
|
|
|
--which is also able to be parsed by lua in most cases
|
|
|
|
function tablex.stringify(t)
|
2020-04-08 11:10:24 +00:00
|
|
|
--if the input is not a table, or it has a tostring metamethod
|
|
|
|
--just use tostring
|
|
|
|
local mt = getmetatable(t)
|
|
|
|
if type(t) ~= "table" or mt and mt.__tostring then
|
2020-04-08 10:50:51 +00:00
|
|
|
return tostring(t)
|
|
|
|
end
|
2020-04-08 11:10:24 +00:00
|
|
|
|
|
|
|
--otherwise, collate into member chunks
|
2020-04-08 10:50:51 +00:00
|
|
|
local chunks = {}
|
2020-04-08 11:10:24 +00:00
|
|
|
--(tracking for already-seen elements from ipairs)
|
2020-04-08 10:50:51 +00:00
|
|
|
local seen = {}
|
|
|
|
--sequential part first
|
|
|
|
for i, v in ipairs(t) do
|
|
|
|
seen[i] = true
|
|
|
|
table.insert(chunks, tablex.stringify(v))
|
|
|
|
end
|
|
|
|
--non sequential follows
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if not seen[k] then
|
|
|
|
--encapsulate anything that's not a string
|
|
|
|
--todo: also keywords
|
|
|
|
if type(k) ~= "string" then
|
|
|
|
k = "[" .. tostring(k) .. "]"
|
|
|
|
end
|
|
|
|
table.insert(chunks, k .. " = " .. tablex.stringify(v))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return "{" .. table.concat(chunks, ", ") .. "}"
|
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--faster unpacking for known-length tables up to 8
|
|
|
|
--gets around nyi in luajit
|
|
|
|
--note: you can use a larger unpack than you need as the rest
|
|
|
|
-- can be discarded, but it "feels dirty" :)
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unpack2(t)
|
2020-03-15 10:22:22 +00:00
|
|
|
return t[1], t[2]
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unpack3(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
return t[1], t[2], t[3]
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unpack4(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
return t[1], t[2], t[3], t[4]
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unpack5(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
return t[1], t[2], t[3], t[4], t[5]
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unpack6(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
return t[1], t[2], t[3], t[4], t[5], t[6]
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unpack7(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
return t[1], t[2], t[3], t[4], t[5], t[6], t[7]
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unpack8(t)
|
2020-03-15 09:28:50 +00:00
|
|
|
return t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]
|
|
|
|
end
|
2020-04-07 03:49:10 +00:00
|
|
|
|
|
|
|
return tablex
|