From c80141a9874345b71ee5e6c37947e1f0825698a7 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 8 Feb 2022 13:58:57 +0100 Subject: Start adding more assumed types and functions --- examples/misc/src/hashmap.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/misc/src/hashmap.rs b/examples/misc/src/hashmap.rs index e26ca582..721a859a 100644 --- a/examples/misc/src/hashmap.rs +++ b/examples/misc/src/hashmap.rs @@ -25,6 +25,7 @@ pub struct HashMap { /// The current number of values in the table num_values: usize, /// The max load factor, expressed as a fraction + /// TODO: use max_load_factor: (usize, usize), /// The table itself slots: Vec>, @@ -89,14 +90,15 @@ impl HashMap { pub fn insert(&mut self, key: Key, value: T) { let hash = hash_key(&key); let hash_mod = hash % self.slots.len(); - HashMap::insert_in_list(key, value, self.slots.get_mut(hash_mod).unwrap()); + // We may want to use slots[...] instead of get_mut... + HashMap::insert_in_list(key, value, &mut self.slots[hash_mod]); } fn get_in_list<'l, 'k>(key: &'k Key, ls: &'l List) -> Option<&'l T> { match ls { List::Nil => None, List::Cons(ckey, cvalue, ls) => { - if ckey == key { + if *ckey == *key { Some(cvalue) } else { HashMap::get_in_list(key, ls) @@ -111,14 +113,14 @@ impl HashMap { pub fn get<'l, 'k>(&'l self, key: &'k Key) -> Option<&'l T> { let hash = hash_key(key); let hash_mod = hash % self.slots.len(); - HashMap::get_in_list(key, self.slots.get(hash_mod).unwrap()) + HashMap::get_in_list(key, &self.slots[hash_mod]) } fn get_mut_in_list<'l, 'k>(key: &'k Key, ls: &'l mut List) -> Option<&'l mut T> { match ls { List::Nil => None, List::Cons(ckey, cvalue, ls) => { - if ckey == key { + if *ckey == *key { Some(cvalue) } else { HashMap::get_mut_in_list(key, ls) @@ -131,14 +133,14 @@ impl HashMap { pub fn get_mut<'l, 'k>(&'l mut self, key: &'k Key) -> Option<&'l mut T> { let hash = hash_key(key); let hash_mod = hash % self.slots.len(); - HashMap::get_mut_in_list(key, self.slots.get_mut(hash_mod).unwrap()) + HashMap::get_mut_in_list(key, &mut self.slots[hash_mod]) } fn remove_from_list(key: &Key, ls: &mut List) -> Option { match ls { List::Nil => None, - List::Cons(ckey, cvalue, tl) => { - if ckey == key { + List::Cons(ckey, _, tl) => { + if *ckey == *key { // We have to move under borrows, so we need to use // [std::mem::replace] in several steps. // Retrieve the tail @@ -163,7 +165,7 @@ impl HashMap { pub fn remove(&mut self, key: &Key) -> Option { let hash = hash_key(key); let hash_mod = hash % self.slots.len(); - HashMap::remove_from_list(key, self.slots.get_mut(hash_mod).unwrap()) + HashMap::remove_from_list(key, &mut self.slots[hash_mod]) } } -- cgit v1.2.3