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
/// Implementations of [`Digits`](malachite_base::num::conversion::traits::Digits), a trait for
/// extracting digits from [`Natural`](crate::natural::Natural)s and constructing
/// [`Natural`](crate::natural::Natural)s from digits.
///
/// # to_digits_asc
/// ```
/// use malachite_base::num::basic::traits::{Two, Zero};
/// use malachite_base::num::conversion::traits::Digits;
/// use malachite_nz::natural::Natural;
///
/// assert!(Natural::ZERO.to_digits_asc(&6u64).is_empty());
/// assert_eq!(Natural::TWO.to_digits_asc(&6u32), &[2]);
/// assert_eq!(Natural::from(123456u32).to_digits_asc(&3u16), &[0, 1, 1, 0, 0, 1, 1, 2, 0, 0, 2]);
/// ```
///
/// # to_digits_desc
/// ```
/// use malachite_base::num::basic::traits::{Two, Zero};
/// use malachite_base::num::conversion::traits::Digits;
/// use malachite_nz::natural::Natural;
///
/// assert!(Natural::ZERO.to_digits_desc(&6u64).is_empty());
/// assert_eq!(Natural::TWO.to_digits_desc(&6u32), &[2]);
/// assert_eq!(Natural::from(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;
/// use malachite_base::strings::ToDebugString;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// Natural::from_digits_asc(&64u64, [0, 0, 0].iter().cloned()).to_debug_string(),
/// "Some(0)"
/// );
/// assert_eq!(
/// Natural::from_digits_asc(&3u64, [0, 1, 1, 0, 0, 1, 1, 2, 0, 0, 2].iter().cloned())
/// .to_debug_string(),
/// "Some(123456)"
/// );
/// assert_eq!(
/// Natural::from_digits_asc(&8u16, [3, 7, 1].iter().cloned()).to_debug_string(),
/// "Some(123)"
/// );
/// assert_eq!(
/// Natural::from_digits_asc(&8u16, [3, 10, 1].iter().cloned()).to_debug_string(),
/// "None"
/// );
/// ```
///
/// # from_digits_desc
/// ```
/// use malachite_base::num::conversion::traits::Digits;
/// use malachite_base::strings::ToDebugString;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// Natural::from_digits_desc(&64u64, [0, 0, 0].iter().cloned()).to_debug_string(),
/// "Some(0)"
/// );
/// assert_eq!(
/// Natural::from_digits_desc(&3u64, [2, 0, 0, 2, 1, 1, 0, 0, 1, 1, 0].iter().cloned())
/// .to_debug_string(),
/// "Some(123456)"
/// );
/// assert_eq!(
/// Natural::from_digits_desc(&8u16, [1, 7, 3].iter().cloned()).to_debug_string(),
/// "Some(123)"
/// );
/// assert_eq!(
/// Natural::from_digits_desc(&8u16, [3, 10, 1].iter().cloned()).to_debug_string(),
/// "None"
/// );
/// ```
pub mod general_digits;
/// An implementation of
/// [`PowerOf2DigitIterable`](malachite_base::num::conversion::traits::PowerOf2DigitIterable), a
/// trait for iterating over a [`Natural`](crate::natural::Natural)'s base-$2^k$ digits.
///
/// # power_of_2_digits
/// ```
/// use itertools::Itertools;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_base::num::conversion::traits::PowerOf2DigitIterable;
/// use malachite_nz::natural::Natural;
///
/// let n = Natural::ZERO;
/// assert!(PowerOf2DigitIterable::<u8>::power_of_2_digits(&n, 2).next().is_none());
///
/// // 107 = 1223_4
/// let n = Natural::from(107u32);
/// assert_eq!(
/// PowerOf2DigitIterable::<u32>::power_of_2_digits(&n, 2).collect_vec(),
/// vec![3, 2, 2, 1]
/// );
///
/// let n = Natural::ZERO;
/// assert!(PowerOf2DigitIterable::<u8>::power_of_2_digits(&n, 2).next_back().is_none());
///
/// // 107 = 1223_4
/// let n = Natural::from(107u32);
/// assert_eq!(
/// PowerOf2DigitIterable::<u32>::power_of_2_digits(&n, 2).rev().collect_vec(),
/// vec![1, 2, 2, 3]
/// );
/// ```
pub mod power_of_2_digit_iterable;
/// Implementations of [`PowerOf2Digits`](malachite_base::num::conversion::traits::PowerOf2Digits),
/// a trait for extracting base-$2^k$ digits from [`Natural`](crate::natural::Natural)s and
/// constructing [`Natural`](crate::natural::Natural)s from such digits.
///
/// # to_power_of_2_digits_asc
/// ```
/// use malachite_base::num::basic::traits::{Two, Zero};
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&Natural::ZERO, 6),
/// Vec::<u64>::new()
/// );
/// assert_eq!(PowerOf2Digits::<u64>::to_power_of_2_digits_asc(&Natural::TWO, 6), vec![2]);
///
/// // 123_10 = 173_8
/// assert_eq!(
/// PowerOf2Digits::<u16>::to_power_of_2_digits_asc(&Natural::from(123u32), 3),
/// vec![3, 7, 1]
/// );
/// ```
///
/// # to_power_of_2_digits_desc
/// ```
/// use malachite_base::num::basic::traits::{Two, Zero};
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&Natural::ZERO, 6),
/// Vec::<u64>::new()
/// );
/// assert_eq!(PowerOf2Digits::<u64>::to_power_of_2_digits_desc(&Natural::TWO, 6), vec![2]);
///
/// // 123_10 = 173_8
/// assert_eq!(
/// PowerOf2Digits::<u16>::to_power_of_2_digits_desc(&Natural::from(123u32), 3),
/// vec![1, 7, 3]
/// );
/// ```
///
/// # from_power_of_2_digits_asc
/// ```
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
/// use malachite_base::strings::ToDebugString;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// Natural::from_power_of_2_digits_asc(6, [0u64, 0, 0].iter().cloned()).to_debug_string(),
/// "Some(0)"
/// );
/// assert_eq!(
/// Natural::from_power_of_2_digits_asc(6, [2u64, 0].iter().cloned()).to_debug_string(),
/// "Some(2)"
/// );
/// assert_eq!(
/// Natural::from_power_of_2_digits_asc(3, [3u16, 7, 1].iter().cloned()).to_debug_string(),
/// "Some(123)"
/// );
/// assert_eq!(
/// Natural::from_power_of_2_digits_asc(3, [100u8].iter().cloned()).to_debug_string(),
/// "None"
/// );
/// ```
///
/// # from_power_of_2_digits_desc
/// ```
/// use malachite_base::num::conversion::traits::PowerOf2Digits;
/// use malachite_base::strings::ToDebugString;
/// use malachite_nz::natural::Natural;
///
/// assert_eq!(
/// Natural::from_power_of_2_digits_desc(6, [0u64, 0, 0].iter().cloned()).to_debug_string(),
/// "Some(0)"
/// );
/// assert_eq!(
/// Natural::from_power_of_2_digits_desc(6, [0u64, 2].iter().cloned()).to_debug_string(),
/// "Some(2)"
/// );
/// assert_eq!(
/// Natural::from_power_of_2_digits_desc(3, [1u16, 7, 3].iter().cloned()).to_debug_string(),
/// "Some(123)"
/// );
/// assert_eq!(
/// Natural::from_power_of_2_digits_desc(3, [100u8].iter().cloned()).to_debug_string(),
/// "None"
/// );
/// ```
pub mod power_of_2_digits;