-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4e61c27
commit 8893c63
Showing
10 changed files
with
577 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/priority_queue | ||
var/list/priority_node/heap = list() | ||
|
||
/priority_node | ||
var/item | ||
var/priority | ||
|
||
/priority_node/New(item, priority) | ||
. = ..() | ||
src.item = item | ||
src.priority = priority | ||
|
||
/priority_queue/proc/enqueue(value, priority) | ||
heap += list(new /priority_node(value, priority)) | ||
bubble_up(heap.len) | ||
|
||
/priority_queue/proc/dequeue() | ||
if (heap.len == 0) | ||
return null | ||
|
||
var/priority_node/top = heap[1] | ||
var/bottom = heap[heap.len] | ||
var/item = top.item | ||
heap -= bottom | ||
if(!heap.len) | ||
qdel(top) | ||
return item | ||
heap[1] = bottom | ||
bubble_down(1) | ||
qdel(top) | ||
return item | ||
|
||
/priority_queue/proc/peek() | ||
if (heap.len == 0) | ||
return null | ||
return heap[1].item | ||
|
||
/priority_queue/proc/is_empty() | ||
return heap.len == 0 | ||
|
||
/priority_queue/proc/bubble_up(index) | ||
while(index > 1) | ||
var/parent = round(index / 2) | ||
|
||
if (heap[parent].priority < heap[index].priority) | ||
break | ||
|
||
swap(index, parent) | ||
index = parent | ||
|
||
|
||
/priority_queue/proc/bubble_down(index) | ||
while(index * 2 <= heap.len) | ||
var/child = index * 2 | ||
|
||
if (child + 1 <= heap.len && heap[child + 1].priority < heap[child].priority) | ||
child++ | ||
|
||
if (heap[index].priority < heap[child].priority) | ||
break | ||
|
||
swap(index, child) | ||
index = child | ||
|
||
|
||
/priority_queue/proc/swap(a, b) | ||
var/list/temp = heap[a] | ||
heap[a] = heap[b] | ||
heap[b] = temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Double linked list node | ||
*/ | ||
/node | ||
var/value | ||
var/prev | ||
var/next | ||
|
||
/* | ||
* Defining a queue based on a double linked list | ||
*/ | ||
/queue | ||
/// Link to the beginning of the list | ||
var/node/head | ||
/// Link to end of list | ||
var/node/tail | ||
/// Number of elements in queue | ||
var/count = 0 | ||
|
||
/* | ||
* Adding an element to the end of the queue | ||
*/ | ||
/queue/proc/enqueue(value) | ||
var/node/new_node = new | ||
new_node.value = value | ||
|
||
if (!tail) | ||
head = new_node | ||
tail = new_node | ||
else | ||
tail.next = new_node | ||
new_node.prev = tail | ||
tail = new_node | ||
count++ | ||
/* | ||
* Retrieving an element from the head of the queue | ||
*/ | ||
/queue/proc/dequeue() | ||
if (!head) | ||
return null | ||
|
||
var/value = head.value | ||
var/node/old_head = head | ||
|
||
head = head.next | ||
if (head) | ||
head.prev = null | ||
else | ||
tail = null | ||
old_head.value = null | ||
qdel(old_head) | ||
count-- | ||
return value | ||
/* | ||
* Returns an element from the beginning of the queue without removing it | ||
*/ | ||
/queue/proc/peek() | ||
if (!head) | ||
return null | ||
return head.value | ||
|
||
/* | ||
* Checking if the queue is empty | ||
*/ | ||
/queue/proc/is_empty() | ||
return count == 0 | ||
|
||
/* | ||
* Returns the number of elements in the queue | ||
*/ | ||
/queue/proc/size() | ||
return count |
Oops, something went wrong.