Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ iai = []
[dependencies]
num-traits = "0.2.19"
indexmap = "2.11.4"
rustc-hash = "2.1.1"
ahash = "0.8.12"
integer-sqrt = "0.1.5"
thiserror = "2.0.17"
deprecate-until = "0.1.1"
Expand Down
11 changes: 7 additions & 4 deletions src/directed/count_paths.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
//! Count the total number of possible paths to reach a destination.

use std::hash::Hash;
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hash};

use rustc_hash::FxHashMap;
use ahash::AHasher;

type AHashMap<K, V> = HashMap<K, V, BuildHasherDefault<AHasher>>;

fn cached_count_paths<T, FN, IN, FS>(
start: T,
successors: &mut FN,
success: &mut FS,
cache: &mut FxHashMap<T, usize>,
cache: &mut AHashMap<T, usize>,
) -> usize
where
T: Eq + Hash,
Expand Down Expand Up @@ -66,6 +69,6 @@ where
start,
&mut successors,
&mut success,
&mut FxHashMap::default(),
&mut AHashMap::default(),
)
}
15 changes: 9 additions & 6 deletions src/directed/dfs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Compute a path using the [depth-first search
//! algorithm](https://en.wikipedia.org/wiki/Depth-first_search).

use std::collections::HashSet;
use std::hash::Hash;
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasherDefault, Hash};
use std::iter::FusedIterator;

use rustc_hash::{FxHashMap, FxHashSet};
use ahash::AHasher;

type AHashMap<K, V> = HashMap<K, V, BuildHasherDefault<AHasher>>;
type AHashSet<T> = HashSet<T, BuildHasherDefault<AHasher>>;

