[//]: # (---)
[//]: # (SPDX-License-Identifier: MIT)
[//]: # (---)
# 🏁 Data structures 🏁
[![Crates.io][crates-badge]][crates-url]
[![Docs.rs][docs-badge]][docs-url]
[![MIT licensed][mit-badge]][mit-url]
[crates-badge]: https://img.shields.io/badge/Crates.io-Latest-blue
[crates-url]: https://crates.io/crates/deep_causality_data_structures
[docs-badge]: https://img.shields.io/badge/Docs.rs-Latest-blue
[docs-url]: https://docs.rs/deep_causality_data_structures/latest/deep_causality_data_structures/
[mit-badge]: https://img.shields.io/badge/License-MIT-blue.svg
[mit-url]: https://github.com/deepcausality-rs/deep_causality/blob/main/LICENSE
This crate provides two data structures used in [DeepCausality](https://github.com/deepcausality-rs/deep_causality):
ArrayGrid and SlidingWindow.
ArrayGrid abstracts over scalars, vectors, and low-dimensional matrices, similar to a tensor but
limited to 1 to 4 dimensions. Every ArrayGrid is a static, fixed-size const generic array.
Because all structural parameters are known at compile time, the compiler can lay the data out
cache-aligned, which makes ArrayGrid faster than a tensor.
The sliding window over-allocates, trading memory for time by delaying the rewind when it reaches
the end of the underlying storage. A sliding window of size N holds approximately C-1 elements
without any array copy, where the capacity C is NxM with N the window size and M a multiple.
The crate implements the window over a vector and over a const generic array; the const generic
version is significantly faster.
## Why?
1) Zero cost abstraction.
2) Zero unsafe.
3) Zero macros.
4) Zero external dependencies.
# Performance:
## ArrayGrid
**Set value:**
| 1D Grid | 604.71 ps | 271.38 ps | 55.1% |
| 2D Grid | 581.33 ps | 417.39 ps | 28.2% |
| 3D Grid | 862.16 ps | 577.04 ps | 33.0% |
| 4D Grid | 1.137 ns | 812.62 ps | 28.5% |
See the [Performance](README_ArrayGrid.md#performance) section
of the [ArrayGrid document](README_ArrayGrid.md) for details.
## Sliding Window
**Single Push:**
| ArrayStorage | ~2.08ns | Optimized for continuous access patterns |
| VectorStorage | ~2.5ns | Good for dynamic sizing |
| UnsafeVectorStorage | ~2.3ns | Better performance than safe vector |
| UnsafeArrayStorage | ~1.9ns | Best performance for sequential and batch operations |
**Sequential Operations:**
| UnsafeArrayStorage | ~550ps | Best cache utilization |
| ArrayStorage | ~605ps | Excellent cache locality |
| UnsafeVectorStorage | ~750ps | Good for mixed workloads |
| VectorStorage | ~850ps | Most predictable |
See the [Performance](README_SlidingWindow.md#performance) section
of the [SlidingWindow document](README_SlidingWindow.md) for details.
## Install
Run:
```bash
cargo add deep_causality_data_structures
```
## Build configurations
| `std`, hosted | default | ✓ |
| `no-std` with a heap | `default-features = false, features = ["no-std", "alloc"]` | ✓ |
| `no-std` on `core` alone, no heap | `default-features = false, features = ["no-std"]` | ✓ |
With `no-std` alone the crate needs only `core` and links into a program that defines no `#[global_allocator]`; `alloc` adds the parts that need a heap. `VectorStorage`, `window_type::new_with_vector_storage` and the `vec()` accessor need `alloc`.
CI builds every supported bare-metal configuration for `thumbv7em-none-eabihf` and links the `core` configuration without an allocator.
[README_NO_STD.md](https://github.com/deepcausality-rs/deep_causality/blob/main/README_NO_STD.md) describes the build configuration of every crate in the workspace.
## Configuration
The crate builds without the standard library, on `core` alone.
* `default`: Enables `std`.
* `std`: Uses the standard library (includes `alloc`).
* `no-std`: Builds on `core` alone; no `#[global_allocator]` is required. `ArrayGrid`,
`ArrayStorage` and `SlidingWindow` over it are available.
* `alloc`: Adds the `Vec`-backed parts: `VectorStorage`, `new_with_vector_storage` and the
`vec()` accessor. Select with `default-features = false, features = ["no-std", "alloc"]`.
Window accessors report an empty or unfilled window as a `&'static str` message.
## Docs
* [API Docs](https://docs.rs/deep_causality_data_structures/latest/deep_causality_data_structures)
* [ArrayGrid Summary](README_ArrayGrid.md)
* [CausalTensor Summary](../deep_causality_tensor/README.md)
* [SlidingWindow Summary](README_SlidingWindow)
## Usage
**ArrayGrid:**
* [Design & Details](README_ArrayGrid)
* [Benchmark](benches/benchmarks)
* [Examples](examples/array_grid)
* [Test](tests/grid_type)
* **SlidingWindow:**
* [Design & Details](README_SlidingWindow.md)
* [Benchmark](benches/benchmarks)
* [Examples](examples/window_type)
* [Test](tests/window_type)
## Prior Art
The project took inspiration from:
* [sliding_features](https://crates.io/crates/sliding_features)
* [sliding-window-aggregation](https://crates.io/crates/sliding-window-aggregation)
* [sliding_window_alt](https://crates.io/crates/sliding_window_alt)
* [sliding_windows](https://crates.io/crates/sliding_windows)
## Contribution
Contributions are welcome, especially documentation, example code, and fixes.
If unsure where to start, open an issue and ask.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in deep_causality by you,
shall be licensed under the MIT licence, without any additional terms or conditions.
## Licence
This project is licensed under the [MIT license](LICENSE).
## Security
For details about security, please read
the [security policy](https://github.com/deepcausality-rs/deep_causality/blob/main/SECURITY.md).