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
use crate::integer::conversion::to_twos_complement_limbs::{
limbs_twos_complement, limbs_twos_complement_in_place,
};
use crate::integer::Integer;
use crate::natural::Natural;
use crate::platform::Limb;
use alloc::vec::Vec;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::Zero;
impl Integer {
/// Converts a slice of [limbs](crate#limbs) to an [`Integer`], in ascending order, so that less
/// significant limbs have lower indices in the input slice.
///
/// The limbs are in two's complement, and the most significant bit of the limbs indicates the
/// sign; if the bit is zero, the [`Integer`] is non-negative, and if the bit is one it is
/// negative. If the slice is empty, zero is returned.
///
/// This function borrows a slice. If taking ownership of a [`Vec`] is possible instead,
/// [`from_owned_twos_complement_limbs_asc`](`Self::from_owned_twos_complement_limbs_asc`) is
/// more efficient.
///
/// This function is more efficient than
/// [`from_twos_complement_limbs_desc`](`Self::from_twos_complement_limbs_desc`).
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::integers::PrimitiveInt;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::platform::Limb;
///
/// if Limb::WIDTH == u32::WIDTH {
/// assert_eq!(Integer::from_twos_complement_limbs_asc(&[]), 0);
/// assert_eq!(Integer::from_twos_complement_limbs_asc(&[123]), 123);
/// assert_eq!(Integer::from_twos_complement_limbs_asc(&[4294967173]), -123);
/// // 10^12 = 232 * 2^32 + 3567587328
/// assert_eq!(
/// Integer::from_twos_complement_limbs_asc(&[3567587328, 232]),
/// 1000000000000u64
/// );
/// assert_eq!(
/// Integer::from_twos_complement_limbs_asc(&[727379968, 4294967063]),
/// -1000000000000i64
/// );
/// }
/// ```
pub fn from_twos_complement_limbs_asc(xs: &[Limb]) -> Integer {
match xs {
&[] => Integer::ZERO,
&[.., last] if !last.get_highest_bit() => Integer::from(Natural::from_limbs_asc(xs)),
xs => -Natural::from_owned_limbs_asc(limbs_twos_complement(xs)),
}
}
/// Converts a slice of [limbs](crate#limbs) to an [`Integer`], in descending order, so that
/// less significant limbs have higher indices in the input slice.
///
/// The limbs are in two's complement, and the most significant bit of the limbs indicates the
/// sign; if the bit is zero, the [`Integer`] is non-negative, and if the bit is one it is
/// negative. If the slice is empty, zero is returned.
///
/// This function borrows a slice. If taking ownership of a [`Vec`] is possible instead,
/// [`from_owned_twos_complement_limbs_desc`](`Self::from_owned_twos_complement_limbs_desc`) is
/// more efficient.
///
/// This function is less efficient than
/// [`from_twos_complement_limbs_asc`](`Self::from_twos_complement_limbs_asc`).
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::integers::PrimitiveInt;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::platform::Limb;
///
/// if Limb::WIDTH == u32::WIDTH {
/// assert_eq!(Integer::from_twos_complement_limbs_desc(&[]), 0);
/// assert_eq!(Integer::from_twos_complement_limbs_desc(&[123]), 123);
/// assert_eq!(Integer::from_twos_complement_limbs_desc(&[4294967173]), -123);
/// // 10^12 = 232 * 2^32 + 3567587328
/// assert_eq!(
/// Integer::from_twos_complement_limbs_desc(&[232, 3567587328]),
/// 1000000000000u64
/// );
/// assert_eq!(
/// Integer::from_twos_complement_limbs_desc(&[4294967063, 727379968]),
/// -1000000000000i64
/// );
/// }
/// ```
pub fn from_twos_complement_limbs_desc(xs: &[Limb]) -> Integer {
Integer::from_owned_twos_complement_limbs_asc(xs.iter().cloned().rev().collect())
}
/// Converts a slice of [limbs](crate#limbs) to an [`Integer`], in ascending order, so that less
/// significant limbs have lower indices in the input slice.
///
/// The limbs are in two's complement, and the most significant bit of the limbs indicates the
/// sign; if the bit is zero, the [`Integer`] is non-negative, and if the bit is one it is
/// negative. If the slice is empty, zero is returned.
///
/// This function takes ownership of a [`Vec`]. If it's necessary to borrow a slice instead, use
/// [`from_twos_complement_limbs_asc`](`Self::from_twos_complement_limbs_asc`)
///
/// This function is more efficient than
/// [`from_owned_twos_complement_limbs_desc`](`Self::from_owned_twos_complement_limbs_desc`).
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::integers::PrimitiveInt;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::platform::Limb;
///
/// if Limb::WIDTH == u32::WIDTH {
/// assert_eq!(Integer::from_owned_twos_complement_limbs_asc(vec![]), 0);
/// assert_eq!(Integer::from_owned_twos_complement_limbs_asc(vec![123]), 123);
/// assert_eq!(Integer::from_owned_twos_complement_limbs_asc(vec![4294967173]), -123);
/// // 10^12 = 232 * 2^32 + 3567587328
/// assert_eq!(
/// Integer::from_owned_twos_complement_limbs_asc(vec![3567587328, 232]),
/// 1000000000000i64
/// );
/// assert_eq!(
/// Integer::from_owned_twos_complement_limbs_asc(vec![727379968, 4294967063]),
/// -1000000000000i64
/// );
/// }
/// ```
pub fn from_owned_twos_complement_limbs_asc(mut xs: Vec<Limb>) -> Integer {
match *xs.as_slice() {
[] => Integer::ZERO,
[.., last] if !last.get_highest_bit() => {
Integer::from(Natural::from_owned_limbs_asc(xs))
}
_ => {
assert!(!limbs_twos_complement_in_place(&mut xs));
-Natural::from_owned_limbs_asc(xs)
}
}
}
/// Converts a slice of [limbs](crate#limbs) to an [`Integer`], in descending order, so that
/// less significant limbs have higher indices in the input slice.
///
/// The limbs are in two's complement, and the most significant bit of the limbs indicates the
/// sign; if the bit is zero, the [`Integer`] is non-negative, and if the bit is one it is
/// negative. If the slice is empty, zero is returned.
///
/// This function takes ownership of a [`Vec`]. If it's necessary to borrow a slice instead, use
/// [`from_twos_complement_limbs_desc`](`Self::from_twos_complement_limbs_desc`).
///
/// This function is less efficient than
/// [`from_owned_twos_complement_limbs_asc`](`Self::from_owned_twos_complement_limbs_asc`).
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::integers::PrimitiveInt;
/// use malachite_nz::integer::Integer;
/// use malachite_nz::platform::Limb;
///
/// if Limb::WIDTH == u32::WIDTH {
/// assert_eq!(Integer::from_owned_twos_complement_limbs_desc(vec![]), 0);
/// assert_eq!(Integer::from_owned_twos_complement_limbs_desc(vec![123]), 123);
/// assert_eq!(Integer::from_owned_twos_complement_limbs_desc(vec![4294967173]), -123);
/// // 10^12 = 232 * 2^32 + 3567587328
/// assert_eq!(
/// Integer::from_owned_twos_complement_limbs_desc(vec![232, 3567587328]),
/// 1000000000000i64
/// );
/// assert_eq!(
/// Integer::from_owned_twos_complement_limbs_desc(vec![4294967063, 727379968]),
/// -1000000000000i64
/// );
/// }
/// ```
pub fn from_owned_twos_complement_limbs_desc(mut xs: Vec<Limb>) -> Integer {
xs.reverse();
Integer::from_owned_twos_complement_limbs_asc(xs)
}
}