/// Compute a path using the [depth-first search
/// algorithm](https://en.wikipedia.org/wiki/Depth-first_search).
Expand Down Expand Up @@ -54,8 +57,8 @@ where
FS: FnMut(&N) -> bool,
{
let mut to_visit = vec![start];
let mut visited = FxHashSet::default();
let mut parents = FxHashMap::default();
let mut visited = AHashSet::default();
let mut parents = AHashMap::default();
while let Some(node) = to_visit.pop() {
if visited.insert(node.clone()) {
if success(&node) {
Expand All @@ -77,7 +80,7 @@ where
None
}

fn build_path<N>(mut node: N, parents: &FxHashMap<N, N>) -> Vec<N>
fn build_path<N>(mut node: N, parents: &AHashMap<N, N>) -> Vec<N>
where
N: Clone + Eq + Hash,
{
Expand Down
18 changes: 11 additions & 7 deletions src/directed/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ use super::reverse_path;
use crate::FxIndexMap;
use indexmap::map::Entry::{Occupied, Vacant};
use num_traits::Zero;
use rustc_hash::{FxHashMap, FxHashSet};
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap};
use std::hash::Hash;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::hash::{BuildHasherDefault, Hash};

use ahash::AHasher;

type AHashMap<K, V> = HashMap<K, V, BuildHasherDefault<AHasher>>;
type AHashSet<T> = HashSet<T, BuildHasherDefault<AHasher>>;

/// Compute a shortest path using the [Dijkstra search
/// algorithm](https://en.wikipedia.org/wiki/Dijkstra's_algorithm).
Expand Down Expand Up @@ -326,9 +330,9 @@ impl<K: Ord> Ord for SmallestHolder<K> {
/// Struct returned by [`dijkstra_reach`].
pub struct DijkstraReachable<N, C, FN> {
to_see: BinaryHeap<SmallestHolder<C>>,
seen: FxHashSet<usize>,
seen: AHashSet<usize>,
parents: FxIndexMap<N, (usize, C)>,
total_costs: FxHashMap<N, C>,
total_costs: AHashMap<N, C>,
successors: FN,
}

Expand Down Expand Up @@ -422,10 +426,10 @@ where
let mut parents: FxIndexMap<N, (usize, C)> = FxIndexMap::default();
parents.insert(start.clone(), (usize::MAX, Zero::zero()));

let mut total_costs = FxHashMap::default();
let mut total_costs = AHashMap::default();
total_costs.insert(start.clone(), Zero::zero());

let seen = FxHashSet::default();
let seen = AHashSet::default();

DijkstraReachable {
to_see,
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ pub mod utils;
mod noderefs;
pub use noderefs::NodeRefs;

use ahash::AHasher;
use indexmap::{IndexMap, IndexSet};
use rustc_hash::FxHasher;
use std::hash::BuildHasherDefault;

type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
type FxIndexSet<K> = IndexSet<K, BuildHasherDefault<FxHasher>>;
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<AHasher>>;
type FxIndexSet<K> = IndexSet<K, BuildHasherDefault<AHasher>>;

/// Export all public functions and structures for an easy access.
pub mod prelude {
Expand Down
20 changes: 12 additions & 8 deletions src/noderefs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use rustc_hash::FxHashSet;
use std::hash::Hash;
use std::collections::HashSet;
use std::hash::{BuildHasherDefault, Hash};
use std::iter::FromIterator;
use std::ops::Deref;

use ahash::AHasher;

type AHashSet<T> = HashSet<T, BuildHasherDefault<AHasher>>;

/// A set of node references.
///
/// Can be created from a single node reference or an iterable of
Expand All @@ -22,19 +26,19 @@ use std::ops::Deref;
/// let refs: NodeRefs<N> = NodeRefs::from_iter([&red, &blue, &green]);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeRefs<'a, N>(FxHashSet<&'a N>)
pub struct NodeRefs<'a, N>(AHashSet<&'a N>)
where
N: Eq + Hash + Clone;

impl<'a, N: Eq + Hash + Clone> FromIterator<&'a N> for NodeRefs<'a, N> {
fn from_iter<T: IntoIterator<Item = &'a N>>(iter: T) -> Self {
NodeRefs(FxHashSet::from_iter(iter))
NodeRefs(AHashSet::from_iter(iter))
}
}

impl<'a, N: Eq + Hash + Clone> From<&'a N> for NodeRefs<'a, N> {
fn from(value: &'a N) -> Self {
NodeRefs(FxHashSet::from_iter([value]))
NodeRefs(AHashSet::from_iter([value]))
}
}

Expand All @@ -57,7 +61,7 @@ impl<'a, N: Eq + Hash + Clone> IntoIterator for &'a NodeRefs<'a, N> {
}

impl<'a, N: Eq + Hash + Clone> Deref for NodeRefs<'a, N> {
type Target = FxHashSet<&'a N>;
type Target = AHashSet<&'a N>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -78,15 +82,15 @@ mod tests {
let refs = NodeRefs::from_iter(&nodes);
assert_eq!(
refs.0,
FxHashSet::from_iter([&nodes[0], &nodes[1], &nodes[2]])
AHashSet::from_iter([&nodes[0], &nodes[1], &nodes[2]])
);
}

#[test]
fn test_from_single_ref() {
let node = Node(42);
let refs = NodeRefs::from(&node);
assert_eq!(refs.0, FxHashSet::from_iter([&node]));
assert_eq!(refs.0, AHashSet::from_iter([&node]));
}

#[test]
Expand Down
14 changes: 10 additions & 4 deletions src/undirected/connected_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::hash::{BuildHasherDefault, Hash};
use std::marker::PhantomData;

use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use ahash::AHasher;

/// A connected component implementation for various generic types.
///
Expand Down Expand Up @@ -107,8 +107,14 @@ where
let (_, gindices) = Self::separate_components(groups);
// Pre-size the hash map to reduce reallocations
let estimated_capacity = gindices.iter().filter(|&&n| n != usize::MAX).count();
let mut gb: FxHashMap<usize, FxHashSet<N>> =
FxHashMap::with_capacity_and_hasher(estimated_capacity, FxBuildHasher);
let mut gb: HashMap<
usize,
HashSet<N, BuildHasherDefault<AHasher>>,
BuildHasherDefault<AHasher>,
> = HashMap::with_capacity_and_hasher(
estimated_capacity,
BuildHasherDefault::<AHasher>::default(),
);
for (i, n) in gindices
.into_iter()
.enumerate()
Expand Down
Loading