Module:TableTools: Difference between revisions

From Meta Wiki
Jump to navigation Jump to search
m>Mr. Stradivarius
m grammar
m>Mr. Stradivarius
add a fancy introductory comment
Line 1: Line 1:
-- This module includes a number of functions that can be useful when dealing with Lua tables.
--[[
------------------------------------------------------------------------------------
--                              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 = {}
local p = {}
Line 8: Line 16:


--[[
--[[
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- isPositiveInteger
-- isPositiveInteger
--
--
Line 15: Line 23:
-- useful for determining whether a given table key is in the array part or the
-- useful for determining whether a given table key is in the array part or the
-- hash part of a table.
-- hash part of a table.
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--]]
--]]
function p.isPositiveInteger(num)
function p.isPositiveInteger(num)
Line 26: Line 34:


--[[
--[[
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- getNumKeys
-- getNumKeys
--
--
-- 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
-- keys that have non-nil values, sorted in numerical order.
-- keys that have non-nil values, sorted in numerical order.
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--]]
--]]
function p.getNumKeys(t)
function p.getNumKeys(t)
Line 46: Line 54:


--[[
--[[
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- getAffixNums
-- getAffixNums
--
--
Line 53: Line 61:
-- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", getAffixNums will
-- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", getAffixNums will
-- return {1, 3, 6}.
-- return {1, 3, 6}.
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--]]
--]]
function p.getAffixNums(t, prefix, suffix)
function p.getAffixNums(t, prefix, suffix)
Line 72: Line 80:


--[[
--[[
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- compressSparseArray
-- compressSparseArray
--
--
Line 78: Line 86:
-- while preserving the order, so that the array can be safely traversed with
-- while preserving the order, so that the array can be safely traversed with
-- ipairs.
-- ipairs.
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--]]
--]]
function p.compressSparseArray(t)
function p.compressSparseArray(t)
Line 91: Line 99:


--[[
--[[
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-- sparseIpairs
-- sparseIpairs
--
--
-- This is an iterator for sparse arrays. It can be used like ipairs, but can
-- This is an iterator for sparse arrays. It can be used like ipairs, but can
-- handle nil values.
-- handle nil values.
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--]]
--]]
function p.sparseIpairs(t)
function p.sparseIpairs(t)

Revision as of 13:10, 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

--[[
------------------------------------------------------------------------------------
-- getNumKeys
--
-- 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.getNumKeys(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

--[[
------------------------------------------------------------------------------------
-- getAffixNums
--
-- 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", getAffixNums will
-- return {1, 3, 6}.
------------------------------------------------------------------------------------
--]]
function p.getAffixNums(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.getNumKeys(t)
	table.sort(nums)
	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.getNumKeys(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