orx_priority_queue/
priority_queue.rs

1use crate::NodeKeyRef;
2
3/// A priority queue which allows pushing (N, K)=(node, key) pairs to the collection,
4/// and popping the foremost element having the lowest key.
5pub trait PriorityQueue<N, K>
6where
7    K: PartialOrd,
8{
9    /// Item providing references to node & key pairs which are yielded by the iterator.
10    type NodeKey<'a>: NodeKeyRef<'a, N, K>
11    where
12        Self: 'a,
13        N: 'a,
14        K: 'a;
15
16    /// An iterator over the (node, key) pairs on the priority queue in an arbitrary order.
17    type Iter<'a>: Iterator<Item = Self::NodeKey<'a>>
18    where
19        Self: 'a,
20        N: 'a,
21        K: 'a;
22
23    /// Number of elements in the queue.
24    ///
25    /// # Examples
26    ///
27    /// ```
28    /// use orx_priority_queue::*;
29    ///
30    /// let mut queue = DaryHeap::<_, _, 16>::default();
31    ///
32    /// queue.push('a', 42);
33    /// queue.push('b', 7);
34    /// assert_eq!(2, queue.len());
35    ///
36    /// _ = queue.pop();
37    /// assert_eq!(1, queue.len());
38    /// ```
39    fn len(&self) -> usize;
40
41    /// Capacity of the heap.
42    fn capacity(&self) -> usize;
43
44    /// Returns whether he queue is empty or not.
45    ///
46    /// # Examples
47    ///
48    /// ```
49    /// use orx_priority_queue::*;
50    ///
51    /// let mut queue = QuaternaryHeap::default();
52    /// assert!(queue.is_empty());
53    ///
54    /// queue.push("wisdom", 42);
55    /// assert!(!queue.is_empty());
56    /// ```
57    fn is_empty(&self) -> bool {
58        self.len() == 0
59    }
60
61    /// Returns, without popping, a reference to the foremost element of the queue;
62    /// returns None if the queue is empty.
63    ///
64    /// # Examples
65    ///
66    /// ```
67    /// use orx_priority_queue::*;
68    ///
69    /// let mut queue = BinaryHeap::default();
70    /// assert_eq!(None, queue.peek());
71    ///
72    /// queue.push(0, 12.0);
73    /// queue.push(42, 1.0);
74    /// queue.push(21, 5.0);
75    /// assert_eq!(Some(&(42, 1.0)), queue.peek());
76    /// ```
77    fn peek(&self) -> Option<Self::NodeKey<'_>>;
78
79    /// Clears the queue.
80    ///
81    /// # Examples
82    ///
83    /// ```
84    /// use orx_priority_queue::*;
85    ///
86    /// let mut queue = QuaternaryHeap::default();
87    /// assert!(queue.is_empty());
88    ///
89    /// queue.push(0, 12.0);
90    /// queue.push(42, 1.0);
91    /// queue.push(21, 5.0);
92    /// assert!(!queue.is_empty());
93    ///
94    /// queue.clear();
95    /// assert!(queue.is_empty());
96    /// ```
97    fn clear(&mut self);
98
99    /// Removes and returns the (node, key) pair with the lowest key in the queue;
100    /// returns None if the queue is empty.
101    ///
102    /// # Examples
103    ///
104    /// ```
105    /// use orx_priority_queue::*;
106    ///
107    /// let mut queue = BinaryHeap::default();
108    /// assert_eq!(None, queue.pop());
109    ///
110    /// queue.push(0, 12.0);
111    /// queue.push(42, 1.0);
112    /// queue.push(21, 5.0);
113    /// assert_eq!(3, queue.len());
114    ///
115    /// assert_eq!(Some((42, 1.0)), queue.pop());
116    /// assert_eq!(2, queue.len());
117    ///
118    /// assert_eq!(Some((21, 5.0)), queue.pop());
119    /// assert_eq!(1, queue.len());
120    ///
121    /// assert_eq!(Some((0, 12.0)), queue.pop());
122    /// assert!(queue.is_empty());
123    ///
124    /// assert_eq!(None, queue.pop());
125    /// ```
126    fn pop(&mut self) -> Option<(N, K)>;
127
128    /// Removes and returns the node with the lowest key in the queue;
129    /// returns None if the queue is empty.
130    ///
131    /// # Examples
132    ///
133    /// ```
134    /// use orx_priority_queue::*;
135    ///
136    /// let mut queue = BinaryHeap::default();
137    /// assert_eq!(None, queue.pop_node());
138    ///
139    /// queue.push(0, 12.0);
140    /// queue.push(42, 1.0);
141    /// queue.push(21, 5.0);
142    /// assert_eq!(3, queue.len());
143    ///
144    /// assert_eq!(Some(42), queue.pop_node());
145    /// assert_eq!(2, queue.len());
146    ///
147    /// assert_eq!(Some(21), queue.pop_node());
148    /// assert_eq!(1, queue.len());
149    ///
150    /// assert_eq!(Some(0), queue.pop_node());
151    /// assert!(queue.is_empty());
152    ///
153    /// assert_eq!(None, queue.pop_node());
154    /// ```
155    fn pop_node(&mut self) -> Option<N>;
156
157    /// Removes and returns the key of the node with the lowest key in the queue;
158    /// returns None if the queue is empty.
159    ///
160    /// # Examples
161    ///
162    /// ```
163    /// use orx_priority_queue::*;
164    ///
165    /// let mut queue = BinaryHeap::default();
166    /// assert_eq!(None, queue.pop_key());
167    ///
168    /// queue.push(0, 12.0);
169    /// queue.push(42, 1.0);
170    /// queue.push(21, 5.0);
171    /// assert_eq!(3, queue.len());
172    ///
173    /// assert_eq!(Some(1.0), queue.pop_key());
174    /// assert_eq!(2, queue.len());
175    ///
176    /// assert_eq!(Some(5.0), queue.pop_key());
177    /// assert_eq!(1, queue.len());
178    ///
179    /// assert_eq!(Some(12.0), queue.pop_key());
180    /// assert!(queue.is_empty());
181    ///
182    /// assert_eq!(None, queue.pop_key());
183    /// ```
184    fn pop_key(&mut self) -> Option<K>;
185
186    /// Pushes the given (`node`, `key`) pair to the queue.
187    ///
188    /// # Examples
189    ///
190    /// ```
191    /// use orx_priority_queue::*;
192    ///
193    /// let mut queue = BinaryHeap::default();
194    /// assert!(queue.is_empty());
195    ///
196    /// queue.push(0, 12.0);
197    /// queue.push(42, 1.0);
198    /// queue.push(21, 5.0);
199    /// assert_eq!(3, queue.len());
200    /// ```
201    fn push(&mut self, node: N, key: K);
202
203    /// Performs the push with given (`node`, `key`) followed by the pop operation.
204    ///
205    /// Since the queue cannot be empty after the push, the return type is not optional.
206    ///
207    /// The reason of merging the calls is that handling two instructions at once is
208    /// significantly more efficient for implementations taking benefit of this composition.
209    /// `DaryHeap` and its variants such the `BinaryHeap` are examples to this (see "benches/push_then_pop.rs").
210    ///
211    /// # Examples
212    ///
213    /// ```
214    /// use orx_priority_queue::*;
215    ///
216    /// let mut queue = BinaryHeap::default();
217    /// assert!(queue.is_empty());
218    ///
219    /// // returns the (node, key) back when the queue is empty
220    /// let popped = queue.push_then_pop(3, 33.3);
221    /// assert_eq!((3, 33.3), popped);
222    /// assert!(queue.is_empty());
223    ///
224    /// queue.push(0, 12.0);
225    /// queue.push(42, 1.0);
226    /// queue.push(21, 5.0);
227    /// assert_eq!(3, queue.len()); // sorted-nodes: 42 (1.0) << 21 (5.0) << 0 (12.0)
228    ///
229    /// let popped = queue.push_then_pop(100, 100.0);
230    /// assert_eq!((42, 1.0), popped);
231    /// assert_eq!(3, queue.len()); // sorted-nodes: 21 (5.0) << 0 (12.0) << 100 (100.0)
232    ///
233    /// let popped = queue.push_then_pop(6, 6.0);
234    /// assert_eq!((21, 5.0), popped);
235    /// assert_eq!(3, queue.len()); // sorted-nodes: 6 (6.0) << 0 (12.0) << 100 (100.0)
236    ///
237    /// let popped = queue.push_then_pop(13, 13.0);
238    /// assert_eq!((6, 6.0), popped);
239    /// assert_eq!(3, queue.len()); // sorted-nodes: 0 (12.0) << 13 (13.0) << 100 (100.0)
240    ///
241    /// assert_eq!(Some((0, 12.0)), queue.pop());
242    /// assert_eq!(Some((13, 13.0)), queue.pop());
243    /// assert_eq!(Some((100, 100.0)), queue.pop());
244    /// assert!(queue.is_empty());
245    /// ```
246    fn push_then_pop(&mut self, node: N, key: K) -> (N, K);
247
248    /// Returns an iterator visiting all values on the heap in arbitrary order.
249    fn iter(&self) -> Self::Iter<'_>;
250}