Skip to content

Commit 9e1d2f3

Browse files
authored
feat(hasher): user AHash instead of the default hasher from IndexMap (yamafaktory#42)
* feat(hasher): user AHash instead of the default hasher from IndexMap * fix(clippy): fix clippy hints
1 parent 38bc981 commit 9e1d2f3

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed

Cargo.lock

Lines changed: 44 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rust-version = "1.56"
1313
version = "2.0.0"
1414

1515
[dependencies]
16+
ahash = "0.8.3"
1617
indexmap = { version = "1.9.2", features = ["rayon"] }
1718
itertools = "0.10.5"
1819
rayon = "1.7.0"

src/core/mod.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod indexes;
77
#[doc(hidden)]
88
pub mod iterator;
99
mod shared;
10+
#[doc(hidden)]
11+
mod types;
1012
mod utils;
1113
#[doc(hidden)]
1214
pub mod vertices;
@@ -18,7 +20,7 @@ use std::{
1820
};
1921

2022
use bi_hash_map::BiHashMap;
21-
use indexmap::{IndexMap, IndexSet};
23+
use types::{AIndexMap, AIndexSet, ARandomState};
2224

2325
// Reexport indexes at this level.
2426
pub use crate::core::indexes::{HyperedgeIndex, VertexIndex};
@@ -65,20 +67,24 @@ impl<HE> Deref for HyperedgeKey<HE> {
6567
pub struct Hypergraph<V, HE> {
6668
/// Vertices are stored as a map whose unique keys are the weights
6769
/// and the values are a set of the hyperedges indexes which include
68-
// the current vertex.
69-
vertices: IndexMap<V, IndexSet<usize>>,
70+
/// the current vertex.
71+
vertices: AIndexMap<V, AIndexSet<usize>>,
7072

7173
/// Hyperedges are stored as a set whose unique keys are a combination of
7274
/// vertices indexes and a weight. Two or more hyperedges can contain
7375
/// the exact same vertices (non-simple hypergraph).
74-
hyperedges: IndexSet<HyperedgeKey<HE>>,
76+
hyperedges: AIndexSet<HyperedgeKey<HE>>,
7577

76-
// Bi-directional maps for hyperedges and vertices.
78+
/// Bi-directional map for hyperedges.
7779
hyperedges_mapping: BiHashMap<HyperedgeIndex>,
80+
81+
/// Bi-directional map for vertices.
7882
vertices_mapping: BiHashMap<VertexIndex>,
7983

80-
// Stable index generation counters.
84+
/// Stable index generation counter for hyperedges.
8185
hyperedges_count: usize,
86+
87+
/// Stable index generation counter for vertices.
8288
vertices_count: usize,
8389
}
8490

@@ -136,10 +142,10 @@ where
136142
Hypergraph {
137143
hyperedges_count: 0,
138144
hyperedges_mapping: BiHashMap::default(),
139-
hyperedges: IndexSet::with_capacity(hyperedges),
145+
hyperedges: AIndexSet::with_capacity_and_hasher(hyperedges, ARandomState::default()),
140146
vertices_count: 0,
141147
vertices_mapping: BiHashMap::default(),
142-
vertices: IndexMap::with_capacity(vertices),
148+
vertices: AIndexMap::with_capacity_and_hasher(vertices, ARandomState::default()),
143149
}
144150
}
145151
}

src/core/types.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use ahash::RandomState;
2+
use indexmap::{IndexMap, IndexSet};
3+
4+
/// Type alias to use `AHash` as a faster hasher for `IndexMap`.
5+
pub(crate) type AIndexMap<K, V> = IndexMap<K, V, RandomState>;
6+
7+
/// Type alias to use `AHash` as a faster hasher for `IndexSet`.
8+
pub(crate) type AIndexSet<T> = IndexSet<T, RandomState>;
9+
10+
/// Type alias for the `AHash` hasher factory.
11+
pub(crate) type ARandomState = RandomState;

src/core/vertices/add_vertex.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use indexmap::IndexSet;
2-
3-
use crate::{errors::HypergraphError, HyperedgeTrait, Hypergraph, VertexIndex, VertexTrait};
1+
use crate::{
2+
core::types::{AIndexSet, ARandomState},
3+
errors::HypergraphError,
4+
HyperedgeTrait, Hypergraph, VertexIndex, VertexTrait,
5+
};
46

57
impl<V, HE> Hypergraph<V, HE>
68
where
@@ -17,7 +19,10 @@ where
1719

1820
self.vertices
1921
.entry(weight)
20-
.or_insert(IndexSet::with_capacity(0));
22+
.or_insert(AIndexSet::with_capacity_and_hasher(
23+
0,
24+
ARandomState::default(),
25+
));
2126

2227
let internal_index = self
2328
.vertices

0 commit comments

Comments
 (0)