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.
- Static N-Dim Prefix Sum (
static_n_dim_prefix_sum) - Static N-Dim Difference Array (
static_n_dim_difference_array) - Dynamic N-Dim Fenwick Tree (
dynamic_n_dim_fenwick_tree) - Dynamic N-Dim Difference Fenwick Tree (
dynamic_n_dim_diff_fenwick_tree) - Dynamic N-Dim Range Fenwick Tree (
dynamic_n_dim_range_fenwick_tree) - 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. Runuv run pytest test/ - C++: Uses
doctest. Compile the files incpp/test/usingg++ -std=c++17and run the resulting executables.
Project details
Release history Release notifications | RSS feed
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 n_dim_data_structures-0.1.0.tar.gz.
File metadata
- Download URL: n_dim_data_structures-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2febca45a8873445548cc4d4af68bfa92a30a911badd36abc4d02566c0089430
|
|
| MD5 |
75e9d94c4f74bd085d8261c6d2ee959d
|
|
| BLAKE2b-256 |
0ccb0ae2403960b200d0be303b67ad860e77655358b5f1843994cb68b23a2b9d
|
File details
Details for the file n_dim_data_structures-0.1.0-py3-none-any.whl.
File metadata
- Download URL: n_dim_data_structures-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fcc91bda0602a5c8a83dacbd7fa760816c0967a5190016ef044acd9b0985d49
|
|
| MD5 |
06396d39887f423992d8a1c954ec1383
|
|
| BLAKE2b-256 |
61c54c38617252f854cdd9cf127e603700469ead8cdc4993675cfab051e69069
|