Skip to main content

A collection of optimized N-dimensional data structures in Python

Project description

N-Dimensional Data Structures

A blazing-fast, polyglot (Python & C++) library providing generalized $N$-dimensional data structures.

Whether you are working in Python for data science and backend engineering, or you need highly-optimized header-only C++ templates to copy-paste into competitive programming platforms like Codeforces and LeetCode, this repository has you covered.

Data Structures Included

All structures support $O(1)$ or $O(\log^N(V))$ operations scaled effortlessly across any arbitrary number of dimensions.

  1. Static N-Dim Prefix Sum (static_n_dim_prefix_sum)
  2. Static N-Dim Difference Array (static_n_dim_difference_array)
  3. Dynamic N-Dim Fenwick Tree (dynamic_n_dim_fenwick_tree)
  4. Dynamic N-Dim Difference Fenwick Tree (dynamic_n_dim_diff_fenwick_tree)
  5. Dynamic N-Dim Range Fenwick Tree (dynamic_n_dim_range_fenwick_tree)
  6. Dynamic N-Dim Segment Tree (dynamic_n_dim_seg_tree)

C++ (Competitive Programming)

The cpp/include/ndim/ directory contains highly optimized, header-only C++17 templates designed specifically for competitive programming.

They use standard <bits/stdc++.h> format with using namespace std; to ensure they are instantly copy-pasteable into online judges without causing scope or header errors. Memory is maintained via flat, contiguous 1D vectors mapped algebraically to $N$ dimensions, preventing memory scattering and ensuring maximum CPU cache efficiency.

Example Usage

#include "dynamic_n_dim_seg_tree.hpp"

// Example: 3D Segment Tree using std::min
auto min_func = [](int64_t a, int64_t b) { return min(a, b); };
int64_t def = 1e18; // infinity

// 4x4x4 grid
DynamicNDimSegTree<int64_t, decltype(min_func)> tree({4, 4, 4}, min_func, def);

tree.update({1, 1, 1}, 5);
tree.update({2, 2, 2}, 10);

int64_t val = tree.query_range({0, 0, 0}, {3, 3, 3}); // 5

Python (Data Science / General)

The python/src/ directory contains the pure-Python implementations. They use 1D list-flattening mathematics similar to numpy under the hood, but operate purely on standard library primitives, ensuring maximum portability without heavy C-extension dependencies.

Installation

The library is configured using uv via pyproject.toml.

cd python
uv sync

Example Usage

from dynamic_n_dim_range_fenwick_tree import DynamicNDimRangeFenwickTree

# 10x10x10 grid
tree = DynamicNDimRangeFenwickTree([10, 10, 10])

# Add 50 to the bounding box from (1, 1, 1) to (5, 5, 5)
tree.add_range([1, 1, 1], [5, 5, 5], 50)

# Query the volume sum from (0, 0, 0) to (3, 3, 3)
total = tree.query_range([0, 0, 0], [3, 3, 3])

Testing

Both ecosystems are rigorously tested with aggressive $10^3$ iteration stress tests across $5$-dimensional constraints.

  • Python: Uses pytest. Run uv run pytest test/
  • C++: Uses doctest. Compile the files in cpp/test/ using g++ -std=c++17 and run the resulting executables.

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.0.tar.gz (9.6 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.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ndim_ds-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c4d083fd28f3a6123a2860e05cdbbc5912a1e85cd6505bed37e35d14c4094788
MD5 4c3be804e813e25d0847511fddfb359d
BLAKE2b-256 4a796b4b5bd561abd45221426cfd99d4ebb9f9d4fe03e6e6fff9a247f15d1afa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ndim_ds-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7b722fbc5b80e14985f253adce158ab84d44fd9f0ddacde8b696e40f7b6d475
MD5 a445fdc1eb034ac8e1718b0e723ac1f3
BLAKE2b-256 55b113e659a512d952d550a6536e52ce4c34939f6501e0b4377b5323e20686e7

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