iter_cartesian 0.1.0

A Cartesian product iterator with double-ended iteration and O(1) length queries.
Documentation
  • Coverage
  • 75%
    3 out of 4 items documented1 out of 3 items with examples
  • Size
  • Source code size: 51.79 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.68 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Sirokovsk

iter_cartesian

A chainable .cartesian() adapter that yields every (a, b) pair from two iterators, the iterator equivalent of a nested loop.

Usage

use iter_cartesian::CartesianExt;

// collect every (a, b) pair
let pairs: Vec<_> = (0..m).cartesian(0..n).collect();

// iterate in reverse
let rev_pairs: Vec<_> = (0..m).cartesian(0..n).rev().collect();

// O(1) overrides
let item = (0..m).cartesian(0..n).nth(k);
let last = (0..m).cartesian(0..n).last();
let total = (0..m).cartesian(0..n).count();

Features

  • Chainable .cartesian() method on any double-ended exact-size iterator
  • Full DoubleEndedIterator support, interleave next() and next_back() correctly
  • O(1) len, nth, and count via overridden iterator methods
  • TrustedLen and FusedIterator implementations

Benchmarks

All O(1) operations confirmed flat across input sizes. Collection is ~54% faster than flat_map, with significantly lower variance.

Benchmark Time
len, 10 × 10 455 ps
len, 1000 × 1000 449 ps
len, 100000 × 100000 447 ps
nth, jump 10 16.6 ns
nth, jump 100,000 16.9 ns
nth, jump 1,000,000 16.6 ns
collect, 1000 × 1000 2.48 ms
flat_map equivalent 5.44 ms (±1.54 ms)

Alternatives

License

MIT OR Apache-2.0