Jump to content

Module:TableTools: Difference between revisions

add type checking
m>Mr. Stradivarius
(add a size function)
m>Mr. Stradivarius
(add type checking)
Line 8: Line 8:
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--]]
--]]
local libraryUtil = require('libraryUtil')


local p = {}
local p = {}
Line 14: Line 16:
local floor = math.floor
local floor = math.floor
local infinity = math.huge
local infinity = math.huge
local checkType = libraryUtil.checkType


-- Define a unique value to represent NaN. This is because NaN cannot be used as a table key.
-- Define a unique value to represent NaN. This is because NaN cannot be used as a table key.
Line 49: Line 52:
for i = 1, select('#', ...) do
for i = 1, select('#', ...) do
local t = select(i, ...)
local t = select(i, ...)
checkType('union', i, t, 'table')
for k, v in pairs(t) do
for k, v in pairs(t) do
local retKey = ret[k]
local retKey = ret[k]
Line 90: Line 94:
for i = 1, select('#', ...) do
for i = 1, select('#', ...) do
local t = select(i, ...)
local t = select(i, ...)
checkType('valueUnion', i, t, 'table')
for k, v in pairs(t) do
for k, v in pairs(t) do
if type(v) == 'number' and tostring(v) == '-nan' then
if type(v) == 'number' and tostring(v) == '-nan' then
Line 121: Line 126:
for i = 1, lim do
for i = 1, lim do
local t = select(i, ...)
local t = select(i, ...)
checkType('intersection', i, t, 'table')
for k, v in pairs(t) do
for k, v in pairs(t) do
local trackVal = track[k]
local trackVal = track[k]
Line 153: Line 159:
for i = 1, lim do
for i = 1, lim do
local t = select(i, ...)
local t = select(i, ...)
checkType('valueIntersection', i, t, 'table')
for k, v in pairs(t) do
for k, v in pairs(t) do
if type(v) == 'number' and tostring(v) == '-nan' then
if type(v) == 'number' and tostring(v) == '-nan' then
Line 183: Line 190:
--]]
--]]
function p.numKeys(t)
function p.numKeys(t)
checkType('numKeys', 1, t, 'table')
local isPositiveInteger = p.isPositiveInteger
local isPositiveInteger = p.isPositiveInteger
local nums = {}
local nums = {}
Line 205: Line 213:
--]]
--]]
function p.affixNums(t, prefix, suffix)
function p.affixNums(t, prefix, suffix)
checkType('affixNums', 1, t, 'table')
prefix = prefix or ''
prefix = prefix or ''
suffix = suffix or ''
suffix = suffix or ''
Line 231: Line 240:
--]]
--]]
function p.compressSparseArray(t)
function p.compressSparseArray(t)
checkType('compressSparseArray', 1, t, 'table')
local ret = {}
local ret = {}
local nums = p.numKeys(t)
local nums = p.numKeys(t)
Line 248: Line 258:
--]]
--]]
function p.sparseIpairs(t)
function p.sparseIpairs(t)
checkType('sparseIpairs', 1, t, 'table')
local nums = p.numKeys(t)
local nums = p.numKeys(t)
local i = 0
local i = 0
Line 269: Line 280:
--]]
--]]
function p.size(t)
function p.size(t)
checkType('size', 1, t, 'table')
local i = 0
local i = 0
for k in pairs(t) do
for k in pairs(t) do
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.