A large portion of relevant modules/templates have now been switched to cargo. Various usages of SMW throughout the wiki need to be replaced by the new functions, in particular item tables. If some pages do not show up but contain no errors, please null-edit them. To see how you can help with the port check out Path_of_Exile_Wiki:To-do_list/SMW_migration (and leave a comment on the talk page if you have questions).

Module:Table

From Path of Exile Wiki
Jump to: navigation, search
This is a programming module, do not invoke it directly.


Overview

Extends the funcitonality of tables.


-- Author: [#OMEGA] - K2
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