#iterator #zip #traversal #traverse #no-alloc #breadth-first

no-std breadth-first-zip

Breadth-first zip guaranteeing a monotonically increasing sum of indices

3 releases (breaking)

0.3.0 Jul 2, 2023
0.2.2 Jul 1, 2023
0.1.0 Jun 29, 2023

#1383 in Algorithms


Used in 2 crates (via derive-quickcheck)

MPL-2.0 license

20KB
374 lines

Breadth-first zip

Property-tested and verified to return every possible value exactly one in a strictly increasing sum of indices.

Why?

For whenever you have a multiple iterators and want to cover every possible value from overall smallest to largest.

E.g. for three instances of 0..3, you'd get this:

0 0 0 # sum = 0
0 0 1 # sum = 1
0 1 0
1 0 0
0 0 2 # sum = 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
0 1 2 # sum = 3
0 2 1
1 0 2
1 1 1
1 2 0
2 0 1
2 1 0
0 2 2 # sum = 4
1 1 2
1 2 1
2 0 2
2 1 1
2 2 0
1 2 2 # sum = 5
2 1 2
2 2 1
2 2 2 # sum = 6

Inputs can be any non-empty iterator, even combining different sizes.


lib.rs:

Breadth-first exhaustive zip for repeatable iterators. Behavior matches the following pseudocode specification:

  • Initialize a counter i at zero.
  • When propmted, pull the first element from each iterator.
    • If any iterator is empty, return None.
  • When prompted again, advance only the last iterator.
  • Continue to do so until the last iterator terminates or reaches its ith element.
    • When it does so, reset it and pull the next element from the second-to-last iterator.
  • Repeat this process until we exhaust the first iterator.
    • When you've done that, increase i and repeat.
  • Once i exceeds the longest iterator's length, we're done: return None.

Dependencies

~295–740KB
~18K SLoC