malachite_base/rational_sequences/cmp.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 crate::rational_sequences::RationalSequence;
10use core::cmp::Ordering::{self, *};
11
12impl<T: Eq + Ord> PartialOrd for RationalSequence<T> {
13 /// Compares a [`RationalSequence`] to another [`RationalSequence`].
14 ///
15 /// See [here](RationalSequence::cmp) for more information.
16 #[inline]
17 fn partial_cmp(&self, other: &RationalSequence<T>) -> Option<Ordering> {
18 Some(self.cmp(other))
19 }
20}
21
22impl<T: Eq + Ord> Ord for RationalSequence<T> {
23 /// Compares a [`RationalSequence`] to another [`RationalSequence`].
24 ///
25 /// The comparison is made lexicographically with respect to the element type's ordering.
26 ///
27 /// # Worst-case complexity
28 /// $T(n) = O(n)$
29 ///
30 /// $M(n) = O(1)$
31 ///
32 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.component_len()`.
33 ///
34 /// # Examples
35 /// ```
36 /// use malachite_base::rational_sequences::RationalSequence;
37 ///
38 /// assert!(
39 /// RationalSequence::from_slice(&[1, 2]) < RationalSequence::from_slices(&[1, 2], &[1])
40 /// );
41 /// assert!(
42 /// RationalSequence::from_slice(&[1, 2, 3])
43 /// < RationalSequence::from_slices(&[1, 2], &[3, 4])
44 /// );
45 /// ```
46 fn cmp(&self, other: &RationalSequence<T>) -> Ordering {
47 if self == other {
48 Equal
49 } else {
50 Iterator::cmp(self.iter(), other.iter())
51 }
52 }
53}