#range #overlap #determine #how #values #ends #open

range-overlap

确定两个值范围是否重叠及其方式

1 个不稳定版本

0.0.1 2024年1月22日

#6 in #ends

Download history 5/week @ 2024-03-31 1/week @ 2024-06-02 17/week @ 2024-06-16 34/week @ 2024-06-23

每月52次下载

MIT/Apache

31KB
328 行代码(不含注释)

range-overlap

一组方法,用于确定两个值范围是否重叠及其方式。

版本控制

版本号将保持在 0.1.0 以下,直到我们有足够的实际使用来确信边缘情况得到正确处理。

示例

最简单的使用是当你只想知道两个端点都定义的范围是否重叠时

use range_overlap::*;

// Does the range 0 to 10 overlap with the range 5 to 15?
assert_eq!(has_incl_overlap(0, 10, 5, 15), true);
// What above 11 to 20?
assert_eq!(has_incl_overlap(0, 10, 11, 20), false);

// Depending on the case, you can include or exclude the ends of the ranges:
assert_eq!(has_incl_overlap(0, 10, 10, 20), true);
assert_eq!(has_excl_overlap(0, 10, 10, 20), false);

如果两个范围重叠的方式很重要,则可以使用其他方法确定

// Check if the second range (5 to 10) is entirely inside the first:
assert_eq!(excl_classify(0, 20, 5, 10), RangeOverlap::AContainsB);
assert_neq!(excl_classify(0, 20, 5, 21), RangeOverlap::AContainsB);

// Check if the one range starts or ends inside the other:
assert_eq!(excl_classify(10, 20, 5, 15), RangeOverlap::AStartsInB);
assert_eq!(excl_classify(0, 10, 5, 15), RangeOverlap::AEndsInB);

// Check if the two ranges are the same:
assert_eq!(excl_classify(0, 10, 0, 10), RanegOverlap::AEqualsB);

最后,如果你有端点在一侧或两侧都为开式的范围,这些也可以处理

// A is fully open - no start or end
assert_eq!(classify_any(None, None, Some(0), Some(10), false), RangeOverlap::AContainsB);

// A has no start, B has no end:
assert_eq!(classify_any(None, Some(10), Some(5), None, false), RangeOverlap::AEndsInB);

无运行时依赖