Module shuffle
Expand description
See shuffle for the system contract and examples.
Reproducible shuffled index cycles for nonrepeating engine variants.
Each cycle is a permutation of 0..count. ShuffleBag::new retains the
original seeded sequence, including possible repeats across cycle boundaries.
Opt into BoundaryPolicy::AvoidImmediateRepeat to exclude those repeats
when there are at least two items. Each allowed permutation is equally likely;
a two-item bag necessarily alternates forever after its random first choice.
An empty bag always returns None; a singleton always returns Some(0).
Cycles are shuffled lazily on their first draw. Inspection never consumes
randomness. ShuffleBag::reset restores the original seed and index order,
discards any unfinished cycle, and clears progress and boundary history.
use ferravex_engine::shuffle::{BoundaryPolicy, ShuffleBag};
let mut bag = ShuffleBag::with_policy(3, 42, BoundaryPolicy::AvoidImmediateRepeat).unwrap();
assert_eq!(bag.boundary_policy(), BoundaryPolicy::AvoidImmediateRepeat);
assert_eq!(bag.len(), 3); // Items per cycle, not an iterator length.
assert!(!bag.is_empty());
assert_eq!(bag.completed_cycles(), 0);
assert_eq!(bag.cycle_position(), 0);
assert_eq!(bag.remaining_in_cycle(), 3);
assert_eq!(bag.last_drawn(), None);
let first = bag.by_ref().take(3).collect::<Vec<_>>();
assert_eq!(bag.completed_cycles(), 1);
assert_eq!(bag.cycle_position(), 3); // Retained until the next draw.
assert_eq!(bag.remaining_in_cycle(), 0);
assert_eq!(bag.last_drawn(), first.last().copied());
assert_ne!(bag.next(), first.last().copied());
assert_eq!(bag.cycle_position(), 1);
bag.reset();
assert_eq!(bag.last_drawn(), None);
assert_eq!(bag.by_ref().take(3).collect::<Vec<_>>(), first);Structs§
- Shuffle
Bag - An endless sequence of shuffled index cycles, or an empty iterator for zero items.
Enums§
- Boundary
Policy - Whether a new cycle may begin with the previous cycle’s final index.