priority_queue/lib.rs
1/*
2 * Copyright 2017 Gianmarco Garrisi and contributors
3 *
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version, or (at your option) under the terms
9 * of the Mozilla Public License version 2.0.
10 *
11 * ----
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * ----
22 *
23 * This Source Code Form is subject to the terms of the Mozilla Public License,
24 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
25 * obtain one at http://mozilla.org/MPL/2.0/.
26 *
27 */
28
29//! This crate provides 2 main data structures:
30//! * a [priority queue](PriorityQueue)
31//! * a [double priority queue](DoublePriorityQueue).
32//!
33//! Both data structures are backed by an hashmap, allowing
34//! to change the priority of an element with some efficient methods in
35//! **O(log(N))** time (worst case).
36//!
37//! The elements (called `Item`s, of type `I`) must implement
38//! [`Hash`](https://doc.rust-lang.org/std/hash/trait.Hash.html)
39//! and [`Eq`](https://doc.rust-lang.org/std/cmp/trait.Eq.html) traits.
40//!
41//! This can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`.
42//! If you implement these yourself, it is important that the following property holds:
43//!
44//! ```text
45//! k1 == k2 -> hash(k1) == hash(k2)
46//! ```
47//!
48//! In other words, if two keys are equal, their hashes must be equal.
49//!
50//! The priority `P` may be any type that implements
51//! [`Ord`](https://doc.rust-lang.org/std/cmp/trait.Ord.html).
52//! For reverse order remember the standard wrapper
53//! [`Reverse<T>`](https://doc.rust-lang.org/std/cmp/struct.Reverse.html)
54//!
55//! # Examples
56//! ```rust
57//! use priority_queue::PriorityQueue;
58//!
59//! let mut pq = PriorityQueue::new();
60//!
61//! assert!(pq.is_empty());
62//! pq.push("Apples", 5);
63//! pq.push("Bananas", 8);
64//! pq.push("Strawberries", 23);
65//!
66//! assert_eq!(pq.peek(), Some((&"Strawberries", &23)));
67//!
68//! pq.change_priority("Bananas", 25);
69//! assert_eq!(pq.peek(), Some((&"Bananas", &25)));
70//!
71//! for (item, _) in pq.into_sorted_iter() {
72//! println!("{}", item); // Will print Bananas, Strawberries, Apples
73//! }
74//! ```
75//!
76//! ## Reverse ordering
77//! ```rust
78//! use priority_queue::PriorityQueue;
79//! use std::cmp::Reverse;
80//!
81//! let mut pq = PriorityQueue::new();
82//!
83//! assert!(pq.is_empty());
84//! pq.push("Apples", Reverse(5));
85//! pq.push("Bananas", Reverse(8));
86//! pq.push("Strawberries", Reverse(23));
87//!
88//! assert_eq!(pq.peek(), Some((&"Apples", &Reverse(5))));
89//!
90//! for (item, _) in pq.into_sorted_iter() {
91//! println!("{}", item); // Will print Apples, Bananas, Strawberries
92//! }
93//! ```
94//!
95//! # Crate features
96//!
97//! * **std** - Use the standard library. This enables the creation of queues
98//! with the standard hasher `RandomState` using the `new` and `with_capacity`
99//! static methods.
100//! This feature is **enabled by default** and can be disabled when compiling
101//! for no_std targets.
102//! * **serde** - Enables serialization/deserialization using serde
103#![cfg_attr(not(feature = "std"), no_std)]
104#![cfg_attr(docsrs, feature(doc_cfg))]
105
106#[cfg(not(feature = "std"))]
107extern crate alloc;
108
109#[cfg(not(feature = "std"))]
110pub(crate) mod std {
111 pub use ::alloc::collections;
112 pub use core::*;
113}
114
115pub mod core_iterators;
116pub mod double_priority_queue;
117pub mod priority_queue;
118mod store;
119
120pub use crate::double_priority_queue::DoublePriorityQueue;
121pub use crate::priority_queue::PriorityQueue;
122
123use indexmap::TryReserveError as IndexMapTryReserveError;
124use std::collections::TryReserveError as StdTryReserveError;
125
126#[derive(Clone, PartialEq, Eq, Debug)]
127pub struct TryReserveError {
128 kind: TryReserveErrorKind,
129}
130
131/// The error type for `try_reserve` methods.
132#[derive(Clone, PartialEq, Eq, Debug)]
133enum TryReserveErrorKind {
134 Std(StdTryReserveError),
135 IndexMap(IndexMapTryReserveError),
136}
137
138#[cfg(feature = "std")]
139impl std::error::Error for TryReserveError {}
140
141impl core::fmt::Display for TryReserveError {
142 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
143 match &self.kind {
144 TryReserveErrorKind::Std(e) => core::fmt::Display::fmt(e, f),
145 TryReserveErrorKind::IndexMap(e) => core::fmt::Display::fmt(e, f),
146 }
147 }
148}
149
150use TryReserveErrorKind::*;
151impl From<StdTryReserveError> for TryReserveError {
152 fn from(source: StdTryReserveError) -> Self {
153 Self { kind: Std(source) }
154 }
155}
156
157impl From<IndexMapTryReserveError> for TryReserveError {
158 fn from(source: IndexMapTryReserveError) -> Self {
159 Self {
160 kind: IndexMap(source),
161 }
162 }
163}