Skip to main content

Highly optimized generalized N-dimensional data structures

Project description

N-Dimensional Data Structures (Python)

A pure-Python library providing generalized, highly optimized N-dimensional data structures. These structures use 1D list-flattening mathematics under the hood to ensure maximum performance and cache locality without relying on heavy C-extensions or Numpy.

Installation

The package is officially available on PyPI:

pip install ndim_ds

Data Structures Included

  1. Static N-Dim Prefix Sum (StaticNDimPrefixSum) Answers range sum queries offline. After all initial values are added, a sweep() computes prefix sums in $O(V)$ time, allowing subsequent $O(1)$ queries of any hyper-rectangular region.

  2. Static N-Dim Difference Array (StaticNDimDifferenceArray) Applies many range additions offline. After deferred $O(1)$ updates are queued, a sweep() runs in $O(V)$ time to finalize the grid, allowing subsequent $O(1)$ point queries.

  3. 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.

  4. 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.

  5. 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.

  6. 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 rigorously tested with aggressive $10^3$ iteration stress tests 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

ndim_ds-0.1.3.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

ndim_ds-0.1.3-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file ndim_ds-0.1.3.tar.gz.

File metadata

  • Download URL: ndim_ds-0.1.3.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.13

File hashes

Hashes for ndim_ds-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a09ba3eddfe96d6f38d7bae59b0f4b56d02d811f0e24e05cdda22ee4c93a3067
MD5 f09bcc5c2a00d69fbc6ab46db8251d6d
BLAKE2b-256 664b8fa3589afc93ba64b8ebb4b87b4ab19927b3b2fb02a24ecec4c39ce7618f

See more details on using hashes here.

File details

Details for the file ndim_ds-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ndim_ds-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.13

File hashes

Hashes for ndim_ds-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a179287bfa2f325656ec618b85e58e9a3d51c2624bbc9962b9e7d84c0e8a65af
MD5 a03606ea83117e22e5dbd7831b40a0f2
BLAKE2b-256 0980e82aa35f2f3ac30609a5a08e2c302e5847facbb0c49d4051e382366e4a72

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