From 495b07666ca02135d0b1071d398ea597dc88ce0c Mon Sep 17 00:00:00 2001 From: Vurich Date: Wed, 6 Dec 2017 18:02:35 +0100 Subject: [PATCH 1/2] Get key reference from set/map --- src/map.rs | 8 ++++++++ src/set.rs | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/src/map.rs b/src/map.rs index c7e5eca..3d436ff 100644 --- a/src/map.rs +++ b/src/map.rs @@ -146,6 +146,14 @@ where } } + /// Returns the value corresponding to the key. + #[inline] + pub fn get_key(&self, key: K) -> Option<&K> { + let hash = Self::hash_key(&key); + + self.find_slot(key, hash).get().map(|node| &node.key) + } + /// Returns the value corresponding to the key. #[inline] pub fn get(&self, key: K) -> Option { diff --git a/src/set.rs b/src/set.rs index aab6afe..04f61af 100644 --- a/src/set.rs +++ b/src/set.rs @@ -55,6 +55,12 @@ where self.map.insert(arena, item, ()); } + /// Gets a reference to the existing value in the set, if it exists + #[inline] + pub fn get(&self, key: I) -> Option<&I> { + self.map.get_key(key) + } + /// Returns `true` if the set contains a value. #[inline] pub fn contains(&self, item: I) -> bool { From ee4745df6e99371d5766c49077debb303045fb40 Mon Sep 17 00:00:00 2001 From: Vurich Date: Wed, 6 Dec 2017 18:05:42 +0100 Subject: [PATCH 2/2] Implement `Default` for set/map --- src/map.rs | 10 ++++++++++ src/set.rs | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/src/map.rs b/src/map.rs index 3d436ff..0320d94 100644 --- a/src/map.rs +++ b/src/map.rs @@ -50,6 +50,16 @@ where last: CopyCell>>, } +impl<'arena, K, V> Default for Map<'arena, K, V> +where + K: 'arena, + V: 'arena + Copy, +{ + fn default() -> Self { + Self::new() + } +} + impl<'arena, K, V> Map<'arena, K, V> where K: 'arena, diff --git a/src/set.rs b/src/set.rs index 04f61af..ad15c22 100644 --- a/src/set.rs +++ b/src/set.rs @@ -12,6 +12,15 @@ pub struct Set<'arena, I: 'arena> { map: Map<'arena, I, ()>, } +impl<'arena, I> Default for Set<'arena, I> +where + I: 'arena, +{ + fn default() -> Self { + Self::new() + } +} + impl<'arena, I> Set<'arena, I> where I: 'arena,