Module:TableTools: Difference between revisions

    m>Mr. Stradivarius
    (remove an unnecessary table.sort from compressSparseArray)
    m>Mr. Stradivarius
    (use fooBar for function names instead of getFooBar)
    Line 35: Line 35:
    --[[
    --[[
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    -- getUnion
    -- union
    --
    --
    -- This returns the union of the values of n tables, as an array. For example, for
    -- This returns the union of the values of n tables, as an array. For example, for
    -- the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, getUnion will return
    -- the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, union will return
    -- {1, 2, 3, 4, 5, 6, 7}.
    -- {1, 2, 3, 4, 5, 6, 7}.
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    --]]
    --]]
    function p.getUnion(...)
    function p.union(...)
    local tables = {...}
    local tables = {...}
    local vals, ret = {}, {}
    local vals, ret = {}, {}
    Line 59: Line 59:
    --[[
    --[[
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    -- getIntersection
    -- intersection
    --
    --
    -- This returns the intersection of the values of n tables, as an array. For
    -- This returns the intersection of the values of n tables, as an array. For
    -- example, for the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6},  
    -- example, for the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6},  
    -- getIntersection will return {3, 5}.
    -- intersection will return {3, 5}.
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    --]]
    --]]
    function p.getIntersection(...)
    function p.intersection(...)
    local tables = {...}
    local tables = {...}
    local vals, ret = {}, {}
    local vals, ret = {}, {}
    Line 87: Line 87:
    --[[
    --[[
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    -- getNumKeys
    -- numKeys
    --
    --
    -- This takes a table and returns an array containing the numbers of any numerical
    -- This takes a table and returns an array containing the numbers of any numerical
    Line 93: Line 93:
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    --]]
    --]]
    function p.getNumKeys(t)
    function p.numKeys(t)
    local isPositiveInteger = p.isPositiveInteger
    local isPositiveInteger = p.isPositiveInteger
    local nums = {}
    local nums = {}
    Line 107: Line 107:
    --[[
    --[[
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    -- getAffixNums
    -- affixNums
    --
    --
    -- This takes a table and returns an array containing the numbers of keys with the
    -- This takes a table and returns an array containing the numbers of keys with the
    -- specified prefix and suffix. For example, for the table
    -- specified prefix and suffix. For example, for the table
    -- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", getAffixNums will
    -- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", affixNums will
    -- return {1, 3, 6}.
    -- return {1, 3, 6}.
    ------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------
    --]]
    --]]
    function p.getAffixNums(t, prefix, suffix)
    function p.affixNums(t, prefix, suffix)
    prefix = prefix or ''
    prefix = prefix or ''
    suffix = suffix or ''
    suffix = suffix or ''
    Line 142: Line 142:
    function p.compressSparseArray(t)
    function p.compressSparseArray(t)
    local ret = {}
    local ret = {}
    local nums = p.getNumKeys(t)
    local nums = p.numKeys(t)
    for _, num in ipairs(nums) do
    for _, num in ipairs(nums) do
    ret[#ret + 1] = t[num]
    ret[#ret + 1] = t[num]
    Line 158: Line 158:
    --]]
    --]]
    function p.sparseIpairs(t)
    function p.sparseIpairs(t)
    local nums = p.getNumKeys(t)
    local nums = p.numKeys(t)
    local i = 0
    local i = 0
    local lim = #nums
    local lim = #nums