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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
// Copyright © 2024 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use core::cmp::Ordering::{self, *};
/// An iterator that generates the [`Ordering`]s of adjacent elements of a given iterator.
///
/// This `struct` is created by [`delta_directions`]; see its documentation for more.
#[derive(Clone, Debug)]
pub struct DeltaDirections<I: Iterator>
where
I::Item: Ord,
{
previous: Option<I::Item>,
xs: I,
}
impl<I: Iterator> Iterator for DeltaDirections<I>
where
I::Item: Ord,
{
type Item = Ordering;
fn next(&mut self) -> Option<Ordering> {
if self.previous.is_none() {
if let Some(x) = self.xs.next() {
self.previous = Some(x);
} else {
return None;
}
}
self.xs.next().and_then(|x| {
let result = Some(x.cmp(self.previous.as_ref().unwrap()));
self.previous = Some(x);
result
})
}
}
/// Returns an iterator that generates the [`Ordering`]s of adjacent pairs of elements of a given
/// iterator.
///
/// To put it another way (at least for types where subtraction is defined), the returned iterator
/// produces the signs of the finite differences of the input iterator.
///
/// $f((x_k)_{k=0}^N) = (\\operatorname{cmp}(x_k, x\_{k-1}))\_{k=1}^N$, where $N$ may be $\infty$.
///
/// The output length is infinite if `xs` is infinite, or $\max(n - 1, 0)$ otherwise, where $n$ is
/// `xs.count()`.
///
/// # Examples
/// ```
/// use itertools::Itertools;
/// use malachite_base::iterators::comparison::delta_directions;
/// use std::cmp::Ordering::*;
///
/// assert_eq!(
/// delta_directions([3, 1, 4, 1, 5, 9].into_iter()).collect_vec(),
/// &[Less, Greater, Less, Greater, Greater]
/// )
/// ```
#[inline]
pub const fn delta_directions<I: Iterator>(xs: I) -> DeltaDirections<I>
where
I::Item: Ord,
{
DeltaDirections { previous: None, xs }
}
/// Determines whether each element of an iterator is greater than the preceding one.
///
/// This function will hang if given an infinite strictly ascending iterator.
///
/// $$
/// f((x_k)\_{k=0}^N) = \\bigwedge\_{k=1}^N{x\_k > x\_{k-1}},
/// $$
/// where $N$ may be $\infty$.
///
/// # Examples
/// ```
/// use malachite_base::iterators::comparison::is_strictly_ascending;
///
/// assert_eq!(is_strictly_ascending([1, 2, 3, 4].into_iter()), true);
/// assert_eq!(is_strictly_ascending([1, 2, 2, 4].into_iter()), false);
/// ```
#[inline]
pub fn is_strictly_ascending<I: Iterator>(xs: I) -> bool
where
I::Item: Ord,
{
delta_directions(xs).all(|x| x == Greater)
}
/// Determines whether each element of an iterator is less than the preceding one.
///
/// This function will hang if given an infinite strictly descending iterator.
///
/// $$
/// f((x_k)\_{k=0}^N) = \\bigwedge\_{k=1}^N{x\_k < x\_{k-1}},
/// $$
/// where $N$ may be $\infty$.
///
/// # Examples
/// ```
/// use malachite_base::iterators::comparison::is_strictly_descending;
///
/// assert_eq!(is_strictly_descending([4, 3, 2, 1].into_iter()), true);
/// assert_eq!(is_strictly_descending([4, 2, 2, 1].into_iter()), false);
/// ```
#[inline]
pub fn is_strictly_descending<I: Iterator>(xs: I) -> bool
where
I::Item: Ord,
{
delta_directions(xs).all(|x| x == Less)
}
/// Determines whether each element of an iterator is greater than or equal to the preceding one.
///
/// This function will hang if given an infinite weakly ascending iterator.
///
/// $$
/// f((x_k)\_{k=0}^N) = \\bigwedge\_{k=1}^N{x\_k \geq x\_{k-1}},
/// $$
/// where $N$ may be $\infty$.
///
/// # Examples
/// ```
/// use malachite_base::iterators::comparison::is_weakly_ascending;
///
/// assert_eq!(is_weakly_ascending([1, 2, 3, 4].into_iter()), true);
/// assert_eq!(is_weakly_ascending([1, 2, 2, 4].into_iter()), true);
/// assert_eq!(is_weakly_ascending([1, 3, 2, 4].into_iter()), false);
/// ```
#[inline]
pub fn is_weakly_ascending<I: Iterator>(xs: I) -> bool
where
I::Item: Ord,
{
delta_directions(xs).all(|x| x != Less)
}
/// Determines whether each element of an iterator is less than or equal to the preceding one.
///
/// This function will hang if given an infinite weakly descending iterator.
///
/// $$
/// f((x_k)\_{k=0}^N) = \\bigwedge\_{k=1}^N{x\_k \leq x\_{k-1}},
/// $$
/// where $N$ may be $\infty$.
///
/// # Examples
/// ```
/// use malachite_base::iterators::comparison::is_weakly_descending;
///
/// assert_eq!(is_weakly_descending([4, 3, 2, 1].into_iter()), true);
/// assert_eq!(is_weakly_descending([4, 2, 2, 1].into_iter()), true);
/// assert_eq!(is_weakly_descending([4, 2, 3, 1].into_iter()), false);
/// ```
#[inline]
pub fn is_weakly_descending<I: Iterator>(xs: I) -> bool
where
I::Item: Ord,
{
delta_directions(xs).all(|x| x != Greater)
}
/// Determines whether the sequence strictly zigzags.
///
/// A strictly zigzagging sequence is one where every odd-indexed element is greater than its
/// even-indexed neighbors, or one where every odd-indexed element is less than its even-indexed
/// neighbors.
///
/// This function will hang if given an infinite strictly zigzagging iterator.
///
/// # Examples
/// ```
/// use malachite_base::iterators::comparison::is_strictly_zigzagging;
///
/// assert_eq!(is_strictly_zigzagging([1, 2, 3, 4].into_iter()), false);
/// assert_eq!(is_strictly_zigzagging([4, 3, 2, 1].into_iter()), false);
/// assert_eq!(is_strictly_zigzagging([1, 3, 2, 4].into_iter()), true);
/// assert_eq!(is_strictly_zigzagging([1, 2, 2, 4].into_iter()), false);
/// ```
pub fn is_strictly_zigzagging<I: Iterator>(xs: I) -> bool
where
I::Item: Ord,
{
let mut previous = None;
for direction in delta_directions(xs) {
if direction == Equal {
return false;
}
if let Some(previous) = previous {
if direction == previous {
return false;
}
}
previous = Some(direction);
}
true
}
/// Determines whether the sequence weakly zigzags.
///
/// A weakly zigzagging sequence is one where every odd-indexed element is greater than or equal to
/// its even-indexed neighbors, or one where every odd-indexed element is less than or equal to its
/// even-indexed neighbors.
///
/// This function will hang if given an infinite strictly zigzagging iterator.
///
/// # Examples
/// ```
/// use malachite_base::iterators::comparison::is_weakly_zigzagging;
///
/// assert_eq!(is_weakly_zigzagging([1, 2, 3, 4].into_iter()), false);
/// assert_eq!(is_weakly_zigzagging([4, 3, 2, 1].into_iter()), false);
/// assert_eq!(is_weakly_zigzagging([1, 3, 2, 4].into_iter()), true);
/// assert_eq!(is_weakly_zigzagging([1, 2, 2, 4].into_iter()), true);
/// ```
pub fn is_weakly_zigzagging<I: Iterator>(xs: I) -> bool
where
I::Item: Ord,
{
let mut previous = None;
for direction in delta_directions(xs) {
if let Some(ref mut previous) = &mut previous {
if direction == *previous {
return false;
}
*previous = previous.reverse();
} else if direction != Equal {
previous = Some(direction);
}
}
true
}