slightly better chunks
This commit is contained in:
parent
2ceb9a413f
commit
6e99a71fde
19
src/utils.rs
19
src/utils.rs
|
@ -3,11 +3,18 @@ use std::ops::Range;
|
||||||
pub fn gen_chunks(n: usize, chunks: usize) -> Vec<Range<usize>> {
|
pub fn gen_chunks(n: usize, chunks: usize) -> Vec<Range<usize>> {
|
||||||
let mut borders = vec![];
|
let mut borders = vec![];
|
||||||
let chunk_size = n / chunks;
|
let chunk_size = n / chunks;
|
||||||
|
let mut remainder = n % chunks;
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
for _ in 0..chunks {
|
for _ in 0..chunks {
|
||||||
let end = start + chunk_size;
|
let end = start + chunk_size;
|
||||||
borders.push(start..end);
|
if remainder > 0 {
|
||||||
start = end;
|
remainder -= 1;
|
||||||
|
borders.push(start..end + 1);
|
||||||
|
start = end + 1;
|
||||||
|
} else {
|
||||||
|
borders.push(start..end);
|
||||||
|
start = end;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Change the last chunk to include the remainder
|
// Change the last chunk to include the remainder
|
||||||
let size = borders.len();
|
let size = borders.len();
|
||||||
|
@ -22,7 +29,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gen_chunks() {
|
fn test_gen_chunks() {
|
||||||
let borders = gen_chunks(10, 3);
|
let borders = gen_chunks(10, 3);
|
||||||
assert_eq!(borders, vec![(0..3), (3..6), (6..10)]);
|
assert_eq!(borders, vec![(0..4), (4..7), (7..10)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -36,4 +43,10 @@ mod test {
|
||||||
let borders = gen_chunks(10, 1);
|
let borders = gen_chunks(10, 1);
|
||||||
assert_eq!(borders, vec![(0..10)]);
|
assert_eq!(borders, vec![(0..10)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gen_chunks4() {
|
||||||
|
let borders = gen_chunks(11, 3);
|
||||||
|
assert_eq!(borders, vec![(0..4), (4..8), (8..11)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue