1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function multiset_new(t)
	local elements = {}
	local multiplicity = {}
	for k,v in pairs(t) do
		if not multiplicity[v] then
			elements[#elements+1] = v
			multiplicity[v] = 1
		else
			multiplicity[v] = multiplicity[v] + 1
		end
	end
	return elements, multiplicity
end
-- iteration function for kicks
function multiset_iter(elements, multiplicity)
	local i, j = 1, 0
	return function()
		j = j + 1
		if j > multiplicity[elements[i]] then
			i = i + 1
			j = 1
		end
		return elements[i]
	end
end
1
2
3
4
elements,multiplicity = multiset_new({1,1,1,1, 4,4,4,4, 5,5,5, 4, 5})
for v in multiset_iter(elements, multiplicity) do
	print(v)
end