This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ordered_table = {} | |
--[[ | |
This implementation of ordered table does not hold performance above functionality. | |
It invokes a metamethod `__newindex` for every access, and | |
while this is not ideal performance wise, the resulting table behaves very closely to | |
a standard Lua table, with the quirk that the keys are ordered by when they are first seen | |
(unless deleted and then reinserted.) | |
--]] | |
-- private unique keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local alias_table = {} | |
function alias_table:new(weights) | |
local total = 0 | |
for _,v in ipairs(weights) do | |
assert(v >= 0, "all weights must be non-negative") | |
total = total + v | |
end | |
assert(total > 0, "total weight must be positive") |