Module:Table: Difference between revisions
Jump to navigation
Jump to search
>OmegaK2 mNo edit summary |
>Vinifera7 (Declare variables locally) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
-- Author: [#OMEGA] - K2 | -- Author: [#OMEGA] - K2 | ||
xtable = {} | local xtable = {} | ||
function xtable:new(tbl) | function xtable:new(tbl) | ||
Line 7: | Line 7: | ||
self.__index = self | self.__index = self | ||
return tbl | return tbl | ||
end | |||
function xtable:insert(value, index) | |||
if index ~= nil then | |||
table.insert(self, index, value) | |||
else | |||
table.insert(self, value) | |||
end | |||
end | |||
function xtable:insertT(value, index) | |||
for _, v in ipairs(value) do | |||
xtable.insert(self, v, index) | |||
end | |||
end | end | ||
Line 18: | Line 32: | ||
function xtable:removeT(value) | function xtable:removeT(value) | ||
for _, v in ipairs(value) do | for _, v in ipairs(value) do | ||
xtable | xtable.remove(self, v) | ||
end | end | ||
end | end | ||
Line 40: | Line 54: | ||
i = i + 1 | i = i + 1 | ||
end | end | ||
return | return nil | ||
end | end | ||
Line 52: | Line 66: | ||
function xtable:containsT(value) | function xtable:containsT(value) | ||
for _, v in ipairs(value) do | for _, v in ipairs(value) do | ||
if not xtable | if not xtable.contains(self, v) then return false end | ||
end | end | ||
return true | return true | ||
Line 62: | Line 76: | ||
table.insert(x, k) | table.insert(x, k) | ||
end | end | ||
return xtable | return xtable.new(x) | ||
end | end | ||
Line 70: | Line 84: | ||
table.insert(x, v) | table.insert(x, v) | ||
end | end | ||
return xtable | return xtable.new(x) | ||
end | end | ||
return xtable | return xtable |
Latest revision as of 11:22, 13 July 2021
This is a meta module.
This module is meant to be used only by other modules. It should not be invoked in wikitext.
Overview
Extends the functionality of tables.
The above documentation is transcluded from Module:Table/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
-- Author: [#OMEGA] - K2
local xtable = {}
function xtable:new(tbl)
tbl = tbl or {}
setmetatable(tbl, self)
self.__index = self
return tbl
end
function xtable:insert(value, index)
if index ~= nil then
table.insert(self, index, value)
else
table.insert(self, value)
end
end
function xtable:insertT(value, index)
for _, v in ipairs(value) do
xtable.insert(self, v, index)
end
end
function xtable:remove(value)
local i = self:index(value)
if i > 0 then
table.remove(self, i)
end
end
function xtable:removeT(value)
for _, v in ipairs(value) do
xtable.remove(self, v)
end
end
function xtable:size()
-- for cases where # operator doesnt want to work
local i = 0
for _, _ in ipairs(self) do
i = i + 1
end
return i
end
function xtable:index(value)
-- no idea why lua starts at one...
local i = 1
for _, v in ipairs(self) do
if v == value then
return i
end
i = i + 1
end
return nil
end
function xtable:contains(value)
for _, v in ipairs(self) do
if v == value then return true end
end
return false
end
function xtable:containsT(value)
for _, v in ipairs(value) do
if not xtable.contains(self, v) then return false end
end
return true
end
function xtable:keys()
x = {}
for k, _ in ipairs(self) do
table.insert(x, k)
end
return xtable.new(x)
end
function xtable:values()
x = {}
for _, v in ipairs(self) do
table.insert(x, v)
end
return xtable.new(x)
end
return xtable