#区间 #1d #结构 #添加 #部分 #重叠 #合并

coalesced_intervals

维护最大合并的 1D 区间数据结构

7 个版本

0.1.6 2024年6月21日
0.1.5 2024年5月12日

#2#重叠

Download history 318/week @ 2024-05-06 151/week @ 2024-05-13 124/week @ 2024-05-20 26/week @ 2024-05-27 3/week @ 2024-06-03 115/week @ 2024-06-17 28/week @ 2024-06-24 135/week @ 2024-07-08 92/week @ 2024-07-15 61/week @ 2024-07-22 109/week @ 2024-07-29

每月397次下载

Apache-2.0

115KB
319

coalesced_intervals: 维护最大合并的 1D 区间

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);
}

依赖项