#interval #structure #1d #overlap #segment #maintain #coalesced

coalesced_intervals

Data structure for maintaining maximally-coalesced 1D intervals

7 releases

0.1.6 Jun 21, 2024
0.1.5 May 12, 2024

#714 in Data structures

Download history 23/week @ 2024-09-27 110/week @ 2024-10-04 88/week @ 2024-10-11 81/week @ 2024-10-18 43/week @ 2024-10-25 229/week @ 2024-11-01 480/week @ 2024-11-08 411/week @ 2024-11-15 569/week @ 2024-11-22 398/week @ 2024-11-29 729/week @ 2024-12-06 634/week @ 2024-12-13 407/week @ 2024-12-20 17/week @ 2024-12-27 48/week @ 2025-01-03 72/week @ 2025-01-10

660 downloads per month

Apache-2.0

115KB
319 lines

coalesced_intervals: maintain maximally coalesced 1D intervals

sample usage diagram

extern crate coalesced_intervals;

fn main() {
    let mut ivals = coalesced_intervals::CoalescedIntervals::new();

    // Add `[0, 1)` and `[2, 3)` (there's a hole in the middle).
    ivals.add(0, 1);
    ivals.add(2, 3);
    assert_eq!(ivals.to_vec(), [(0, 1), (2, 3)]);

    // By adding `[1, 2)` we end up with a coalesced segment `[0, 3)`.
    ivals.add(1, 2);
    assert_eq!(ivals.to_vec(), [(0, 3)]);

    // We can see that the coalesced interval has partial overlap
    // with other related intervals.
    assert!(ivals.contains_partial(-1, 1));
    assert!(ivals.contains_partial(1, 2));
    assert!(ivals.contains_partial(2, 4));

    // We can ask for the interval containing some target value.
    assert_eq!(ivals.get_interval_containing(1), Some((0, 3)));
    assert_eq!(ivals.get_interval_containing(4), None);

    // We can ask for the interval that starts at-or-after some value.
    assert_eq!(ivals.get_first_start_from(-1), Some((0, 3)));
    assert_eq!(ivals.get_first_start_from(0), Some((0, 3)));
    assert_eq!(ivals.get_first_start_from(1), None);
}

Dependencies