pub struct TwowayMap<TA, TB> { /* private fields */ }Expand description
For this assignment you are to complete the implementation of a two-way hashmap (bijective hashmap) by implementing a data structure that contains two hashmaps underneath, one in each direction. The information in the two maps must be consistent and reflect a one-to-one relationship. You must be able to lookup and remove either value using the other. Since the key and value will have to be stored in both maps, you should use Rc. I’ve defined the basic struct for you.
Implementations§
Source§impl<TA: Hash + Eq, TB: Hash + Eq> TwowayMap<TA, TB>
Complete the implementation of the bijective hashmap. Essentially this
means completing the set function.
impl<TA: Hash + Eq, TB: Hash + Eq> TwowayMap<TA, TB>
Complete the implementation of the bijective hashmap. Essentially this
means completing the set function.
Sourcepub fn set(&mut self, a: TA, b: TB) -> (Option<TA>, Option<TB>)
pub fn set(&mut self, a: TA, b: TB) -> (Option<TA>, Option<TB>)
insert or change the association between a and b. Note you will have to
remove previous associations for a and b in either map. For example,
if you map “Monday” to 1 and “Tuesday” to 2, and call .set(“Monday”,2),
you will have to delete FOUR entries first before inserting the new ones.
Please note, given an x:Rc<T>, Rc::into_inner(x) will return Option<T>,
taking the value out IF there is only one strong reference count for the
Rc. Also, given a x:&Rc<T>, x.as_ref() will give you a &T. This
function should return the previously associated values.
Study the functions that have already been defined for you for hints.
Sourcepub fn forward_get(&self, x: &TA) -> Option<&TB>
pub fn forward_get(&self, x: &TA) -> Option<&TB>
this function has been defined for you. Note that because of
deref coercion, it’s ok to look up with a &TA, and not
necessarily a &Rc<TA>. However, you will have to apply the
as_ref() transformation on the value that’s looked up.
Sourcepub fn backward_get(&self, x: &TB) -> Option<&TA>
pub fn backward_get(&self, x: &TB) -> Option<&TA>
complete the definition of this function
Sourcepub fn forward_remove(&mut self, x: &TA) -> Option<TB>
pub fn forward_remove(&mut self, x: &TA) -> Option<TB>
This function has been defined for you. Note that calling
Rc::into_inner at the end will succeed because the duplicate
has been removed from the backwards map.
Sourcepub fn backward_remove(&mut self, x: &TB) -> Option<TA>
pub fn backward_remove(&mut self, x: &TB) -> Option<TA>
complete the definition of this function