malachite_base/orderings/
mod.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use core::cmp::Ordering::{self, *};
10
11pub(crate) const ORDERINGS: [Ordering; 3] = [Equal, Less, Greater];
12
13/// Converts a string to an [`Ordering`].
14///
15/// If the string does not represent a valid [`Ordering`], `None` is returned.
16///
17/// # Worst-case complexity
18/// Constant time and additional memory.
19///
20/// # Examples
21/// ```
22/// use malachite_base::orderings::ordering_from_str;
23/// use std::cmp::Ordering::*;
24///
25/// assert_eq!(ordering_from_str("Equal"), Some(Equal));
26/// assert_eq!(ordering_from_str("Less"), Some(Less));
27/// assert_eq!(ordering_from_str("Greater"), Some(Greater));
28/// assert_eq!(ordering_from_str("abc"), None);
29/// ```
30#[inline]
31pub fn ordering_from_str(src: &str) -> Option<Ordering> {
32    match src {
33        "Equal" => Some(Equal),
34        "Less" => Some(Less),
35        "Greater" => Some(Greater),
36        _ => None,
37    }
38}
39
40/// Iterators that generate [`Ordering`]s without repetition.
41pub mod exhaustive;
42#[cfg(feature = "random")]
43/// Iterators that generate [`Ordering`]s randomly.
44pub mod random;