2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
|
|
|
extra table routines
|
|
|
|
]]
|
|
|
|
|
2021-11-11 02:42:03 +00:00
|
|
|
local path = (...):gsub("tablex", "")
|
2020-05-19 02:03:45 +00:00
|
|
|
local assert = require(path .. "assert")
|
|
|
|
|
2022-05-23 04:48:52 +00:00
|
|
|
--for spairs
|
|
|
|
--(can be replaced with eg table.sort to use that instead)
|
|
|
|
local sort = require(path .. "sort")
|
|
|
|
local spairs_sort = sort.stable_sort
|
|
|
|
|
|
|
|
--apply prototype to module if it isn't the global table
|
|
|
|
--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
|
|
|
|
|
2020-04-14 03:53:10 +00:00
|
|
|
--insert to the back of a table, returning the table for possible chaining
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.push(t, v)
|
2020-04-14 03:53:10 +00:00
|
|
|
table.insert(t, v)
|
|
|
|
return t
|
2020-01-29 03:26:28 +00:00
|
|
|
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
|
|
|
|
|
2020-04-14 03:53:10 +00:00
|
|
|
--insert to the front of a table, returning the table for possible chaining
|
2020-04-07 03:49:10 +00:00
|
|
|
function tablex.unshift(t, v)
|
2020-04-14 03:53:10 +00:00
|
|
|
table.insert(t, 1, v)
|
|
|
|
return t
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-05-04 10:52:47 +00:00
|
|
|
--swap two indices of a table
|
|
|
|
--(easier to read and generally less typing than the common idiom)
|
|
|
|
function tablex.swap(t, i, j)
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
end
|
2021-06-09 09:49:04 +00:00
|
|
|
|
|
|
|
--swap the element at i to the back of the table, and remove it
|
|
|
|
--avoids linear cost of removal at the expense of messing with the order of the table
|
|
|
|
function tablex.swap_and_pop(t, i)
|
|
|
|
tablex.swap(t, i, #t)
|
|
|
|
return tablex.pop(t)
|
|
|
|
end
|
2021-10-03 09:31:55 +00:00
|
|
|
|
2021-10-07 23:17:34 +00:00
|
|
|
--rotate the elements of a table t by amount slots
|
|
|
|
-- amount 1: {1, 2, 3, 4} -> {2, 3, 4, 1}
|
|
|
|
-- amount -1: {1, 2, 3, 4} -> {4, 1, 2, 3}
|
2021-10-03 09:31:55 +00:00
|
|
|
function tablex.rotate(t, amount)
|
2021-10-07 23:17:34 +00:00
|
|
|
if #t > 1 then
|
2021-10-07 23:19:30 +00:00
|
|
|
while amount >= 1 do
|
2021-10-07 23:17:34 +00:00
|
|
|
tablex.push(t, tablex.shift(t))
|
|
|
|
amount = amount - 1
|
|
|
|
end
|
2021-10-07 23:19:30 +00:00
|
|
|
while amount <= -1 do
|
2021-10-07 23:17:34 +00:00
|
|
|
tablex.unshift(t, tablex.pop(t))
|
|
|
|
amount = amount + 1
|
|
|
|
end
|
2021-10-03 09:31:55 +00:00
|
|
|
end
|
2021-10-07 23:17:34 +00:00
|
|
|
return t
|
2021-10-03 09:31:55 +00:00
|
|
|
end
|
|
|
|
|
2022-05-23 04:48:52 +00:00
|
|
|
--default comparison from sort.lua
|
|
|
|
local default_less = sort.default_less
|
2021-04-30 01:46:51 +00:00
|
|
|
|
|
|
|
--check if a function is sorted based on a "less" or "comes before" ordering comparison
|
|
|
|
--if any item is "less" than the item before it, we are not sorted
|
|
|
|
--(use stable_sort to )
|
|
|
|
function tablex.is_sorted(t, less)
|
|
|
|
less = less or default_less
|
|
|
|
for i = 1, #t - 1 do
|
|
|
|
if less(t[i + 1], t[i]) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
2021-04-16 00:09:04 +00:00
|
|
|
end
|
|
|
|
|
2020-04-14 11:52:33 +00:00
|
|
|
--insert to the first position before the first larger element in the table
|
2021-05-11 00:59:59 +00:00
|
|
|
-- ({1, 2, 2, 3}, 2) -> {1, 2, 2, 2 (inserted here), 3}
|
2020-04-14 11:52:33 +00:00
|
|
|
--if this is used on an already sorted table, the table will remain sorted and not need re-sorting
|
2021-05-11 00:59:59 +00:00
|
|
|
--(you can sort beforehand if you don't know)
|
2020-04-14 11:52:33 +00:00
|
|
|
--return the table for possible chaining
|
|
|
|
function tablex.insert_sorted(t, v, less)
|
2021-04-30 01:46:51 +00:00
|
|
|
less = less or default_less
|
|
|
|
local low = 1
|
|
|
|
local high = #t
|
|
|
|
while low <= high do
|
|
|
|
local mid = math.floor((low + high) / 2)
|
|
|
|
local mid_val = t[mid]
|
|
|
|
if less(v, mid_val) then
|
|
|
|
high = mid - 1
|
|
|
|
else
|
2021-05-11 00:59:59 +00:00
|
|
|
low = mid + 1
|
2020-04-14 11:52:33 +00:00
|
|
|
end
|
|
|
|
end
|
2021-05-11 00:59:59 +00:00
|
|
|
table.insert(t, low, v)
|
2020-04-14 11:52:33 +00:00
|
|
|
return t
|
|
|
|
end
|
2021-04-30 01:46:51 +00:00
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--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)
|
2020-07-02 07:04:47 +00:00
|
|
|
return r and r:random(min, max)
|
2020-01-29 03:26:28 +00:00
|
|
|
or _global_random(min, max)
|
|
|
|
end
|
|
|
|
|
2021-07-05 22:29:45 +00:00
|
|
|
--pick a random value from a table (or nil if it's empty)
|
|
|
|
function tablex.random_index(t, r)
|
|
|
|
if #t == 0 then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return _random(1, #t, r)
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--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
|
2021-07-05 22:29:45 +00:00
|
|
|
return t[tablex.random_index(t, r)]
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-03-09 09:07:41 +00:00
|
|
|
--take a random value from a table (or nil if it's empty)
|
|
|
|
function tablex.take_random(t, r)
|
|
|
|
if #t == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
2021-07-05 22:29:45 +00:00
|
|
|
return table.remove(t, tablex.random_index(t, r))
|
2021-03-09 09:07:41 +00:00
|
|
|
end
|
|
|
|
|
2021-11-22 23:58:56 +00:00
|
|
|
--return a random value from table t based on weights w provided (or nil empty)
|
|
|
|
-- w should be the same length as t
|
|
|
|
-- todo:
|
2021-11-17 23:35:26 +00:00
|
|
|
-- provide normalisation outside of this function, require normalised weights
|
2021-11-18 09:08:29 +00:00
|
|
|
function tablex.pick_weighted_random(t, w, r)
|
2021-11-17 18:20:04 +00:00
|
|
|
if #t == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
2021-11-22 23:58:56 +00:00
|
|
|
if #w ~= #t then
|
|
|
|
error("tablex.pick_weighted_random weight and value tables should be the same length")
|
|
|
|
end
|
2021-11-17 18:20:04 +00:00
|
|
|
local sum = 0
|
2021-11-18 09:08:29 +00:00
|
|
|
for _, weight in ipairs(w) do
|
2021-11-17 18:20:04 +00:00
|
|
|
sum = sum + weight
|
|
|
|
end
|
|
|
|
local rnd = _random(nil, nil, r) * sum
|
|
|
|
sum = 0
|
2021-11-18 09:08:29 +00:00
|
|
|
for i, weight in ipairs(w) do
|
2021-11-17 18:20:04 +00:00
|
|
|
sum = sum + weight
|
|
|
|
if rnd <= sum then
|
2021-11-18 09:08:29 +00:00
|
|
|
return t[i]
|
2021-11-17 18:20:04 +00:00
|
|
|
end
|
|
|
|
end
|
2021-11-17 23:35:26 +00:00
|
|
|
--shouldn't get here but safety if using a random that returns >= 1
|
2021-11-18 09:08:29 +00:00
|
|
|
return tablex.back(t)
|
2021-11-17 18:20:04 +00:00
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--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
|
2021-05-20 19:33:52 +00:00
|
|
|
local j = _random(i, #t, r)
|
2020-01-29 03:26:28 +00:00
|
|
|
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-08-24 10:54:43 +00:00
|
|
|
--trim a table to a certain maximum length
|
|
|
|
function tablex.trim(t, l)
|
|
|
|
while #t > l do
|
|
|
|
table.remove(t)
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
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 = {}
|
2022-03-07 01:00:05 +00:00
|
|
|
for k, v in pairs(t) do
|
2020-03-15 09:28:50 +00:00
|
|
|
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 = {}
|
2022-03-07 01:00:05 +00:00
|
|
|
for k, v in pairs(t) do
|
2020-03-15 09:28:50 +00:00
|
|
|
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
|
2021-03-05 01:07:28 +00:00
|
|
|
function tablex.append_inplace(t1, t2, ...)
|
2022-03-07 01:00:05 +00:00
|
|
|
for i, v in ipairs(t2) do
|
2020-04-08 10:50:51 +00:00
|
|
|
table.insert(t1, v)
|
|
|
|
end
|
2021-03-05 01:07:28 +00:00
|
|
|
if ... then
|
2022-03-02 18:12:30 +00:00
|
|
|
return tablex.append_inplace(t1, ...)
|
2021-03-05 01:07:28 +00:00
|
|
|
end
|
2020-04-08 10:50:51 +00:00
|
|
|
return t1
|
|
|
|
end
|
|
|
|
|
|
|
|
--return a new sequence with the elements of both t1 and t2
|
2021-03-05 01:07:28 +00:00
|
|
|
function tablex.append(t1, ...)
|
2020-04-08 10:50:51 +00:00
|
|
|
local r = {}
|
2021-03-05 01:07:28 +00:00
|
|
|
tablex.append_inplace(r, t1, ...)
|
2020-04-08 10:50:51 +00:00
|
|
|
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 = {}
|
2022-03-07 01:00:05 +00:00
|
|
|
for i, v in ipairs(t) do
|
2020-04-08 11:18:52 +00:00
|
|
|
if not seen[v] then
|
|
|
|
seen[v] = true
|
|
|
|
table.insert(r, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2021-06-29 04:46:40 +00:00
|
|
|
--(might already exist depending on environment)
|
2021-05-04 00:38:31 +00:00
|
|
|
if not tablex.clear then
|
2021-06-29 04:46:40 +00:00
|
|
|
local imported
|
|
|
|
--pull in from luajit if possible
|
|
|
|
imported, tablex.clear = pcall(require, "table.clear")
|
|
|
|
if not imported then
|
|
|
|
--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", "tablex.clear - t", 1)
|
|
|
|
local k = next(t)
|
|
|
|
while k ~= nil do
|
|
|
|
t[k] = nil
|
|
|
|
k = next(t)
|
|
|
|
end
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 16:11:57 +00:00
|
|
|
-- Copy a table
|
2022-03-04 05:59:18 +00:00
|
|
|
-- See shallow_overlay to shallow copy into an existing table to avoid garbage.
|
2022-03-02 16:11:57 +00:00
|
|
|
function tablex.shallow_copy(t)
|
2022-03-07 01:00:05 +00:00
|
|
|
assert:type(t, "table", "tablex.shallow_copy - t", 1)
|
2022-03-04 05:59:18 +00:00
|
|
|
if type(t) == "table" then
|
|
|
|
local into = {}
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
into[k] = v
|
2020-03-12 10:15:18 +00:00
|
|
|
end
|
2022-03-04 05:59:18 +00:00
|
|
|
return into
|
2020-03-12 10:15:18 +00:00
|
|
|
end
|
2022-03-04 05:59:18 +00:00
|
|
|
return t
|
2020-03-12 10:15:18 +00:00
|
|
|
end
|
|
|
|
|
2022-03-07 01:00:05 +00:00
|
|
|
--alias
|
|
|
|
tablex.copy = tablex.shallow_copy
|
|
|
|
|
|
|
|
--implementation for deep copy
|
|
|
|
--traces stuff that has already been copied, to handle circular references
|
|
|
|
local function _deep_copy_impl(t, already_copied)
|
2022-03-04 05:59:18 +00:00
|
|
|
local clone = t
|
|
|
|
if type(t) == "table" then
|
2022-03-07 01:00:05 +00:00
|
|
|
if already_copied[t] then
|
|
|
|
--something we've already encountered before
|
|
|
|
clone = already_copied[t]
|
2022-03-04 05:59:18 +00:00
|
|
|
elseif type(t.copy) == "function" then
|
2022-03-07 01:00:05 +00:00
|
|
|
--something that provides its own copy function
|
2022-03-04 05:59:18 +00:00
|
|
|
clone = t:copy()
|
2022-03-07 01:00:05 +00:00
|
|
|
assert:type(clone, "table", "member copy() function didn't return a copy")
|
2022-03-04 05:59:18 +00:00
|
|
|
else
|
2022-03-07 01:00:05 +00:00
|
|
|
--a plain table to clone
|
2022-03-04 05:59:18 +00:00
|
|
|
clone = {}
|
2022-06-09 06:36:02 +00:00
|
|
|
already_copied[t] = clone
|
2022-03-04 05:59:18 +00:00
|
|
|
for k, v in pairs(t) do
|
2022-03-07 01:00:05 +00:00
|
|
|
clone[k] = _deep_copy_impl(v, already_copied)
|
2020-03-12 10:15:18 +00:00
|
|
|
end
|
2022-03-04 05:59:18 +00:00
|
|
|
setmetatable(clone, getmetatable(t))
|
2020-03-12 10:15:18 +00:00
|
|
|
end
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
2022-03-04 05:59:18 +00:00
|
|
|
return clone
|
2020-03-12 10:15:18 +00:00
|
|
|
end
|
2022-03-07 01:00:05 +00:00
|
|
|
|
2022-03-02 16:11:57 +00:00
|
|
|
-- Recursively copy values of a table.
|
|
|
|
-- Retains the same keys as original table -- they're not cloned.
|
|
|
|
function tablex.deep_copy(t)
|
2022-03-07 01:00:05 +00:00
|
|
|
assert:type(t, "table", "tablex.deep_copy - t", 1)
|
|
|
|
return _deep_copy_impl(t, {})
|
2022-03-02 16:11:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Overlay tables directly onto one another, merging them together.
|
|
|
|
-- Doesn't merge tables within.
|
|
|
|
-- Takes as many tables as required,
|
|
|
|
-- overlays them in passed order onto the first,
|
|
|
|
-- and returns the first table.
|
|
|
|
function tablex.shallow_overlay(dest, ...)
|
|
|
|
assert:type(dest, "table", "tablex.shallow_overlay - dest", 1)
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
local t = select(i, ...)
|
|
|
|
assert:type(t, "table", "tablex.shallow_overlay - ...", 1)
|
2022-03-07 01:00:05 +00:00
|
|
|
for k, v in pairs(t) do
|
2022-03-02 16:11:57 +00:00
|
|
|
dest[k] = v
|
|
|
|
end
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
2022-03-02 16:11:57 +00:00
|
|
|
return dest
|
|
|
|
end
|
|
|
|
|
2022-03-07 01:00:05 +00:00
|
|
|
tablex.overlay = tablex.shallow_overlay
|
|
|
|
|
2022-03-02 16:11:57 +00:00
|
|
|
-- Overlay tables directly onto one another, merging them together into something like a union.
|
|
|
|
-- Also overlays nested tables, but doesn't clone them (so a nested table may be added to dest).
|
|
|
|
-- Takes as many tables as required,
|
|
|
|
-- overlays them in passed order onto the first,
|
|
|
|
-- and returns the first table.
|
|
|
|
function tablex.deep_overlay(dest, ...)
|
|
|
|
assert:type(dest, "table", "tablex.deep_overlay - dest", 1)
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
local t = select(i, ...)
|
|
|
|
assert:type(t, "table", "tablex.deep_overlay - ...", 1)
|
2022-03-07 01:00:05 +00:00
|
|
|
for k, v in pairs(t) do
|
2022-03-02 16:11:57 +00:00
|
|
|
if type(v) == "table" and type(dest[k]) == "table" then
|
|
|
|
tablex.deep_overlay(dest[k], v)
|
|
|
|
else
|
|
|
|
dest[k] = v
|
|
|
|
end
|
|
|
|
end
|
2020-05-12 04:14:08 +00:00
|
|
|
end
|
2022-03-02 16:11:57 +00:00
|
|
|
return dest
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
|
|
|
|
2020-04-29 10:57:05 +00:00
|
|
|
--collapse the first level of a table into a new table of reduced dimensionality
|
|
|
|
--will collapse {{1, 2}, 3, {4, 5, 6}} into {1, 2, 3, 4, 5, 6}
|
|
|
|
--useful when collating multiple result sets, or when you got 2d data when you wanted 1d data.
|
|
|
|
--in the former case you may just want to append_inplace though :)
|
|
|
|
--note that non-tabular elements in the base level are preserved,
|
|
|
|
-- but _all_ tables are collapsed; this includes any table-based types (eg a batteries.vec2),
|
|
|
|
-- so they can't exist in the base level
|
|
|
|
-- (... or at least, their non-ipairs members won't survive the collapse)
|
|
|
|
function tablex.collapse(t)
|
2020-05-19 02:03:45 +00:00
|
|
|
assert:type(t, "table", "tablex.collapse - t", 1)
|
2020-04-29 10:57:05 +00:00
|
|
|
local r = {}
|
|
|
|
for _, v in ipairs(t) do
|
|
|
|
if type(v) == "table" then
|
2022-03-02 18:03:57 +00:00
|
|
|
for _, w in ipairs(v) do
|
|
|
|
table.insert(r, w)
|
2020-04-29 10:57:05 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
table.insert(r, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2021-11-23 00:40:31 +00:00
|
|
|
--check if two tables have equal contents at the first level
|
|
|
|
--slow, as it needs two loops
|
|
|
|
function tablex.shallow_equal(a, b)
|
|
|
|
if a == b then return true end
|
|
|
|
for k, v in pairs(a) do
|
|
|
|
if b[k] ~= v then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2022-03-02 16:11:30 +00:00
|
|
|
-- second loop to ensure a isn't missing any keys from b.
|
2022-03-07 01:00:05 +00:00
|
|
|
-- we don't compare the values - if any are missing we're not equal
|
2021-11-23 00:40:31 +00:00
|
|
|
for k, v in pairs(b) do
|
2022-03-02 16:11:30 +00:00
|
|
|
if a[k] == nil then
|
2021-11-23 00:40:31 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--check if two tables have equal contents all the way down
|
|
|
|
--slow, as it needs two potentially recursive loops
|
|
|
|
function tablex.deep_equal(a, b)
|
|
|
|
if a == b then return true end
|
|
|
|
--not equal on type
|
|
|
|
if type(a) ~= type(b) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
--bottomed out
|
|
|
|
if type(a) ~= "table" then
|
|
|
|
return a == b
|
|
|
|
end
|
|
|
|
for k, v in pairs(a) do
|
|
|
|
if not tablex.deep_equal(v, b[k]) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2022-03-07 01:00:05 +00:00
|
|
|
-- second loop to ensure a isn't missing any keys from b
|
|
|
|
-- we don't compare the values - if any are missing we're not equal
|
2021-11-23 00:40:31 +00:00
|
|
|
for k, v in pairs(b) do
|
2022-03-02 16:11:30 +00:00
|
|
|
if a[k] == nil then
|
2021-11-23 00:40:31 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-07-02 07:04:47 +00:00
|
|
|
--alias
|
|
|
|
tablex.flatten = tablex.collapse
|
|
|
|
|
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
|
|
|
|
2021-11-11 02:47:24 +00:00
|
|
|
--internal: reverse iterator function
|
|
|
|
local function _ripairs_iter(t, i)
|
|
|
|
i = i - 1
|
|
|
|
local v = t[i]
|
|
|
|
if v then
|
|
|
|
return i, v
|
|
|
|
end
|
2021-11-10 04:45:01 +00:00
|
|
|
end
|
|
|
|
|
2021-11-11 02:47:24 +00:00
|
|
|
--iterator that works like ipairs, but in reverse order, with indices from #t to 1
|
|
|
|
--similar to ipairs, it will only consider sequential until the first nil value in the table.
|
|
|
|
function tablex.ripairs(t)
|
|
|
|
return _ripairs_iter, t, #t + 1
|
2021-11-10 04:45:01 +00:00
|
|
|
end
|
|
|
|
|
2022-05-23 04:48:52 +00:00
|
|
|
--works like pairs, but returns sorted table
|
|
|
|
-- generates a fair bit of garbage but very nice for more stable output
|
|
|
|
-- less function gets keys the of the table as its argument; if you want to sort on the values they map to then
|
|
|
|
-- you'll likely need a closure
|
|
|
|
function tablex.spairs(t, less)
|
|
|
|
less = less or default_less
|
|
|
|
--gather the keys
|
|
|
|
local keys = tablex.keys(t)
|
2022-05-18 05:17:31 +00:00
|
|
|
|
2022-05-23 04:48:52 +00:00
|
|
|
spairs_sort(keys, less)
|
2022-05-18 05:17:31 +00:00
|
|
|
|
|
|
|
local i = 0
|
|
|
|
return function()
|
|
|
|
i = i + 1
|
|
|
|
if keys[i] then
|
|
|
|
return keys[i], t[keys[i]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-07-02 07:04:47 +00:00
|
|
|
return tablex
|