Module vec_altern

Source
Expand description

§VecAltern

The vec_altern module provides an iterator, VecAltern, for alternately traversing multiple iterators.

§Usage

To use VecAltern, create a new instance with VecAltern::new() and add iterators using the add method (or add_and for a build pattern). You can also use the macro altern for more concise syntax and little performance optimization. The next method will then yield elements from the added iterators in a round-robin fashion until all iterators are exhausted.

§Examples

use combin_iterator::altern::VecAltern;
let vec1 = vec![1, 4, 7, 9];
let vec2 = vec![2, 5];
let vec3 = vec![3, 6, 8];

// // Create a VecAltern iterator and add individual iterators, with a build pattern.
let mut iter = VecAltern::new();
iter.add(vec1.iter());
iter.add(vec2.iter());
iter.add(vec3.iter());

assert_eq!(iter.collect::<Vec<_>>(), vec![&1, &2, &3, &4, &5, &6, &7, &8, &9]);

// You can also use a build pattern:
let iter = VecAltern::new().add_and(vec1.iter()).add_and(vec2.iter()).add_and(vec3.iter());
assert_eq!(iter.collect::<Vec<_>>(), vec![&1, &2, &3, &4, &5, &6, &7, &8, &9]);

// Alternatively, use the `altern!` macro for a more concise syntax, and some perfomance optimization
use combin_iterator::altern;
let iter_macro = altern!(vec1.iter(), vec2.iter(), vec3.iter());

// Both iterators should yield the same results
assert_eq!(iter_macro.collect::<Vec<_>>(), vec![&1, &2, &3, &4, &5, &6, &7, &8, &9]);

§Notes

  • The altern! macro provides a convenient way to create an Altern iterator with a cleaner syntax, and a little performance optimization (with the function: with_capacity, like in Vec). If you know at compile time how many iter you will altern between, then use the altern! macro.

Structs§

VecAltern
Struct to altern between several iterator