pub struct SortedPair<T: PartialOrd>(/* private fields */);Expand description
A pair of elements sorted in increasing order.
This structure guarantees that the first element is always less than or equal to the second element. It is useful for representing edges, connections, or any unordered pair where you want canonical ordering (e.g., ensuring that edge (A, B) and edge (B, A) are treated as the same edge).
The sorted pair implements Deref to (T, T) for convenient access to the elements.
§Examples
§Creating a Sorted Pair
// Create a pair - the elements will be sorted automatically
let pair1 = SortedPair::new(5, 2);
let pair2 = SortedPair::new(2, 5);
// Both pairs are equal because they contain the same sorted elements
assert_eq!(pair1, pair2);
// Access elements via dereferencing
assert_eq!(pair1.0, 2);
assert_eq!(pair1.1, 5);§Using as HashMap Keys
use std::collections::HashMap;
let mut edge_weights = HashMap::new();
// These represent the same edge, so they'll map to the same entry
edge_weights.insert(SortedPair::new(1, 3), 10.0);
edge_weights.insert(SortedPair::new(3, 1), 20.0); // Overwrites previous
assert_eq!(edge_weights.len(), 1);
assert_eq!(edge_weights.get(&SortedPair::new(1, 3)), Some(&20.0));§Representing Graph Edges
// Represent undirected edges in a graph
let edges = vec![
SortedPair::new(0, 1), // Edge between vertices 0 and 1
SortedPair::new(1, 2), // Edge between vertices 1 and 2
SortedPair::new(2, 0), // Edge between vertices 2 and 0
];
// Check if a specific edge exists (order doesn't matter)
let query_edge = SortedPair::new(2, 1);
assert!(edges.contains(&query_edge));§Ordering
let pair1 = SortedPair::new(1, 5);
let pair2 = SortedPair::new(2, 3);
let pair3 = SortedPair::new(1, 6);
// Pairs are compared lexicographically (first element, then second)
assert!(pair1 < pair2); // (1, 5) < (2, 3)
assert!(pair1 < pair3); // (1, 5) < (1, 6)Implementations§
Source§impl<T: PartialOrd> SortedPair<T>
impl<T: PartialOrd> SortedPair<T>
Sourcepub fn new(element1: T, element2: T) -> Self
pub fn new(element1: T, element2: T) -> Self
Sorts two elements in increasing order into a new pair.
§Arguments
element1- The first elementelement2- The second element
§Returns
A SortedPair where the smaller element comes first.
§Examples
let pair = SortedPair::new(10, 5);
// Elements are automatically sorted
assert_eq!(pair.0, 5);
assert_eq!(pair.1, 10);Trait Implementations§
Source§impl<T: Clone + PartialOrd> Clone for SortedPair<T>
impl<T: Clone + PartialOrd> Clone for SortedPair<T>
Source§fn clone(&self) -> SortedPair<T>
fn clone(&self) -> SortedPair<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug + PartialOrd> Debug for SortedPair<T>
impl<T: Debug + PartialOrd> Debug for SortedPair<T>
Source§impl<T: PartialOrd> Deref for SortedPair<T>
impl<T: PartialOrd> Deref for SortedPair<T>
Source§impl<T: Hash + PartialOrd> Hash for SortedPair<T>
impl<T: Hash + PartialOrd> Hash for SortedPair<T>
Source§impl<T: Ord + PartialOrd> Ord for SortedPair<T>
impl<T: Ord + PartialOrd> Ord for SortedPair<T>
Source§fn cmp(&self, other: &SortedPair<T>) -> Ordering
fn cmp(&self, other: &SortedPair<T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialEq + PartialOrd> PartialEq for SortedPair<T>
impl<T: PartialEq + PartialOrd> PartialEq for SortedPair<T>
Source§impl<T: PartialOrd + PartialOrd> PartialOrd for SortedPair<T>
impl<T: PartialOrd + PartialOrd> PartialOrd for SortedPair<T>
impl<T: Copy + PartialOrd> Copy for SortedPair<T>
impl<T: Eq + PartialOrd> Eq for SortedPair<T>
impl<T: PartialOrd> StructuralPartialEq for SortedPair<T>
Auto Trait Implementations§
impl<T> Freeze for SortedPair<T>where
T: Freeze,
impl<T> RefUnwindSafe for SortedPair<T>where
T: RefUnwindSafe,
impl<T> Send for SortedPair<T>where
T: Send,
impl<T> Sync for SortedPair<T>where
T: Sync,
impl<T> Unpin for SortedPair<T>where
T: Unpin,
impl<T> UnwindSafe for SortedPair<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.