1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// A priority queue which allows pushing (N, K)=(node, key) pairs to the collection,
/// and popping the foremost element having the lowest key.
pub trait PriorityQueue<N, K>: Clone
where
    K: PartialOrd,
{
    /// Number of elements in the queue.
    fn len(&self) -> usize;

    /// Returns whether he queue is empty or not.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the nodes and keys currently in the queue as a slice;
    /// not necessarily sorted.
    fn as_slice(&self) -> &[(N, K)];

    /// Returns, without popping, a reference to the foremost element of the queue;
    /// returns None if the queue is empty.
    fn peek(&self) -> Option<&(N, K)>;

    /// Clears the queue.
    fn clear(&mut self);

    /// Removes and returns the (node, key) pair with the lowest key in the queue;
    /// returns None if the queue is empty.
    fn pop(&mut self) -> Option<(N, K)>;
    /// Removes and returns the node with the lowest key in the queue;
    /// returns None if the queue is empty.
    fn pop_node(&mut self) -> Option<N>;
    /// Removes and returns the key of the node with the lowest key in the queue;
    /// returns None if the queue is empty.
    fn pop_key(&mut self) -> Option<K>;

    /// Pushes the given (`node`, `key`) pair to the queue.
    fn push(&mut self, node: N, key: K);

    /// Performs the push with given (`node`, `key`) followed by the pop operation.
    ///
    /// Since the queue cannot be empty after the push, the return type is not optional.
    ///
    /// The reason of merging the calls is that handling two instructions at once
    /// is more efficient for certain implementations, such as for the binary heap.
    fn push_then_pop(&mut self, node: N, key: K) -> (N, K);

    /// Test method which returns whether or not the queue is valid.
    #[cfg(test)]
    fn is_valid(&self) -> bool;
}