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

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.0.tar.gz (624.6 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.0-cp38-abi3-macosx_11_0_arm64.whl (214.8 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for intervaltree_rs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eeee0fd725a157a1954730bbb49c26f688cd5f5c58f2a58c10d7099544eb33e6
MD5 7a7df65d0b13e80f9479b2890296237e
BLAKE2b-256 c1f62568a5072419b974d37c0f05d64cd72fa112d27cb110c906c1c349c6a801

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for intervaltree_rs-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 87c94418bfa8b9d35cad1787ba25f0130e9f92be72cb9327b4bed3da8890cb4d
MD5 457432a88cb488ffb45b532364efb68c
BLAKE2b-256 1a4f181cdb8548b5fd6393aafda43269976ed0c504eb509b0196d1c89f1ab76b

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