extracted sorting into a separate file. may later replace the unstable sort in the TSM table by a stable sort

This commit is contained in:
Manuel Simon Hirsig
2016-02-02 15:59:26 +01:00
parent f32e188cec
commit d7af47d1b2
11 changed files with 191 additions and 147 deletions
-69
View File
@@ -1,10 +1,6 @@
local private, public = {}, {}
Aux.util = public
Aux.util.LT = {}
Aux.util.EQ = {}
Aux.util.GT = {}
local merge, copy_array
function Aux.util.pass()
@@ -179,71 +175,6 @@ function Aux.util.take(n, xs)
return ys
end
function Aux.util.merge_sort(A, comp)
local n = getn(A)
local B = {}
local width = 1
while width <= n do
for i=1, n, 2 * width do
merge(A, i, min(i + width, n), min(i + 2 * width - 1, n), B, comp)
end
copy_array(B, A, n)
width = 2 * width
end
end
function merge(A, start1, start2, last, B, comp)
local i1 = start1
local i2 = start2
for i=start1,last do
if i1 < start2 and (i2 > last or comp(A[i1], A[i2]) == Aux.util.LT or comp(A[i1], A[i2]) == Aux.util.EQ) then
B[i] = A[i1]
i1 = i1 + 1
else
B[i] = A[i2]
i2 = i2 + 1
end
end
end
function copy_array(A, B, n)
for i=1,n do
B[i] = A[i]
end
end
function Aux.util.invert_order(ordering)
if ordering == Aux.util.LT then
return Aux.util.GT
elseif ordering == Aux.util.GT then
return Aux.util.LT
else
return Aux.util.EQ
end
end
function Aux.util.compare(a, b, nil_ordering)
nil_ordering = nil_ordering or Aux.util.EQ
if not a and b then
return nil_ordering
elseif a and not b then
return Aux.util.invert_order(nil_ordering)
elseif not a and not b then
return Aux.util.EQ
elseif a < b then
return Aux.util.LT
elseif a > b then
return Aux.util.GT
else
return Aux.util.EQ
end
end
function Aux.util.index_of(value, array)
for i, item in ipairs(array) do
if item == value then