Skip to main content

malachite_nz/integer/exhaustive/
mod.rs

1// Copyright © 2026 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::integer::Integer;
10use core::iter::{Chain, Once, Rev, once};
11use itertools::{Interleave, Itertools};
12use malachite_base::num::basic::traits::{NegativeOne, One, Zero};
13
14/// Generates all [`Integer`]s in a finite interval, in ascending order.
15///
16/// This `struct` is created by the [`integer_increasing_range`] and
17/// [`integer_increasing_inclusive_range`]; see their documentation for more.
18#[derive(Clone, Debug, Eq, Hash, PartialEq)]
19pub struct IntegerIncreasingRange {
20    a: Integer,
21    b: Integer,
22}
23
24impl Iterator for IntegerIncreasingRange {
25    type Item = Integer;
26
27    fn next(&mut self) -> Option<Integer> {
28        if self.a == self.b {
29            None
30        } else {
31            let result = self.a.clone();
32            self.a += Integer::ONE;
33            Some(result)
34        }
35    }
36}
37
38impl DoubleEndedIterator for IntegerIncreasingRange {
39    fn next_back(&mut self) -> Option<Integer> {
40        if self.a == self.b {
41            None
42        } else {
43            self.b -= Integer::ONE;
44            Some(self.b.clone())
45        }
46    }
47}
48
49/// Generates all [`Integer`]s greater than or equal to some [`Integer`], in ascending order.
50///
51/// This `struct` is created by [`integer_increasing_range_to_infinity`]; see its documentation for
52/// more.
53#[derive(Clone, Debug, Eq, Hash, PartialEq)]
54pub struct IntegerIncreasingRangeToInfinity {
55    a: Integer,
56}
57
58impl Iterator for IntegerIncreasingRangeToInfinity {
59    type Item = Integer;
60
61    fn next(&mut self) -> Option<Integer> {
62        let result = self.a.clone();
63        self.a += Integer::ONE;
64        Some(result)
65    }
66}
67
68/// Generates all [`Integer`]s less than or equal to some [`Integer`], in ascending order.
69///
70/// This `struct` is created by [`integer_decreasing_range_to_negative_infinity`]; see its
71/// documentation for more.
72#[derive(Clone, Debug, Eq, Hash, PartialEq)]
73pub struct IntegerDecreasingRangeToNegativeInfinity {
74    a: Integer,
75}
76
77impl Iterator for IntegerDecreasingRangeToNegativeInfinity {
78    type Item = Integer;
79
80    fn next(&mut self) -> Option<Integer> {
81        let result = self.a.clone();
82        self.a -= Integer::ONE;
83        Some(result)
84    }
85}
86
87/// Generates all [`Integer`]s in a finite interval, in order of increasing absolute value.
88///
89/// This `struct` is created [`exhaustive_integer_range`] and
90/// [`exhaustive_integer_inclusive_range`]; see their documentation for more.
91#[derive(Clone, Debug)]
92pub enum ExhaustiveIntegerRange {
93    NonNegative(IntegerIncreasingRange),
94    NonPositive(Rev<IntegerIncreasingRange>),
95    BothSigns(
96        Chain<Once<Integer>, Interleave<IntegerIncreasingRange, Rev<IntegerIncreasingRange>>>,
97    ),
98}
99
100impl Iterator for ExhaustiveIntegerRange {
101    type Item = Integer;
102
103    fn next(&mut self) -> Option<Integer> {
104        match self {
105            Self::NonNegative(xs) => xs.next(),
106            Self::NonPositive(xs) => xs.next(),
107            Self::BothSigns(xs) => xs.next(),
108        }
109    }
110}
111
112/// Generates all [`Integer`]s greater than or equal to some [`Integer`], in order of increasing
113/// absolute value.
114///
115/// This `struct` is created by [`exhaustive_integer_range_to_infinity`]; see its documentation for
116/// more.
117#[derive(Clone, Debug)]
118pub enum ExhaustiveIntegerRangeToInfinity {
119    NonNegative(IntegerIncreasingRangeToInfinity),
120    BothSigns(
121        Chain<
122            Once<Integer>,
123            Interleave<IntegerIncreasingRangeToInfinity, Rev<IntegerIncreasingRange>>,
124        >,
125    ),
126}
127
128impl Iterator for ExhaustiveIntegerRangeToInfinity {
129    type Item = Integer;
130
131    fn next(&mut self) -> Option<Integer> {
132        match self {
133            Self::NonNegative(xs) => xs.next(),
134            Self::BothSigns(xs) => xs.next(),
135        }
136    }
137}
138
139/// Generates all [`Integer`]s less than or equal to some [`Integer`], in order of increasing
140/// absolute value.
141///
142/// This `struct` is created by [`exhaustive_integer_range_to_negative_infinity`]; see its
143/// documentation for more.
144#[derive(Clone, Debug)]
145pub enum ExhaustiveIntegerRangeToNegativeInfinity {
146    NonPositive(IntegerDecreasingRangeToNegativeInfinity),
147    BothSigns(
148        Chain<
149            Once<Integer>,
150            Interleave<IntegerIncreasingRange, IntegerDecreasingRangeToNegativeInfinity>,
151        >,
152    ),
153}
154
155impl Iterator for ExhaustiveIntegerRangeToNegativeInfinity {
156    type Item = Integer;
157
158    fn next(&mut self) -> Option<Integer> {
159        match self {
160            Self::NonPositive(xs) => xs.next(),
161            Self::BothSigns(xs) => xs.next(),
162        }
163    }
164}
165
166#[doc(hidden)]
167pub type IntegerUpDown =
168    Interleave<IntegerIncreasingRangeToInfinity, IntegerDecreasingRangeToNegativeInfinity>;
169
170/// Generates all [`Integer`]s, in order of increasing absolute value. When two [`Integer`]s have
171/// the same absolute value, the positive one comes first.
172///
173/// # Worst-case complexity per iteration
174/// $T(i) = O(i)$
175///
176/// $M(i) = O(1)$, amortized.
177///
178/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
179///
180/// # Examples
181/// ```
182/// use malachite_base::iterators::prefix_to_string;
183/// use malachite_nz::integer::exhaustive::exhaustive_integers;
184///
185/// assert_eq!(
186///     prefix_to_string(exhaustive_integers(), 10),
187///     "[0, 1, -1, 2, -2, 3, -3, 4, -4, 5, ...]"
188/// )
189/// ```
190#[inline]
191pub fn exhaustive_integers() -> Chain<Once<Integer>, IntegerUpDown> {
192    once(Integer::ZERO).chain(exhaustive_nonzero_integers())
193}
194
195/// Generates all natural [`Integer`]s in ascending order.
196///
197/// The output length is infinite.
198///
199/// # Worst-case complexity per iteration
200/// $T(i) = O(i)$
201///
202/// $M(i) = O(1)$, amortized.
203///
204/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
205/// ```
206/// use malachite_base::iterators::prefix_to_string;
207/// use malachite_nz::integer::exhaustive::exhaustive_natural_integers;
208///
209/// assert_eq!(
210///     prefix_to_string(exhaustive_natural_integers(), 10),
211///     "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...]"
212/// )
213/// ```
214#[inline]
215pub const fn exhaustive_natural_integers() -> IntegerIncreasingRangeToInfinity {
216    integer_increasing_range_to_infinity(Integer::ZERO)
217}
218
219/// Generates all positive [`Integer`]s in ascending order.
220///
221/// The output length is infinite.
222///
223/// # Worst-case complexity per iteration
224/// $T(i) = O(i)$
225///
226/// $M(i) = O(1)$, amortized.
227///
228/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
229///
230/// # Examples
231/// ```
232/// use malachite_base::iterators::prefix_to_string;
233/// use malachite_nz::integer::exhaustive::exhaustive_positive_integers;
234///
235/// assert_eq!(
236///     prefix_to_string(exhaustive_positive_integers(), 10),
237///     "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"
238/// )
239/// ```
240#[inline]
241pub const fn exhaustive_positive_integers() -> IntegerIncreasingRangeToInfinity {
242    integer_increasing_range_to_infinity(Integer::ONE)
243}
244
245/// Generates all negative [`Integer`]s in descending order.
246///
247/// The output length is infinite.
248///
249/// # Worst-case complexity per iteration
250/// $T(i) = O(i)$
251///
252/// $M(i) = O(1)$, amortized.
253///
254/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
255///
256/// # Examples
257/// ```
258/// use malachite_base::iterators::prefix_to_string;
259/// use malachite_nz::integer::exhaustive::exhaustive_negative_integers;
260///
261/// assert_eq!(
262///     prefix_to_string(exhaustive_negative_integers(), 10),
263///     "[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, ...]"
264/// )
265/// ```
266#[inline]
267pub const fn exhaustive_negative_integers() -> IntegerDecreasingRangeToNegativeInfinity {
268    integer_decreasing_range_to_negative_infinity(Integer::NEGATIVE_ONE)
269}
270
271/// Generates all nonzero [`Integer`]s, in order of increasing absolute value. When two [`Integer`]s
272/// have the same absolute value, the positive one comes first.
273///
274/// The output length is infinite.
275///
276/// # Worst-case complexity per iteration
277/// $T(i) = O(i)$
278///
279/// $M(i) = O(1)$, amortized.
280///
281/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
282///
283/// # Examples
284/// ```
285/// use malachite_base::iterators::prefix_to_string;
286/// use malachite_nz::integer::exhaustive::exhaustive_nonzero_integers;
287///
288/// assert_eq!(
289///     prefix_to_string(exhaustive_nonzero_integers(), 10),
290///     "[1, -1, 2, -2, 3, -3, 4, -4, 5, -5, ...]"
291/// )
292/// ```
293#[inline]
294pub fn exhaustive_nonzero_integers() -> IntegerUpDown {
295    exhaustive_positive_integers().interleave(exhaustive_negative_integers())
296}
297
298/// Generates all [`Integer`]s in the half-open interval $[a, b)$, in ascending order.
299///
300/// $a$ must be less than or equal to $b$. If $a$ and $b$ are equal, the range is empty. To generate
301/// all [`Integer`]s in an infinite interval in ascending or descending order, use
302/// [`integer_increasing_range_to_infinity`] or [`integer_decreasing_range_to_negative_infinity`].
303///
304/// The output is $(k)_{k=a}^{b-1}$.
305///
306/// The output length is $b - a$.
307///
308/// # Worst-case complexity per iteration
309/// $T(i) = O(i)$
310///
311/// $M(i) = O(i)$
312///
313/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
314///
315/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
316/// we exclude the cases where the the previously-generated value is positive and its
317/// least-significant limb is `Limb::MAX`, the worst case space and time complexities are constant.
318///
319/// # Panics
320/// Panics if $a > b$.
321///
322/// # Examples
323/// ```
324/// use itertools::Itertools;
325/// use malachite_base::strings::ToDebugString;
326/// use malachite_nz::integer::exhaustive::integer_increasing_range;
327/// use malachite_nz::integer::Integer;
328///
329/// assert_eq!(
330///     integer_increasing_range(Integer::from(-4), Integer::from(4))
331///         .collect_vec()
332///         .to_debug_string(),
333///     "[-4, -3, -2, -1, 0, 1, 2, 3]"
334/// )
335/// ```
336#[inline]
337pub fn integer_increasing_range(a: Integer, b: Integer) -> IntegerIncreasingRange {
338    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
339    IntegerIncreasingRange { a, b }
340}
341
342/// Generates all [`Integer`]s in the closed interval $[a, b]$, in ascending order.
343///
344/// $a$ must be less than or equal to $b$. If $a$ and $b$ are equal, the range contains a single
345/// element. To generate all [`Integer`]s in an infinite interval in ascending or descending order,
346/// use [`integer_increasing_range_to_infinity`] or
347/// [`integer_decreasing_range_to_negative_infinity`].
348///
349/// The output is $(k)_{k=a}^{b}$.
350///
351/// The output length is $b - a + 1$.
352///
353/// # Worst-case complexity per iteration
354/// $T(i) = O(i)$
355///
356/// $M(i) = O(i)$
357///
358/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
359///
360/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
361/// we exclude the cases where the the previously-generated value is positive and its
362/// least-significant limb is `Limb::MAX`, the worst case space and time complexities are constant.
363///
364/// # Panics
365/// Panics if $a > b$.
366///
367/// # Examples
368/// ```
369/// use itertools::Itertools;
370/// use malachite_base::strings::ToDebugString;
371/// use malachite_nz::integer::exhaustive::integer_increasing_inclusive_range;
372/// use malachite_nz::integer::Integer;
373///
374/// assert_eq!(
375///     integer_increasing_inclusive_range(Integer::from(-4), Integer::from(4))
376///         .collect_vec()
377///         .to_debug_string(),
378///     "[-4, -3, -2, -1, 0, 1, 2, 3, 4]"
379/// )
380/// ```
381#[inline]
382pub fn integer_increasing_inclusive_range(a: Integer, b: Integer) -> IntegerIncreasingRange {
383    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
384    IntegerIncreasingRange {
385        a,
386        b: b + Integer::ONE,
387    }
388}
389
390/// Generates all [`Integer`]s greater than or equal to some number $a$, in ascending order.
391///
392/// The output is $(k)_{k=a}^{\infty}$.
393///
394/// The output length is infinite.
395///
396/// # Worst-case complexity per iteration
397/// $T(i) = O(i)$
398///
399/// $M(i) = O(i)$
400///
401/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
402///
403/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
404/// we exclude the cases where the the previously-generated value is positive and its
405/// least-significant limb is `Limb::MAX`, the worst case space and time complexities are constant.
406///
407/// # Examples
408/// ```
409/// use malachite_base::iterators::prefix_to_string;
410/// use malachite_nz::integer::exhaustive::integer_increasing_range_to_infinity;
411/// use malachite_nz::integer::Integer;
412///
413/// assert_eq!(
414///     prefix_to_string(integer_increasing_range_to_infinity(Integer::from(-4)), 10),
415///     "[-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...]"
416/// )
417/// ```
418#[inline]
419pub const fn integer_increasing_range_to_infinity(a: Integer) -> IntegerIncreasingRangeToInfinity {
420    IntegerIncreasingRangeToInfinity { a }
421}
422
423/// Generates all [`Integer`]s less than or equal to some number $a$, in descending order.
424///
425/// The output is $(-k)_{k=-a}^{\infty}$.
426///
427/// The output length is infinite.
428///
429/// # Worst-case complexity per iteration
430/// $T(i) = O(i)$
431///
432/// $M(i) = O(i)$
433///
434/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
435///
436/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
437/// we exclude the cases where the the previously-generated value is negative and the
438/// least-significant limb of its absolute value is `Limb::MAX`, the worst case space and time
439/// complexities are constant.
440///
441/// # Examples
442/// ```
443/// use malachite_base::iterators::prefix_to_string;
444/// use malachite_nz::integer::exhaustive::integer_decreasing_range_to_negative_infinity;
445/// use malachite_nz::integer::Integer;
446///
447/// assert_eq!(
448///     prefix_to_string(
449///         integer_decreasing_range_to_negative_infinity(Integer::from(4)),
450///         10
451///     ),
452///     "[4, 3, 2, 1, 0, -1, -2, -3, -4, -5, ...]"
453/// )
454/// ```
455#[inline]
456pub const fn integer_decreasing_range_to_negative_infinity(
457    a: Integer,
458) -> IntegerDecreasingRangeToNegativeInfinity {
459    IntegerDecreasingRangeToNegativeInfinity { a }
460}
461
462/// Generates all [`Integer`]s in the half-open interval $[a, b)$, in order of increasing absolute
463/// value.
464///
465/// When two [`Integer`]s have the same absolute value, the positive one comes first. $a$ must be
466/// less than or equal to $b$. If $a$ and $b$ are equal, the range is empty.
467///
468/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
469/// \operatorname{sgn}(-x_j))$ whenever $i, j \\in [0, b - a)$ and $i < j$.
470///
471/// The output length is $b - a$.
472///
473/// # Worst-case complexity per iteration
474/// $T(i) = O(i)$
475///
476/// $M(i) = O(i)$
477///
478/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
479///
480/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
481/// we exclude the cases where the the least-significant limb of the absolute value of the
482/// previously-generated value is `Limb::MAX`, the worst case space and time complexities are
483/// constant.
484///
485/// # Panics
486/// Panics if $a > b$.
487///
488/// # Examples
489/// ```
490/// use itertools::Itertools;
491/// use malachite_base::strings::ToDebugString;
492/// use malachite_nz::integer::exhaustive::exhaustive_integer_range;
493/// use malachite_nz::integer::Integer;
494///
495/// assert_eq!(
496///     exhaustive_integer_range(Integer::from(-4), Integer::from(4))
497///         .collect_vec()
498///         .to_debug_string(),
499///     "[0, 1, -1, 2, -2, 3, -3, -4]"
500/// )
501/// ```
502pub fn exhaustive_integer_range(a: Integer, b: Integer) -> ExhaustiveIntegerRange {
503    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
504    if a >= 0 {
505        ExhaustiveIntegerRange::NonNegative(integer_increasing_range(a, b))
506    } else if b <= 0 {
507        ExhaustiveIntegerRange::NonPositive(integer_increasing_range(a, b).rev())
508    } else {
509        ExhaustiveIntegerRange::BothSigns(
510            once(Integer::ZERO).chain(
511                integer_increasing_range(Integer::ONE, b)
512                    .interleave(integer_increasing_range(a, Integer::ZERO).rev()),
513            ),
514        )
515    }
516}
517
518/// Generates all [`Integer`]s in the closed interval $[a, b]$, in order of increasing absolute
519/// value.
520///
521/// When two [`Integer`]s have the same absolute value, the positive one comes first. $a$ must be
522/// less than or equal to $b$. If $a$ and $b$ are equal, the range contains a single element.
523///
524/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
525/// \operatorname{sgn}(-x_j))$ whenever $i, j \\in [0, b - a]$ and $i < j$.
526///
527/// The output length is $b - a + 1$.
528///
529/// # Worst-case complexity per iteration
530/// $T(i) = O(i)$
531///
532/// $M(i) = O(i)$
533///
534/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
535///
536/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
537/// we exclude the cases where the the least-significant limb of the absolute value of the
538/// previously-generated value is `Limb::MAX`, the worst case space and time complexities are
539/// constant.
540///
541/// # Panics
542/// Panics if $a > b$.
543///
544/// # Examples
545/// ```
546/// use itertools::Itertools;
547/// use malachite_base::strings::ToDebugString;
548/// use malachite_nz::integer::exhaustive::exhaustive_integer_inclusive_range;
549/// use malachite_nz::integer::Integer;
550///
551/// assert_eq!(
552///     exhaustive_integer_inclusive_range(Integer::from(-4), Integer::from(4))
553///         .collect_vec()
554///         .to_debug_string(),
555///     "[0, 1, -1, 2, -2, 3, -3, 4, -4]"
556/// )
557/// ```
558pub fn exhaustive_integer_inclusive_range(a: Integer, b: Integer) -> ExhaustiveIntegerRange {
559    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
560    if a >= 0 {
561        ExhaustiveIntegerRange::NonNegative(integer_increasing_inclusive_range(a, b))
562    } else if b <= 0 {
563        ExhaustiveIntegerRange::NonPositive(integer_increasing_inclusive_range(a, b).rev())
564    } else {
565        ExhaustiveIntegerRange::BothSigns(
566            once(Integer::ZERO).chain(
567                integer_increasing_inclusive_range(Integer::ONE, b)
568                    .interleave(integer_increasing_inclusive_range(a, Integer::NEGATIVE_ONE).rev()),
569            ),
570        )
571    }
572}
573
574/// Generates all [`Integer`]s greater than or equal to some number $a$, in order of increasing
575/// absolute value.
576///
577/// When two [`Integer`]s have the same absolute value, the positive one comes first.
578///
579/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
580/// \operatorname{sgn}(-x_j))$ whenever $i < j$.
581///
582/// The output length is infinite.
583///
584/// # Worst-case complexity per iteration
585/// $T(i) = O(i)$
586///
587/// $M(i) = O(i)$
588///
589/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
590///
591/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
592/// we exclude the cases where the the previously-generated value is positive and its
593/// least-significant limb is `Limb::MAX`, the worst case space and time complexities are constant.
594///
595/// # Examples
596/// ```
597/// use malachite_base::iterators::prefix_to_string;
598/// use malachite_nz::integer::exhaustive::exhaustive_integer_range_to_infinity;
599/// use malachite_nz::integer::Integer;
600///
601/// assert_eq!(
602///     prefix_to_string(exhaustive_integer_range_to_infinity(Integer::from(-2)), 10),
603///     "[0, 1, -1, 2, -2, 3, 4, 5, 6, 7, ...]"
604/// )
605/// ```
606#[inline]
607pub fn exhaustive_integer_range_to_infinity(a: Integer) -> ExhaustiveIntegerRangeToInfinity {
608    if a >= 0 {
609        ExhaustiveIntegerRangeToInfinity::NonNegative(integer_increasing_range_to_infinity(a))
610    } else {
611        ExhaustiveIntegerRangeToInfinity::BothSigns(
612            once(Integer::ZERO).chain(
613                integer_increasing_range_to_infinity(Integer::ONE)
614                    .interleave(integer_increasing_range(a, Integer::ZERO).rev()),
615            ),
616        )
617    }
618}
619
620/// Generates all [`Integer`]s less than or equal to some number $a$, in order of increasing
621/// absolute value.
622///
623/// When two [`Integer`]s have the same absolute value, the positive one comes first.
624///
625/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
626/// \operatorname{sgn}(-x_j))$ whenever $i < j$.
627///
628/// The output length is infinite.
629///
630/// # Worst-case complexity per iteration
631/// $T(i) = O(i)$
632///
633/// $M(i) = O(i)$
634///
635/// where $T$ is time, $M$ is additional memory, and $i$ is the iteration number.
636///
637/// Although the time and space complexities are worst-case linear, the worst case is very rare. If
638/// we exclude the cases where the the previously-generated value is positive and its
639/// least-significant limb is `Limb::MAX`, the worst case space and time complexities are constant.
640///
641/// # Examples
642/// ```
643/// use malachite_base::iterators::prefix_to_string;
644/// use malachite_nz::integer::exhaustive::exhaustive_integer_range_to_negative_infinity;
645/// use malachite_nz::integer::Integer;
646///
647/// assert_eq!(
648///     prefix_to_string(
649///         exhaustive_integer_range_to_negative_infinity(Integer::from(2)),
650///         10
651///     ),
652///     "[0, 1, -1, 2, -2, -3, -4, -5, -6, -7, ...]"
653/// )
654/// ```
655#[inline]
656pub fn exhaustive_integer_range_to_negative_infinity(
657    a: Integer,
658) -> ExhaustiveIntegerRangeToNegativeInfinity {
659    if a <= 0 {
660        ExhaustiveIntegerRangeToNegativeInfinity::NonPositive(
661            integer_decreasing_range_to_negative_infinity(a),
662        )
663    } else {
664        ExhaustiveIntegerRangeToNegativeInfinity::BothSigns(once(Integer::ZERO).chain(
665            integer_increasing_range(Integer::ONE, a + Integer::ONE).interleave(
666                integer_decreasing_range_to_negative_infinity(Integer::NEGATIVE_ONE),
667            ),
668        ))
669    }
670}