Skip to content

Commit 9177791

Browse files
committed
re-implement my microopts on top of the new cleaned up tg versions
1 parent 3891e52 commit 9177791

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

code/__HELPERS/sorts/sort_instance.dm

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
3232

3333

3434
/datum/sort_instance/proc/timSort(start, end)
35-
runBases.Cut()
36-
runLens.Cut()
35+
runBases.len = 0
36+
runLens.len = 0
3737

3838
var/remaining = end - start
3939

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

6161
//add data about run to queue
62-
runBases.Add(start)
63-
runLens.Add(runLen)
62+
runBases += start
63+
runLens += runLen
6464

6565
//maybe merge
6666
mergeCollapse()
@@ -179,8 +179,10 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
179179
//This method is called each time a new run is pushed onto the stack.
180180
//So the invariants are guaranteed to hold for i<stackSize upon entry to the method
181181
/datum/sort_instance/proc/mergeCollapse()
182-
while(runBases.len >= 2)
183-
var/n = runBases.len - 1
182+
var/list/runBases = src.runBases
183+
var/list/runLens = src.runLens
184+
while(length(runBases) >= 2)
185+
var/n = length(runBases) - 1
184186
if(n > 1 && runLens[n-1] <= runLens[n] + runLens[n+1])
185187
if(runLens[n-1] < runLens[n+1])
186188
--n
@@ -194,8 +196,10 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
194196
//Merges all runs on the stack until only one remains.
195197
//Called only once, to finalise the sort
196198
/datum/sort_instance/proc/mergeForceCollapse()
197-
while(runBases.len >= 2)
198-
var/n = runBases.len - 1
199+
var/list/runBases = src.runBases
200+
var/list/runLens = src.runLens
201+
while(length(runBases) >= 2)
202+
var/n = length(runBases) - 1
199203
if(n > 1 && runLens[n-1] < runLens[n+1])
200204
--n
201205
mergeAt(n)
@@ -205,6 +209,9 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
205209
//Run i must be the penultimate or antepenultimate run on the stack
206210
//In other words, i must be equal to stackSize-2 or stackSize-3
207211
/datum/sort_instance/proc/mergeAt(i)
212+
var/list/runBases = src.runBases
213+
var/list/runLens = src.runLens
214+
var/list/L = src.L
208215
//ASSERT(runBases.len >= 2)
209216
//ASSERT(i >= 1)
210217
//ASSERT(i == runBases.len - 1 || i == runBases.len - 2)
@@ -579,24 +586,26 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
579586
return
580587

581588
var/minRun = minRunLength(remaining)
589+
var/list/runBases = src.runBases
590+
var/list/runLens = src.runLens
582591

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

586595
binarySort(start, start+runLen, start)
587596

588597
//add data about run to queue
589-
runBases.Add(start)
590-
runLens.Add(runLen)
598+
runBases += start
599+
runLens += runLen
591600

592601
//Advance to find next run
593602
start += runLen
594603
remaining -= runLen
595604

596605
while(remaining > 0)
597606

598-
while(runBases.len >= 2)
599-
var/n = runBases.len - 1
607+
while(length(runBases) >= 2)
608+
var/n = length(runBases) - 1
600609
if(n > 1 && runLens[n-1] <= runLens[n] + runLens[n+1])
601610
if(runLens[n-1] < runLens[n+1])
602611
--n
@@ -606,15 +615,17 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
606615
else
607616
break //Invariant is established
608617

609-
while(runBases.len >= 2)
610-
var/n = runBases.len - 1
618+
while(length(runBases) >= 2)
619+
var/n = length(runBases) - 1
611620
if(n > 1 && runLens[n-1] < runLens[n+1])
612621
--n
613622
mergeAt2(n)
614623

615624
return L
616625

617626
/datum/sort_instance/proc/mergeAt2(i)
627+
var/list/runBases = src.runBases
628+
var/list/runLens = src.runLens
618629
var/list/L = src.L
619630
var/cursor1 = runBases[i]
620631
var/cursor2 = runBases[i+1]

0 commit comments

Comments
 (0)