even_split

Function even_split 

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

Splits a total number into even batches.

This function takes a total number and a maximum batch size, and attempts to divide the total into as many even batches as possible, without exceeding the maximum batch size.

§Arguments

  • total - The total number to be split.
  • max_batch_size - The maximum allowed 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.

§Examples

use batch_maestro::even_split;
use std::num::NonZeroUsize;

let (num_batches, batch_sizes) = even_split(50, 8).unwrap();
assert_eq!(num_batches, 10);
assert_eq!(batch_sizes, vec![NonZeroUsize::new(5).unwrap(); 10]);