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

Flip: turn your BiMap<L, R> into BiMap<R, L> #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,30 @@ where
inner: self.right2left.range::<Wrapper<_>, _>((start, end)),
}
}

/// Swaps the left side and the right side of the bimap.
///
/// # Examples
///
/// ```
/// use std::iter::FromIterator;
/// use bimap::BiBTreeMap;
///
/// let int_char = BiBTreeMap::from_iter([(0, '0'), (1, '1')]);
/// let char_int = BiBTreeMap::from_iter([('0', 0), ('1', 1)]);
/// assert_eq!(int_char.clone().flip(), char_int);
/// assert_eq!(int_char, char_int.flip());
/// ```
pub fn flip(self) -> BiBTreeMap<R, L> {
let BiBTreeMap {
left2right,
right2left,
} = self;
BiBTreeMap {
left2right: right2left,
right2left: left2right,
}
}
}

impl<L, R> Clone for BiBTreeMap<L, R>
Expand Down
24 changes: 24 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ where
inner: self.right2left.iter(),
}
}

/// Swaps the left side and the right side of the bimap.
///
/// # Examples
///
/// ```
/// use std::iter::FromIterator;
/// use bimap::BiHashMap;
///
/// let int_char = BiHashMap::<_, _>::from_iter([(0, '0'), (1, '1')]);
/// let char_int = BiHashMap::<_, _>::from_iter([('0', 0), ('1', 1)]);
/// assert_eq!(int_char.clone().flip(), char_int);
/// assert_eq!(int_char, char_int.flip());
/// ```
pub fn flip(self) -> BiHashMap<R, L, RS, LS> {
let BiHashMap {
left2right,
right2left,
} = self;
BiHashMap {
left2right: right2left,
right2left: left2right,
}
}
}

impl<L, R, LS, RS> BiHashMap<L, R, LS, RS>
Expand Down