Function split_with_min_batch

Source
pub fn split_with_min_batch(
    total: usize,
    max_batch_size: usize,
    min_batch_size: usize,
) -> Result<(usize, Vec<NonZeroUsize>), String>
Expand description

Splits a total number into even batches, ensuring each batch meets a minimum size requirement.

§Arguments

  • total - The total number to be split.
  • max_batch_size - The maximum allowed size for each batch.
  • min_batch_size - The minimum required size for each batch.

§Returns

A Result containing a tuple with:

  1. The number of batches.
  2. A vector of NonZeroUsize representing the size of each batch.

§Errors

Returns an error if:

  • The total is zero.
  • The max_batch_size is zero.
  • The min_batch_size is greater than max_batch_size.
  • It’s impossible to create batches that meet the minimum size requirement.

§Examples

use batch_maestro::split_with_min_batch;
use std::num::NonZeroUsize;

let (num_batches, batch_sizes) = split_with_min_batch(100, 30, 20).unwrap();
assert_eq!(num_batches, 4);
assert_eq!(batch_sizes, vec![NonZeroUsize::new(25).unwrap(); 4]);