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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
// 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/>.
use crate::num::arithmetic::traits::ShrRound;
use crate::num::basic::integers::PrimitiveInt;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::num::conversion::traits::{
FromOtherTypeSlice, SplitInHalf, VecFromOtherType, VecFromOtherTypeSlice, WrappingFrom,
};
use crate::rounding_modes::RoundingMode::*;
use alloc::vec;
use alloc::vec::Vec;
const fn from_other_type_slice_ident<T: PrimitiveUnsigned>(xs: &[T]) -> T {
if xs.is_empty() {
T::ZERO
} else {
xs[0]
}
}
macro_rules! impl_slice_traits_ident {
($a:ty) => {
impl FromOtherTypeSlice<$a> for $a {
/// Converts a slice of one type of value to a single value of the same type.
///
/// $$
/// f((x_k)_{k=0}^{n-1}) = \\begin{cases}
/// 0 & \text{if} \\quad n = 0, \\\\
/// x_0 & \\text{otherwise},
/// \\end{cases}
/// $$
/// where $W$ is the width of the type.
///
/// The slice is interpreted as the base-$2^W$ digits of the value, in ascending order,
/// where $W$ is the width of the type. If there's more than one element in the input
/// slice, the value wraps and all elements past the first are ignored. This means that
/// if the slice is empty, the output value is 0; otherwise, it's the first element of
/// the slice.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::slice#from_other_type_slice).
#[inline]
fn from_other_type_slice(xs: &[$a]) -> Self {
from_other_type_slice_ident(xs)
}
}
impl VecFromOtherTypeSlice<$a> for $a {
/// Converts a slice of one type of value to a [`Vec`] of the same type.
///
/// In this case, it just converts the slice to a [`Vec`] the usual way.
///
/// # 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
/// See [here](super::slice#vec_from_other_type_slice).
#[inline]
fn vec_from_other_type_slice(xs: &[$a]) -> Vec<Self> {
xs.to_vec()
}
}
impl VecFromOtherType<$a> for $a {
/// Converts a value of one type to a [`Vec`] of the same type.
///
/// In this case, it just creates a one-element [`Vec`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::slice#vec_from_other_type).
#[inline]
fn vec_from_other_type(x: $a) -> Vec<Self> {
::alloc::vec![x]
}
}
};
}
fn from_other_type_slice_large_to_small<
A: PrimitiveUnsigned,
B: PrimitiveUnsigned + WrappingFrom<A>,
>(
xs: &[A],
) -> B {
if xs.is_empty() {
B::ZERO
} else {
B::wrapping_from(xs[0])
}
}
fn vec_from_other_type_slice_large_to_small<
A: PrimitiveUnsigned,
B: PrimitiveUnsigned + WrappingFrom<A>,
>(
xs: &[A],
) -> Vec<B> {
let log_size_ratio = A::LOG_WIDTH - B::LOG_WIDTH;
let mut out = ::alloc::vec![B::ZERO; xs.len() << log_size_ratio];
for (chunk, &u) in out.chunks_exact_mut(1 << log_size_ratio).zip(xs.iter()) {
let mut u = u;
for x in chunk {
*x = B::wrapping_from(u);
u >>= B::WIDTH;
}
}
out
}
fn vec_from_other_type_large_to_small<
A: PrimitiveUnsigned,
B: PrimitiveUnsigned + WrappingFrom<A>,
>(
mut x: A,
) -> Vec<B> {
let mut xs = ::alloc::vec![B::ZERO; 1 << (A::LOG_WIDTH - B::LOG_WIDTH)];
for out in &mut xs {
*out = B::wrapping_from(x);
x >>= B::WIDTH;
}
xs
}
macro_rules! impl_slice_traits_large_to_small {
($a:ident, $b:ident) => {
impl FromOtherTypeSlice<$a> for $b {
/// Converts a slice of one type of unsigned integer to a single value of a smaller
/// unsigned type.
///
/// $$
/// f((x_k)_{k=0}^{n-1}) = \\begin{cases}
/// 0 & \text{if} \\quad n = 0, \\\\
/// y & \\text{otherwise},
/// \\end{cases}
/// $$
/// where $0 \leq y < 2^W$, $x = y + k2^W$ for some integer $k$, and $W$ is the width of
/// the output type.
///
/// The slice is interpreted as the base-$2^W$ digits of the value, in ascending order,
/// where $W$ is the width of the type. If the slice is empty, the output value is 0;
/// otherwise, it consists of the least-significant bits of the first element of the
/// slice.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::slice#from_other_type_slice).
#[inline]
fn from_other_type_slice(xs: &[$a]) -> Self {
from_other_type_slice_large_to_small(xs)
}
}
impl VecFromOtherTypeSlice<$a> for $b {
/// Converts a slice of one type of unsigned integer to a [`Vec`] of a smaller unsigned
/// type.
///
/// Each value of the input slice will be broken up into several values in the output
/// [`Vec`].
///
/// Let $V$ be the the width of the input type and $W$ the width of the output type.
///
/// $f((x_k)_ {k=0}^{n-1}) = (y_k)_ {k=0}^{m-1}$, where
///
/// $$
/// \sum_{j=0}^{n-1}2^{jV}x_j = \sum_{j=0}^{m-1}2^{jW}y_j,
/// $$
///
/// $y_j < 2^W$ for all $j$, and $m = 2^{V-W}n$.
///
/// # 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
/// See [here](super::slice#vec_from_other_type_slice).
#[inline]
fn vec_from_other_type_slice(xs: &[$a]) -> Vec<Self> {
vec_from_other_type_slice_large_to_small(xs)
}
}
impl VecFromOtherType<$a> for $b {
/// Converts a value of one type of unsigned integer to a [`Vec`] of a smaller unsigned
/// type.
///
/// The input value will be broken up into several values in the output [`Vec`].
///
/// $f(x) = (y_k)_{k=0}^{m-1}$, where $x = \sum_{j=0}^{m-1}2^{jW}y_j$ and $m =
/// 2^{V-W}n$.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::slice#vec_from_other_type).
#[inline]
fn vec_from_other_type(x: $a) -> Vec<Self> {
vec_from_other_type_large_to_small(x)
}
}
};
}
fn from_other_type_slice_small_to_large<
A: PrimitiveUnsigned,
B: PrimitiveUnsigned + WrappingFrom<A>,
>(
xs: &[A],
) -> B {
let mut result = B::ZERO;
let mut offset = 0;
for &u in xs.iter().take(1 << (B::LOG_WIDTH - A::LOG_WIDTH)) {
result |= B::wrapping_from(u) << offset;
offset += A::WIDTH;
}
result
}
fn vec_from_other_type_slice_small_to_large<
A: PrimitiveUnsigned,
B: PrimitiveUnsigned + WrappingFrom<A>,
>(
xs: &[A],
) -> Vec<B> {
let log_size_ratio = B::LOG_WIDTH - A::LOG_WIDTH;
let mut out = ::alloc::vec![B::ZERO; xs.len().shr_round(log_size_ratio, Ceiling).0];
for (x, chunk) in out.iter_mut().zip(xs.chunks(1 << log_size_ratio)) {
*x = from_other_type_slice_small_to_large(chunk);
}
out
}
fn vec_from_other_type_small_to_large<A, B: WrappingFrom<A>>(x: A) -> Vec<B> {
::alloc::vec![B::wrapping_from(x)]
}
macro_rules! impl_slice_traits_small_to_large {
($a:ident, $b:ident) => {
impl FromOtherTypeSlice<$a> for $b {
/// Converts a slice of one type of unsigned integer to a single value of a larger
/// unsigned type.
///
/// Let $V$ be the the width of the input type and $W$ the width of the output type.
///
/// $f((x_k)_{k=0}^{n-1}) = y$, where $y < 2^W$ and
///
/// $$
/// y = k2^W + \sum_{j=0}^{n-1}2^{jV}x_j
/// $$
///
/// for some integer $k$.
///
/// If the input slice contains more values than necessary to build an output value, the
/// trailing values are ignored. If the input slice contains too few values to build an
/// output value, the most-significant bits of the output value are set to 0.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::slice#from_other_type_slice).
#[inline]
fn from_other_type_slice(xs: &[$a]) -> Self {
from_other_type_slice_small_to_large(xs)
}
}
impl VecFromOtherTypeSlice<$a> for $b {
/// Converts a slice of one type of unsigned integer to a [`Vec`] of a larger unsigned
/// type.
///
/// Adjacent chunks of values in the input slice will be joined into values of the
/// output [`Vec`]. If the last few elements of the input slice don't make up a full
/// chunk, the most-significant bits of the last output value are set to 0.
///
/// Let $V$ be the the width of the input type and $W$ the width of the output type.
///
/// $f((x_k)_ {k=0}^{n-1}) = (y_k)_ {k=0}^{m-1}$, where
///
/// $$
/// \sum_{j=0}^{n-1}2^{jV}x_j = \sum_{j=0}^{m-1}2^{jW}y_j,
/// $$
///
/// $y_j < 2^W$ for all $j$, and $m = \lceil n / 2^{W-V} \rceil$.
///
/// # 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
/// See [here](super::slice#vec_from_other_type_slice).
#[inline]
fn vec_from_other_type_slice(xs: &[$a]) -> Vec<Self> {
vec_from_other_type_slice_small_to_large(xs)
}
}
impl VecFromOtherType<$a> for $b {
/// Converts a value of one type of unsigned integer to a [`Vec`] of a larger unsigned
/// type.
///
/// The output [`Vec`] only contains one value. The least-significant bits of the output
/// value contain the input value, and the most-significant bits are set to 0.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::slice#vec_from_other_type).
#[inline]
fn vec_from_other_type(x: $a) -> Vec<Self> {
vec_from_other_type_small_to_large(x)
}
}
};
}
apply_to_unsigneds!(impl_slice_traits_ident);
impl_slice_traits_large_to_small!(u16, u8);
impl_slice_traits_large_to_small!(u32, u8);
impl_slice_traits_large_to_small!(u32, u16);
impl_slice_traits_large_to_small!(u64, u8);
impl_slice_traits_large_to_small!(u64, u16);
impl_slice_traits_large_to_small!(u64, u32);
impl_slice_traits_large_to_small!(u128, u8);
impl_slice_traits_large_to_small!(u128, u16);
impl_slice_traits_large_to_small!(u128, u32);
impl_slice_traits_large_to_small!(u128, u64);
impl_slice_traits_large_to_small!(u128, usize);
impl_slice_traits_large_to_small!(usize, u8);
impl_slice_traits_large_to_small!(usize, u16);
impl_slice_traits_small_to_large!(u8, u16);
impl_slice_traits_small_to_large!(u8, u32);
impl_slice_traits_small_to_large!(u8, u64);
impl_slice_traits_small_to_large!(u8, u128);
impl_slice_traits_small_to_large!(u8, usize);
impl_slice_traits_small_to_large!(u16, u32);
impl_slice_traits_small_to_large!(u16, u64);
impl_slice_traits_small_to_large!(u16, u128);
impl_slice_traits_small_to_large!(u16, usize);
impl_slice_traits_small_to_large!(u32, u64);
impl_slice_traits_small_to_large!(u32, u128);
impl_slice_traits_small_to_large!(u64, u128);
impl_slice_traits_small_to_large!(usize, u128);
impl FromOtherTypeSlice<u32> for usize {
/// Converts a slice of `u32`s to a single `usize`.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#from_other_type_slice).
fn from_other_type_slice(xs: &[u32]) -> Self {
if usize::WIDTH == u32::WIDTH {
if xs.is_empty() {
0
} else {
usize::wrapping_from(xs[0])
}
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
let mut result = 0;
let mut offset = 0;
for &u in xs.iter().take(2) {
result |= usize::wrapping_from(u) << offset;
offset += u32::WIDTH;
}
result
}
}
}
impl VecFromOtherTypeSlice<u32> for usize {
/// Converts a slice of [`u32`]s to a [`Vec`] of [`usize`]s.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// See [here](super::slice#vec_from_other_type_slice).
fn vec_from_other_type_slice(xs: &[u32]) -> Vec<Self> {
let mut out;
if usize::WIDTH == u32::WIDTH {
out = vec![0; xs.len()];
for (x, &u) in out.iter_mut().zip(xs.iter()) {
*x = usize::wrapping_from(u);
}
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
out = vec![0; xs.len().shr_round(1, Ceiling).0];
for (x, chunk) in out.iter_mut().zip(xs.chunks(2)) {
*x = usize::from_other_type_slice(chunk);
}
}
out
}
}
impl VecFromOtherType<u32> for usize {
/// Converts a [`u32`] to a [`Vec`] of [`usize`]s.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#vec_from_other_type).
#[inline]
fn vec_from_other_type(x: u32) -> Vec<Self> {
vec![usize::wrapping_from(x)]
}
}
impl FromOtherTypeSlice<u64> for usize {
/// Converts a slice of [`u64`]s to a single [`usize`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#from_other_type_slice).
#[inline]
fn from_other_type_slice(xs: &[u64]) -> Self {
if xs.is_empty() {
0
} else {
usize::wrapping_from(xs[0])
}
}
}
impl VecFromOtherTypeSlice<u64> for usize {
/// Converts a slice of [`u64`]s to a [`Vec`] of [`usize`]s.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// See [here](super::slice#vec_from_other_type_slice).
#[allow(arithmetic_overflow)]
fn vec_from_other_type_slice(xs: &[u64]) -> Vec<Self> {
let mut out;
if usize::WIDTH == u32::WIDTH {
out = ::alloc::vec![0; xs.len() << 1];
for (chunk, &u) in out.chunks_exact_mut(2).zip(xs.iter()) {
let mut u = u;
for x in chunk {
*x = usize::wrapping_from(u);
u >>= usize::WIDTH;
}
}
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
out = ::alloc::vec![0; xs.len()];
for (x, &u) in out.iter_mut().zip(xs.iter()) {
*x = usize::wrapping_from(u);
}
}
out
}
}
impl VecFromOtherType<u64> for usize {
/// Converts a [`u64`] to a [`Vec`] of [`usize`]s.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#vec_from_other_type).
fn vec_from_other_type(x: u64) -> Vec<Self> {
if usize::WIDTH == u32::WIDTH {
let (upper, lower) = x.split_in_half();
::alloc::vec![usize::wrapping_from(lower), usize::wrapping_from(upper)]
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
::alloc::vec![usize::wrapping_from(x)]
}
}
}
impl FromOtherTypeSlice<usize> for u32 {
/// Converts a slice of [`usize`]s to a single [`u32`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#from_other_type_slice).
#[inline]
fn from_other_type_slice(xs: &[usize]) -> Self {
if xs.is_empty() {
0
} else {
u32::wrapping_from(xs[0])
}
}
}
impl VecFromOtherTypeSlice<usize> for u32 {
/// Converts a slice of [`usize`]s to a [`Vec`] of [`u32`]s.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// See [here](super::slice#vec_from_other_type_slice).
#[allow(arithmetic_overflow)]
fn vec_from_other_type_slice(xs: &[usize]) -> Vec<Self> {
let mut out;
if usize::WIDTH == u32::WIDTH {
out = ::alloc::vec![0; xs.len()];
for (x, &u) in out.iter_mut().zip(xs.iter()) {
*x = u32::wrapping_from(u);
}
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
out = ::alloc::vec![0; xs.len() << 1];
for (chunk, &u) in out.chunks_exact_mut(2).zip(xs.iter()) {
let mut u = u;
for x in chunk {
*x = u32::wrapping_from(u);
u >>= u32::WIDTH;
}
}
}
out
}
}
impl VecFromOtherType<usize> for u32 {
/// Converts a [`usize`] to a [`Vec`] of [`u32`]s.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#vec_from_other_type).
#[allow(arithmetic_overflow)]
fn vec_from_other_type(x: usize) -> Vec<Self> {
if usize::WIDTH == u32::WIDTH {
::alloc::vec![u32::wrapping_from(x)]
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
let (upper, lower) = u64::wrapping_from(x).split_in_half();
::alloc::vec![lower, upper]
}
}
}
impl FromOtherTypeSlice<usize> for u64 {
/// Converts a slice of [`usize`]s to a single [`u64`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#from_other_type_slice).
fn from_other_type_slice(xs: &[usize]) -> Self {
if usize::WIDTH == u32::WIDTH {
let mut result = 0;
let mut offset = 0;
for &u in xs.iter().take(2) {
result |= u64::wrapping_from(u) << offset;
offset += usize::WIDTH;
}
result
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
if xs.is_empty() {
0
} else {
u64::wrapping_from(xs[0])
}
}
}
}
impl VecFromOtherTypeSlice<usize> for u64 {
/// Converts a slice of [`usize`]s to a [`Vec`] of [`u64`]s.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
///
/// See [here](super::slice#vec_from_other_type_slice).
fn vec_from_other_type_slice(xs: &[usize]) -> Vec<Self> {
let mut out;
if usize::WIDTH == u32::WIDTH {
out = ::alloc::vec![0; xs.len().shr_round(1, Ceiling).0];
for (x, chunk) in out.iter_mut().zip(xs.chunks(2)) {
*x = u64::from_other_type_slice(chunk);
}
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
out = ::alloc::vec![0; xs.len()];
for (x, &u) in out.iter_mut().zip(xs.iter()) {
*x = u64::wrapping_from(u);
}
}
out
}
}
impl VecFromOtherType<usize> for u64 {
/// Converts a [`usize`] to a [`Vec`] of [`u64`]s.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::slice#vec_from_other_type).
#[inline]
fn vec_from_other_type(x: usize) -> Vec<Self> {
::alloc::vec![u64::wrapping_from(x)]
}
}