Modul:TableTools: Unterschied zwischen den Versionen
add type checking
(add a size function) |
(add type checking) |
||
| Zeile 8: | Zeile 8: | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
--]] | --]] | ||
local libraryUtil = require('libraryUtil') | |||
local p = {} | local p = {} | ||
| Zeile 14: | Zeile 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. | ||
| Zeile 49: | Zeile 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] | ||
| Zeile 90: | Zeile 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 | ||
| Zeile 121: | Zeile 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] | ||
| Zeile 153: | Zeile 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 | ||
| Zeile 183: | Zeile 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 = {} | ||
| Zeile 205: | Zeile 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 '' | ||
| Zeile 231: | Zeile 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) | ||
| Zeile 248: | Zeile 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 | ||
| Zeile 269: | Zeile 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 | ||