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
// 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/>.

/// [`Digits`](super::traits::Digits), a trait for extracting digits from numbers and constructing
/// numbers from digits.
///
/// # to_digits_asc
/// ```
/// use malachite_base::num::conversion::traits::Digits;
///
/// assert_eq!(0u8.to_digits_asc(&6u64), &[]);
/// assert_eq!(2u16.to_digits_asc(&6u32), &[2]);
/// assert_eq!(
///     123456u32.to_digits_asc(&3u16),
///     &[0, 1, 1, 0, 0, 1, 1, 2, 0, 0, 2]
/// );
/// ```
///
/// # to_digits_desc
/// ```
/// use malachite_base::num::conversion::traits::Digits;
///
/// assert_eq!(0u8.to_digits_asc(&6u64), &[]);
/// assert_eq!(2u16.to_digits_asc(&6u32), &[2]);
/// assert_eq!(
///     123456u32.to_digits_desc(&3u16),
///     &[2, 0, 0, 2, 1, 1, 0, 0, 1, 1, 0]
/// );
/// ```
///
/// # from_digits_asc
/// ```
/// use malachite_base::num::conversion::traits::Digits;
///
/// assert_eq!(
///     u8::from_digits_asc(&64, [0u64, 0, 0].iter().cloned()),
///     Some(0)
/// );
/// assert_eq!(
///     u32::from_digits_asc(&3, [0u64, 1, 1, 0, 0, 1, 1, 2, 0, 0, 2].iter().cloned()),
///     Some(123456)
/// );
/// assert_eq!(
///     u32::from_digits_asc(&8, [3u16, 7, 1].iter().cloned()),
///     Some(123)
/// );
///
/// assert!(u64::from_digits_asc(&64, [1u8; 1000].iter().cloned()).is_none());
/// assert!(u64::from_digits_asc(&2, [2u8].iter().cloned()).is_none());
/// assert!(u8::from_digits_asc(&1000, [1u16, 2, 3].iter().cloned()).is_none());
/// ```
///
/// # from_digits_desc
/// ```
/// use malachite_base::num::conversion::traits::Digits;
///
/// assert_eq!(
///     u8::from_digits_desc(&64, [0u64, 0, 0].iter().cloned()),
///     Some(0)
/// );
/// assert_eq!(
///     u32::from_digits_desc(&3, [2u64, 0, 0, 2, 1, 1, 0, 0, 1, 1, 0].iter().cloned()),
///     Some(123456)
/// );
/// assert_eq!(
///     u32::from_digits_desc(&8, [1u16, 7, 3].iter().cloned()),
///     Some(123)
/// );
///
/// assert!(u64::from_digits_desc(&64, [1u8; 1000].iter().cloned()).is_none());
/// assert!(u64::from_digits_desc(&2, [2u8].iter().cloned()).is_none());
/// assert!(u8::from_digits_desc(&1000, [1u16, 2, 3].iter().cloned()).is_none());
/// ```
pub mod general_digits;
/// [`PowerOf2DigitIterable`](super::traits::PowerOf2DigitIterable), a trait for producing
/// [`PrimitivePowerOf2DigitIterator`](power_of_2_digit_iterable::PrimitivePowerOf2DigitIterator), a
/// double-ended iterator for iterating over a number's base-$2^k$ digits.
///
/// # power_of_2_digits
/// ```
/// use itertools::Itertools;
/// use malachite_base::num::conversion::traits::PowerOf2DigitIterable;
///
/// let mut digits = PowerOf2DigitIterable::<u8>::power_of_2_digits(0u8, 2);
/// assert!(digits.next().is_none());
///
/// // 107 = 1101011b
/// let digits = PowerOf2DigitIterable::<u8>::power_of_2_digits(107u32, 2);
/// assert_eq!(digits.collect_vec(), &[3, 2, 2, 1]);
///
/// let mut digits = PowerOf2DigitIterable::<u8>::power_of_2_digits(0u8, 2);
/// assert!(digits.next_back().is_none());
///
/// // 107 = 1101011b
/// let digits = PowerOf2DigitIterable::<u8>::power_of_2_digits(107u32, 2);
/// assert_eq!(digits.rev().collect_vec(), &[1, 2, 2, 3]);
/// ```
pub mod power_of_2_digit_iterable;
/// [`PowerOf2Digits`](super::traits::PowerOf2Digits), a trait for extracting base-$2^k$ $digits
/// from numbers and constructing numbers from digits.
///
/// # to_power_of_2_digits_asc
/// ```
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
///
/// assert_eq!(
///     PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&0u8, 6),
///     &[]
/// );
/// assert_eq!(
///     PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&2u16, 6),
///     &[2]
/// );
/// // 123_10 = 173_8
/// assert_eq!(
///     PowerOf2Digits::<u16>::to_power_of_2_digits_asc(&123u32, 3),
///     &[3, 7, 1]
/// );
/// ```
///
/// # to_power_of_2_digits_desc
/// ```
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
///
/// assert_eq!(
///     PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&0u8, 6),
///     &[]
/// );
/// assert_eq!(
///     PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&2u16, 6),
///     &[2]
/// );
/// // 123_10 = 173_8
/// assert_eq!(
///     PowerOf2Digits::<u16>::to_power_of_2_digits_desc(&123u32, 3),
///     &[1, 7, 3]
/// );
/// ```
///
/// # from_power_of_2_digits_asc
/// ```
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
///
/// assert_eq!(
///     u8::from_power_of_2_digits_asc(6, [0u64, 0, 0].iter().cloned()),
///     Some(0)
/// );
/// assert_eq!(
///     u16::from_power_of_2_digits_asc(6, [2u64, 0].iter().cloned()),
///     Some(2)
/// );
/// assert_eq!(
///     u32::from_power_of_2_digits_asc(3, [3u16, 7, 1].iter().cloned()),
///     Some(123)
/// );
///
/// assert!(u8::from_power_of_2_digits_asc(4, [1u64; 100].iter().cloned()).is_none());
/// assert!(u8::from_power_of_2_digits_asc(1, [2u64].iter().cloned()).is_none());
/// ```
///
/// # from_power_of_2_digits_desc
/// ```
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
///
/// assert_eq!(
///     u8::from_power_of_2_digits_desc(6, [0u64, 0, 0].iter().cloned()),
///     Some(0)
/// );
/// assert_eq!(
///     u16::from_power_of_2_digits_desc(6, [0u64, 2].iter().cloned()),
///     Some(2)
/// );
/// assert_eq!(
///     u32::from_power_of_2_digits_desc(3, [1u16, 7, 3].iter().cloned()),
///     Some(123)
/// );
///
/// assert!(u8::from_power_of_2_digits_desc(4, [1u64; 100].iter().cloned()).is_none());
/// assert!(u8::from_power_of_2_digits_desc(1, [2u64].iter().cloned()).is_none());
/// ```
pub mod power_of_2_digits;