add argsort function

This commit is contained in:
Connor Olding 2018-06-08 02:46:00 +02:00
parent 374fa4d876
commit d33bdfea62

View File

@ -8,6 +8,7 @@ local min = math.min
local pairs = pairs
local random = math.random
local select = select
local sort = table.sort
local sqrt = math.sqrt
local function signbyte(x)
@ -153,6 +154,14 @@ local function rbool()
return 0.5 >= random()
end
local function argsort(t, comp, out)
comp = comp or function(a, b) return a < b end
out = out or {}
for i=1, #t do out[i] = i end
sort(out, function(a, b) return comp(t[a], t[b]) end)
return out
end
return {
signbyte=signbyte,
boolean_xor=boolean_xor,
@ -173,4 +182,5 @@ return {
argmax2=argmax2,
rchoice2=rchoice2,
rbool=rbool,
argsort=argsort,
}