iter-set-ops

Crates.ioiter-set-ops
lib.rsiter-set-ops
version0.2.3
sourcesrc
created_at2024-05-06 22:39:39.250977
updated_at2024-05-14 07:15:58.420222
descriptionFast set operations on an arbitrary number of sorted deduplicated iterators
homepagehttps://crates.io/crates/iter-set-ops
repositoryhttps://github.com/imartayan/iter-set-ops
max_upload_size
id1231575
size29,438
Igor Martayan (imartayan)

documentation

https://docs.rs/iter-set-ops

README

iter-set-ops

crates.io docs

Fast set operations on an arbitrary number of sorted deduplicated iterators.

Features

  • set operations for an arbitrary number of sets
  • zero copy: each item is reused from its iterator, no copy is made
  • custom comparison operator with merge_iters_by / intersect_iters_by
  • lazy intersection: the computation stops as soon as one of the sets is exhausted

Example usage

use iter_set_ops::intersect_iters;

let it1 = 1u8..=5;
let it2 = 3u8..=7;
let it3 = 2u8..=4;
let mut iters = [it1, it2, it3];

// intersect it1, it2 and it3
let res: Vec<_> = intersect_iters(&mut iters).collect();

assert_eq!(res, vec![3, 4]);

// the computation stops before exhausting all the iterators
assert!(iters[1].next().is_some());
use iter_set_ops::merge_iters_by;

let it1 = (1u8..=5).rev();
let it2 = (3u8..=7).rev();
let it3 = (2u8..=4).rev();
let mut iters = [it1, it2, it3];

// merge it1, it2 and it3 using a reverse comparison operator
let res: Vec<_> = merge_iters_by(&mut iters, |x, y| y.cmp(x)).collect();

assert_eq!(res, vec![7, 6, 5, 4, 3, 2, 1]);
use iter_set_ops::merge_iters_detailed;

let it1 = 1u8..=2;
let it2 = 2u8..=3;
let mut iters = [it1, it2];

// merge it1 and it2 while keeping the details of each item
let res: Vec<_> = merge_iters_detailed(&mut iters).collect();

assert_eq!(
    res,
    vec![
        vec![(0, 1)], // `1` comes from the first iterator
        vec![(1, 2), (0, 2)], // `2` comes from both iterators
        vec![(1, 3)] // `3` comes from the second iterator
    ]
);
Commit count: 34

cargo fmt