Skip to content

Commit 59777a7

Browse files
authoredFeb 1, 2025
Merge pull request #58 from jmr/push-docs
Clarify documentation and add doctests
2 parents a4a311c + fd3eb6b commit 59777a7

File tree

1 file changed

+73
-17
lines changed

1 file changed

+73
-17
lines changed
 

‎src/priority_queue/mod.rs

+73-17
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,21 @@ where
332332
{
333333
/// Insert the item-priority pair into the queue.
334334
///
335-
/// If an element equal to `item` was already into the queue,
336-
/// it is updated and the old value of its priority returned in `Some`;
337-
/// otherwise, returns `None`.
335+
/// If an element equal to `item` is already in the queue, its priority
336+
/// is updated and the old priority is returned in `Some`; otherwise,
337+
/// `item` is inserted with `priority` and `None` is returned.
338+
///
339+
/// # Example
340+
/// ```
341+
/// # use priority_queue::PriorityQueue;
342+
/// let mut pq = PriorityQueue::new();
343+
/// assert_eq!(pq.push("Apples", 5), None);
344+
/// assert_eq!(pq.get_priority("Apples"), Some(&5));
345+
/// assert_eq!(pq.push("Apples", 6), Some(5));
346+
/// assert_eq!(pq.get_priority("Apples"), Some(&6));
347+
/// assert_eq!(pq.push("Apples", 4), Some(6));
348+
/// assert_eq!(pq.get_priority("Apples"), Some(&4));
349+
/// ```
338350
///
339351
/// Computes in **O(log(N))** time.
340352
pub fn push(&mut self, item: I, priority: P) -> Option<P> {
@@ -371,13 +383,29 @@ where
371383
///
372384
/// If an element equal to `item` is already in the queue with a
373385
/// lower priority, its priority is increased to the new one
374-
/// without replacing the element and the old priority is returned.
375-
/// Otherwise, the new element is inserted into the queue.
376-
///
377-
/// Returns `Some` if an element equal to `item` is already in the
378-
/// queue. If its priority is higher then `priority`, the latter is returned back,
379-
/// otherwise, the old priority is contained in the Option.
380-
/// If the item is not in the queue, `None` is returned.
386+
/// without replacing the element and the old priority is returned
387+
/// in `Some`.
388+
///
389+
/// If an element equal to `item` is already in the queue with an
390+
/// equal or higher priority, its priority is not changed and the
391+
/// `priority` argument is returned in `Some`.
392+
///
393+
/// If no element equal to `item` is already in the queue, the new
394+
/// element is inserted and `None` is returned.
395+
///
396+
/// # Example
397+
/// ```
398+
/// # use priority_queue::PriorityQueue;
399+
/// let mut pq = PriorityQueue::new();
400+
/// assert_eq!(pq.push_increase("Apples", 5), None);
401+
/// assert_eq!(pq.get_priority("Apples"), Some(&5));
402+
/// assert_eq!(pq.push_increase("Apples", 6), Some(5));
403+
/// assert_eq!(pq.get_priority("Apples"), Some(&6));
404+
/// // Already present with higher priority, so requested (lower)
405+
/// // priority is returned.
406+
/// assert_eq!(pq.push_increase("Apples", 4), Some(4));
407+
/// assert_eq!(pq.get_priority("Apples"), Some(&6));
408+
/// ```
381409
///
382410
/// Computes in **O(log(N))** time.
383411
pub fn push_increase(&mut self, item: I, priority: P) -> Option<P> {
@@ -393,13 +421,29 @@ where
393421
///
394422
/// If an element equal to `item` is already in the queue with a
395423
/// higher priority, its priority is decreased to the new one
396-
/// without replacing the element and the old priority is returned.
397-
/// Otherwise, the new element is inserted into the queue.
398-
///
399-
/// Returns `Some` if an element equal to `item` is already in the
400-
/// queue. If its priority is lower then `priority`, the latter is returned back,
401-
/// otherwise, the old priority is contained in the Option.
402-
/// If the item is not in the queue, `None` is returned.
424+
/// without replacing the element and the old priority is returned
425+
/// in `Some`.
426+
///
427+
/// If an element equal to `item` is already in the queue with an
428+
/// equal or lower priority, its priority is not changed and the
429+
/// `priority` argument is returned in `Some`.
430+
///
431+
/// If no element equal to `item` is already in the queue, the new
432+
/// element is inserted and `None` is returned.
433+
///
434+
/// # Example
435+
/// ```
436+
/// # use priority_queue::PriorityQueue;
437+
/// let mut pq = PriorityQueue::new();
438+
/// assert_eq!(pq.push_decrease("Apples", 5), None);
439+
/// assert_eq!(pq.get_priority("Apples"), Some(&5));
440+
/// assert_eq!(pq.push_decrease("Apples", 4), Some(5));
441+
/// assert_eq!(pq.get_priority("Apples"), Some(&4));
442+
/// // Already present with lower priority, so requested (higher)
443+
/// // priority is returned.
444+
/// assert_eq!(pq.push_decrease("Apples", 6), Some(6));
445+
/// assert_eq!(pq.get_priority("Apples"), Some(&4));
446+
/// ```
403447
///
404448
/// Computes in **O(log(N))** time.
405449
pub fn push_decrease(&mut self, item: I, priority: P) -> Option<P> {
@@ -416,6 +460,18 @@ where
416460
/// The argument `item` is only used for lookup, and is not used to overwrite the item's data
417461
/// in the priority queue.
418462
///
463+
/// # Example
464+
/// ```
465+
/// # use priority_queue::PriorityQueue;
466+
/// let mut pq = PriorityQueue::new();
467+
/// assert_eq!(pq.change_priority("Apples", 5), None);
468+
/// assert_eq!(pq.get_priority("Apples"), None);
469+
/// assert_eq!(pq.push("Apples", 6), None);
470+
/// assert_eq!(pq.get_priority("Apples"), Some(&6));
471+
/// assert_eq!(pq.change_priority("Apples", 4), Some(6));
472+
/// assert_eq!(pq.get_priority("Apples"), Some(&4));
473+
/// ```
474+
///
419475
/// The item is found in **O(1)** thanks to the hash table.
420476
/// The operation is performed in **O(log(N))** time.
421477
pub fn change_priority<Q: ?Sized>(&mut self, item: &Q, new_priority: P) -> Option<P>

0 commit comments

Comments
 (0)