Struct SortedPair

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

Source

pub fn new(element1: T, element2: T) -> Self

Sorts two elements in increasing order into a new pair.

§Arguments
  • element1 - The first element
  • element2 - 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>

Source§

fn clone(&self) -> SortedPair<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + PartialOrd> Debug for SortedPair<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialOrd> Deref for SortedPair<T>

Source§

type Target = (T, T)

The resulting type after dereferencing.
Source§

fn deref(&self) -> &(T, T)

Dereferences the value.
Source§

impl<T: Hash + PartialOrd> Hash for SortedPair<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord + PartialOrd> Ord for SortedPair<T>

Source§

fn cmp(&self, other: &SortedPair<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + PartialOrd> PartialEq for SortedPair<T>

Source§

fn eq(&self, other: &SortedPair<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + PartialOrd> PartialOrd for SortedPair<T>

Source§

fn partial_cmp(&self, other: &SortedPair<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy + PartialOrd> Copy for SortedPair<T>

Source§

impl<T: Eq + PartialOrd> Eq for SortedPair<T>

Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> Downcast for T
where T: Any,

Source§

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>

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)

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)

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
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,