Skip to main content

BijectiveMap

Struct BijectiveMap 

Source
pub struct BijectiveMap<KT, VT, const CAP: usize = 16> { /* private fields */ }
Expand description

This is your bijective map struct

Implementations§

Source§

impl<KT: Hash + Eq, VT: Hash + Eq, const CAP: usize> BijectiveMap<KT, VT, CAP>

Source

pub fn new() -> Self

creates a new BijectiveMap with default initial capacity:

Source

pub fn get_by_key(&mut self, key: &KT) -> Option<&VT>

Returns a immutable reference to the value corresponding to the given key, if it exists. Note that there can’t be a Option<&mut T> version of this method because changing the value will require adjusting other associations in the map. This function has been completed and you can consult its source as hint.

Source§

impl<KT: Hash + Eq, VT: Hash + Eq, const CAP: usize> BijectiveMap<KT, VT, CAP>

Place all your code in another impl block, and fully implement the methods BijectiveMap::get_by_val, BijectiveMap::take_by_key, BijectiveMap::take_by_val, and BijectiveMap::set.

Source

pub fn len(&self) -> usize

Returns number of key-value pairs in this map. This one’s too easy so I just did it for you again. I will give away more stuff if you come to me for help …

Source

pub fn get_by_val(&mut self, val: &VT) -> Option<&KT>

This is the first method you need to implement. Returns an immutable reference to the key associated with the given value, if it exists. The dummy provided here just returns None and you need to rewrite it appropriately.

Source

pub fn take_by_key(&mut self, key: &KT) -> Option<(KT, VT)>

This is another method you need to implement. Removes key-value pair from map by searching for it by key. Returns the key and value if found. Note that this function is “take” and not “get” because it moves the actual key and value out of the map. It doesn’t return references. Sometimes you may need to convert an Option<T> to an Option<&T>. To do that, given such an Option<T> opt, use the expression opt.as_ref().

The dummy implemenetation provided here just returns None

Source

pub fn take_by_val(&mut self, val: &VT) -> Option<(KT, VT)>

Dummies always return None. Are you a dummy?

Source

pub fn set(&mut self, key: KT, val: VT) -> Option<(KT, VT)>

Change or add key-val pair, return a pair to represent information that was deleted. Note that the returned key,value, if they exist, may indicate a different key or value that existed in the map before the change (see “daynum” example that you should replicate in main). Replace the dummy implementation.

Auto Trait Implementations§

§

impl<KT, VT, const CAP: usize> Freeze for BijectiveMap<KT, VT, CAP>

§

impl<KT, VT, const CAP: usize> RefUnwindSafe for BijectiveMap<KT, VT, CAP>

§

impl<KT, VT, const CAP: usize> Send for BijectiveMap<KT, VT, CAP>
where KT: Send, VT: Send,

§

impl<KT, VT, const CAP: usize> Sync for BijectiveMap<KT, VT, CAP>
where KT: Sync, VT: Sync,

§

impl<KT, VT, const CAP: usize> Unpin for BijectiveMap<KT, VT, CAP>
where KT: Unpin, VT: Unpin,

§

impl<KT, VT, const CAP: usize> UnsafeUnpin for BijectiveMap<KT, VT, CAP>

§

impl<KT, VT, const CAP: usize> UnwindSafe for BijectiveMap<KT, VT, CAP>
where KT: UnwindSafe, VT: UnwindSafe,

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.