Skip to content

Commit

Permalink
Add an example with Reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianmarco Garrisi committed Feb 28, 2024
1 parent 97b1547 commit 6dd30c7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ Then use the data structure inside your Rust source code as in the following Exa

Remember that, if you need serde support, you should compile using `--features serde`.

## Example

## Examples
```rust
use priority_queue::PriorityQueue;

Expand All @@ -43,6 +42,26 @@ fn main() {
}
}
```
By default, the highest priority element will be extracted first. The order can be easily reversed using the standard wrapper [`Reverse<T>`](https://doc.rust-lang.org/std/cmp/struct.Reverse.html).
```rust
use priority_queue::PriorityQueue;
use std::cmp::Reverse;

fn main() {
let mut pq = PriorityQueue::new();

assert!(pq.is_empty());
pq.push("Apples", Reverse(5));
pq.push("Bananas", Reverse(8));
pq.push("Strawberries", Reverse(23));

assert_eq!(pq.peek(), Some((&"Apples", &Reverse(5))));

for (item, _) in pq.into_sorted_iter() {
println!("{}", item);
}
}
```

## Speeding up

Expand Down
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//! For reverse order remember the standard wrapper
//! [`Reverse<T>`](https://doc.rust-lang.org/std/cmp/struct.Reverse.html)
//!
//! # Example
//! # Examples
//! ```rust
//! use priority_queue::PriorityQueue;
//!
Expand All @@ -64,6 +64,28 @@
//! println!("{}", item);
//! }
//! ```
//!
//! Reverse ordering
//! ```rust
//! use priority_queue::PriorityQueue;
//! use std::cmp::Reverse;
//!
//! fn main() {
//! let mut pq = PriorityQueue::new();
//!
//! assert!(pq.is_empty());
//! pq.push("Apples", Reverse(5));
//! pq.push("Bananas", Reverse(8));
//! pq.push("Strawberries", Reverse(23));
//!
//! assert_eq!(pq.peek(), Some((&"Apples", &Reverse(5))));
//!
//! for (item, _) in pq.into_sorted_iter() {
//! println!("{}", item);
//! }
//! }
//! ```
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
Expand Down

0 comments on commit 6dd30c7

Please sign in to comment.