Crates.io | rseven-splitter |
lib.rs | rseven-splitter |
version | 0.1.0 |
source | src |
created_at | 2024-07-28 07:10:37.268868 |
updated_at | 2024-07-28 07:10:37.268868 |
description | A Rust crate for evenly splitting a total number into batches, with customizable maximum batch size. |
homepage | |
repository | https://github.com/aeromilai/rseven-splitter |
max_upload_size | |
id | 1317812 |
size | 32,205 |
A Rust crate for evenly splitting a total number into batches, with customizable maximum batch size.
Add this to your Cargo.toml
:
[dependencies]
rseven-splitter = "0.1.0"
use rseven_splitter::even_split;
fn main() {
match even_split(128, 8) {
Ok((num_batches, batch_sizes)) => {
println!("Number of batches: {}", num_batches);
println!("Batch sizes: {:?}", batch_sizes);
},
Err(e) => println!("Error: {}", e),
}
}
pub fn even_split(total: usize, max_batch_size: usize) -> Result<(usize, Vec<NonZeroUsize>), String>
total
: The total number to be split.max_batch_size
: The maximum allowed size for each batch.Returns a tuple containing:
NonZeroUsize
representing the size of each batch.use std::num::NonZeroUsize;
assert_eq!(even_split(50, 8), Ok((10, vec![NonZeroUsize::new(5).unwrap(); 10])));
assert_eq!(even_split(128, 8), Ok((16, vec![NonZeroUsize::new(8).unwrap(); 16])));
assert_eq!(even_split(46, 8), Ok((46, vec![NonZeroUsize::new(1).unwrap(); 46])));
assert_eq!(even_split(7, 8), Ok((1, vec![NonZeroUsize::new(7).unwrap()])));
assert_eq!(even_split(17, 8), Ok((17, vec![NonZeroUsize::new(1).unwrap(); 17])));
Note: The function always returns the largest possible batch size that evenly divides the total, without exceeding the max_batch_size
. If no such division is possible, it returns batches of size 1.
RsEven Splitter is versatile and can be applied to various scenarios:
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Please make sure to update tests as appropriate.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
For details on our releases, see the Changelog.