[−][src]Enum magnitude::Magnitude
Represent magnitude of values.
Type Magnitude
can be either Finite
or infinite(which itself can be PosInfinite
or NegInfinite
).
This enum is useful when you need to work with algorithms like Dijkstra's Shortest Path or Floyd–Warshall algorithm that require infinite values in order to be written elegantly.
One simple example can be finding the max value in a vector:
use magnitude::Magnitude; fn find_max(vec: &Vec<Magnitude<i32>>) -> Magnitude<i32> { let mut max = Magnitude::NegInfinite; for val in vec { if *val > max { max = *val; } } max } let vec: Vec<Magnitude<i32>> = vec![2.into(), 3.into(), 6.into(), (-10).into()]; assert_eq!(find_max(&vec), 6.into());
You can do all valid comparison(==, !=, >, <, >=, <=) and arithmetic(+, -, *, /, +=, -=, *=, /=) operations on magnitudes.
Invalid operations are listed below which means any other operation is valid.
Invalid operations
- Comparison:
- two
PosInfinite
- two
NegInfinite
- two
- Arithmetic:
- Add:
PosInfinite
+NegInfinite
- Sub:
PosInfinit
-PosInfinit
NegInfinit
-NegInfinit
- Mul:
- zero *
PosInfinite
- zero *
NegInfinite
- zero *
- Div:
- non-zero /
PosInfinite
- non-zero /
NegInfinite
PosInfinite
/ zeroNegInfinite
/ zeroPosInfinite
/PosInfinite
PosInfinite
/NegInfinite
NegInfinite
/PosInfinite
NegInfinite
/NegInfinite
- non-zero /
- Add:
Examples
Convert a vector of numbers into a vector of magnitudes
use magnitude::Magnitude; let _ : Vec<Magnitude<i32>> = vec![1, 2, 3, 4].iter().map(|value| Magnitude::Finite(*value)).collect();
Variants
A finite value
Positive infinity
Negative infinity
Implementations
impl<T> Magnitude<T>
[src]
pub fn is_pos_infinite(&self) -> bool
[src]
Returns true
if magnitude is PosInfinite
, false
otherwise
pub fn is_neg_infinite(&self) -> bool
[src]
Returns true
if magnitude is NegInfinite
, false
otherwise
pub fn is_finite(&self) -> bool
[src]
Returns true
if magnitude is Finite
, false
otherwise
pub fn as_ref(&self) -> Option<&T>
[src]
Returns Some(&T)
if magnitude is Finite
, None
otherwise
pub fn as_ref_mut(&mut self) -> Option<&mut T>
[src]
Returns Some(&T)
if magnitude is Finite
, None
otherwise
Trait Implementations
impl<T: Add<Output = T>> Add<Magnitude<T>> for Magnitude<T>
[src]
type Output = Self
The resulting type after applying the +
operator.
fn add(self, rhs: Self) -> Self::Output
[src]
impl<T: Add<Output = T> + Clone> AddAssign<Magnitude<T>> for Magnitude<T>
[src]
fn add_assign(&mut self, rhs: Self)
[src]
impl<T: Clone> Clone for Magnitude<T>
[src]
impl<T: Copy> Copy for Magnitude<T>
[src]
impl<T: Debug> Debug for Magnitude<T>
[src]
impl<T: Div<Output = T> + PartialOrd + Zero> Div<Magnitude<T>> for Magnitude<T>
[src]
type Output = Self
The resulting type after applying the /
operator.
fn div(self, rhs: Self) -> Self::Output
[src]
impl<T: Div<Output = T> + Clone + PartialOrd + Zero> DivAssign<Magnitude<T>> for Magnitude<T>
[src]
fn div_assign(&mut self, rhs: Self)
[src]
impl<T: PartialEq> Eq for Magnitude<T>
[src]
impl<T> From<T> for Magnitude<T>
[src]
impl<T: Mul<Output = T> + PartialOrd + Zero> Mul<Magnitude<T>> for Magnitude<T>
[src]
type Output = Self
The resulting type after applying the *
operator.
fn mul(self, rhs: Self) -> Self::Output
[src]
impl<T: Mul<Output = T> + Clone + PartialOrd + Zero> MulAssign<Magnitude<T>> for Magnitude<T>
[src]
fn mul_assign(&mut self, rhs: Self)
[src]
impl<T: Neg<Output = T>> Neg for Magnitude<T>
[src]
type Output = Self
The resulting type after applying the -
operator.
fn neg(self) -> Self::Output
[src]
impl<T: Ord> Ord for Magnitude<T>
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<T: PartialEq> PartialEq<Magnitude<T>> for Magnitude<T>
[src]
impl<T: PartialOrd> PartialOrd<Magnitude<T>> for Magnitude<T>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T: Sub<Output = T>> Sub<Magnitude<T>> for Magnitude<T>
[src]
type Output = Self
The resulting type after applying the -
operator.
fn sub(self, rhs: Self) -> Self::Output
[src]
impl<T: Sub<Output = T> + Clone> SubAssign<Magnitude<T>> for Magnitude<T>
[src]
fn sub_assign(&mut self, rhs: Self)
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for Magnitude<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for Magnitude<T> where
T: Send,
T: Send,
impl<T> Sync for Magnitude<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Magnitude<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Magnitude<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,