0.1.0 |
|
---|
#8 in #even
11KB
51 lines
RsEven Splitter
A Rust crate for evenly splitting a total number into batches, with customizable maximum batch size.
Features
- Split a total number into even batches
- Specify a maximum batch size
- Returns both the number of batches and the size of each batch
- Handles edge cases and provides meaningful errors
Installation
Add this to your Cargo.toml
:
[dependencies]
rseven-splitter = "0.1.0"
Usage
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),
}
}
API
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:
- The number of batches.
- A vector of
NonZeroUsize
representing the size of each batch.
Examples
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.
Use Cases
RsEven Splitter is versatile and can be applied to various scenarios:
- Task Distribution: Evenly distribute tasks among workers in parallel processing systems.
- Load Balancing: Distribute network traffic or database queries across multiple servers.
- Resource Allocation: Allocate resources (e.g., memory, disk space) evenly across different processes or users.
- Inventory Management: Divide inventory items evenly across different storage locations or shipments.
- Time Management: Split a total time duration into even segments for schedules or timetables.
- Financial Applications: Divide sums of money evenly for budgeting or investment purposes.
- Data Partitioning: Evenly partition large datasets for distributed processing or storage.
- Batch Processing: Create evenly sized batches for any kind of batch processing operation.
- UI/UX Design: Evenly distribute elements in user interfaces, such as grid layouts or menu items.
- Educational Applications: Create equal-sized groups of students for group projects or activities.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Please make sure to update tests as appropriate.
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- aeromilai - Initial work - aeromilai
See also the list of contributors who participated in this project.
Acknowledgements
- Inspired by the need for efficient and flexible resource distribution in various domains.
- Thanks to the Rust community for providing excellent tools and libraries.
Changelog
For details on our releases, see the Changelog.