Skip to main content

Hashable-element interval set with first-insert ordered active queries.

Project description

PythonRangeable

PyPI Python License

Reference Python implementation of Rangeable<Element> — a generic, integer-coordinate, closed-interval set container with first-insert ordered active queries.

Installation

pip install rangeable

Usage

from dataclasses import dataclass
from rangeable import Rangeable

@dataclass(frozen=True, slots=True)
class Strong: pass

@dataclass(frozen=True, slots=True)
class Italic: pass

@dataclass(frozen=True, slots=True)
class Link:
    url: str

r: Rangeable = Rangeable()
r.insert(Strong(), start=2, end=5)
r.insert(Strong(), start=3, end=7)        # merges with [2, 5] → [2, 7]
r.insert(Strong(), start=9, end=11)       # disjoint
r.insert(Italic(), start=3, end=8)

r.get_range(Strong())   # [(2, 7), (9, 11)]
r.get_range(Italic())   # [(3, 8)]

r[4].objs               # (Strong(), Italic())   first-insert order
r[8].objs               # (Italic(),)
r[10].objs              # (Strong(),)

Sweep iteration via transitions

for event in r.transitions(lo=0, hi=15):
    print(event.coordinate, event.kind.value, event.element)

API

Member Returns Notes
Rangeable() constructor empty container
r.insert(e, *, start, end) Rangeable (chainable) raises InvalidIntervalError on start > end
r[i] Slot[E] Slot.objs is the active-set tuple
r.get_range(e) list[tuple[int, int]] merged disjoint ranges
r.transitions(*, lo, hi) list[TransitionEvent[E]] hi=None means +∞
r.count / len(r) int distinct elements
r.empty / bool(r) bool
iter(r) Iterator[(E, list[(int, int)])] first-insert order
r.copy() Rangeable[E] deep copy
r.version int unchanged on idempotent insert

Semantics

  • End is inclusive: [a, b] covers a..=b, both ends.
  • Same-element merging: equal elements (by __eq__ + __hash__) merge on overlap or integer adjacency. [2, 4] ∪ [5, 7] = [2, 7].
  • Idempotent insert: re-inserting a contained interval does not bump version.
  • Out-of-order rejected: r.insert(e, start=5, end=2) raises InvalidIntervalError.
  • Active-set ordering: deterministic — first-insert order of the element.
  • Coordinate sentinel: a close event for an interval ending at the optional int_max sentinel carries coordinate is None (None == +∞ per RFC §4.7). Python ints are unbounded, so this only matters when integrating with bounded-int languages; the fixture does not exercise it.

See RangeableRFC § 4 for normative semantics and § 10 for the 23-case test contract.

Cross-language consistency

This Python implementation joins the Ruby, Swift, JS, Kotlin and Go implementations. All six share a 160-op / 86-probe JSON fixture and produce byte-identical outputs.

See also

Development

python -m pip install -e ".[dev]"
pytest -q

The suite covers the full RFC § 10 contract, the cross-language fixture replay, and a property test against a brute-force oracle.

License

MIT (c) ZhgChgLi

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

rangeable-2.0.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

rangeable-2.0.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file rangeable-2.0.0.tar.gz.

File metadata

  • Download URL: rangeable-2.0.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for rangeable-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0934fa3ba93218767d2dde1a831287102a5b93b9fd6ddd47e2f1f4da280bce3a
MD5 261bb89f28a4b0f1f414344bc3838caa
BLAKE2b-256 beb7db2966a1bd2aeb6577360dae0990ed88d2cc02c982464b1c957ff644c425

See more details on using hashes here.

File details

Details for the file rangeable-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: rangeable-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for rangeable-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2cf5b62fcad50f3546ffcfe55584bb0894bdef09520aa9973d1f53afa5b7a5c3
MD5 f8f3ed24252b1784e924e27bef237b69
BLAKE2b-256 272f8a17c1c0dde6b58e8a24894d339c04e2d1c613907031997ff35c1635668e

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