Module:Parameters/remove holes

< Module:Parameters
Revision as of 09:21, 8 December 2023 by wiktionary>Benwing2 (optimize by checking whether we need to compress before copying; also be more careful with false vs. nil)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Parameters/remove holes/doc

-- A helper function that removes empty numeric indexes in a table,
-- so that the values are tightly packed like in a normal Lua table.
-- equivalent to require("Module:table").compressSparseArray
return function(t)
	local highest = 0
	for num, _ in pairs(t) do
		if type(num) == "number" and num > 0 and num < math.huge and math.floor(num) == num then
			highest = math.max(highest, num)
		end
	end
	local need_to_compress = false
	for i = 1, highest do
		if t[i] == nil then
			need_to_compress = true
			break
		end
	end
	if not need_to_compress then
		-- The previous algorithm always copied, which implicitly removed 'maxindex' (and other non-numeric keys).
		-- Some code calls next(val) to check for a value being present, which depends on 'maxindex' not being present,
		-- so remove it.
		t.maxindex = nil
		return t
	else
		local ret = {}
		local index = 1
		for i = 1, highest do
			if t[i] ~= nil then
				ret[index] = t[i]
				index = index + 1
			end
		end
		return ret
	end
end