3 unstable releases
| 0.2.1 | Mar 8, 2024 |
|---|---|
| 0.2.0 | Jul 10, 2023 |
| 0.1.0 | Jul 10, 2023 |
#2144 in Algorithms
53 downloads per month
Used in tree-experiments
16KB
136 lines
swap3
Provides utility functions for simultaneously swapping three values by rotating them
either left (abc → bca) or right (abc → cab). These functions can come in handy e.g.
when rotating elements of a binary tree in list representation.
The provided functions work on arbitrary types and do not require the type to be Clone, Copy
or Default.
Crate features
unsafe- Theunsafefeature enables the use of (potentially faster) unsafe code. It is disabled by default; when disabled,forbid(unsafe_code)is implied.
Examples
For individual references, the swap3_bca (rotate left) and swap3_cab (rotate right)
functions are available:
let mut a = 10;
let mut b = 20;
let mut c = 30;
swap3::swap3_bca(&mut a, &mut b, &mut c);
assert_eq!([a, b, c], [20, 30, 10]);
For slices, the swap3_bca_slice and swap3_cab_slice functions can be used:
let mut vec = vec![10, 20, 30, 40, 50, 60];
swap3::swap3_bca_slice(&mut vec, 0, 1, 4);
assert_eq!(vec, &[20, 50, 30, 40, 10, 60]);
... or using the Swap3 trait imported from the prelude:
use swap3::prelude::*;
let mut vec = vec![10, 20, 30, 40, 50, 60];
vec.swap3_bca(0, 1, 4);
assert_eq!(vec, &[20, 50, 30, 40, 10, 60]);
swap3
Provides utility functions for simultaneously swapping three values by rotating them
either left (abc → bca) or right (abc → cab). These functions can come in handy e.g.
when rotating elements of a binary tree in list representation.
The provided functions work on arbitrary types and do not require the type to be Clone, Copy
or Default.
Examples
For individual references, the swap3_bca (rotate left) and swap3_cab (rotate right)
functions are available:
fn swap3_bca() {
let mut a = 10;
let mut b = 20;
let mut c = 30;
swap3::swap3_bca(&mut a, &mut b, &mut c);
assert_eq!([a, b, c], [20, 30, 10]);
}
For slices, the swap3_bca_slice and swap3_cab_slice functions can be used:
use swap3::prelude::*;
fn swap3_bca() {
let mut vec = vec![10, 20, 30, 40, 50, 60];
vec.swap3_bca(0, 1, 4); // or swap3_bca_slice(&mut vec, 0, 1, 4)
assert_eq!(vec, &[20, 50, 30, 40, 10, 60]);
}