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
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::Arbi;
impl Arbi {
/// Reverses the byte order of the absolute value of the integer.
///
/// The sign remains unchanged.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// let a = Arbi::from(0x12345678_u32);
/// assert_eq!(a.swap_bytes(), 0x78563412);
/// ```
///
/// ## Complexity
/// \\( O(n) \\)
pub fn swap_bytes(mut self) -> Self {
self.swap_bytes_mut();
self
}
/// Reverses the byte order of the absolute value of the integer.
///
/// The sign remains unchanged.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// let mut a = Arbi::from(0x12345678_u32);
/// a.swap_bytes_mut();
/// assert_eq!(a, 0x78563412);
/// ```
pub fn swap_bytes_mut(&mut self) {
let len = self.vec.len();
for i in 0..(len / 2) {
self.vec[i] = self.vec[i].swap_bytes();
self.vec[len - 1 - i] = self.vec[len - 1 - i].swap_bytes();
self.vec.swap(i, len - 1 - i);
}
if len % 2 != 0 {
self.vec[len / 2] = self.vec[len / 2].swap_bytes();
}
self.trim();
}
/// Reverses the byte order of the absolute value of the integer.
///
/// The sign remains unchanged.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// let a = Arbi::from(0x12345678_u32);
/// assert_eq!(a.swap_bytes_ref(), 0x78563412);
/// ```
pub fn swap_bytes_ref(&self) -> Self {
let ret = self.clone();
ret.swap_bytes()
}
}
#[cfg(test)]
mod tests {
use crate::util::test::{get_seedable_rng, get_uniform_die, Distribution};
use crate::Arbi;
use crate::{DDigit, Digit};
#[test]
fn smoke() {
let (mut rng, _) = get_seedable_rng();
let die_d = get_uniform_die(Digit::MIN, Digit::MAX);
let die_dd = get_uniform_die(Digit::MAX as DDigit + 1, DDigit::MAX);
for _ in 0..i16::MAX {
let digit = die_d.sample(&mut rng);
let digit_arbi = Arbi::from(digit);
assert_eq!(digit_arbi.swap_bytes(), digit.swap_bytes() as u128);
let ddigit = die_dd.sample(&mut rng);
let ddigit_arbi = Arbi::from(ddigit);
assert_eq!(ddigit_arbi.swap_bytes(), ddigit.swap_bytes() as u128);
}
}
#[test]
fn boundaries() {
let zero = Arbi::zero();
assert_eq!(zero.swap_bytes(), 0_u32.swap_bytes() as u128);
let a = Arbi::from(Digit::MAX - 1);
assert_eq!(a.swap_bytes(), (Digit::MAX - 1).swap_bytes() as u128);
let a = Arbi::from(Digit::MAX);
assert_eq!(a.swap_bytes(), Digit::MAX.swap_bytes() as u128);
let a = Arbi::from(Digit::MAX as DDigit + 1);
assert_eq!(
a.clone().swap_bytes(),
(Digit::MAX as DDigit + 1).swap_bytes() as u128
);
let a = Arbi::from(DDigit::MAX - 1);
assert_eq!(a.swap_bytes(), (DDigit::MAX - 1).swap_bytes() as u128);
let a = Arbi::from(DDigit::MAX);
assert_eq!(a.swap_bytes(), DDigit::MAX.swap_bytes() as u128);
}
}