Skip to content

Commit 800bd3a

Browse files
committed
data_structures: Add deterministic FxHashMap and FxHashSet wrappers
StableMap A wrapper for FxHashMap that allows to insert, remove, get and get_mut but no iteration support. StableSet A wrapper for FxHashSet that allows to insert, remove, get and create a sorted vector from a hashset but no iteration support.
1 parent f3c8eba commit 800bd3a

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

src/librustc_data_structures/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub mod box_region;
7373
pub mod const_cstr;
7474
pub mod flock;
7575
pub mod fx;
76+
pub mod stable_map;
7677
pub mod graph;
7778
pub mod indexed_vec;
7879
pub mod jobserver;
@@ -84,6 +85,7 @@ pub mod small_c_str;
8485
pub mod snapshot_map;
8586
pub use ena::snapshot_vec;
8687
pub mod sorted_map;
88+
pub mod stable_set;
8789
#[macro_use] pub mod stable_hasher;
8890
pub mod sync;
8991
pub mod sharded;
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
pub use rustc_hash::FxHashMap;
2+
use std::borrow::Borrow;
3+
use std::collections::hash_map::Entry;
4+
use std::fmt;
5+
use std::hash::Hash;
6+
7+
/// A deterministic wrapper around FxHashMap that does not provide iteration support.
8+
///
9+
/// It supports insert, remove, get and get_mut functions from FxHashMap.
10+
/// It also allows to convert hashmap to a sorted vector with the method `into_sorted_vector()`.
11+
#[derive(Clone)]
12+
pub struct StableMap<K, V> {
13+
base: FxHashMap<K, V>,
14+
}
15+
16+
impl<K, V> Default for StableMap<K, V>
17+
where
18+
K: Eq + Hash,
19+
{
20+
fn default() -> StableMap<K, V> {
21+
StableMap::new()
22+
}
23+
}
24+
25+
impl<K, V> fmt::Debug for StableMap<K, V>
26+
where
27+
K: Eq + Hash + fmt::Debug,
28+
V: fmt::Debug,
29+
{
30+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31+
write!(f, "{:?}", self.base)
32+
}
33+
}
34+
35+
impl<K, V> PartialEq for StableMap<K, V>
36+
where
37+
K: Eq + Hash,
38+
V: PartialEq,
39+
{
40+
fn eq(&self, other: &StableMap<K, V>) -> bool {
41+
self.base == other.base
42+
}
43+
}
44+
45+
impl<K, V> Eq for StableMap<K, V>
46+
where
47+
K: Eq + Hash,
48+
V: Eq,
49+
{}
50+
51+
impl<K, V> StableMap<K, V>
52+
where
53+
K: Eq + Hash,
54+
{
55+
pub fn new() -> StableMap<K, V> {
56+
StableMap { base: FxHashMap::default() }
57+
}
58+
59+
pub fn into_sorted_vector(self) -> Vec<(K, V)>
60+
where
61+
K: Ord + Copy,
62+
{
63+
let mut vector = self.base.into_iter().collect::<Vec<_>>();
64+
vector.sort_unstable_by_key(|pair| pair.0);
65+
vector
66+
}
67+
68+
pub fn entry(&mut self, k: K) -> Entry<'_, K, V> {
69+
self.base.entry(k)
70+
}
71+
72+
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
73+
where
74+
K: Borrow<Q>,
75+
Q: Hash + Eq,
76+
{
77+
self.base.get(k)
78+
}
79+
80+
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
81+
where
82+
K: Borrow<Q>,
83+
Q: Hash + Eq,
84+
{
85+
self.base.get_mut(k)
86+
}
87+
88+
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
89+
self.base.insert(k, v)
90+
}
91+
92+
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
93+
where
94+
K: Borrow<Q>,
95+
Q: Hash + Eq,
96+
{
97+
self.base.remove(k)
98+
}
99+
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
pub use rustc_hash::FxHashSet;
2+
use std::borrow::Borrow;
3+
use std::fmt;
4+
use std::hash::Hash;
5+
6+
/// A deterministic wrapper around FxHashSet that does not provide iteration support.
7+
///
8+
/// It supports insert, remove, get functions from FxHashSet.
9+
/// It also allows to convert hashset to a sorted vector with the method `into_sorted_vector()`.
10+
#[derive(Clone)]
11+
pub struct StableSet<T> {
12+
base: FxHashSet<T>,
13+
}
14+
15+
impl<T> Default for StableSet<T>
16+
where
17+
T: Eq + Hash,
18+
{
19+
fn default() -> StableSet<T> {
20+
StableSet::new()
21+
}
22+
}
23+
24+
impl<T> fmt::Debug for StableSet<T>
25+
where
26+
T: Eq + Hash + fmt::Debug,
27+
{
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
write!(f, "{:?}", self.base)
30+
}
31+
}
32+
33+
impl<T> PartialEq<StableSet<T>> for StableSet<T>
34+
where
35+
T: Eq + Hash,
36+
{
37+
fn eq(&self, other: &StableSet<T>) -> bool {
38+
self.base == other.base
39+
}
40+
}
41+
42+
impl<T> Eq for StableSet<T> where T: Eq + Hash {}
43+
44+
impl<T: Hash + Eq> StableSet<T> {
45+
pub fn new() -> StableSet<T> {
46+
StableSet { base: FxHashSet::default() }
47+
}
48+
49+
pub fn into_sorted_vector(self) -> Vec<T>
50+
where
51+
T: Ord,
52+
{
53+
let mut vector = self.base.into_iter().collect::<Vec<_>>();
54+
vector.sort_unstable();
55+
vector
56+
}
57+
58+
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
59+
where
60+
T: Borrow<Q>,
61+
Q: Hash + Eq,
62+
{
63+
self.base.get(value)
64+
}
65+
66+
pub fn insert(&mut self, value: T) -> bool {
67+
self.base.insert(value)
68+
}
69+
70+
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
71+
where
72+
T: Borrow<Q>,
73+
Q: Hash + Eq,
74+
{
75+
self.base.remove(value)
76+
}
77+
}

0 commit comments

Comments
 (0)