raptorq/src/util.rs
Slesarew 5a720829fa
feat: support no_std (#143)
* feat: support no_std

`metal` feature supports `no_std` in configuration `default-features = false, features = ["metal"]`.
Float calculation is done via `micromath` crate.

All previously available functionality remains under default `std` feature.

Some tweaking of `python` and `wasm` features was done to compile tests.

* feat: get rid of floats (#2)

* feat: remove conversion to f64, fix features

* chore: uncomment symbols_required checker, fmt

* revert: add cdylib target for python support

* fix: generalize crate type

---------

Co-authored-by: varovainen <99664267+varovainen@users.noreply.github.com>
2023-02-02 18:07:41 -08:00

47 lines
1.6 KiB
Rust

// Get two non-overlapping ranges starting at i & j, both with length len
pub fn get_both_ranges<T>(
vector: &mut [T],
i: usize,
j: usize,
len: usize,
) -> (&mut [T], &mut [T]) {
debug_assert_ne!(i, j);
debug_assert!(i + len <= vector.len());
debug_assert!(j + len <= vector.len());
if i < j {
debug_assert!(i + len <= j);
let (first, last) = vector.split_at_mut(j);
return (&mut first[i..(i + len)], &mut last[0..len]);
} else {
debug_assert!(j + len <= i);
let (first, last) = vector.split_at_mut(i);
return (&mut last[0..len], &mut first[j..(j + len)]);
}
}
pub fn get_both_indices<T>(vector: &mut [T], i: usize, j: usize) -> (&mut T, &mut T) {
debug_assert_ne!(i, j);
debug_assert!(i < vector.len());
debug_assert!(j < vector.len());
if i < j {
let (first, last) = vector.split_at_mut(j);
return (&mut first[i], &mut last[0]);
} else {
let (first, last) = vector.split_at_mut(i);
return (&mut last[0], &mut first[j]);
}
}
// This should eventually become <https://doc.rust-lang.org/std/primitive.u64.html#method.div_ceil>
// when it gets stabilized, and this function should be removed.
// (1) the result is known to not overflow u32 from elsewhere;
// (2) `denom` is known to not be `0` from elsewhere.
// TODO this is definitely not always the case! Let's do something about it.
pub fn int_div_ceil(num: u64, denom: u64) -> u32 {
if num % denom == 0 {
(num / denom) as u32
} else {
(num / denom + 1) as u32
}
}