Skip to main content

TwowayMap

Struct TwowayMap 

Source
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.

Source

pub fn new() -> Self

creates a new twowaymap

Source

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.

Source

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.

Source

pub fn backward_get(&self, x: &TB) -> Option<&TA>

complete the definition of this function

Source

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.

Source

pub fn backward_remove(&mut self, x: &TB) -> Option<TA>

complete the definition of this function

Source

pub fn len(&self) -> usize

this function has been defined for you

Source

pub fn iter<'t>(&'t self) -> impl Iterator<Item = (&'t TA, &'t TB)>

this function has been defined for you

Auto Trait Implementations§

§

impl<TA, TB> Freeze for TwowayMap<TA, TB>

§

impl<TA, TB> RefUnwindSafe for TwowayMap<TA, TB>

§

impl<TA, TB> !Send for TwowayMap<TA, TB>

§

impl<TA, TB> !Sync for TwowayMap<TA, TB>

§

impl<TA, TB> Unpin for TwowayMap<TA, TB>

§

impl<TA, TB> UnsafeUnpin for TwowayMap<TA, TB>

§

impl<TA, TB> UnwindSafe for TwowayMap<TA, TB>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.