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;
122use crate::store::log2_fast;
123
124use indexmap::TryReserveError as IndexMapTryReserveError;
125use std::collections::TryReserveError as StdTryReserveError;
126
127#[derive(Clone, PartialEq, Eq, Debug)]
128pub struct TryReserveError {
129 kind: TryReserveErrorKind,
130}
131
132/// The error type for `try_reserve` methods.
133#[derive(Clone, PartialEq, Eq, Debug)]
134enum TryReserveErrorKind {
135 Std(StdTryReserveError),
136 IndexMap(IndexMapTryReserveError),
137}
138
139#[cfg(feature = "std")]
140impl std::error::Error for TryReserveError {}
141
142impl core::fmt::Display for TryReserveError {
143 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
144 match &self.kind {
145 TryReserveErrorKind::Std(e) => core::fmt::Display::fmt(e, f),
146 TryReserveErrorKind::IndexMap(e) => core::fmt::Display::fmt(e, f),
147 }
148 }
149}
150
151use TryReserveErrorKind::*;
152impl From<StdTryReserveError> for TryReserveError {
153 fn from(source: StdTryReserveError) -> Self {
154 Self { kind: Std(source) }
155 }
156}
157
158impl From<IndexMapTryReserveError> for TryReserveError {
159 fn from(source: IndexMapTryReserveError) -> Self {
160 Self {
161 kind: IndexMap(source),
162 }
163 }
164}
165
166// `rebuild` takes O(len1 + len2) operations
167// and about 2 * (len1 + len2) comparisons in the worst case
168// while `extend` takes O(len2 * log_2(len1)) operations
169// and about 1 * len2 * log_2(len1) comparisons in the worst case,
170// assuming len1 >= len2.
171fn better_to_rebuild(len1: usize, len2: usize) -> bool {
172 // log(1) == 0, so the inequation always falsy
173 // log(0) is inapplicable and produces panic
174 if len1 <= 1 {
175 return false;
176 }
177
178 2 * (len1 + len2) < len2 * log2_fast(len1)
179}