add argsort function
This commit is contained in:
parent
374fa4d876
commit
d33bdfea62
1 changed files with 10 additions and 0 deletions
10
util.lua
10
util.lua
|
@ -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,
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue