[−][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:
PosInfinite
-PosInfinite
NegInfinite
-NegInfinite
- 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:
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
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(&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.
pub fn add(self, rhs: Self) -> Self::Output
[src]
impl<T: Add<Output = T> + Copy> AddAssign<Magnitude<T>> for Magnitude<T>
[src]
pub 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.
pub fn div(self, rhs: Self) -> Self::Output
[src]
impl<T: Div<Output = T> + Copy + PartialOrd + Zero> DivAssign<Magnitude<T>> for Magnitude<T>
[src]
pub fn div_assign(&mut self, rhs: Self)
[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.
pub fn mul(self, rhs: Self) -> Self::Output
[src]
impl<T: Mul<Output = T> + Copy + PartialOrd + Zero> MulAssign<Magnitude<T>> for Magnitude<T>
[src]
pub 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.
pub fn neg(self) -> Self::Output
[src]
impl<T: Ord> Ord for Magnitude<T>
[src]
pub fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<T: PartialEq> PartialEq<Magnitude<T>> for Magnitude<T>
[src]
pub fn eq(&self, other: &Self) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T: PartialOrd> PartialOrd<Magnitude<T>> for Magnitude<T>
[src]
pub fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub 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.
pub fn sub(self, rhs: Self) -> Self::Output
[src]
impl<T: Sub<Output = T> + Copy> SubAssign<Magnitude<T>> for Magnitude<T>
[src]
pub 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>,