/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.10.0/src/math.rs
Line | Count | Source |
1 | | use std::ops::{Bound, Range, RangeBounds}; |
2 | | |
3 | | /// Divide `n` by `divisor`, and round up to the nearest integer |
4 | | /// if not evenly divisible. |
5 | | #[inline] |
6 | 0 | pub(super) fn div_round_up(n: usize, divisor: usize) -> usize { |
7 | 0 | debug_assert!(divisor != 0, "Division by zero!"); |
8 | 0 | if n == 0 { |
9 | 0 | 0 |
10 | | } else { |
11 | 0 | (n - 1) / divisor + 1 |
12 | | } |
13 | 0 | } Unexecuted instantiation: rayon::math::div_round_up Unexecuted instantiation: rayon::math::div_round_up |
14 | | |
15 | | /// Normalize arbitrary `RangeBounds` to a `Range` |
16 | 0 | pub(super) fn simplify_range(range: impl RangeBounds<usize>, len: usize) -> Range<usize> { |
17 | 0 | let start = match range.start_bound() { |
18 | 0 | Bound::Unbounded => 0, |
19 | 0 | Bound::Included(&i) if i <= len => i, |
20 | 0 | Bound::Excluded(&i) if i < len => i + 1, |
21 | 0 | bound => panic!("range start {:?} should be <= length {}", bound, len), |
22 | | }; |
23 | 0 | let end = match range.end_bound() { |
24 | 0 | Bound::Unbounded => len, |
25 | 0 | Bound::Excluded(&i) if i <= len => i, |
26 | 0 | Bound::Included(&i) if i < len => i + 1, |
27 | 0 | bound => panic!("range end {:?} should be <= length {}", bound, len), |
28 | | }; |
29 | 0 | if start > end { |
30 | 0 | panic!( |
31 | 0 | "range start {:?} should be <= range end {:?}", |
32 | 0 | range.start_bound(), |
33 | 0 | range.end_bound() |
34 | | ); |
35 | 0 | } |
36 | 0 | start..end |
37 | 0 | } Unexecuted instantiation: rayon::math::simplify_range::<core::ops::range::RangeFull> Unexecuted instantiation: rayon::math::simplify_range::<_> |
38 | | |
39 | | #[cfg(test)] |
40 | | mod tests { |
41 | | use super::*; |
42 | | |
43 | | #[test] |
44 | | fn check_div_round_up() { |
45 | | assert_eq!(0, div_round_up(0, 5)); |
46 | | assert_eq!(1, div_round_up(5, 5)); |
47 | | assert_eq!(1, div_round_up(1, 5)); |
48 | | assert_eq!(2, div_round_up(3, 2)); |
49 | | assert_eq!( |
50 | | usize::max_value() / 2 + 1, |
51 | | div_round_up(usize::max_value(), 2) |
52 | | ); |
53 | | } |
54 | | } |