Skip to content

Commit

Permalink
Merge branch 'upgrade-indexmap'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianmarco Garrisi committed Feb 27, 2024
2 parents dccb29e + d2f4001 commit dabe215
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 155 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "priority-queue"
version = "1.4.0"
version = "2.0.0"
authors = ["Gianmarco Garrisi <[email protected]>"]
description = "A Priority Queue implemented as a heap with a function to efficiently change the priority of an item."
repository = "https://github.com/garro95/priority-queue"
Expand All @@ -9,25 +9,25 @@ readme = "README.md"
keywords = ["priority", "queue", "heap"]
categories = ["data-structures", "algorithms"]
license = "LGPL-3.0 OR MPL-2.0"
edition = "2018"

build = "build.rs"
edition = "2021"

[build-dependencies]
autocfg = "1"

[dependencies]
indexmap = "1"
indexmap = {version = "2", features = [], default-features = false}
serde = { version = "1", optional = true }

[dev-dependencies]
serde_test = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
uuid = {version= "1", features = ["v4", "serde"] }
hashbrown = "0.13"
hashbrown = "0.14"

[features]
default = ["std"]
std = ["indexmap/std"]
benchmarks = []

[workspace]
Expand Down
7 changes: 0 additions & 7 deletions build.rs

This file was deleted.

10 changes: 2 additions & 8 deletions src/core_iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@
//!
//! Usually you don't need to explicitly `use` any of the types declared here.
#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
pub(crate) mod std {
pub use core::*;
pub mod alloc {
pub use ::alloc::*;
}
pub mod collections {
pub use ::alloc::collections::*;
}
pub use ::alloc::vec;
pub use core::*;
}

use std::hash::Hash;
Expand Down
25 changes: 11 additions & 14 deletions src/double_priority_queue/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@
//!
//! Usually you don't need to explicitly `use` any of the types declared here.
#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
pub(crate) mod std {
pub use core::*;
pub mod alloc {
pub use ::alloc::*;
}
pub mod collections {
pub use ::alloc::collections::*;
}
pub use ::alloc::vec;
pub use core::*;
}

use core::hash::BuildHasher;
use std::cmp::{Eq, Ord};
#[cfg(has_std)]
#[cfg(feature = "std")]
use std::collections::hash_map::RandomState;
use std::hash::Hash;
use std::iter::*;
Expand All @@ -52,7 +47,7 @@ use crate::DoublePriorityQueue;
///
/// The item is mutable too, but it is a logical error to modify it in a way that
/// changes the result of any of `hash` or `eq`.
#[cfg(has_std)]
#[cfg(feature = "std")]
pub struct IterMut<'a, I: 'a, P: 'a, H: 'a = RandomState>
where
I: Hash + Eq,
Expand All @@ -62,7 +57,7 @@ where
pos: usize,
}

#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
pub struct IterMut<'a, I: 'a, P: 'a, H: 'a>
where
I: Hash + Eq,
Expand All @@ -86,14 +81,16 @@ impl<'a, 'b: 'a, I: 'a, P: 'a, H: 'a> Iterator for IterMut<'a, I, P, H>
where
I: Hash + Eq,
P: Ord,
H: BuildHasher,
{
type Item = (&'a mut I, &'a mut P);
fn next(&mut self) -> Option<Self::Item> {
use indexmap::map::MutableKeys;
let r: Option<(&'a mut I, &'a mut P)> = self
.pq
.store
.map
.get_index_mut(self.pos)
.get_index_mut2(self.pos)
.map(|(i, p)| (i as *mut I, p as *mut P))
.map(|(i, p)| unsafe { (i.as_mut().unwrap(), p.as_mut().unwrap()) });
self.pos += 1;
Expand All @@ -119,7 +116,7 @@ where
/// Since it implements [`DoubleEndedIterator`], this iterator can be reversed at any time
/// calling `rev`, at which point, elements will be extracted from the one with maximum priority
/// to the one with minimum priority.
#[cfg(has_std)]
#[cfg(feature = "std")]
pub struct IntoSortedIter<I, P, H = RandomState>
where
I: Hash + Eq,
Expand All @@ -128,7 +125,7 @@ where
pub(crate) pq: DoublePriorityQueue<I, P, H>,
}

#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
pub struct IntoSortedIter<I, P, H>
where
I: Hash + Eq,
Expand Down
98 changes: 51 additions & 47 deletions src/double_priority_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
pub mod iterators;

#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
use std::vec::Vec;

use crate::core_iterators::{IntoIter, Iter};
Expand All @@ -32,7 +32,7 @@ use iterators::*;

use std::borrow::Borrow;
use std::cmp::{Eq, Ord};
#[cfg(has_std)]
#[cfg(feature = "std")]
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
use std::iter::{Extend, FromIterator, IntoIterator, Iterator};
Expand Down Expand Up @@ -79,7 +79,7 @@ use std::mem::replace;
/// }
/// ```
#[derive(Clone)]
#[cfg(has_std)]
#[cfg(feature = "std")]
pub struct DoublePriorityQueue<I, P, H = RandomState>
where
I: Hash + Eq,
Expand All @@ -89,7 +89,7 @@ where
}

