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//! # #[cfg(feature = "std")] {
58//! use priority_queue::PriorityQueue;
59//!
60//! let mut pq = PriorityQueue::new();
61//!
62//! assert!(pq.is_empty());
63//! pq.push("Apples", 5);
64//! pq.push("Bananas", 8);
65//! pq.push("Strawberries", 23);
66//!
67//! assert_eq!(pq.peek(), Some((&"Strawberries", &23)));
68//!
69//! pq.change_priority("Bananas", 25);
70//! assert_eq!(pq.peek(), Some((&"Bananas", &25)));
71//!
72//! for (item, _) in pq.into_sorted_iter() {
73//!     println!("{}", item); // Will print Bananas, Strawberries, Apples
74//! }
75//! # }
76//! ```
77//!
78//! ## Reverse ordering
79//! ```rust
80//! # #[cfg(feature = "std")] {
81//! use priority_queue::PriorityQueue;
82//! use std::cmp::Reverse;
83//!
84//! let mut pq = PriorityQueue::new();
85//!
86//! assert!(pq.is_empty());
87//! pq.push("Apples", Reverse(5));
88//! pq.push("Bananas", Reverse(8));
89//! pq.push("Strawberries", Reverse(23));
90//!
91//! assert_eq!(pq.peek(), Some((&"Apples", &Reverse(5))));
92//!
93//! for (item, _) in pq.into_sorted_iter() {
94//!     println!("{}", item); // Will print Apples, Bananas, Strawberries
95//! }
96//! # }
97//! ```
98//!
99//! # Crate features
100//!
101//! * **std** - Use the standard library. This enables the creation of queues
102//!   with the standard hasher `RandomState` using the `new` and `with_capacity`
103//!   static methods.
104//!   This feature is **enabled by default** and can be disabled when compiling
105//!   for no_std targets.
106//! * **serde** - Enables serialization/deserialization using serde
107#![cfg_attr(not(feature = "std"), no_std)]
108#![cfg_attr(docsrs, feature(doc_cfg))]
109
110#[cfg(not(feature = "std"))]
111extern crate alloc;
112
113#[cfg(not(feature = "std"))]
114pub(crate) mod std {
115    pub use ::alloc::collections;
116    pub use core::*;
117}
118
119pub mod core_iterators;
120pub mod double_priority_queue;
121pub mod priority_queue;
122mod store;
123
124pub use crate::double_priority_queue::DoublePriorityQueue;
125pub use crate::priority_queue::PriorityQueue;
126use crate::store::log2_fast;
127
128use indexmap::TryReserveError as IndexMapTryReserveError;
129use std::collections::TryReserveError as StdTryReserveError;
130
131#[derive(Clone, PartialEq, Eq, Debug)]
132pub struct TryReserveError {
133    kind: TryReserveErrorKind,
134}
135
136/// The error type for `try_reserve` methods.
137#[derive(Clone, PartialEq, Eq, Debug)]
138enum TryReserveErrorKind {
139    Std(StdTryReserveError),
140    IndexMap(IndexMapTryReserveError),
141}
142
143#[cfg(feature = "std")]
144impl std::error::Error for TryReserveError {}
145
146impl core::fmt::Display for TryReserveError {
147    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
148        match &self.kind {
149            TryReserveErrorKind::Std(e) => core::fmt::Display::fmt(e, f),
150            TryReserveErrorKind::IndexMap(e) => core::fmt::Display::fmt(e, f),
151        }
152    }
153}
154
155use TryReserveErrorKind::*;
156impl From<StdTryReserveError> for TryReserveError {
157    fn from(source: StdTryReserveError) -> Self {
158        Self { kind: Std(source) }
159    }
160}
161
162impl From<IndexMapTryReserveError> for TryReserveError {
163    fn from(source: IndexMapTryReserveError) -> Self {
164        Self {
165            kind: IndexMap(source),
166        }
167    }
168}
169
170// `rebuild` takes O(len1 + len2) operations
171// and about 2 * (len1 + len2) comparisons in the worst case
172// while `extend` takes O(len2 * log_2(len1)) operations
173// and about 1 * len2 * log_2(len1) comparisons in the worst case,
174// assuming len1 >= len2.
175fn better_to_rebuild(len1: usize, len2: usize) -> bool {
176    // log(1) == 0, so the inequation always falsy
177    // log(0) is inapplicable and produces panic
178    if len1 <= 1 {
179        return false;
180    }
181
182    2 * (len1 + len2) < len2 * log2_fast(len1)
183}