aboutsummaryrefslogtreecommitdiffstats
path: root/util/array.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/array.lua')
-rw-r--r--util/array.lua47
1 files changed, 45 insertions, 2 deletions
diff --git a/util/array.lua b/util/array.lua
index 0b60a4fd..c33a5ef1 100644
--- a/util/array.lua
+++ b/util/array.lua
@@ -10,6 +10,7 @@ local t_insert, t_sort, t_remove, t_concat
= table.insert, table.sort, table.remove, table.concat;
local setmetatable = setmetatable;
+local getmetatable = getmetatable;
local math_random = math.random;
local math_floor = math.floor;
local pairs, ipairs = pairs, ipairs;
@@ -40,6 +41,10 @@ function array_mt.__add(a1, a2)
end
function array_mt.__eq(a, b)
+ if getmetatable(a) ~= array_mt or getmetatable(b) ~= array_mt then
+ -- Lua 5.3+ calls this if both operands are tables, even if metatables differ
+ return false;
+ end
if #a == #b then
for i = 1, #a do
if a[i] ~= b[i] then
@@ -109,6 +114,40 @@ function array_base.filter(outa, ina, func)
return outa;
end
+function array_base.slice(outa, ina, i, j)
+ if j == nil then
+ j = -1;
+ end
+ if j < 0 then
+ j = #ina + (j+1);
+ end
+ if i < 0 then
+ i = #ina + (i+1);
+ end
+ if i < 1 then
+ i = 1;
+ end
+ if j > #ina then
+ j = #ina;
+ end
+ if i > j then
+ for idx = 1, #outa do
+ outa[idx] = nil;
+ end
+ return outa;
+ end
+
+ for idx = 1, 1+j-i do
+ outa[idx] = ina[i+(idx-1)];
+ end
+ if ina == outa then
+ for idx = 2+j-i, #outa do
+ outa[idx] = nil;
+ end
+ end
+ return outa;
+end
+
function array_base.sort(outa, ina, ...)
if ina ~= outa then
outa:append(ina);
@@ -129,9 +168,13 @@ function array_base.unique(outa, ina)
end);
end
-function array_base.pluck(outa, ina, key)
+function array_base.pluck(outa, ina, key, default)
for i = 1, #ina do
- outa[i] = ina[i][key];
+ local v = ina[i][key];
+ if v == nil then
+ v = default;
+ end
+ outa[i] = v;
end
return outa;
end