#[derive(Clone)]
#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
pub struct DoublePriorityQueue<I, P, H>
where
I: Hash + Eq,
Expand Down Expand Up @@ -118,7 +118,7 @@ where
}
}

#[cfg(has_std)]
#[cfg(feature = "std")]
impl<I, P> DoublePriorityQueue<I, P>
where
P: Ord,
Expand Down Expand Up @@ -230,48 +230,6 @@ where
})
}

/// Returns the couple (item, priority) with the lowest
/// priority in the queue, or None if it is empty.
///
/// The item is a mutable reference, but it's a logic error to modify it
/// in a way that change the result of `Hash` or `Eq`.
///
/// The priority cannot be modified with a call to this function.
/// To modify the priority use `push`, `change_priority` or
/// `change_priority_by`.
///
/// Computes in **O(1)** time
pub fn peek_min_mut(&mut self) -> Option<(&mut I, &P)> {
self.find_min()
.and_then(move |i| {
self.store
.map
.get_index_mut(unsafe { *self.store.heap.get_unchecked(i.0) }.0)
})
.map(|(k, v)| (k, &*v))
}

/// Returns the couple (item, priority) with the greatest
/// priority in the queue, or None if it is empty.
///
/// The item is a mutable reference, but it's a logic error to modify it
/// in a way that change the result of `Hash` or `Eq`.
///
/// The priority cannot be modified with a call to this function.
/// To modify the priority use `push`, `change_priority` or
/// `change_priority_by`.
///
/// Computes in **O(1)** time
pub fn peek_max_mut(&mut self) -> Option<(&mut I, &P)> {
self.find_max()
.and_then(move |i| {
self.store
.map
.get_index_mut(unsafe { *self.store.heap.get_unchecked(i.0) }.0)
})
.map(|(k, v)| (k, &*v))
}

/// Returns the number of elements the internal map can hold without
/// reallocating.
///
Expand Down Expand Up @@ -471,6 +429,51 @@ where
})
}

/// Returns the couple (item, priority) with the lowest
/// priority in the queue, or None if it is empty.
///
/// The item is a mutable reference, but it's a logic error to modify it
/// in a way that change the result of `Hash` or `Eq`.
///
/// The priority cannot be modified with a call to this function.
/// To modify the priority use `push`, `change_priority` or
/// `change_priority_by`.
///
/// Computes in **O(1)** time
pub fn peek_min_mut(&mut self) -> Option<(&mut I, &P)> {
use indexmap::map::MutableKeys;

self.find_min()
.and_then(move |i| {
self.store
.map
.get_index_mut2(unsafe { *self.store.heap.get_unchecked(i.0) }.0)
})
.map(|(k, v)| (k, &*v))
}

/// Returns the couple (item, priority) with the greatest
/// priority in the queue, or None if it is empty.
///
/// The item is a mutable reference, but it's a logic error to modify it
/// in a way that change the result of `Hash` or `Eq`.
///
/// The priority cannot be modified with a call to this function.
/// To modify the priority use `push`, `change_priority` or
/// `change_priority_by`.
///
/// Computes in **O(1)** time
pub fn peek_max_mut(&mut self) -> Option<(&mut I, &P)> {
use indexmap::map::MutableKeys;
self.find_max()
.and_then(move |i| {
self.store
.map
.get_index_mut2(unsafe { *self.store.heap.get_unchecked(i.0) }.0)
})
.map(|(k, v)| (k, &*v))
}

/// Change the priority of an Item using the provided function.
/// Return a boolean value where `true` means the item was in the queue and update was successful
///
Expand Down Expand Up @@ -886,6 +889,7 @@ impl<'a, I, P, H> IntoIterator for &'a mut DoublePriorityQueue<I, P, H>
where
I: Hash + Eq,
P: Ord,
H: BuildHasher,
{
type Item = (&'a mut I, &'a mut P);
type IntoIter = IterMut<'a, I, P, H>;
Expand Down
13 changes: 3 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,14 @@
//! println!("{}", item);
//! }
//! ```
#![cfg_attr(not(has_std), no_std)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(has_std))]
#[cfg(not(feature = "std"))]
pub(crate) mod std {
pub use core::*;
pub mod alloc {
pub use ::alloc::*;
}
pub mod collections {
pub use ::alloc::collections::*;
}
pub use ::alloc::vec;
}

pub mod core_iterators;
Expand Down
Loading

0 comments on commit dabe215

Please sign in to comment.