sstadick / rust-lapper

Rust implementation of a fast, easy, interval tree library nim-lapper
https://docs.rs/rust-lapper
MIT License
55 stars 7 forks source link

interval rather than length #14

Closed yuxuanyuan closed 2 years ago

yuxuanyuan commented 2 years ago

Hi, Would it be possible to report the merged or overalapped intervals rather than the length?

sstadick commented 2 years ago

Hello! Could you provide a bit more context, i.e. what function(s) are you looking at?

yuxuanyuan commented 2 years ago

Thanks for your reply. What I'm looking for is a function which can return all overlapped intervals in a list of intervals. The function is someting like:

fn intersect_intervals(intervals:Vec<Iv>) -> Vec<Iv> {}
sstadick commented 2 years ago

Ah, I see. I think you could use the primitives in lapper to do that already in a not terribly inefficient way. I think there are more efficient ways to do this though, possibly involving HashSets. Realistically I probably won't be doing much work on rust-lapper in the near future to add Hash to Interval, or add a specific function for doing intersect_intervals.

However, if you wanted to make a PR adding Hash, or adding a truly efficient intersect_intervals method I'd be totally open to that!

use rust_lapper::{Interval, Lapper};

fn main() {
    let mut intervals = (0..100)
        .step_by(5)
        .map(|x| Interval {
            start: x,
            stop: x + 2,
            val: true,
        })
        .collect::<Vec<Interval<usize, bool>>>();
    intervals.push(Interval {
        start: 96,
        stop: 98,
        val: true,
    });
    let lapper = Lapper::new(intervals);
    let mut found = Vec::new();
    let mut cursor = 0;
    for i in lapper.iter() {
        if lapper.seek(i.start, i.stop, &mut cursor).count() > 1 {
            found.push(i);
        }
    }
    eprintln!("{:?}", found);
}
sstadick commented 2 years ago

Closing for now, feel free to re-open with any further questions.