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

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

n_dim_data_structures-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for n_dim_data_structures-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2febca45a8873445548cc4d4af68bfa92a30a911badd36abc4d02566c0089430
MD5 75e9d94c4f74bd085d8261c6d2ee959d
BLAKE2b-256 0ccb0ae2403960b200d0be303b67ad860e77655358b5f1843994cb68b23a2b9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for n_dim_data_structures-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fcc91bda0602a5c8a83dacbd7fa760816c0967a5190016ef044acd9b0985d49
MD5 06396d39887f423992d8a1c954ec1383
BLAKE2b-256 61c54c38617252f854cdd9cf127e603700469ead8cdc4993675cfab051e69069

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