A collection of various things from the Asterisk server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.3 KiB

local function read_table(filename, file_table, errout)
local function parse_line(line, file_table, errout)
local s, e = string.find(line, " : ")
if s == nil or e == nil then
errout("error: bad line")
return
end
local cum = tonumber(string.sub(line, 1, s - 1))
local file = string.sub(line, e + 1)
table.insert(file_table, { cum = cum, file = file })
end
local tabf = io.open(filename, "r")
if tabf == nil then
errout("could not open table file " .. filename)
return
end
for line in tabf:lines() do
parse_line(line, file_table, errout)
end
io.close(tabf)
end
local function search_table(file_table, cum)
local s = 1
local e = #file_table - 1
while true do
if s >= e then
return s
end
local sidx = math.floor((s + e) / 2)
if cum < file_table[sidx].cum then
e = sidx
else
s = sidx + 1
end
end
end
local ftab = {}
read_table("outtab.txt", ftab, function(em) print(em) end)
--for idx, ent in ipairs(ftab) do
-- print(tostring(idx) .. " cwgt(" .. tostring(ent.file) .. ") = " .. tostring(ent.cum))
--end
local uw = ftab[#ftab - 1].cum
math.randomseed(os.time())
for i=1,100 do
local eidx = search_table(ftab, math.random() * uw)
local fname = ftab[eidx].file
print(fname)
end
return { read_table = read_table, search_table = search_table }