initial commit - cross reference with 5th port - obviously has compile errors

This commit is contained in:
LetterJay
2016-07-03 02:17:19 -05:00
commit 35a1723e98
4355 changed files with 2221257 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
//////////////////////
//Heap object
//////////////////////
/Heap
var/list/L
var/cmp
/Heap/New(compare)
L = new()
cmp = compare
/Heap/proc/IsEmpty()
return !L.len
//Insert and place at its position a new node in the heap
/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)
/Heap/proc/Pop()
if(!L.len)
return 0
. = L[1]
L[1] = L[L.len]
L.Cut(L.len)
Sink(1)
//Get a node up to its right position in the heap
/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
/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
/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
/Heap/proc/ReSort(atom/A)
var/index = L.Find(A)
Swim(index)
Sink(index)
/Heap/proc/List()
. = L.Copy()
+191
View File
@@ -0,0 +1,191 @@
//Ok so it's technically a double linked list, bite me.
/datum/linked_list
var/datum/linked_node/head
var/datum/linked_node/tail
var/node_amt = 0
/datum/linked_node
var/value = null
var/datum/linked_list/linked_list = null
var/datum/linked_node/next_node = null
var/datum/linked_node/previous_node = null
/datum/linked_list/proc/IsEmpty()
. = (node_amt <= 0)
//Add a linked_node (or value, creating a linked_node) at position
//the added node BECOMES the position-th element,
//eg: add("Test",5), the 5th node is now "Test", the previous 5th moves up to become the 6th
/datum/linked_list/proc/Add(node, position)
var/datum/linked_node/adding
if(istype(node, /datum/linked_node))
adding = node
else
adding = new()
adding.value = node
if(!adding.linked_list || (adding.linked_list && (adding.linked_list != src)))
node_amt++
adding.linked_list = src
if(position && position < node_amt)
//Replacing head
if(position == 1)
if(head)
head.previous_node = adding
adding.next_node = head
head = adding
//Replacing any middle node
else
var/location = 0
var/datum/linked_node/at
while((location != position) && (location <= node_amt))
if(at)
if(at.next_node)
at = at.next_node
else
break
else
at = head
location++
//Push at up and assume it's place as the position-th element
if(at && at.previous_node)
at.previous_node.next_node = adding
adding.previous_node = at.previous_node
at.previous_node = adding
adding.next_node = at
return
//Replacing tail
if(tail)
tail.next_node = adding
adding.previous_node = tail
if(!tail.previous_node)
head = tail
tail = adding
//Remove a linked_node or the linked_node of a value
//If you specify a value the FIRST ONE is removed
/datum/linked_list/proc/Remove(node)
var/datum/linked_node/removing
if(istype(node,/datum/linked_node))
removing = node
else
//optimise removing head and tail, no point looping for them, especially the tail
if(removing == head)
removing = head
else if(removing == tail)
removing = tail
else
var/location = 1
var/current_value = null
var/datum/linked_node/at = null
while((current_value != node) && (location <= node_amt))
if(at)
if(at.next_node)
at = at.next_node
else
at = head
location++
if(at)
current_value = at.value
if(current_value == node)
removing = at
break
//Adjust pointers of where removing -was- in the chain.
if(removing)
if(removing.previous_node)
if(removing == tail)
tail = removing.previous_node
if(removing.next_node)
if(removing == head)
head = removing.next_node
removing.next_node.previous_node = removing.previous_node
removing.previous_node.next_node = removing.next_node
else
removing.previous_node.next_node = null
else
if(removing.next_node)
if(removing == head)
head = removing.next_node
removing.next_node.previous_node = null
//if this is still true at this point, there's no more nodes to replace them with
if(removing == head)
head = null
if(removing == tail)
tail = null
removing.next_node = null
removing.previous_node = null
if(removing.linked_list == src)
node_amt--
removing.linked_list = null
return removing
return 0
//Removes and deletes a node or value
/datum/linked_list/proc/RemoveDelete(node)
var/datum/linked_node/dead = Remove(node)
if(dead)
qdel(dead)
return 1
return 0
//Empty the linked_list, deleting all nodes
/datum/linked_list/proc/Empty()
var/datum/linked_node/n = head
while(n)
var/next = n.next_node
Remove(n)
qdel(n)
n = next
node_amt = 0
//Some debugging tools
/datum/linked_list/proc/CheckNodeLinks()
var/datum/linked_node/n = head
while(n)
. = "|[n.value]|"
if(n.previous_node)
. = "[n.previous_node.value]<-" + .
if(n.next_node)
. += "->[n.next_node.value]"
n = n.next_node
. += "<BR>"
/datum/linked_list/proc/DrawNodeLinks()
. = "|<-"
var/datum/linked_node/n = head
while(n)
if(n.previous_node)
. += "<-"
. += "[n.value]"
if(n.next_node)
. += "->"
n = n.next_node
. += "->|"
/datum/linked_list/proc/ToList()
. = list()
var/datum/linked_node/n = head
while(n)
. += n
n = n.next_node
+83
View File
@@ -0,0 +1,83 @@
//////////////////////
//PriorityQueue object
//////////////////////
//an ordered list, using the cmp proc to weight the list elements
/PriorityQueue
var/list/L //the actual queue
var/cmp //the weight function used to order the queue
/PriorityQueue/New(compare)
L = new()
cmp = compare
/PriorityQueue/proc/IsEmpty()
return !L.len
//return the index the element should be in the priority queue using dichotomic search
/PriorityQueue/proc/FindElementIndex(atom/A)
var/i = 1
var/j = L.len
var/mid
while(i < j)
mid = round((i+j)/2)
if(call(cmp)(L[mid],A) < 0)
i = mid + 1
else
j = mid
if(i == 1 || i == L.len) //edge cases
return (call(cmp)(L[i],A) > 0) ? i : i+1
else
return i
//add an element in the list,
//immediatly ordering it to its position using dichotomic search
/PriorityQueue/proc/Enqueue(atom/A)
if(!L.len)
L.Add(A)
return
L.Insert(FindElementIndex(A),A)
//removes and returns the first element in the queue
/PriorityQueue/proc/Dequeue()
if(!L.len)
return 0
. = L[1]
Remove(.)
//removes an element
/PriorityQueue/proc/Remove(atom/A)
return L.Remove(A)
//returns a copy of the elements list
/PriorityQueue/proc/List()
. = L.Copy()
//return the position of an element or 0 if not found
/PriorityQueue/proc/Seek(atom/A)
. = L.Find(A)
//return the element at the i_th position
/PriorityQueue/proc/Get(i)
if(i > L.len || i < 1)
return 0
return L[i]
//replace the passed element at it's right position using the cmp proc
/PriorityQueue/proc/ReSort(atom/A)
var/i = Seek(A)
if(i == 0)
return
while(i < L.len && call(cmp)(L[i],L[i+1]) > 0)
L.Swap(i,i+1)
i++
while(i > 1 && call(cmp)(L[i],L[i-1]) <= 0) //last inserted element being first in case of ties (optimization)
L.Swap(i,i-1)
i--
+56
View File
@@ -0,0 +1,56 @@
/datum/stack
var/list/stack = list()
var/max_elements = 0
/datum/stack/New(list/elements,max)
..()
if(elements)
stack = elements.Copy()
if(max)
max_elements = max
/datum/stack/proc/Pop()
if(is_empty())
return null
. = stack[stack.len]
stack.Cut(stack.len,0)
/datum/stack/proc/Push(element)
if(max_elements && (stack.len+1 > max_elements))
return null
stack += element
/datum/stack/proc/Top()
if(is_empty())
return null
. = stack[stack.len]
/datum/stack/proc/is_empty()
. = stack.len ? 0 : 1
//Rotate entire stack left with the leftmost looping around to the right
/datum/stack/proc/RotateLeft()
if(is_empty())
return 0
. = stack[1]
stack.Cut(1,2)
Push(.)
//Rotate entire stack to the right with the rightmost looping around to the left
/datum/stack/proc/RotateRight()
if(is_empty())
return 0
. = stack[stack.len]
stack.Cut(stack.len,0)
stack.Insert(1,.)
/datum/stack/proc/Copy()
var/datum/stack/S=new()
S.stack = stack.Copy()
S.max_elements = max_elements
return S
/datum/stack/proc/Clear()
stack.Cut()