mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-11 10:43:20 +00:00
Various backend fixes/tweaks (#7855)
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
if(toIndex <= 0)
|
||||
toIndex += L.len + 1
|
||||
|
||||
var/datum/sort_instance/SI = GLOB.sortInstance
|
||||
var/datum/sortInstance/SI = GLOB.sortInstance
|
||||
if(!SI)
|
||||
SI = new
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
//When we get into galloping mode, we stay there until both runs win less often than MIN_GALLOP consecutive times.
|
||||
#define MIN_GALLOP 7
|
||||
|
||||
//This is a global instance to allow much of this code to be reused. The interfaces are kept separately
|
||||
GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
|
||||
/datum/sort_instance
|
||||
//This is a global instance to allow much of this code to be reused. The interfaces are kept separately
|
||||
GLOBAL_DATUM_INIT(sortInstance, /datum/sortInstance, new()) //ChompEDIT
|
||||
/datum/sortInstance
|
||||
//The array being sorted.
|
||||
var/list/L
|
||||
|
||||
@@ -32,7 +32,7 @@ GLOBAL_DATUM_INIT(sortInstance, /datum/sort_instance, new())
|
||||
var/list/runLens = list()
|
||||
|
||||
|
||||
/datum/sort_instance/proc/timSort(start, end)
|
||||
/datum/sortInstance/proc/timSort(start, end)
|
||||
runBases.Cut()
|
||||
runLens.Cut()
|
||||
|
||||
@@ -97,12 +97,13 @@ lo the index of the first element in the range to be sorted
|
||||
hi the index after the last element in the range to be sorted
|
||||
start the index of the first element in the range that is not already known to be sorted
|
||||
*/
|
||||
/datum/sort_instance/proc/binarySort(lo, hi, start)
|
||||
/datum/sortInstance/proc/binarySort(lo, hi, start)
|
||||
//ASSERT(lo <= start && start <= hi)
|
||||
if(start <= lo)
|
||||
start = lo + 1
|
||||
|
||||
for(,start < hi, ++start)
|
||||
var/list/L = src.L
|
||||
for(start in start to hi - 1)
|
||||
var/pivot = fetchElement(L,start)
|
||||
|
||||
//set left and right to the index where pivot belongs
|
||||
@@ -134,7 +135,7 @@ For its intended use in a stable mergesort, the strictness of the
|
||||
definition of "descending" is needed so that the call can safely
|
||||
reverse a descending sequence without violating stability.
|
||||
*/
|
||||
/datum/sort_instance/proc/countRunAndMakeAscending(lo, hi)
|
||||
/datum/sortInstance/proc/countRunAndMakeAscending(lo, hi)
|
||||
//ASSERT(lo < hi)
|
||||
|
||||
var/runHi = lo + 1
|
||||
@@ -164,7 +165,7 @@ reverse a descending sequence without violating stability.
|
||||
|
||||
//Returns the minimum acceptable run length for an array of the specified length.
|
||||
//Natural runs shorter than this will be extended with binarySort
|
||||
/datum/sort_instance/proc/minRunLength(n)
|
||||
/datum/sortInstance/proc/minRunLength(n)
|
||||
//ASSERT(n >= 0)
|
||||
var/r = 0 //becomes 1 if any bits are shifted off
|
||||
while(n >= MIN_MERGE)
|
||||
@@ -177,7 +178,7 @@ reverse a descending sequence without violating stability.
|
||||
// runLen[i-2] > runLen[i-1]
|
||||
//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()
|
||||
/datum/sortInstance/proc/mergeCollapse()
|
||||
while(runBases.len >= 2)
|
||||
var/n = runBases.len - 1
|
||||
if(n > 1 && runLens[n-1] <= runLens[n] + runLens[n+1])
|
||||
@@ -192,7 +193,7 @@ reverse a descending sequence without violating stability.
|
||||
|
||||
//Merges all runs on the stack until only one remains.
|
||||
//Called only once, to finalise the sort
|
||||
/datum/sort_instance/proc/mergeForceCollapse()
|
||||
/datum/sortInstance/proc/mergeForceCollapse()
|
||||
while(runBases.len >= 2)
|
||||
var/n = runBases.len - 1
|
||||
if(n > 1 && runLens[n-1] < runLens[n+1])
|
||||
@@ -203,7 +204,7 @@ reverse a descending sequence without violating stability.
|
||||
//Merges the two consecutive runs at stack indices i and i+1
|
||||
//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)
|
||||
/datum/sortInstance/proc/mergeAt(i)
|
||||
//ASSERT(runBases.len >= 2)
|
||||
//ASSERT(i >= 1)
|
||||
//ASSERT(i == runBases.len - 1 || i == runBases.len - 2)
|
||||
@@ -257,7 +258,7 @@ reverse a descending sequence without violating stability.
|
||||
|
||||
Returns the index at which to insert element 'key'
|
||||
*/
|
||||
/datum/sort_instance/proc/gallopLeft(key, base, len, hint)
|
||||
/datum/sortInstance/proc/gallopLeft(key, base, len, hint)
|
||||
//ASSERT(len > 0 && hint >= 0 && hint < len)
|
||||
|
||||
var/lastOffset = 0
|
||||
@@ -316,7 +317,7 @@ reverse a descending sequence without violating stability.
|
||||
* @param c the comparator used to order the range, and to search
|
||||
* @return the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k]
|
||||
*/
|
||||
/datum/sort_instance/proc/gallopRight(key, base, len, hint)
|
||||
/datum/sortInstance/proc/gallopRight(key, base, len, hint)
|
||||
//ASSERT(len > 0 && hint >= 0 && hint < len)
|
||||
|
||||
var/offset = 1
|
||||
@@ -368,7 +369,7 @@ reverse a descending sequence without violating stability.
|
||||
|
||||
//Merges two adjacent runs in-place in a stable fashion.
|
||||
//For performance this method should only be called when len1 <= len2!
|
||||
/datum/sort_instance/proc/mergeLo(base1, len1, base2, len2)
|
||||
/datum/sortInstance/proc/mergeLo(base1, len1, base2, len2)
|
||||
//ASSERT(len1 > 0 && len2 > 0 && base1 + len1 == base2)
|
||||
|
||||
var/cursor1 = base1
|
||||
@@ -470,7 +471,7 @@ reverse a descending sequence without violating stability.
|
||||
//ASSERT(len1 > 1)
|
||||
|
||||
|
||||
/datum/sort_instance/proc/mergeHi(base1, len1, base2, len2)
|
||||
/datum/sortInstance/proc/mergeHi(base1, len1, base2, len2)
|
||||
//ASSERT(len1 > 0 && len2 > 0 && base1 + len1 == base2)
|
||||
|
||||
var/cursor1 = base1 + len1 - 1 //start at end of sublists
|
||||
@@ -570,7 +571,7 @@ reverse a descending sequence without violating stability.
|
||||
//ASSERT(len2 > 0)
|
||||
|
||||
|
||||
/datum/sort_instance/proc/mergeSort(start, end)
|
||||
/datum/sortInstance/proc/mergeSort(start, end)
|
||||
var/remaining = end - start
|
||||
|
||||
//If array is small, do an insertion sort
|
||||
@@ -615,7 +616,7 @@ reverse a descending sequence without violating stability.
|
||||
|
||||
return L
|
||||
|
||||
/datum/sort_instance/proc/mergeAt2(i)
|
||||
/datum/sortInstance/proc/mergeAt2(i)
|
||||
var/cursor1 = runBases[i]
|
||||
var/cursor2 = runBases[i+1]
|
||||
|
||||
@@ -626,20 +627,17 @@ reverse a descending sequence without violating stability.
|
||||
var/val2 = fetchElement(L,cursor2)
|
||||
|
||||
while(1)
|
||||
if(call(cmp)(val1,val2) < 0)
|
||||
if(call(cmp)(val1,val2) <= 0)
|
||||
if(++cursor1 >= end1)
|
||||
break
|
||||
val1 = fetchElement(L,cursor1)
|
||||
else
|
||||
moveElement(L,cursor2,cursor1)
|
||||
|
||||
++cursor2
|
||||
if(++cursor2 >= end2)
|
||||
break
|
||||
++end1
|
||||
++cursor1
|
||||
//if(++cursor1 >= end1)
|
||||
// break
|
||||
|
||||
val2 = fetchElement(L,cursor2)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user