Difference between revisions of "Module:TableTools"

From BESA® Wiki
Jump to: navigation, search
(start module with useful tools for dealing with Lua tables)
(add getNumKeys function and improve the comments, add the beginnings of a sparseIpairs function)
Line 9: Line 9:
 
--[[
 
--[[
 
-----------------------------------------------------------------------------------
 
-----------------------------------------------------------------------------------
-- Helper functions
+
-- 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 the table.
 
-----------------------------------------------------------------------------------
 
-----------------------------------------------------------------------------------
 
--]]
 
--]]
 
+
function p.isPositiveInteger(num)
local function isPositiveInteger(num)
+
-- Returns true if the given number is a positive integer, and false if not.
+
 
if type(num) == 'number' and num >= 1 and floor(num) == num and num < infinity then
 
if type(num) == 'number' and num >= 1 and floor(num) == num and num < infinity then
 
return true
 
return true
Line 24: Line 27:
 
--[[
 
--[[
 
-----------------------------------------------------------------------------------
 
-----------------------------------------------------------------------------------
-- compressSparseArray
+
-- getNumKeys
 
--
 
--
-- This takes an array with one or more nil values, and removes the nil values
+
-- This takes a table and returns an array containing the numbers of any numerical
-- while preserving the order, so that the array can be safely traversed with
+
-- keys that have non-nil values, sorted in numerical order.
-- ipairs.
+
 
-----------------------------------------------------------------------------------
 
-----------------------------------------------------------------------------------
 
--]]
 
--]]
function p.compressSparseArray(t)
+
function p.getNumKeys(t)
local nums, ret = {}, {}
+
local isPositiveInteger = p.isPositiveInteger
 +
local nums = {}
 
for k, v in pairs(t) do
 
for k, v in pairs(t) do
 
if isPositiveInteger(k) then
 
if isPositiveInteger(k) then
Line 38: Line 41:
 
end
 
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)
 
table.sort(nums)
 
for _, num in ipairs(nums) do
 
for _, num in ipairs(nums) do
Line 44: Line 63:
 
return ret
 
return ret
 
end
 
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)
  
 
return p
 
return p

Revision as of 12:33, 15 December 2013

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

Script error: Lua error: Internal error: The interpreter exited with status 126.

-- This module includes a number of functions that can be useful when dealing with Lua tables.

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 the 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

--[[
-----------------------------------------------------------------------------------
-- 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)

return p