| Crates.io | xrange |
| lib.rs | xrange |
| version | 0.1.5 |
| created_at | 2026-01-13 09:12:53.218574+00 |
| updated_at | 2026-01-15 09:24:51.937273+00 |
| description | High-performance range overlap search algorithms / 高性能区间重叠搜索算法 |
| homepage | https://github.com/js0-site/jdb/tree/main/xrange |
| repository | https://github.com/js0-site/jdb.git |
| max_upload_size | |
| id | 2039739 |
| size | 78,711 |
xrange offers high-performance range overlap detection and searching algorithms. It features a generic overlap check and a specialized, binary-search-optimized finder for sorted, disjoint ranges, which is widely used in storage engine metadata (like SSTables).
use std::ops::Range;
use xrange::overlap_for_sorted;
fn main() {
let ranges: Vec<Range<i32>> = vec![0..5, 5..10, 10..15, 15..20];
let query = 8..12;
// Find ranges that overlap with 8..12
let result: Vec<_> = overlap_for_sorted(query, &ranges).collect();
// Output: [5..10, 10..15]
for r in result {
println!("{:?}", r);
}
}
RangeBounds.partition_point) to locate overlapping slices in O(log N) time.O(1).The algorithm leverages the property that items are sorted and disjoint:
graph TD
Start[Start] --> FindStart{Find Start Index}
FindStart --> CheckGap{Is First Item After Query?}
CheckGap -- Yes --> ReturnEmpty[Return Empty]
CheckGap -- No --> FindEnd{Find End Index}
FindEnd --> ReturnSlice[Return Slice]
src/lib.rs: Entry point and re-exports.src/is_overlap.rs: Generic trait implementation for checking overlap.src/overlap_for_sorted.rs: The core algorithm for sorted slices.is_overlappub fn is_overlap<R1, R2>(r1: &R1, r2: &R2) -> bool
Determines if two ranges overlap. Handles all bound types (Included, Excluded, Unbounded).
overlap_for_sortedpub fn overlap_for_sorted<T, B, R1, R2>(range: R2, slice: &[T]) -> impl Iterator<Item = &T>
Returns an iterator over all items in the slice that overlap with range.
Precondition: slice must be sorted by start_bound and contain disjoint ranges.
The concept of Binary Search was first mentioned by John Mauchly in 1946, but the first binary search that worked correctly for all sizes of arrays was not published until 1962, nearly 16 years later! Simple ideas are often the hardest to get perfectly right.
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
xrange 提供高性能的区间重叠检测与搜索算法。它包含通用的重叠检查功能,以及针对有序、不重叠区间(常见于 SSTable 元数据)优化的二分搜索查找器。
use std::ops::Range;
use xrange::overlap_for_sorted;
fn main() {
let ranges: Vec<Range<i32>> = vec![0..5, 5..10, 10..15, 15..20];
let query = 8..12;
// 查找与 8..12 重叠的区间
let result: Vec<_> = overlap_for_sorted(query, &ranges).collect();
// 输出: [5..10, 10..15]
for r in result {
println!("{:?}", r);
}
}
RangeBounds 的类型。partition_point)在 O(log N) 时间内定位重叠切片。O(1)。本算法充分利用了数据有序且互不重叠的特性:
graph TD
Start[开始] --> FindStart{二分查找起点}
FindStart --> CheckGap{首项是否在查询后?}
CheckGap -- 是 --> ReturnEmpty[返回空]
CheckGap -- 否 --> FindEnd{二分查找终点}
FindEnd --> ReturnSlice[返回切片]
src/lib.rs: 模块导出与入口。src/is_overlap.rs: 通用的重叠检测实现。src/overlap_for_sorted.rs: 针对有序切片的核心算法。is_overlappub fn is_overlap<R1, R2>(r1: &R1, r2: &R2) -> bool
判断两个范围是否重叠。支持所有边界类型(Included, Excluded, Unbounded)。
overlap_for_sortedpub fn overlap_for_sorted<T, B, R1, R2>(range: R2, slice: &[T]) -> impl Iterator<Item = &T>
返回 slice 中所有与 range 重叠的项的迭代器。
前置条件:slice 必须按 start_bound 排序且包含互不重叠的区间。
二分查找(Binary Search)的概念最早由 John Mauchly 在 1946 年提出,但直到 1962 年,也就是整整 16 年后,第一个能够正确处理所有数组长度的二分查找算法才被通过并发布!这告诉我们要写对看似简单的基础算法其实并不容易。
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: