Skip to content

Commit 13de4c5

Browse files
authored
Merge pull request #2 from Vurich/set-tools-pr
Some additions to set and map
2 parents dfe8f23 + ee4745d commit 13de4c5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/map.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ where
5050
last: CopyCell<Option<&'arena MapNode<'arena, K, V>>>,
5151
}
5252

53+
impl<'arena, K, V> Default for Map<'arena, K, V>
54+
where
55+
K: 'arena,
56+
V: 'arena + Copy,
57+
{
58+
fn default() -> Self {
59+
Self::new()
60+
}
61+
}
62+
5363
impl<'arena, K, V> Map<'arena, K, V>
5464
where
5565
K: 'arena,
@@ -146,6 +156,14 @@ where
146156
}
147157
}
148158

159+
/// Returns the value corresponding to the key.
160+
#[inline]
161+
pub fn get_key(&self, key: K) -> Option<&K> {
162+
let hash = Self::hash_key(&key);
163+
164+
self.find_slot(key, hash).get().map(|node| &node.key)
165+
}
166+
149167
/// Returns the value corresponding to the key.
150168
#[inline]
151169
pub fn get(&self, key: K) -> Option<V> {

src/set.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ pub struct Set<'arena, I: 'arena> {
1212
map: Map<'arena, I, ()>,
1313
}
1414

15+
impl<'arena, I> Default for Set<'arena, I>
16+
where
17+
I: 'arena,
18+
{
19+
fn default() -> Self {
20+
Self::new()
21+
}
22+
}
23+
1524
impl<'arena, I> Set<'arena, I>
1625
where
1726
I: 'arena,
@@ -55,6 +64,12 @@ where
5564
self.map.insert(arena, item, ());
5665
}
5766

67+
/// Gets a reference to the existing value in the set, if it exists
68+
#[inline]
69+
pub fn get(&self, key: I) -> Option<&I> {
70+
self.map.get_key(key)
71+
}
72+
5873
/// Returns `true` if the set contains a value.
5974
#[inline]
6075
pub fn contains(&self, item: I) -> bool {

0 commit comments

Comments
 (0)