[][src]Enum magnitude::Magnitude

pub enum Magnitude<T> {
    Finite(T),
    PosInfinite,
    NegInfinite,
}

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
  • Arithmetic:
    • Add:
      • PosInfinite + NegInfinite
    • Sub:
      • PosInfinite - PosInfinite
      • NegInfinite - NegInfinite
    • Mul:
      • zero * PosInfinite
      • zero * NegInfinite
    • Div:
      • non-zero / PosInfinite
      • non-zero / NegInfinite
      • PosInfinite / zero
      • NegInfinite / zero
      • PosInfinite / PosInfinite
      • PosInfinite / NegInfinite
      • NegInfinite / PosInfinite
      • NegInfinite / NegInfinite

Relationship of Magnitude with f64 and f32 infinities

Magnitude as of version 0.2.0 treat f64::INFINITY, f64::NEG_INFINITY, f32::INFINITY, f32::NEG_INFINITY as infinites:

use magnitude::Magnitude;

let pos_inf: Magnitude<f64> = f64::INFINITY.into();
let neg_inf: Magnitude<f64> = f64::NEG_INFINITY.into();
assert!(pos_inf.is_pos_infinite());
assert!(neg_inf.is_neg_infinite());

let pos_inf: Magnitude<f32> = f32::INFINITY.into();
let neg_inf: Magnitude<f32> = f32::NEG_INFINITY.into();
assert!(pos_inf.is_pos_infinite());
assert!(neg_inf.is_neg_infinite());

Examples

Convert a vector of numbers into a vector of magnitudes

use magnitude::Magnitude;

let magnitude_vec = Magnitude::from_vec(&vec![1,2,3]);

assert_eq!(magnitude_vec[0], 1.into());
assert_eq!(magnitude_vec[1], 2.into());
assert_eq!(magnitude_vec[2], 3.into());

Variants

Finite(T)

A finite value

PosInfinite

Positive infinity

NegInfinite

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(&mut T) if magnitude is Finite, None otherwise

pub fn unwrap(self) -> T[src]

Consumes the Magnitude and returns the value inside Finite. Panics if Magnitude contains an infinity

Example

use magnitude::Magnitude;
let one: Magnitude<i32> = 1.into();

assert_eq!(one.unwrap(), 1);

impl<T: Any + Copy> Magnitude<T>[src]

pub fn from_vec(vec: &Vec<T>) -> Vec<Magnitude<T>>[src]

Converts a vector of value into a vector of Magnitude

Example

use magnitude::Magnitude;

let magnitude_vec = Magnitude::from_vec(&vec![1,2,3]);

assert_eq!(magnitude_vec[0], 1.into());
assert_eq!(magnitude_vec[1], 2.into());
assert_eq!(magnitude_vec[2], 3.into());

Trait Implementations

impl<T: Add<Output = T>> Add<Magnitude<T>> for Magnitude<T>[src]

type Output = Self

The resulting type after applying the + operator.

impl<T: Add<Output = T> + Copy> AddAssign<Magnitude<T>> for Magnitude<T>[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.

impl<T: Div<Output = T> + Copy + PartialOrd + Zero> DivAssign<Magnitude<T>> for Magnitude<T>[src]

impl<T: PartialEq> Eq for Magnitude<T>[src]

impl<T: Any> 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.

impl<T: Mul<Output = T> + Copy + PartialOrd + Zero> MulAssign<Magnitude<T>> for Magnitude<T>[src]

impl<T: Neg<Output = T>> Neg for Magnitude<T>[src]

type Output = Self

The resulting type after applying the - operator.

impl<T: Ord> Ord for Magnitude<T>[src]

impl<T: PartialEq> PartialEq<Magnitude<T>> for Magnitude<T>[src]

impl<T: PartialOrd> PartialOrd<Magnitude<T>> for Magnitude<T>[src]

impl<T: Sub<Output = T>> Sub<Magnitude<T>> for Magnitude<T>[src]

type Output = Self

The resulting type after applying the - operator.

impl<T: Sub<Output = T> + Copy> SubAssign<Magnitude<T>> for Magnitude<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Magnitude<T> where
    T: RefUnwindSafe

impl<T> Send for Magnitude<T> where
    T: Send

impl<T> Sync for Magnitude<T> where
    T: Sync

impl<T> Unpin for Magnitude<T> where
    T: Unpin

impl<T> UnwindSafe for Magnitude<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[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]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.