基于Rust的区间树
Intervaltree with Rust Back End

原始链接: https://github.com/Athe-kunal/intervaltree_rs

## intervaltree_rs:Python 的 Rust 区间树 `intervaltree_rs` 使用 PyO3 将用 Rust 编写的高性能区间树实现带到 Python。该包允许高效地管理和查询区间。 用户可以从元组列表 (left, right, payload) 创建一个 `IntervalTree`,插入新的区间,并在指定的范围内搜索重叠。`search` 方法支持包含或排除边界。还可以使用它们的 (left, right) 键删除区间。 **安装:** 需要 Rust 工具链、Python 3.8+ 和 `maturin`。使用 `maturin develop` 进行原地开发构建,或使用 `maturin build` 构建可分发的 wheel。然后,该包可以在 Python 中作为 `intervaltree_rs` 访问。 核心功能经过 Rust 单元测试的全面测试,确保可靠性和性能。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 区间树,使用 Rust 后端 (github.com/athe-kunal) 6 分,作者 athekunal 2 小时前 | 隐藏 | 过去 | 收藏 | 2 条评论 athekunal 2 小时前 [–] 我构建了一个项目,用 Rust 实现了区间树,并暴露了 PyO3 绑定,作为 Python 原生区间树的替代品。它速度明显更快,并且我将添加更多功能,例如 AVL 树和红黑树来实现平衡。回复 eru 0 分钟前 | 父评论 [–] 如果你想要平衡树,可以看看 Rust 标准库对 BTreeMap 的处理方式。回复 考虑申请 YC 2026 冬季批次!申请截止日期为 11 月 10 日 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

This crate exposes an interval tree implementation written in Rust to Python via PyO3. The Python wrapper provides the ability to build a tree from tuples, insert additional intervals, search for overlaps, and delete intervals by their (left, right) key.

  • Rust toolchain (for compiling the extension module)
  • Python 3.8+
  • maturin for building/installing the package
python -m venv .venv
source .venv/bin/activate
pip install maturin
maturin develop

You can install the package with (also with uv)

pip install intervaltree_rs

maturin develop builds the extension module in-place and installs it into the active virtual environment, making it importable as intervaltree_rs.

Once installed, you can use the interval tree directly from Python:

from intervaltree_rs import IntervalTree

# Build a tree from tuples: (left, right, payload)
intervals = [
    (5, 10, "a"),
    (12, 18, "b"),
    (1, 4, "c"),
]
tree = IntervalTree.from_tuples(intervals)

# Insert another interval
tree.insert((8, 11, "d"))

# Search for overlaps. Inclusive bounds are enabled by default.
hits = tree.search(9, 10)
for left, right, value in hits:
    print(left, right, value)

# Delete by the interval key
removed = tree.delete((12, 18))
print("Removed:", removed)

IntervalTree.search(ql, qr, inclusive=True) accepts an inclusive flag. Set it to False to perform exclusive range queries.

Building a distributable wheel

To build a wheel that you can distribute or upload to PyPI, run:

The built wheels will be placed under target/wheels/.

The Python bindings are covered by Rust unit tests. Run them with:

联系我们 contact @ memedata.com