malachite_base/sets/
exhaustive.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::vecs::exhaustive::{
10    ExhaustiveOrderedUniqueCollections, LexFixedLengthOrderedUniqueCollections,
11    LexOrderedUniqueCollections, ShortlexOrderedUniqueCollections,
12};
13#[cfg(not(feature = "test_build"))]
14use alloc::collections::BTreeSet;
15use core::hash::Hash;
16#[cfg(not(feature = "test_build"))]
17use hashbrown::HashSet;
18#[cfg(feature = "test_build")]
19use std::collections::{BTreeSet, HashSet};
20
21/// Generates [`HashSet`]s of a given size with elements from a single iterator.
22///
23/// The [`HashSet`]s are ordered lexicographically with respect to the order of the element
24/// iterator.
25///
26/// The source iterator should not repeat any elements, but this is not enforced.
27///
28/// If $k$ is 0, the output length is 1.
29///
30/// If $k$ is nonzero and the input iterator is infinite, the output length is also infinite.
31///
32/// If $k$ is nonzero and the input iterator length is $n$, the output length is $\binom{n}{k}$.
33///
34/// If $k$ is 0, the output consists of one empty [`HashSet`].
35///
36/// If `xs` is empty, the output is also empty, unless $k$ is 0.
37///
38/// # Examples
39/// ```
40/// use itertools::Itertools;
41/// use malachite_base::sets::exhaustive::lex_hash_sets_fixed_length;
42/// use maplit::hashset;
43///
44/// let xss = lex_hash_sets_fixed_length(4, 1..=6).collect_vec();
45/// assert_eq!(
46///     xss,
47///     &[
48///         hashset! {1, 2, 3, 4},
49///         hashset! {1, 2, 3, 5},
50///         hashset! {1, 2, 3, 6},
51///         hashset! {1, 2, 4, 5},
52///         hashset! {1, 2, 4, 6},
53///         hashset! {1, 2, 5, 6},
54///         hashset! {1, 3, 4, 5},
55///         hashset! {1, 3, 4, 6},
56///         hashset! {1, 3, 5, 6},
57///         hashset! {1, 4, 5, 6},
58///         hashset! {2, 3, 4, 5},
59///         hashset! {2, 3, 4, 6},
60///         hashset! {2, 3, 5, 6},
61///         hashset! {2, 4, 5, 6},
62///         hashset! {3, 4, 5, 6}
63///     ]
64/// );
65/// ```
66#[inline]
67pub fn lex_hash_sets_fixed_length<I: Iterator>(
68    k: u64,
69    xs: I,
70) -> LexFixedLengthOrderedUniqueCollections<I, HashSet<I::Item>>
71where
72    I::Item: Clone + Eq + Hash,
73{
74    LexFixedLengthOrderedUniqueCollections::new(k, xs)
75}
76
77/// Generates [`HashSet`]s with elements from a single iterator.
78///
79/// The [`HashSet`]s are generated in order of increasing length, and within each length they are
80/// ordered lexicographically with respect to the order of the element iterator.
81///
82/// The source iterator should not repeat any elements, but this is not enforced.
83///
84/// The iterator should be finite; if it is infinite, [`HashSet`]s of length 2 and above will never
85/// be generated.
86///
87/// If the input iterator is infinite, the output length is also infinite.
88///
89/// If the input iterator length is $n$, the output length is $2^n$.
90///
91/// If `xs` is empty, the output consists of a single empty [`HashSet`].
92///
93/// # Examples
94/// ```
95/// use itertools::Itertools;
96/// use malachite_base::sets::exhaustive::shortlex_hash_sets;
97/// use maplit::hashset;
98///
99/// let xss = shortlex_hash_sets(1..=4).collect_vec();
100/// assert_eq!(
101///     xss,
102///     &[
103///         hashset! {},
104///         hashset! {1},
105///         hashset! {2},
106///         hashset! {3},
107///         hashset! {4},
108///         hashset! {1, 2},
109///         hashset! {1, 3},
110///         hashset! {1, 4},
111///         hashset! {2, 3},
112///         hashset! {2, 4},
113///         hashset! {3, 4},
114///         hashset! {1, 2, 3},
115///         hashset! {1, 2, 4},
116///         hashset! {1, 3, 4},
117///         hashset! {2, 3, 4},
118///         hashset! {1, 2, 3, 4}
119///     ]
120/// );
121/// ```
122#[inline]
123pub fn shortlex_hash_sets<I: Clone + Iterator>(
124    xs: I,
125) -> ShortlexOrderedUniqueCollections<I, HashSet<I::Item>>
126where
127    I::Item: Clone + Eq + Hash,
128{
129    shortlex_hash_sets_length_inclusive_range(0, u64::MAX, xs)
130}
131
132/// Generates [`HashSet`]s with a mininum length, with elements from a single iterator.
133///
134/// The [`HashSet`]s are generated in order of increasing length, and within each length they are
135/// ordered lexicographically with respect to the order of the element iterator.
136///
137/// The source iterator should not repeat any elements, but this is not enforced.
138///
139/// The iterator should be finite; if it is infinite, [`HashSet`]s of length `\max(2, \ell + 1)` and
140/// above will never be generated.
141///
142/// If the input iterator is infinite, the output length is also infinite.
143///
144/// If the input iterator length is $n$ and the `min_length` is $\ell$, the output length is
145/// $$
146/// \sum_{i=\ell}^n \binom{n}{i}.
147/// $$
148///
149/// # Examples
150/// ```
151/// use itertools::Itertools;
152/// use malachite_base::sets::exhaustive::shortlex_hash_sets_min_length;
153/// use maplit::hashset;
154///
155/// let xss = shortlex_hash_sets_min_length(2, 1..=4).collect_vec();
156/// assert_eq!(
157///     xss,
158///     &[
159///         hashset! {1, 2},
160///         hashset! {1, 3},
161///         hashset! {1, 4},
162///         hashset! {2, 3},
163///         hashset! {2, 4},
164///         hashset! {3, 4},
165///         hashset! {1, 2, 3},
166///         hashset! {1, 2, 4},
167///         hashset! {1, 3, 4},
168///         hashset! {2, 3, 4},
169///         hashset! {1, 2, 3, 4}
170///     ]
171/// );
172/// ```
173#[inline]
174pub fn shortlex_hash_sets_min_length<I: Clone + Iterator>(
175    min_length: u64,
176    xs: I,
177) -> ShortlexOrderedUniqueCollections<I, HashSet<I::Item>>
178where
179    I::Item: Clone + Eq + Hash,
180{
181    shortlex_hash_sets_length_inclusive_range(min_length, u64::MAX, xs)
182}
183
184/// Generates [`HashSet`]s, with lengths in a range $[a, b)$, with elements from a single iterator.
185///
186/// The [`HashSet`]s are generated in order of increasing length, and within each length they are
187/// ordered lexicographically with respect to the order of the element iterator.
188///
189/// The source iterator should not repeat any elements, but this is not enforced.
190///
191/// The iterator should be finite; if it is infinite, [`HashSet`]s of length `\max(2, a + 1)` and
192/// above will never be generated.
193///
194/// If $a \leq b$, the output is empty.
195///
196/// If $a = 0$ and $b = 1$, the output consists of a single empty [`HashSet`].
197///
198/// If the input iterator is infinite and $0 < a < b$, the output length is also infinite.
199///
200/// If the input iterator length is $n$, the output length is
201/// $$
202/// \sum_{i=a}^b - 1 \binom{n}{i}.
203/// $$
204///
205/// # Examples
206/// ```
207/// use itertools::Itertools;
208/// use malachite_base::sets::exhaustive::shortlex_hash_sets_length_range;
209/// use maplit::hashset;
210///
211/// let xss = shortlex_hash_sets_length_range(2, 4, 1..=4).collect_vec();
212/// assert_eq!(
213///     xss,
214///     &[
215///         hashset! {1, 2},
216///         hashset! {1, 3},
217///         hashset! {1, 4},
218///         hashset! {2, 3},
219///         hashset! {2, 4},
220///         hashset! {3, 4},
221///         hashset! {1, 2, 3},
222///         hashset! {1, 2, 4},
223///         hashset! {1, 3, 4},
224///         hashset! {2, 3, 4},
225///     ]
226/// );
227/// ```
228#[inline]
229pub fn shortlex_hash_sets_length_range<I: Clone + Iterator>(
230    mut a: u64,
231    mut b: u64,
232    xs: I,
233) -> ShortlexOrderedUniqueCollections<I, HashSet<I::Item>>
234where
235    I::Item: Clone + Eq + Hash,
236{
237    if b == 0 {
238        // Transform an empty (x, 0) range into (2, 1), which is also empty but doesn't cause
239        // overflow
240        a = 2;
241        b = 1;
242    }
243    shortlex_hash_sets_length_inclusive_range(a, b - 1, xs)
244}
245
246/// Generates [`HashSet`]s, with lengths in a range $[a, b]$, with elements from a single iterator.
247///
248/// The [`HashSet`]s are generated in order of increasing length, and within each length they are
249/// ordered lexicographically with respect to the order of the element iterator.
250///
251/// The source iterator should not repeat any elements, but this is not enforced.
252///
253/// The iterator should be finite; if it is infinite, [`HashSet`]s of length `\max(2, a + 1)` and
254/// above will never be generated.
255///
256/// If $a < b$, the output is empty.
257///
258/// If $a = b = 0$, the output consists of a single empty [`HashSet`].
259///
260/// If the input iterator is infinite and $0 < a \leq b$, the output length is also infinite.
261///
262/// If the input iterator length is $n$, the output length is
263/// $$
264/// \sum_{i=a}^b \binom{n}{i}.
265/// $$
266///
267/// # Examples
268/// ```
269/// use itertools::Itertools;
270/// use malachite_base::sets::exhaustive::shortlex_hash_sets_length_inclusive_range;
271/// use maplit::hashset;
272///
273/// let xss = shortlex_hash_sets_length_inclusive_range(2, 3, 1..=4).collect_vec();
274/// assert_eq!(
275///     xss,
276///     &[
277///         hashset! {1, 2},
278///         hashset! {1, 3},
279///         hashset! {1, 4},
280///         hashset! {2, 3},
281///         hashset! {2, 4},
282///         hashset! {3, 4},
283///         hashset! {1, 2, 3},
284///         hashset! {1, 2, 4},
285///         hashset! {1, 3, 4},
286///         hashset! {2, 3, 4},
287///     ]
288/// );
289/// ```
290#[inline]
291pub fn shortlex_hash_sets_length_inclusive_range<I: Clone + Iterator>(
292    a: u64,
293    b: u64,
294    xs: I,
295) -> ShortlexOrderedUniqueCollections<I, HashSet<I::Item>>
296where
297    I::Item: Clone + Eq + Hash,
298{
299    ShortlexOrderedUniqueCollections::new(a, b, xs)
300}
301
302/// Generates [`HashSet`]s with elements from a single iterator.
303///
304/// The [`HashSet`]s are ordered lexicographically with respect to the order of the element
305/// iterator.
306///
307/// The source iterator should not repeat any elements, but this is not enforced.
308///
309/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
310/// generated.
311///
312/// If the input iterator is infinite, the output length is also infinite.
313///
314/// If the input iterator length is $n$, the output length is $2^n$.
315///
316/// If `xs` is empty, the output consists of a single empty [`HashSet`].
317///
318/// # Examples
319/// ```
320/// use itertools::Itertools;
321/// use malachite_base::sets::exhaustive::lex_hash_sets;
322/// use maplit::hashset;
323///
324/// let xss = lex_hash_sets(1..=4).collect_vec();
325/// assert_eq!(
326///     xss,
327///     &[
328///         hashset! {},
329///         hashset! {1},
330///         hashset! {1, 2},
331///         hashset! {1, 2, 3},
332///         hashset! {1, 2, 3, 4},
333///         hashset! {1, 2, 4},
334///         hashset! {1, 3},
335///         hashset! {1, 3, 4},
336///         hashset! {1, 4},
337///         hashset! {2},
338///         hashset! {2, 3},
339///         hashset! {2, 3, 4},
340///         hashset! {2, 4},
341///         hashset! {3},
342///         hashset! {3, 4},
343///         hashset! {4}
344///     ]
345/// );
346/// ```
347#[inline]
348pub fn lex_hash_sets<I: Clone + Iterator>(xs: I) -> LexOrderedUniqueCollections<I, HashSet<I::Item>>
349where
350    I::Item: Clone + Eq + Hash,
351{
352    lex_hash_sets_length_inclusive_range(0, u64::MAX, xs)
353}
354
355/// Generates [`HashSet`]s with a mininum length, with elements from a single iterator.
356///
357/// The [`HashSet`]s are ordered lexicographically with respect to the order of the element
358/// iterator.
359///
360/// The source iterator should not repeat any elements, but this is not enforced.
361///
362/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
363/// generated.
364///
365/// If the input iterator is infinite, the output length is also infinite.
366///
367/// If the input iterator length is $n$ and the `min_length` is $\ell$, the output length is
368/// $$
369/// \sum_{i=\ell}^n \binom{n}{i}.
370/// $$
371///
372/// # Examples
373/// ```
374/// use itertools::Itertools;
375/// use malachite_base::sets::exhaustive::lex_hash_sets_min_length;
376/// use maplit::hashset;
377///
378/// let xss = lex_hash_sets_min_length(2, 1..=4).collect_vec();
379/// assert_eq!(
380///     xss,
381///     &[
382///         hashset! {1, 2},
383///         hashset! {1, 2, 3},
384///         hashset! {1, 2, 3, 4},
385///         hashset! {1, 2, 4},
386///         hashset! {1, 3},
387///         hashset! {1, 3, 4},
388///         hashset! {1, 4},
389///         hashset! {2, 3},
390///         hashset! {2, 3, 4},
391///         hashset! {2, 4},
392///         hashset! {3, 4},
393///     ]
394/// );
395/// ```
396#[inline]
397pub fn lex_hash_sets_min_length<I: Clone + Iterator>(
398    min_length: u64,
399    xs: I,
400) -> LexOrderedUniqueCollections<I, HashSet<I::Item>>
401where
402    I::Item: Clone + Eq + Hash,
403{
404    lex_hash_sets_length_inclusive_range(min_length, u64::MAX, xs)
405}
406
407/// Generates [`HashSet`]s, with lengths in a range $[a, b)$, with elements from a single iterator.
408///
409/// The [`HashSet`]s are ordered lexicographically with respect to the order of the element
410/// iterator.
411///
412/// The source iterator should not repeat any elements, but this is not enforced.
413///
414/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
415/// generated.
416///
417/// If $a \leq b$, the output is empty.
418///
419/// If $a = 0$ and $b = 1$, the output consists of a single empty [`HashSet`].
420///
421/// If the input iterator is infinite and $0 < a < b$, the output length is also infinite.
422///
423/// If the input iterator length is $n$, the output length is
424/// $$
425/// \sum_{i=a}^b - 1 \binom{n}{i}.
426/// $$
427///
428/// # Examples
429/// ```
430/// use itertools::Itertools;
431/// use malachite_base::sets::exhaustive::lex_hash_sets_length_range;
432/// use maplit::hashset;
433///
434/// let xss = lex_hash_sets_length_range(2, 4, 1..=4).collect_vec();
435/// assert_eq!(
436///     xss,
437///     &[
438///         hashset! {1, 2},
439///         hashset! {1, 2, 3},
440///         hashset! {1, 2, 4},
441///         hashset! {1, 3},
442///         hashset! {1, 3, 4},
443///         hashset! {1, 4},
444///         hashset! {2, 3},
445///         hashset! {2, 3, 4},
446///         hashset! {2, 4},
447///         hashset! {3, 4},
448///     ]
449/// );
450/// ```
451#[inline]
452pub fn lex_hash_sets_length_range<I: Clone + Iterator>(
453    mut a: u64,
454    mut b: u64,
455    xs: I,
456) -> LexOrderedUniqueCollections<I, HashSet<I::Item>>
457where
458    I::Item: Clone + Eq + Hash,
459{
460    if b == 0 {
461        // Transform an empty (x, 0) range into (2, 1), which is also empty but doesn't cause
462        // overflow
463        a = 2;
464        b = 1;
465    }
466    lex_hash_sets_length_inclusive_range(a, b - 1, xs)
467}
468
469/// Generates [`HashSet`]s, with lengths in a range $[a, b]$, with elements from a single iterator.
470///
471/// The [`HashSet`]s are ordered lexicographically with respect to the order of the element
472/// iterator.
473///
474/// The source iterator should not repeat any elements, but this is not enforced.
475///
476/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
477/// generated.
478///
479/// If $a < b$, the output is empty.
480///
481/// If $a = b = 0$, the output consists of a single empty [`HashSet`].
482///
483/// If the input iterator is infinite and $0 < a \leq b$, the output length is also infinite.
484///
485/// If the input iterator length is $n$, the output length is
486/// $$
487/// \sum_{i=a}^b \binom{n}{i}.
488/// $$
489///
490/// # Examples
491/// ```
492/// use itertools::Itertools;
493/// use malachite_base::sets::exhaustive::lex_hash_sets_length_inclusive_range;
494/// use maplit::hashset;
495///
496/// let xss = lex_hash_sets_length_inclusive_range(2, 3, 1..=4).collect_vec();
497/// assert_eq!(
498///     xss,
499///     &[
500///         hashset! {1, 2},
501///         hashset! {1, 2, 3},
502///         hashset! {1, 2, 4},
503///         hashset! {1, 3},
504///         hashset! {1, 3, 4},
505///         hashset! {1, 4},
506///         hashset! {2, 3},
507///         hashset! {2, 3, 4},
508///         hashset! {2, 4},
509///         hashset! {3, 4},
510///     ]
511/// );
512/// ```
513#[inline]
514pub fn lex_hash_sets_length_inclusive_range<I: Clone + Iterator>(
515    a: u64,
516    b: u64,
517    xs: I,
518) -> LexOrderedUniqueCollections<I, HashSet<I::Item>>
519where
520    I::Item: Clone + Eq + Hash,
521{
522    LexOrderedUniqueCollections::new(a, b, xs)
523}
524
525/// Generates [`HashSet`]s of a given size with elements from a single iterator.
526///
527/// The source iterator should not repeat any elements, but this is not enforced.
528///
529/// If $k$ is 0, the output length is 1.
530///
531/// If $k$ is nonzero and the input iterator is infinite, the output length is also infinite.
532///
533/// If $k$ is nonzero and the input iterator length is $n$, the output length is $\binom{n}{k}$.
534///
535/// If $k$ is 0, the output consists of one empty [`HashSet`].
536///
537/// If `xs` is empty, the output is also empty, unless $k$ is 0.
538///
539/// # Examples
540/// ```
541/// use itertools::Itertools;
542/// use malachite_base::sets::exhaustive::exhaustive_hash_sets_fixed_length;
543/// use maplit::hashset;
544///
545/// let xss = exhaustive_hash_sets_fixed_length(4, 1..=6).collect_vec();
546/// assert_eq!(
547///     xss,
548///     &[
549///         hashset! {1, 2, 3, 4},
550///         hashset! {1, 2, 3, 5},
551///         hashset! {1, 2, 4, 5},
552///         hashset! {1, 3, 4, 5},
553///         hashset! {2, 3, 4, 5},
554///         hashset! {1, 2, 3, 6},
555///         hashset! {1, 2, 4, 6},
556///         hashset! {1, 3, 4, 6},
557///         hashset! {2, 3, 4, 6},
558///         hashset! {1, 2, 5, 6},
559///         hashset! {1, 3, 5, 6},
560///         hashset! {2, 3, 5, 6},
561///         hashset! {1, 4, 5, 6},
562///         hashset! {2, 4, 5, 6},
563///         hashset! {3, 4, 5, 6}
564///     ]
565/// );
566/// ```
567#[inline]
568pub fn exhaustive_hash_sets_fixed_length<I: Clone + Iterator>(
569    k: u64,
570    xs: I,
571) -> ExhaustiveOrderedUniqueCollections<I, HashSet<I::Item>>
572where
573    I::Item: Clone + Eq + Hash,
574{
575    exhaustive_hash_sets_length_inclusive_range(k, k, xs)
576}
577
578/// Generates [`HashSet`]s with elements from a single iterator.
579///
580/// The source iterator should not repeat any elements, but this is not enforced.
581///
582/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
583/// generated.
584///
585/// If the input iterator is infinite, the output length is also infinite.
586///
587/// If the input iterator length is $n$, the output length is $2^n$.
588///
589/// If `xs` is empty, the output consists of a single empty [`HashSet`].
590///
591/// # Examples
592/// ```
593/// use itertools::Itertools;
594/// use malachite_base::sets::exhaustive::exhaustive_hash_sets;
595/// use maplit::hashset;
596///
597/// let xss = exhaustive_hash_sets(1..=4).collect_vec();
598/// assert_eq!(
599///     xss,
600///     &[
601///         hashset! {},
602///         hashset! {1},
603///         hashset! {2},
604///         hashset! {1, 2},
605///         hashset! {3},
606///         hashset! {1, 3},
607///         hashset! {2, 3},
608///         hashset! {1, 2, 3},
609///         hashset! {4},
610///         hashset! {1, 4},
611///         hashset! {2, 4},
612///         hashset! {1, 2, 4},
613///         hashset! {3, 4},
614///         hashset! {1, 3, 4},
615///         hashset! {2, 3, 4},
616///         hashset! {1, 2, 3, 4}
617///     ]
618/// );
619/// ```
620#[inline]
621pub fn exhaustive_hash_sets<I: Clone + Iterator>(
622    xs: I,
623) -> ExhaustiveOrderedUniqueCollections<I, HashSet<I::Item>>
624where
625    I::Item: Clone + Eq + Hash,
626{
627    exhaustive_hash_sets_length_inclusive_range(0, u64::MAX, xs)
628}
629
630/// Generates [`HashSet`]s with a mininum length, with elements from a single iterator.
631///
632/// The source iterator should not repeat any elements, but this is not enforced.
633///
634/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
635/// generated.
636///
637/// If the input iterator is infinite, the output length is also infinite.
638///
639/// If the input iterator length is $n$ and the `min_length` is $\ell$, the output length is
640/// $$
641/// \sum_{i=\ell}^n \binom{n}{i}.
642/// $$
643///
644/// # Examples
645/// ```
646/// use itertools::Itertools;
647/// use malachite_base::sets::exhaustive::exhaustive_hash_sets_min_length;
648/// use maplit::hashset;
649///
650/// let xss = exhaustive_hash_sets_min_length(2, 1..=4).collect_vec();
651/// assert_eq!(
652///     xss,
653///     &[
654///         hashset! {1, 2},
655///         hashset! {1, 3},
656///         hashset! {2, 3},
657///         hashset! {1, 2, 3},
658///         hashset! {1, 4},
659///         hashset! {2, 4},
660///         hashset! {1, 2, 4},
661///         hashset! {3, 4},
662///         hashset! {1, 3, 4},
663///         hashset! {2, 3, 4},
664///         hashset! {1, 2, 3, 4}
665///     ]
666/// );
667/// ```
668#[inline]
669pub fn exhaustive_hash_sets_min_length<I: Clone + Iterator>(
670    min_length: u64,
671    xs: I,
672) -> ExhaustiveOrderedUniqueCollections<I, HashSet<I::Item>>
673where
674    I::Item: Clone + Eq + Hash,
675{
676    exhaustive_hash_sets_length_inclusive_range(min_length, u64::MAX, xs)
677}
678
679/// Generates [`HashSet`]s, with lengths in a range $[a, b)$, with elements from a single iterator.
680///
681/// The source iterator should not repeat any elements, but this is not enforced.
682///
683/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
684/// generated.
685///
686/// If $a \leq b$, the output is empty.
687///
688/// If $a = 0$ and $b = 1$, the output consists of a single empty [`HashSet`].
689///
690/// If the input iterator is infinite and $0 < a < b$, the output length is also infinite.
691///
692/// If the input iterator length is $n$, the output length is
693/// $$
694/// \sum_{i=a}^b - 1 \binom{n}{i}.
695/// $$
696///
697/// # Examples
698/// ```
699/// use itertools::Itertools;
700/// use malachite_base::sets::exhaustive::exhaustive_hash_sets_length_range;
701/// use maplit::hashset;
702///
703/// let xss = exhaustive_hash_sets_length_range(2, 4, 1..=4).collect_vec();
704/// assert_eq!(
705///     xss,
706///     &[
707///         hashset! {1, 2},
708///         hashset! {1, 3},
709///         hashset! {2, 3},
710///         hashset! {1, 2, 3},
711///         hashset! {1, 4},
712///         hashset! {2, 4},
713///         hashset! {1, 2, 4},
714///         hashset! {3, 4},
715///         hashset! {1, 3, 4},
716///         hashset! {2, 3, 4}
717///     ]
718/// );
719/// ```
720#[inline]
721pub fn exhaustive_hash_sets_length_range<I: Clone + Iterator>(
722    a: u64,
723    b: u64,
724    xs: I,
725) -> ExhaustiveOrderedUniqueCollections<I, HashSet<I::Item>>
726where
727    I::Item: Clone + Eq + Hash,
728{
729    if a >= b {
730        ExhaustiveOrderedUniqueCollections::None
731    } else {
732        exhaustive_hash_sets_length_inclusive_range(a, b - 1, xs)
733    }
734}
735
736/// Generates [`HashSet`]s, with lengths in a range $[a, b]$, with elements from a single iterator.
737///
738/// The source iterator should not repeat any elements, but this is not enforced.
739///
740/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
741/// generated.
742///
743/// If $a < b$, the output is empty.
744///
745/// If $a = b = 0$, the output consists of a single empty [`HashSet`].
746///
747/// If the input iterator is infinite and $0 < a \leq b$, the output length is also infinite.
748///
749/// If the input iterator length is $n$, the output length is
750/// $$
751/// \sum_{i=a}^b \binom{n}{i}.
752/// $$
753///
754/// # Examples
755/// ```
756/// use itertools::Itertools;
757/// use malachite_base::sets::exhaustive::exhaustive_hash_sets_length_inclusive_range;
758/// use maplit::hashset;
759///
760/// let xss = exhaustive_hash_sets_length_inclusive_range(2, 3, 1..=4).collect_vec();
761/// assert_eq!(
762///     xss,
763///     &[
764///         hashset! {1, 2},
765///         hashset! {1, 3},
766///         hashset! {2, 3},
767///         hashset! {1, 2, 3},
768///         hashset! {1, 4},
769///         hashset! {2, 4},
770///         hashset! {1, 2, 4},
771///         hashset! {3, 4},
772///         hashset! {1, 3, 4},
773///         hashset! {2, 3, 4}
774///     ]
775/// );
776/// ```
777#[inline]
778pub fn exhaustive_hash_sets_length_inclusive_range<I: Clone + Iterator>(
779    a: u64,
780    b: u64,
781    xs: I,
782) -> ExhaustiveOrderedUniqueCollections<I, HashSet<I::Item>>
783where
784    I::Item: Clone + Eq + Hash,
785{
786    ExhaustiveOrderedUniqueCollections::new(a, b, xs)
787}
788
789/// Generates [`BTreeSet`]s of a given size with elements from a single iterator.
790///
791/// The [`BTreeSet`]s are ordered lexicographically with respect to the order of the element
792/// iterator.
793///
794/// The source iterator should not repeat any elements, but this is not enforced.
795///
796/// If $k$ is 0, the output length is 1.
797///
798/// If $k$ is nonzero and the input iterator is infinite, the output length is also infinite.
799///
800/// If $k$ is nonzero and the input iterator length is $n$, the output length is $\binom{n}{k}$.
801///
802/// If $k$ is 0, the output consists of one empty [`BTreeSet`].
803///
804/// If `xs` is empty, the output is also empty, unless $k$ is 0.
805///
806/// # Examples
807/// ```
808/// use itertools::Itertools;
809/// use malachite_base::sets::exhaustive::lex_b_tree_sets_fixed_length;
810/// use maplit::btreeset;
811///
812/// let xss = lex_b_tree_sets_fixed_length(4, 1..=6).collect_vec();
813/// assert_eq!(
814///     xss,
815///     &[
816///         btreeset! {1, 2, 3, 4},
817///         btreeset! {1, 2, 3, 5},
818///         btreeset! {1, 2, 3, 6},
819///         btreeset! {1, 2, 4, 5},
820///         btreeset! {1, 2, 4, 6},
821///         btreeset! {1, 2, 5, 6},
822///         btreeset! {1, 3, 4, 5},
823///         btreeset! {1, 3, 4, 6},
824///         btreeset! {1, 3, 5, 6},
825///         btreeset! {1, 4, 5, 6},
826///         btreeset! {2, 3, 4, 5},
827///         btreeset! {2, 3, 4, 6},
828///         btreeset! {2, 3, 5, 6},
829///         btreeset! {2, 4, 5, 6},
830///         btreeset! {3, 4, 5, 6}
831///     ]
832/// );
833/// ```
834#[inline]
835pub fn lex_b_tree_sets_fixed_length<I: Iterator>(
836    k: u64,
837    xs: I,
838) -> LexFixedLengthOrderedUniqueCollections<I, BTreeSet<I::Item>>
839where
840    I::Item: Clone + Ord,
841{
842    LexFixedLengthOrderedUniqueCollections::new(k, xs)
843}
844
845/// Generates [`BTreeSet`]s with elements from a single iterator.
846///
847/// The [`BTreeSet`]s are generated in order of increasing length, and within each length they are
848/// ordered lexicographically with respect to the order of the element iterator.
849///
850/// The source iterator should not repeat any elements, but this is not enforced.
851///
852/// The iterator should be finite; if it is infinite, [`BTreeSet`]s of length 2 and above will never
853/// be generated.
854///
855/// If the input iterator is infinite, the output length is also infinite.
856///
857/// If the input iterator length is $n$, the output length is $2^n$.
858///
859/// If `xs` is empty, the output consists of a single empty [`HashSet`].
860///
861/// # Examples
862/// ```
863/// use itertools::Itertools;
864/// use malachite_base::sets::exhaustive::shortlex_b_tree_sets;
865/// use maplit::btreeset;
866///
867/// let xss = shortlex_b_tree_sets(1..=4).collect_vec();
868/// assert_eq!(
869///     xss,
870///     &[
871///         btreeset! {},
872///         btreeset! {1},
873///         btreeset! {2},
874///         btreeset! {3},
875///         btreeset! {4},
876///         btreeset! {1, 2},
877///         btreeset! {1, 3},
878///         btreeset! {1, 4},
879///         btreeset! {2, 3},
880///         btreeset! {2, 4},
881///         btreeset! {3, 4},
882///         btreeset! {1, 2, 3},
883///         btreeset! {1, 2, 4},
884///         btreeset! {1, 3, 4},
885///         btreeset! {2, 3, 4},
886///         btreeset! {1, 2, 3, 4}
887///     ]
888/// );
889/// ```
890#[inline]
891pub fn shortlex_b_tree_sets<I: Clone + Iterator>(
892    xs: I,
893) -> ShortlexOrderedUniqueCollections<I, BTreeSet<I::Item>>
894where
895    I::Item: Clone + Ord,
896{
897    shortlex_b_tree_sets_length_inclusive_range(0, u64::MAX, xs)
898}
899
900/// Generates [`BTreeSet`]s with a mininum length, with elements from a single iterator.
901///
902/// The [`BTreeSet`]s are generated in order of increasing length, and within each length they are
903/// ordered lexicographically with respect to the order of the element iterator.
904///
905/// The source iterator should not repeat any elements, but this is not enforced.
906///
907/// The iterator should be finite; if it is infinite, [`BTreeSet`]s of length `\max(2, \ell + 1)`
908/// and above will never be generated.
909///
910/// If the input iterator is infinite, the output length is also infinite.
911///
912/// If the input iterator length is $n$ and the `min_length` is $\ell$, the output length is
913/// $$
914/// \sum_{i=\ell}^n \binom{n}{i}.
915/// $$
916///
917/// # Examples
918/// ```
919/// use itertools::Itertools;
920/// use malachite_base::sets::exhaustive::shortlex_b_tree_sets_min_length;
921/// use maplit::btreeset;
922///
923/// let xss = shortlex_b_tree_sets_min_length(2, 1..=4).collect_vec();
924/// assert_eq!(
925///     xss,
926///     &[
927///         btreeset! {1, 2},
928///         btreeset! {1, 3},
929///         btreeset! {1, 4},
930///         btreeset! {2, 3},
931///         btreeset! {2, 4},
932///         btreeset! {3, 4},
933///         btreeset! {1, 2, 3},
934///         btreeset! {1, 2, 4},
935///         btreeset! {1, 3, 4},
936///         btreeset! {2, 3, 4},
937///         btreeset! {1, 2, 3, 4}
938///     ]
939/// );
940/// ```
941#[inline]
942pub fn shortlex_b_tree_sets_min_length<I: Clone + Iterator>(
943    min_length: u64,
944    xs: I,
945) -> ShortlexOrderedUniqueCollections<I, BTreeSet<I::Item>>
946where
947    I::Item: Clone + Ord,
948{
949    shortlex_b_tree_sets_length_inclusive_range(min_length, u64::MAX, xs)
950}
951
952/// Generates [`BTreeSet`]s, with lengths in a range $[a, b)$, with elements from a single iterator.
953///
954/// The [`BTreeSet`]s are generated in order of increasing length, and within each length they are
955/// ordered lexicographically with respect to the order of the element iterator.
956///
957/// The source iterator should not repeat any elements, but this is not enforced.
958///
959/// The iterator should be finite; if it is infinite, [`BTreeSet`]s of length `\max(2, a + 1)` and
960/// above will never be generated.
961///
962/// If $a \leq b$, the output is empty.
963///
964/// If $a = 0$ and $b = 1$, the output consists of a single empty [`BTreeSet`].
965///
966/// If the input iterator is infinite and $0 < a < b$, the output length is also infinite.
967///
968/// If the input iterator length is $n$, the output length is
969/// $$
970/// \sum_{i=a}^b - 1 \binom{n}{i}.
971/// $$
972///
973/// # Examples
974/// ```
975/// use itertools::Itertools;
976/// use malachite_base::sets::exhaustive::shortlex_b_tree_sets_length_range;
977/// use maplit::btreeset;
978///
979/// let xss = shortlex_b_tree_sets_length_range(2, 4, 1..=4).collect_vec();
980/// assert_eq!(
981///     xss,
982///     &[
983///         btreeset! {1, 2},
984///         btreeset! {1, 3},
985///         btreeset! {1, 4},
986///         btreeset! {2, 3},
987///         btreeset! {2, 4},
988///         btreeset! {3, 4},
989///         btreeset! {1, 2, 3},
990///         btreeset! {1, 2, 4},
991///         btreeset! {1, 3, 4},
992///         btreeset! {2, 3, 4},
993///     ]
994/// );
995/// ```
996#[inline]
997pub fn shortlex_b_tree_sets_length_range<I: Clone + Iterator>(
998    mut a: u64,
999    mut b: u64,
1000    xs: I,
1001) -> ShortlexOrderedUniqueCollections<I, BTreeSet<I::Item>>
1002where
1003    I::Item: Clone + Ord,
1004{
1005    if b == 0 {
1006        // Transform an empty (x, 0) range into (2, 1), which is also empty but doesn't cause
1007        // overflow
1008        a = 2;
1009        b = 1;
1010    }
1011    shortlex_b_tree_sets_length_inclusive_range(a, b - 1, xs)
1012}
1013
1014/// Generates [`BTreeSet`]s, with lengths in a range $[a, b]$, with elements from a single iterator.
1015///
1016/// The [`BTreeSet`]s are generated in order of increasing length, and within each length they are
1017/// ordered lexicographically with respect to the order of the element iterator.
1018///
1019/// The source iterator should not repeat any elements, but this is not enforced.
1020///
1021/// The iterator should be finite; if it is infinite, [`BTreeSet`]s of length `\max(2, a + 1)` and
1022/// above will never be generated.
1023///
1024/// If $a < b$, the output is empty.
1025///
1026/// If $a = b = 0$, the output consists of a single empty [`BTreeSet`].
1027///
1028/// If the input iterator is infinite and $0 < a \leq b$, the output length is also infinite.
1029///
1030/// If the input iterator length is $n$, the output length is
1031/// $$
1032/// \sum_{i=a}^b \binom{n}{i}.
1033/// $$
1034///
1035/// # Examples
1036/// ```
1037/// use itertools::Itertools;
1038/// use malachite_base::sets::exhaustive::shortlex_b_tree_sets_length_inclusive_range;
1039/// use maplit::btreeset;
1040///
1041/// let xss = shortlex_b_tree_sets_length_inclusive_range(2, 3, 1..=4).collect_vec();
1042/// assert_eq!(
1043///     xss,
1044///     &[
1045///         btreeset! {1, 2},
1046///         btreeset! {1, 3},
1047///         btreeset! {1, 4},
1048///         btreeset! {2, 3},
1049///         btreeset! {2, 4},
1050///         btreeset! {3, 4},
1051///         btreeset! {1, 2, 3},
1052///         btreeset! {1, 2, 4},
1053///         btreeset! {1, 3, 4},
1054///         btreeset! {2, 3, 4},
1055///     ]
1056/// );
1057/// ```
1058#[inline]
1059pub fn shortlex_b_tree_sets_length_inclusive_range<I: Clone + Iterator>(
1060    a: u64,
1061    b: u64,
1062    xs: I,
1063) -> ShortlexOrderedUniqueCollections<I, BTreeSet<I::Item>>
1064where
1065    I::Item: Clone + Ord,
1066{
1067    ShortlexOrderedUniqueCollections::new(a, b, xs)
1068}
1069
1070/// Generates [`BTreeSet`]s with elements from a single iterator.
1071///
1072/// The [`BTreeSet`]s are ordered lexicographically with respect to the order of the element
1073/// iterator.
1074///
1075/// The source iterator should not repeat any elements, but this is not enforced.
1076///
1077/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1078/// generated.
1079///
1080/// If the input iterator is infinite, the output length is also infinite.
1081///
1082/// If the input iterator length is $n$, the output length is $2^n$.
1083///
1084/// If `xs` is empty, the output consists of a single empty [`BTreeSet`].
1085///
1086/// # Examples
1087/// ```
1088/// use itertools::Itertools;
1089/// use malachite_base::sets::exhaustive::lex_b_tree_sets;
1090/// use maplit::btreeset;
1091///
1092/// let xss = lex_b_tree_sets(1..=4).collect_vec();
1093/// assert_eq!(
1094///     xss,
1095///     &[
1096///         btreeset! {},
1097///         btreeset! {1},
1098///         btreeset! {1, 2},
1099///         btreeset! {1, 2, 3},
1100///         btreeset! {1, 2, 3, 4},
1101///         btreeset! {1, 2, 4},
1102///         btreeset! {1, 3},
1103///         btreeset! {1, 3, 4},
1104///         btreeset! {1, 4},
1105///         btreeset! {2},
1106///         btreeset! {2, 3},
1107///         btreeset! {2, 3, 4},
1108///         btreeset! {2, 4},
1109///         btreeset! {3},
1110///         btreeset! {3, 4},
1111///         btreeset! {4}
1112///     ]
1113/// );
1114/// ```
1115#[inline]
1116pub fn lex_b_tree_sets<I: Clone + Iterator>(
1117    xs: I,
1118) -> LexOrderedUniqueCollections<I, BTreeSet<I::Item>>
1119where
1120    I::Item: Clone + Ord,
1121{
1122    lex_b_tree_sets_length_inclusive_range(0, u64::MAX, xs)
1123}
1124
1125/// Generates [`BTreeSet`]s with a mininum length, with elements from a single iterator.
1126///
1127/// The [`BTreeSet`]s are ordered lexicographically with respect to the order of the element
1128/// iterator.
1129///
1130/// The source iterator should not repeat any elements, but this is not enforced.
1131///
1132/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1133/// generated.
1134///
1135/// If the input iterator is infinite, the output length is also infinite.
1136///
1137/// If the input iterator length is $n$ and the `min_length` is $\ell$, the output length is
1138/// $$
1139/// \sum_{i=\ell}^n \binom{n}{i}.
1140/// $$
1141///
1142/// # Examples
1143/// ```
1144/// use itertools::Itertools;
1145/// use malachite_base::sets::exhaustive::lex_b_tree_sets_min_length;
1146/// use maplit::btreeset;
1147///
1148/// let xss = lex_b_tree_sets_min_length(2, 1..=4).collect_vec();
1149/// assert_eq!(
1150///     xss,
1151///     &[
1152///         btreeset! {1, 2},
1153///         btreeset! {1, 2, 3},
1154///         btreeset! {1, 2, 3, 4},
1155///         btreeset! {1, 2, 4},
1156///         btreeset! {1, 3},
1157///         btreeset! {1, 3, 4},
1158///         btreeset! {1, 4},
1159///         btreeset! {2, 3},
1160///         btreeset! {2, 3, 4},
1161///         btreeset! {2, 4},
1162///         btreeset! {3, 4},
1163///     ]
1164/// );
1165/// ```
1166#[inline]
1167pub fn lex_b_tree_sets_min_length<I: Clone + Iterator>(
1168    min_length: u64,
1169    xs: I,
1170) -> LexOrderedUniqueCollections<I, BTreeSet<I::Item>>
1171where
1172    I::Item: Clone + Ord,
1173{
1174    lex_b_tree_sets_length_inclusive_range(min_length, u64::MAX, xs)
1175}
1176
1177/// Generates [`BTreeSet`]s, with lengths in a range $[a, b)$, with elements from a single iterator.
1178///
1179/// The [`BTreeSet`]s are ordered lexicographically with respect to the order of the element
1180/// iterator.
1181///
1182/// The source iterator should not repeat any elements, but this is not enforced.
1183///
1184/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1185/// generated.
1186///
1187/// If $a \leq b$, the output is empty.
1188///
1189/// If $a = 0$ and $b = 1$, the output consists of a single empty [`BTreeSet`].
1190///
1191/// If the input iterator is infinite and $0 < a < b$, the output length is also infinite.
1192///
1193/// If the input iterator length is $n$, the output length is
1194/// $$
1195/// \sum_{i=a}^b - 1 \binom{n}{i}.
1196/// $$
1197///
1198/// # Examples
1199/// ```
1200/// use itertools::Itertools;
1201/// use malachite_base::sets::exhaustive::lex_b_tree_sets_length_range;
1202/// use maplit::btreeset;
1203///
1204/// let xss = lex_b_tree_sets_length_range(2, 4, 1..=4).collect_vec();
1205/// assert_eq!(
1206///     xss,
1207///     &[
1208///         btreeset! {1, 2},
1209///         btreeset! {1, 2, 3},
1210///         btreeset! {1, 2, 4},
1211///         btreeset! {1, 3},
1212///         btreeset! {1, 3, 4},
1213///         btreeset! {1, 4},
1214///         btreeset! {2, 3},
1215///         btreeset! {2, 3, 4},
1216///         btreeset! {2, 4},
1217///         btreeset! {3, 4},
1218///     ]
1219/// );
1220/// ```
1221#[inline]
1222pub fn lex_b_tree_sets_length_range<I: Clone + Iterator>(
1223    mut a: u64,
1224    mut b: u64,
1225    xs: I,
1226) -> LexOrderedUniqueCollections<I, BTreeSet<I::Item>>
1227where
1228    I::Item: Clone + Ord,
1229{
1230    if b == 0 {
1231        // Transform an empty (x, 0) range into (2, 1), which is also empty but doesn't cause
1232        // overflow
1233        a = 2;
1234        b = 1;
1235    }
1236    lex_b_tree_sets_length_inclusive_range(a, b - 1, xs)
1237}
1238
1239/// Generates [`BTreeSet`]s, with lengths in a range $[a, b]$, with elements from a single iterator.
1240///
1241/// The [`BTreeSet`]s are ordered lexicographically with respect to the order of the element
1242/// iterator.
1243///
1244/// The source iterator should not repeat any elements, but this is not enforced.
1245///
1246/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1247/// generated.
1248///
1249/// If $a < b$, the output is empty.
1250///
1251/// If $a = b = 0$, the output consists of a single empty [`BTreeSet`].
1252///
1253/// If the input iterator is infinite and $0 < a \leq b$, the output length is also infinite.
1254///
1255/// If the input iterator length is $n$, the output length is
1256/// $$
1257/// \sum_{i=a}^b \binom{n}{i}.
1258/// $$
1259///
1260/// # Examples
1261/// ```
1262/// use itertools::Itertools;
1263/// use malachite_base::sets::exhaustive::lex_b_tree_sets_length_inclusive_range;
1264/// use maplit::btreeset;
1265///
1266/// let xss = lex_b_tree_sets_length_inclusive_range(2, 3, 1..=4).collect_vec();
1267/// assert_eq!(
1268///     xss,
1269///     &[
1270///         btreeset! {1, 2},
1271///         btreeset! {1, 2, 3},
1272///         btreeset! {1, 2, 4},
1273///         btreeset! {1, 3},
1274///         btreeset! {1, 3, 4},
1275///         btreeset! {1, 4},
1276///         btreeset! {2, 3},
1277///         btreeset! {2, 3, 4},
1278///         btreeset! {2, 4},
1279///         btreeset! {3, 4},
1280///     ]
1281/// );
1282/// ```
1283#[inline]
1284pub fn lex_b_tree_sets_length_inclusive_range<I: Clone + Iterator>(
1285    a: u64,
1286    b: u64,
1287    xs: I,
1288) -> LexOrderedUniqueCollections<I, BTreeSet<I::Item>>
1289where
1290    I::Item: Clone + Ord,
1291{
1292    LexOrderedUniqueCollections::new(a, b, xs)
1293}
1294
1295/// Generates [`BTreeSet`]s of a given size with elements from a single iterator.
1296///
1297/// The source iterator should not repeat any elements, but this is not enforced.
1298///
1299/// If $k$ is 0, the output length is 1.
1300///
1301/// If $k$ is nonzero and the input iterator is infinite, the output length is also infinite.
1302///
1303/// If $k$ is nonzero and the input iterator length is $n$, the output length is $\binom{n}{k}$.
1304///
1305/// If $k$ is 0, the output consists of one empty [`BTreeSet`].
1306///
1307/// If `xs` is empty, the output is also empty, unless $k$ is 0.
1308///
1309/// # Examples
1310/// ```
1311/// use itertools::Itertools;
1312/// use malachite_base::sets::exhaustive::exhaustive_b_tree_sets_fixed_length;
1313/// use maplit::btreeset;
1314///
1315/// let xss = exhaustive_b_tree_sets_fixed_length(4, 1..=6).collect_vec();
1316/// assert_eq!(
1317///     xss,
1318///     &[
1319///         btreeset! {1, 2, 3, 4},
1320///         btreeset! {1, 2, 3, 5},
1321///         btreeset! {1, 2, 4, 5},
1322///         btreeset! {1, 3, 4, 5},
1323///         btreeset! {2, 3, 4, 5},
1324///         btreeset! {1, 2, 3, 6},
1325///         btreeset! {1, 2, 4, 6},
1326///         btreeset! {1, 3, 4, 6},
1327///         btreeset! {2, 3, 4, 6},
1328///         btreeset! {1, 2, 5, 6},
1329///         btreeset! {1, 3, 5, 6},
1330///         btreeset! {2, 3, 5, 6},
1331///         btreeset! {1, 4, 5, 6},
1332///         btreeset! {2, 4, 5, 6},
1333///         btreeset! {3, 4, 5, 6}
1334///     ]
1335/// );
1336/// ```
1337#[inline]
1338pub fn exhaustive_b_tree_sets_fixed_length<I: Clone + Iterator>(
1339    k: u64,
1340    xs: I,
1341) -> ExhaustiveOrderedUniqueCollections<I, BTreeSet<I::Item>>
1342where
1343    I::Item: Clone + Ord,
1344{
1345    exhaustive_b_tree_sets_length_inclusive_range(k, k, xs)
1346}
1347
1348/// Generates [`BTreeSet`]s with elements from a single iterator.
1349///
1350/// The source iterator should not repeat any elements, but this is not enforced.
1351///
1352/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1353/// generated.
1354///
1355/// If the input iterator is infinite, the output length is also infinite.
1356///
1357/// If the input iterator length is $n$, the output length is $2^n$.
1358///
1359/// If `xs` is empty, the output consists of a single empty [`BTreeSet`].
1360///
1361/// # Examples
1362/// ```
1363/// use itertools::Itertools;
1364/// use malachite_base::sets::exhaustive::exhaustive_b_tree_sets;
1365/// use maplit::btreeset;
1366///
1367/// let xss = exhaustive_b_tree_sets(1..=4).collect_vec();
1368/// assert_eq!(
1369///     xss,
1370///     &[
1371///         btreeset! {},
1372///         btreeset! {1},
1373///         btreeset! {2},
1374///         btreeset! {1, 2},
1375///         btreeset! {3},
1376///         btreeset! {1, 3},
1377///         btreeset! {2, 3},
1378///         btreeset! {1, 2, 3},
1379///         btreeset! {4},
1380///         btreeset! {1, 4},
1381///         btreeset! {2, 4},
1382///         btreeset! {1, 2, 4},
1383///         btreeset! {3, 4},
1384///         btreeset! {1, 3, 4},
1385///         btreeset! {2, 3, 4},
1386///         btreeset! {1, 2, 3, 4}
1387///     ]
1388/// );
1389/// ```
1390#[inline]
1391pub fn exhaustive_b_tree_sets<I: Clone + Iterator>(
1392    xs: I,
1393) -> ExhaustiveOrderedUniqueCollections<I, BTreeSet<I::Item>>
1394where
1395    I::Item: Clone + Ord,
1396{
1397    exhaustive_b_tree_sets_length_inclusive_range(0, u64::MAX, xs)
1398}
1399
1400/// Generates [`BTreeSet`]s with a mininum length, with elements from a single iterator.
1401///
1402/// The [`BTreeSet`]s are ordered lexicographically with respect to the order of the element
1403/// iterator.
1404///
1405/// The source iterator should not repeat any elements, but this is not enforced.
1406///
1407/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1408/// generated.
1409///
1410/// If the input iterator is infinite, the output length is also infinite.
1411///
1412/// If the input iterator length is $n$ and the `min_length` is $\ell$, the output length is
1413/// $$
1414/// \sum_{i=\ell}^n \binom{n}{i}.
1415/// $$
1416///
1417/// # Examples
1418/// ```
1419/// use itertools::Itertools;
1420/// use malachite_base::sets::exhaustive::exhaustive_b_tree_sets_min_length;
1421/// use maplit::btreeset;
1422///
1423/// let xss = exhaustive_b_tree_sets_min_length(2, 1..=4).collect_vec();
1424/// assert_eq!(
1425///     xss,
1426///     &[
1427///         btreeset! {1, 2},
1428///         btreeset! {1, 3},
1429///         btreeset! {2, 3},
1430///         btreeset! {1, 2, 3},
1431///         btreeset! {1, 4},
1432///         btreeset! {2, 4},
1433///         btreeset! {1, 2, 4},
1434///         btreeset! {3, 4},
1435///         btreeset! {1, 3, 4},
1436///         btreeset! {2, 3, 4},
1437///         btreeset! {1, 2, 3, 4}
1438///     ]
1439/// );
1440/// ```
1441#[inline]
1442pub fn exhaustive_b_tree_sets_min_length<I: Clone + Iterator>(
1443    min_length: u64,
1444    xs: I,
1445) -> ExhaustiveOrderedUniqueCollections<I, BTreeSet<I::Item>>
1446where
1447    I::Item: Clone + Ord,
1448{
1449    exhaustive_b_tree_sets_length_inclusive_range(min_length, u64::MAX, xs)
1450}
1451
1452/// Generates [`BTreeSet`]s, with lengths in a range $[a, b)$, with elements from a single iterator.
1453///
1454/// The source iterator should not repeat any elements, but this is not enforced.
1455///
1456/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1457/// generated.
1458///
1459/// If $a \leq b$, the output is empty.
1460///
1461/// If $a = 0$ and $b = 1$, the output consists of a single empty [`BTreeSet`].
1462///
1463/// If the input iterator is infinite and $0 < a < b$, the output length is also infinite.
1464///
1465/// If the input iterator length is $n$, the output length is
1466/// $$
1467/// \sum_{i=a}^b - 1 \binom{n}{i}.
1468/// $$
1469///
1470/// # Examples
1471/// ```
1472/// use itertools::Itertools;
1473/// use malachite_base::sets::exhaustive::exhaustive_b_tree_sets_length_range;
1474/// use maplit::btreeset;
1475///
1476/// let xss = exhaustive_b_tree_sets_length_range(2, 4, 1..=4).collect_vec();
1477/// assert_eq!(
1478///     xss,
1479///     &[
1480///         btreeset! {1, 2},
1481///         btreeset! {1, 3},
1482///         btreeset! {2, 3},
1483///         btreeset! {1, 2, 3},
1484///         btreeset! {1, 4},
1485///         btreeset! {2, 4},
1486///         btreeset! {1, 2, 4},
1487///         btreeset! {3, 4},
1488///         btreeset! {1, 3, 4},
1489///         btreeset! {2, 3, 4}
1490///     ]
1491/// );
1492/// ```
1493#[inline]
1494pub fn exhaustive_b_tree_sets_length_range<I: Clone + Iterator>(
1495    a: u64,
1496    b: u64,
1497    xs: I,
1498) -> ExhaustiveOrderedUniqueCollections<I, BTreeSet<I::Item>>
1499where
1500    I::Item: Clone + Ord,
1501{
1502    if a >= b {
1503        ExhaustiveOrderedUniqueCollections::None
1504    } else {
1505        exhaustive_b_tree_sets_length_inclusive_range(a, b - 1, xs)
1506    }
1507}
1508
1509/// Generates [`BTreeSet`]s, with lengths in a range $[a, b]$, with elements from a single iterator.
1510///
1511/// The source iterator should not repeat any elements, but this is not enforced.
1512///
1513/// The iterator should be finite; if it is infinite, only prefixes of the iterator will be
1514/// generated.
1515///
1516/// If $a < b$, the output is empty.
1517///
1518/// If $a = b = 0$, the output consists of a single empty [`BTreeSet`].
1519///
1520/// If the input iterator is infinite and $0 < a \leq b$, the output length is also infinite.
1521///
1522/// If the input iterator length is $n$, the output length is
1523/// $$
1524/// \sum_{i=a}^b \binom{n}{i}.
1525/// $$
1526///
1527/// # Examples
1528/// ```
1529/// use itertools::Itertools;
1530/// use malachite_base::sets::exhaustive::exhaustive_b_tree_sets_length_inclusive_range;
1531/// use maplit::btreeset;
1532///
1533/// let xss = exhaustive_b_tree_sets_length_inclusive_range(2, 3, 1..=4).collect_vec();
1534/// assert_eq!(
1535///     xss,
1536///     &[
1537///         btreeset! {1, 2},
1538///         btreeset! {1, 3},
1539///         btreeset! {2, 3},
1540///         btreeset! {1, 2, 3},
1541///         btreeset! {1, 4},
1542///         btreeset! {2, 4},
1543///         btreeset! {1, 2, 4},
1544///         btreeset! {3, 4},
1545///         btreeset! {1, 3, 4},
1546///         btreeset! {2, 3, 4}
1547///     ]
1548/// );
1549/// ```
1550#[inline]
1551pub fn exhaustive_b_tree_sets_length_inclusive_range<I: Clone + Iterator>(
1552    a: u64,
1553    b: u64,
1554    xs: I,
1555) -> ExhaustiveOrderedUniqueCollections<I, BTreeSet<I::Item>>
1556where
1557    I::Item: Clone + Ord,
1558{
1559    ExhaustiveOrderedUniqueCollections::new(a, b, xs)
1560}