Crates.io | rsbatch-maestro |
lib.rs | rsbatch-maestro |
version | 0.2.1 |
source | src |
created_at | 2024-07-28 09:59:02.545369 |
updated_at | 2024-07-28 21:01:43.144191 |
description | A Rust crate for flexible batch splitting and management with various strategies. |
homepage | |
repository | https://github.com/aeromilai/rsbatch-maestro |
max_upload_size | |
id | 1317908 |
size | 46,440 |
A Rust crate for flexible batch splitting and management with various strategies.
Add this to your Cargo.toml
:
[dependencies]
rsbatch-maestro = "0.2.0"
use rsbatch_maestro::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),
}
}
The crate provides the following functions:
pub fn even_split(total: usize, max_batch_size: usize) -> Result<(usize, Vec<NonZeroUsize>), String>
pub fn uneven_split(total: usize, max_batch_size: usize) -> Result<(usize, Vec<NonZeroUsize>), String>
pub fn split_by_count(total: usize, num_batches: usize) -> Result<Vec<NonZeroUsize>, String>
pub fn split_with_remainder(total: usize, batch_size: usize) -> Result<(Vec<NonZeroUsize>, usize), String>
pub fn split_weighted(total: usize, weights: Vec<usize>) -> Result<Vec<NonZeroUsize>, String>
pub fn split_range(total: usize, min_batch_size: usize, max_batch_size: usize) -> Result<Vec<(usize, usize)>, String>
pub fn optimize_split(total: usize, min_batches: usize, max_batches: usize) -> Result<(usize, Vec<NonZeroUsize>), String>
pub fn split_with_min_batch(total: usize, max_batch_size: usize, min_batch_size: usize) -> Result<(usize, Vec<NonZeroUsize>), String>
pub fn split_to_nearest(total: usize, target_batch_size: usize) -> Result<(usize, Vec<NonZeroUsize>), String>
pub fn merge_batches(batches: Vec<NonZeroUsize>, merge_count: usize) -> Result<Vec<NonZeroUsize>, String>
pub fn rebalance_batches(batches: Vec<NonZeroUsize>) -> Vec<NonZeroUsize>
For detailed documentation on each function, please refer to the API documentation.
use rsbatch_maestro::*;
use std::num::NonZeroUsize;
// Even split
assert_eq!(even_split(50, 8), Ok((10, vec![NonZeroUsize::new(5).unwrap(); 10])));
// Uneven split
assert_eq!(uneven_split(50, 8), Ok((7, vec![NonZeroUsize::new(8).unwrap(); 6].into_iter().chain(vec![NonZeroUsize::new(2).unwrap()]).collect())));
// Split by count
assert_eq!(split_by_count(50, 8), Ok(vec![NonZeroUsize::new(7).unwrap(); 2].into_iter().chain(vec![NonZeroUsize::new(6).unwrap(); 6]).collect()));
// Split with remainder
assert_eq!(split_with_remainder(50, 8), Ok((vec![NonZeroUsize::new(8).unwrap(); 6], 2)));
// Split weighted
assert_eq!(split_weighted(100, vec![1, 2, 3, 4]), Ok(vec![NonZeroUsize::new(10).unwrap(), NonZeroUsize::new(20).unwrap(), NonZeroUsize::new(30).unwrap(), NonZeroUsize::new(40).unwrap()]));
// More examples for other functions can be found in the API documentation
Batch Maestro 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.