Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] OccupiedEntry.replace_ #11

Merged
merged 2 commits into from
Jul 2, 2020
Merged
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
62 changes: 61 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,6 @@ impl<K: Debug, V: Debug, S> Debug for Entry<'_, K, V, S> {
///
/// [`Entry`]: enum.Entry.html
pub struct OccupiedEntry<'a, K, V, S> {
#[allow(dead_code)] // remove when replace_ methods are implemented
key: Option<K>,
elem: Bucket<(K, V)>,
table: &'a mut HashMap<K, V, S>,
Expand Down Expand Up @@ -1857,6 +1856,67 @@ impl<'a, K, V, S> OccupiedEntry<'a, K, V, S> {
// restarting the iterator, though that would come at a performance penalty...
//
// when implemented, remove the #[allow(dead_code)] on OccupiedEntry::key

/// Replaces the entry, returning the old key and value. The new key in the hash map will be
/// the key used to create this entry.
///
/// # Examples
///
/// ```
/// use griddle::hash_map::{Entry, HashMap};
/// use std::rc::Rc;
///
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
/// map.insert(Rc::new("Stringthing".to_string()), 15);
///
/// let my_key = Rc::new("Stringthing".to_string());
///
/// if let Entry::Occupied(entry) = map.entry(my_key.clone()) {
/// // Also replace the key with a handle to our other key.
/// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
/// }
///
/// assert_eq!(map[&my_key], 16);
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn replace_entry(self, value: V) -> (K, V) {
let entry = unsafe { self.elem.as_mut() };

let old_key = mem::replace(&mut entry.0, self.key.unwrap());
let old_value = mem::replace(&mut entry.1, value);

(old_key, old_value)
}

/// Replaces the key in the hash map with the key used to create this entry.
///
/// # Examples
///
/// ```
/// use griddle::hash_map::{Entry, HashMap};
/// use std::rc::Rc;
///
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
/// let mut known_strings: Vec<Rc<String>> = Vec::new();
///
/// // Initialise known strings, run program, etc.
///
/// reclaim_memory(&mut map, &known_strings);
///
/// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
/// for s in known_strings {
/// if let Entry::Occupied(entry) = map.entry(s.clone()) {
/// // Replaces the entry's key with our version of it in `known_strings`.
/// entry.replace_key();
/// }
/// }
/// }
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn replace_key(self) -> K {
let entry = unsafe { self.elem.as_mut() };
mem::replace(&mut entry.0, self.key.unwrap())
}
}

impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
Expand Down
58 changes: 58 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,32 @@ where
self.map.insert(value, ()).is_none()
}

/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
/// one. Returns the replaced value.
///
/// # Examples
///
/// ```
/// use griddle::HashSet;
///
/// let mut set = HashSet::new();
/// set.insert(Vec::<i32>::new());
///
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
/// set.replace(Vec::with_capacity(10));
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn replace(&mut self, value: T) -> Option<T> {
match self.map.entry(value) {
map::Entry::Occupied(occupied) => Some(occupied.replace_key()),
map::Entry::Vacant(vacant) => {
vacant.insert(());
None
}
}
}

/// Removes a value from the set. Returns whether the value was
/// present in the set.
///
Expand Down Expand Up @@ -1681,6 +1707,38 @@ mod test_set {
assert_eq!(format!("{:?}", empty), "{}");
}

#[test]
fn test_replace() {
use core::hash;

#[derive(Debug)]
struct Foo(&'static str, i32);

impl PartialEq for Foo {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl Eq for Foo {}

impl hash::Hash for Foo {
fn hash<H: hash::Hasher>(&self, h: &mut H) {
self.0.hash(h);
}
}

let mut s = HashSet::new();
assert_eq!(s.replace(Foo("a", 1)), None);
assert_eq!(s.len(), 1);
assert_eq!(s.replace(Foo("a", 2)), Some(Foo("a", 1)));
assert_eq!(s.len(), 1);

let mut it = s.iter();
assert_eq!(it.next(), Some(&Foo("a", 2)));
assert_eq!(it.next(), None);
}

#[test]
fn test_extend_ref() {
let mut a = HashSet::new();
Expand Down