Module:TableTools: Difference between revisions

From TEPwiki, Urth's Encyclopedia
Jump to navigation Jump to search
Content added Content deleted
(remove an unnecessary table.sort from compressSparseArray)
(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:
--[[
--[[
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- intersection
-- getIntersection
--
--
-- 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:
--[[
--[[
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- affixNums
-- getAffixNums
--
--
-- 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

Revision as of 20:55, 15 December 2013

Documentation for this module may be created at Module:TableTools/doc

--[[
------------------------------------------------------------------------------------
--                               TableTools                                       --
--                                                                                --
-- This module includes a number of functions for dealing with Lua tables.        --
-- It is a meta-module, meant to be called from other Lua modules, and should     --
-- not be called directly from #invoke.                                           --
------------------------------------------------------------------------------------
--]]

local p = {}

-- Define often-used variables and functions.
local floor = math.floor
local infinity = math.huge

--[[
------------------------------------------------------------------------------------
-- isPositiveInteger
--
-- This function returns true if the given number is a positive integer, and false
-- if not. Although it doesn't operate on tables, it is included here as it is
-- useful for determining whether a given table key is in the array part or the
-- hash part of a table.
------------------------------------------------------------------------------------
--]]
function p.isPositiveInteger(num)
	if type(num) == 'number' and num >= 1 and floor(num) == num and num < infinity then
		return true
	else
		return false
	end
end

--[[
------------------------------------------------------------------------------------
-- union
--
-- 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}, union will return
-- {1, 2, 3, 4, 5, 6, 7}.
------------------------------------------------------------------------------------
--]]
function p.union(...)
	local tables = {...}
	local vals, ret = {}, {}
	for _, t in ipairs(tables) do
		for k, v in pairs(t) do
			vals[v] = true
		end
	end
	for val in pairs(vals) do
		ret[#ret + 1] = val
	end
	table.sort(ret)
	return ret
end	

--[[
------------------------------------------------------------------------------------
-- intersection
--
-- 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}, 
-- intersection will return {3, 5}.
------------------------------------------------------------------------------------
--]]
function p.intersection(...)
	local tables = {...}
	local vals, ret = {}, {}
	local lim = #tables
	for _, t in ipairs(tables) do
		for k, v in pairs(t) do
			local valCount = vals[v] or 0
			vals[v] = valCount + 1
		end
	end
	for val, count in pairs(vals) do
		if count == lim then
			ret[#ret + 1] = val
		end
	end
	table.sort(ret)
	return ret
end

--[[
------------------------------------------------------------------------------------
-- numKeys
--
-- This takes a table and returns an array containing the numbers of any numerical
-- keys that have non-nil values, sorted in numerical order.
------------------------------------------------------------------------------------
--]]
function p.numKeys(t)
	local isPositiveInteger = p.isPositiveInteger
	local nums = {}
	for k, v in pairs(t) do
		if isPositiveInteger(k) then
			nums[#nums + 1] = k
		end
	end
	table.sort(nums)
	return nums
end

--[[
------------------------------------------------------------------------------------
-- affixNums
--
-- This takes a table and returns an array containing the numbers of keys with the
-- specified prefix and suffix. For example, for the table
-- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", affixNums will
-- return {1, 3, 6}.
------------------------------------------------------------------------------------
--]]
function p.affixNums(t, prefix, suffix)
	prefix = prefix or ''
	suffix = suffix or ''
	local nums = {}
	for k, v in pairs(t) do
		if type(k) == 'string' then			
			local num = mw.ustring.match(k, '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$')
			if num then
				nums[#nums + 1] = tonumber(num)
			end
		end
	end
	table.sort(nums)
	return nums
end

--[[
------------------------------------------------------------------------------------
-- compressSparseArray
--
-- This takes an array with one or more nil values, and removes the nil values
-- while preserving the order, so that the array can be safely traversed with
-- ipairs.
------------------------------------------------------------------------------------
--]]
function p.compressSparseArray(t)
	local ret = {}
	local nums = p.numKeys(t)
	for _, num in ipairs(nums) do
		ret[#ret + 1] = t[num]
	end
	return ret
end

--[[
------------------------------------------------------------------------------------
-- sparseIpairs
--
-- This is an iterator for sparse arrays. It can be used like ipairs, but can
-- handle nil values.
------------------------------------------------------------------------------------
--]]
function p.sparseIpairs(t)
	local nums = p.numKeys(t)
	local i = 0
	local lim = #nums
	return function ()
		i = i + 1
		if i <= lim then
			local key = nums[i]
			return key, t[key]
		end
	end
end

return p