Skip to content

Commit

Permalink
re-implement my microopts on top of the new cleaned up tg versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Jan 9, 2025
1 parent 3891e52 commit 9177791
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions code/__HELPERS/sorts/sort_instance.dm
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())


/datum/sort_instance/proc/timSort(start, end)
runBases.Cut()
runLens.Cut()
runBases.len = 0
runLens.len = 0

var/remaining = end - start

Expand All @@ -59,8 +59,8 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
runLen = force

//add data about run to queue
runBases.Add(start)
runLens.Add(runLen)
runBases += start
runLens += runLen

//maybe merge
mergeCollapse()
Expand Down Expand Up @@ -179,8 +179,10 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
//This method is called each time a new run is pushed onto the stack.
//So the invariants are guaranteed to hold for i<stackSize upon entry to the method
/datum/sort_instance/proc/mergeCollapse()
while(runBases.len >= 2)
var/n = runBases.len - 1
var/list/runBases = src.runBases
var/list/runLens = src.runLens
while(length(runBases) >= 2)
var/n = length(runBases) - 1
if(n > 1 && runLens[n-1] <= runLens[n] + runLens[n+1])
if(runLens[n-1] < runLens[n+1])
--n
Expand All @@ -194,8 +196,10 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
//Merges all runs on the stack until only one remains.
//Called only once, to finalise the sort
/datum/sort_instance/proc/mergeForceCollapse()
while(runBases.len >= 2)
var/n = runBases.len - 1
var/list/runBases = src.runBases
var/list/runLens = src.runLens
while(length(runBases) >= 2)
var/n = length(runBases) - 1
if(n > 1 && runLens[n-1] < runLens[n+1])
--n
mergeAt(n)
Expand All @@ -205,6 +209,9 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
//Run i must be the penultimate or antepenultimate run on the stack
//In other words, i must be equal to stackSize-2 or stackSize-3
/datum/sort_instance/proc/mergeAt(i)
var/list/runBases = src.runBases
var/list/runLens = src.runLens
var/list/L = src.L
//ASSERT(runBases.len >= 2)
//ASSERT(i >= 1)
//ASSERT(i == runBases.len - 1 || i == runBases.len - 2)
Expand Down Expand Up @@ -579,24 +586,26 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
return

var/minRun = minRunLength(remaining)
var/list/runBases = src.runBases
var/list/runLens = src.runLens

do
var/runLen = (remaining <= minRun) ? remaining : minRun

binarySort(start, start+runLen, start)

//add data about run to queue
runBases.Add(start)
runLens.Add(runLen)
runBases += start
runLens += runLen

//Advance to find next run
start += runLen
remaining -= runLen

while(remaining > 0)

while(runBases.len >= 2)
var/n = runBases.len - 1
while(length(runBases) >= 2)
var/n = length(runBases) - 1
if(n > 1 && runLens[n-1] <= runLens[n] + runLens[n+1])
if(runLens[n-1] < runLens[n+1])
--n
Expand All @@ -606,15 +615,17 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
else
break //Invariant is established

while(runBases.len >= 2)
var/n = runBases.len - 1
while(length(runBases) >= 2)
var/n = length(runBases) - 1
if(n > 1 && runLens[n-1] < runLens[n+1])
--n
mergeAt2(n)

return L

/datum/sort_instance/proc/mergeAt2(i)
var/list/runBases = src.runBases
var/list/runLens = src.runLens
var/list/L = src.L
var/cursor1 = runBases[i]
var/cursor2 = runBases[i+1]
Expand Down

0 comments on commit 9177791

Please sign in to comment.