1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/// Provides the constant 0.
#[allow(clippy::declare_interior_mutable_const)]
pub trait Zero {
const ZERO: Self;
}
/// Provides the constant 1.
#[allow(clippy::declare_interior_mutable_const)]
pub trait One {
const ONE: Self;
}
/// Provides the constant 2.
#[allow(clippy::declare_interior_mutable_const)]
pub trait Two {
const TWO: Self;
}
/// Provides the constant -1.
#[allow(clippy::declare_interior_mutable_const)]
pub trait NegativeOne {
const NEGATIVE_ONE: Self;
}
/// Provides the constant 1/2.
#[allow(clippy::declare_interior_mutable_const)]
pub trait OneHalf {
const ONE_HALF: Self;
}
/// The [Iverson bracket](https://en.wikipedia.org/wiki/Iverson_bracket): converts a [`bool`] to 0
/// or 1.
pub trait Iverson {
fn iverson(b: bool) -> Self;
}
/// Converts a [`bool`] to 0 or 1.
///
/// This function is known as the [Iverson bracket](https://en.wikipedia.org/wiki/Iverson_bracket).
///
/// $$
/// f(P) = \[P\] = \\begin{cases}
/// 1 & \text{if} \\quad P, \\\\
/// 0 & \\text{otherwise}.
/// \\end{cases}
/// $$
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::Iverson;
///
/// assert_eq!(u32::iverson(false), 0);
/// assert_eq!(i8::iverson(true), 1);
/// ```
impl<T: One + Sized + Zero> Iverson for T {
#[inline]
fn iverson(b: bool) -> T {
if b {
T::ONE
} else {
T::ZERO
}
}
}