Skip to content

Commit

Permalink
[MIRROR] Document /PriorityQueue (#2442)
Browse files Browse the repository at this point in the history
Co-authored-by: SierraKomodo <[email protected]>
Co-authored-by: UEDCommander <[email protected]>
  • Loading branch information
3 people authored Sep 4, 2024
1 parent 2bdce68 commit 1cd133c
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions code/__datastructures/priority_queue.dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,66 @@

//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
/// List. The actual queue.
var/list/L
/// Proc path. The weight function used to order the queue.
var/cmp

/PriorityQueue/New(compare)
L = new()
cmp = compare

/// Checks if the queue is currently empty. Returns boolean. Mirrors to `!length(L)`
/PriorityQueue/proc/IsEmpty()
SHOULD_BE_PURE(TRUE)
return !length(L)

//add an element in the list,
//immediatly ordering it to its position using dichotomic search
/// Adds an element to the queue, immediately ordering it to its position using the function defined in `cmp`.
/PriorityQueue/proc/Enqueue(atom/A)
ADD_SORTED(L, A, cmp)

//removes and returns the first element in the queue
/// Removes and returns the first element in the queue, or `FALSE` if the queue is empty.
/PriorityQueue/proc/Dequeue()
if(!length(L))
return 0
return FALSE
. = L[1]

Remove(.)

//removes an element
/// Removes an element from the queue. Returns boolean, indicating whether an item was removed or not. Mirrors to `L.Remove(A)`
/PriorityQueue/proc/Remove(atom/A)
. = L.Remove(A)

//returns a copy of the elements list
/// Returns a copy of the queue as a list. Mirrors to `L.Copy()`
/PriorityQueue/proc/List()
RETURN_TYPE(/list)
SHOULD_BE_PURE(TRUE)
. = L.Copy()

//return the position of an element or 0 if not found
/// Returns the position of an element or `FALSE` if not found. Mirrors to `L.Find(A)`
/PriorityQueue/proc/Seek(atom/A)
SHOULD_BE_PURE(TRUE)
. = L.Find(A)

//return the element at the i_th position
/// Returns the element at position `i` (1-indexed), or `FALSE` if the position does not exist.
/PriorityQueue/proc/Get(i)
SHOULD_BE_PURE(TRUE)
if(i > length(L) || i < 1)
return 0
return FALSE
return L[i]

//return the length of the queue
/// Returns the length of the queue. Mirrors to `length(L)`
/PriorityQueue/proc/Length()
SHOULD_BE_PURE(TRUE)
. = length(L)

//replace the passed element at it's right position using the cmp proc
/**
* Repositions the given element to the correct position in the queue using the function defined in `cmp`.
*
* The element must already exist in the queue to be resorted.
*
* Has no return value.
*/
/PriorityQueue/proc/ReSort(atom/A)
var/i = Seek(A)
if(i == 0)
Expand Down

0 comments on commit 1cd133c

Please sign in to comment.