Generalized N-dimensional data structures
Project description
N-Dimensional Data Structures (Python)
A pure-Python library providing generalized N-dimensional data structures. These structures use 1D list-flattening mathematics under the hood to improve cache locality without relying on C-extensions or Numpy.
Installation
The package is officially available on PyPI:
pip install ndim_ds
Data Structures Included
-
Static N-Dim Prefix Sum (
StaticNDimPrefixSum) Answers range sum queries offline. After all initial values are added, asweep()computes prefix sums in $O(V)$ time, allowing subsequent $O(1)$ queries of any hyper-rectangular region. -
Static N-Dim Difference Array (
StaticNDimDifferenceArray) Applies many range additions offline. After deferred $O(1)$ updates are queued, asweep()runs in $O(V)$ time to finalize the grid, allowing subsequent $O(1)$ point queries. -
Dynamic N-Dim Fenwick Tree (
DynamicNDimFenwickTree) Also known as a Binary Indexed Tree. Supports $O(\log^N(V))$ point updates and prefix sum queries across $N$ dimensions. -
Dynamic N-Dim Difference Fenwick Tree (
DynamicNDimDiffFenwickTree) A Fenwick tree modified to support $O(\log^N(V))$ range updates and point queries across $N$ dimensions. -
Dynamic N-Dim Range Fenwick Tree (
DynamicNDimRangeFenwickTree) Supports $O(\log^N(V))$ range updates and range sum queries across $N$ dimensions. Internally uses $2^N$ algebraic Fenwick trees to correctly scale the range updates. -
Dynamic N-Dim Segment Tree (
DynamicNDimSegTree) Supports $O(\log^N(V))$ point updates and range queries across $N$ dimensions for any associative operation (e.g.,min,max,gcd).
Global Usage
Once installed globally via pip, you can directly import the data structures into any Python script on your machine.
Example
from ndim_ds import StaticNDimPrefixSum, DynamicNDimRangeFenwickTree
# --- Static Example ---
# Initialize a 10x10x10 static grid
grid = StaticNDimPrefixSum([10, 10, 10])
grid.add([1, 1, 1], 50)
grid.add([5, 5, 5], 25)
grid.sweep() # Finalize the grid in O(V) time
# Query the volume sum from (0, 0, 0) to (3, 3, 3) in O(1) time
total_static = grid.query_range([0, 0, 0], [3, 3, 3])
# --- Dynamic Example ---
# Initialize a 10x10x10 dynamic range tree
tree = DynamicNDimRangeFenwickTree([10, 10, 10])
# Add 50 to the bounding box from (1, 1, 1) to (5, 5, 5) in O(log^N(V)) time
tree.add_range([1, 1, 1], [5, 5, 5], 50)
# Query the volume sum from (0, 0, 0) to (3, 3, 3) in O(log^N(V)) time
total_dynamic = tree.query_range([0, 0, 0], [3, 3, 3])
Testing
The codebase is tested with randomized operations across 5-dimensional constraints using pytest.
To run tests locally from source:
uv run pytest 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ndim_ds-0.1.4.tar.gz.
File metadata
- Download URL: ndim_ds-0.1.4.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8cc2733eac6336b146615244c5bad985d75cb5bdf14aa16516e7f26aa62894d
|
|
| MD5 |
acc6707ffe01145b70ae2da8e257f4d0
|
|
| BLAKE2b-256 |
08f580d6e0ed4fb2365ff2a95b7f6d44109154c716aa9e7b909a0009b9177c75
|
File details
Details for the file ndim_ds-0.1.4-py3-none-any.whl.
File metadata
- Download URL: ndim_ds-0.1.4-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcd6bb7d56f6224b405538250c7a56d3feb841a845794be9f92d16db7341abb7
|
|
| MD5 |
267d3cae37f332a03897351436644adf
|
|
| BLAKE2b-256 |
efa62ee39c9ed5033a359b72394f711e930ce6e2691e1ab31af0e7c60d7d3080
|