Skip to main content

Discrete interval storage and algebra on integers (allowing for positive and negative infinity), stored as coalesced intervals.

Project description

drange

Discrete interval algebra on integers (with +/- infinity), stored as coalesced runs.

  • Fast set-like operations over huge or infinite sets of integers.
  • Canonical internal form: sorted, disjoint, adjacency-merged closed intervals.
  • Human-readable serialization with simple round-trips.
  • Hashable, fully typed, and tested.

Examples: ([1:3],[7:9]) # {1,2,3,7,8,9} () # empty ([<:>]) # universal ([<:5],[7:12]) # negative infinity to 5, plus 7 through 12 ([-10:>]) # all naturals from -10 to positive infinity (4,5,10,20,[22:25]) # singletons amid runs. Note 4 and 5 ([4:5],10,20,[22:25]) # [4:5] would also be valid


Install

pip install drange

## How-tos

### Import
from drange.interval_set import IntervalSet

### Build from disparate sources:
A = IntervalSet.from_items([1,2,2,3,10,11,12])  # -> ([1:3],[10:12])
B = IntervalSet.from_string("([<:0],[100:>])")

### Merge adjacent and overlapping ranges:
A = IntervalSet.from_string("([1:3],[4:6])")
assert A.to_string() == "([1:6])"

### Compute complement within a finite window
S = IntervalSet.from_string("([2:5],[9:10])")
neg = ~S                                    # -> ([<:1],[6:8],[11:>])
list_not_S = list(neg.iter_values(0,12))    # -> [0,1,6,7,8,11,12]
S_not = IntervalSet.from_items(list_not_s)  # -> (0,1,[6:8],11,12)

### Subset checks (allowing infinities)
A = IntervalSet.from_string("([<:10])")
B = IntervalSet.from_string("([<:20])")
assert A <= B and A < B

### Equality
A = IntervalSet.from_string("([1:4])")
B = IntervalSet.from_string("([3:6])")
lhs = A ^ B
rhs = (A | B) - (A & B)
assert lhs == rhs

### Serialization/deserialization
s = "([4:5],10,15,20,[22:25],[28:29])"
iv_set = IntervalSet.from_string(s)
assert iv_set.to_string() == s


## Performance notes
- Operations are linear in the number of stored runs (`r`), not in the number of elements.
- Union/intersection/difference/symmetric-difference: O(A.r + B.r)
- Membership test: O(log r) via binary search on starts.
- Memory scales with the number of runs, not elements.

## License
MIT. See LICENSE

## Changelog
See CHANGELOG.md. Current release: 0.1.0

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

drange-0.1.0.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

drange-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: drange-0.1.0.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for drange-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bf5ed0b0fa48b019eb3ed15423c3717085f201e3f67dec0674164b94c04105b3
MD5 02c546aeef2b3d3f7d6f77cfa3f43723
BLAKE2b-256 93e29deaa1712d2d18ba60b86c6ec49e3072f4865bea0045dbc4894114cda3c6

See more details on using hashes here.

File details

Details for the file drange-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: drange-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for drange-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e7995378238714632747ab3e1e27368f7ecfa90bd9f35126b1ad820a405ecb1e
MD5 0c9c3ed8b72807dd0248758ca1e7c627
BLAKE2b-256 66c7bd070df23400189542db831ba0629d475a6fb7489a0f6ddd8183bfeda096

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