Skip to main content

Fast interval tree (Rust) with Python bindings

Project description

Intervaltree in rust

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.

Requirements

  • Rust toolchain (for compiling the extension module)
  • Python 3.8+
  • maturin for building/installing the package

Quick start

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.

Usage

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)

Search options

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:

maturin build --release

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

Running tests

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

cargo test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

intervaltree_rs-0.1.1.tar.gz (635.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

intervaltree_rs-0.1.1-cp38-abi3-macosx_11_0_arm64.whl (216.0 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file intervaltree_rs-0.1.1.tar.gz.

File metadata

  • Download URL: intervaltree_rs-0.1.1.tar.gz
  • Upload date:
  • Size: 635.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for intervaltree_rs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bbe59aded265c772433d5c83a065f7b53b482463dd34fd2293f85c63aac9ca91
MD5 e69126988fffab17e709e555f69aab94
BLAKE2b-256 e9d23d42d477ff13124e15e16a6d18660d6ccc13b890691787307a061707f697

See more details on using hashes here.

File details

Details for the file intervaltree_rs-0.1.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for intervaltree_rs-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33f961f4349d622ff593aef89563f8ff50e2c542cfbca04d6492a06d8b28eb54
MD5 739ca0e43a315e59460a522e56740781
BLAKE2b-256 49b367d99f0d2f63016d792b3c4894d80ceac56651fdfd018fb2ad9bdc5664f7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page