Files
fulpstation/code/__HELPERS/heap.dm
nemvar b87f65d95e Converts ALL typepaths to lowercase (#51642)
* Case of lower

* More changes

* Ruins the nice 420 diff, brainfart when doing the second batch of conversions

* More changes

* Next batch. I think

* Converts even more paths

* Restarts bots

* Capital Free Zone

* Come on travis, do something

* Renames areas

* Bots, please stop dying

* Updates CONTRIBUTING.md and updates a few paths I missed.

* APC recgarftzfvas
/obj/item/computer_hardware/recharger/apc to /obj/item/computer_hardware/recharger/apc_recharger
2020-06-17 20:47:57 -03:00

77 lines
1.5 KiB
Plaintext

//////////////////////
//datum/heap object
//////////////////////
/datum/heap
var/list/L
var/cmp
/datum/heap/New(compare)
L = new()
cmp = compare
/datum/heap/proc/IsEmpty()
return !L.len
//Insert and place at its position a new node in the heap
/datum/heap/proc/Insert(atom/A)
L.Add(A)
Swim(L.len)
//removes and returns the first element of the heap
//(i.e the max or the min dependant on the comparison function)
/datum/heap/proc/Pop()
if(!L.len)
return 0
. = L[1]
L[1] = L[L.len]
L.Cut(L.len)
if(L.len)
Sink(1)
//Get a node up to its right position in the heap
/datum/heap/proc/Swim(var/index)
var/parent = round(index * 0.5)
while(parent > 0 && (call(cmp)(L[index],L[parent]) > 0))
L.Swap(index,parent)
index = parent
parent = round(index * 0.5)
//Get a node down to its right position in the heap
/datum/heap/proc/Sink(var/index)
var/g_child = GetGreaterChild(index)
while(g_child > 0 && (call(cmp)(L[index],L[g_child]) < 0))
L.Swap(index,g_child)
index = g_child
g_child = GetGreaterChild(index)
//Returns the greater (relative to the comparison proc) of a node children
//or 0 if there's no child
/datum/heap/proc/GetGreaterChild(var/index)
if(index * 2 > L.len)
return 0
if(index * 2 + 1 > L.len)
return index * 2
if(call(cmp)(L[index * 2],L[index * 2 + 1]) < 0)
return index * 2 + 1
else
return index * 2
//Replaces a given node so it verify the heap condition
/datum/heap/proc/ReSort(atom/A)
var/index = L.Find(A)
Swim(index)
Sink(index)
/datum/heap/proc/List()
. = L.Copy()