Path of Exile Wiki

Please consider helping keep the wiki up to date. Check the to-do list of updates needed for version 3.14.0.

Game data exports will becoming later as the technical changes in addition to regular changes take some more time.

READ MORE

Path of Exile Wiki
(module with utility functions for table)
 
mNo edit summary
Line 72: Line 72:
 
return xtable:new(x)
 
return xtable:new(x)
 
end
 
end
  +
  +
return xtable

Revision as of 12:06, 9 July 2015

Template info icon Module documentation[view] [edit] [history] [purge]

Overview

Extends the functionality of tables.

-- Author: [#OMEGA] - K2
xtable = {}

function xtable:new(tbl)
    tbl = tbl or {}
    setmetatable(tbl, self)
    self.__index = self
    return tbl
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(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 0
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(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