https://github.com/noir-lang/sparse_array
https://github.com/noir-lang/sparse_array
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/noir-lang/sparse_array
- Owner: noir-lang
- License: apache-2.0
- Created: 2024-10-03T07:32:44.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-07-23T02:46:32.000Z (4 months ago)
- Last Synced: 2025-07-23T04:24:37.548Z (4 months ago)
- Language: Noir
- Size: 25.4 KB
- Stars: 2
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-noir - Sparse Array - efficient immutable and mutable sparse arrays (Libraries / Data Type Manipulation)
README
# sparse_array
Noir library that implements efficient sparse arrays, both constant (SparseArray) and mutable (MutSparseArray)
## Noir version compatibility
This library is tested with all Noir stable releases from v0.36.0.
## Benchmarks
TODO
## Installation
In your _Nargo.toml_ file, add the version of this library you would like to install under dependency:
```
[dependencies]
sparse_array = { tag = "v0.1.0", git = "https://github.com/noir-lang/sparse_array" }
```
## `sparse_array`
### Usage
```rust
use dep::sparse_array::{SparseArray, MutSparseArray}
// a sparse array of size 10,000 with 10 nonzero values
fn example_sparse_array(nonzero_indices: [Field; 10], nonzero_values: [Field; 10]) {
let sparse_array_size = 10000;
let array: SparseArray<10, Field> = SparseArray::create(nonzero_indices, nonzero_values, sparse_array_size);
assert(array.get(999) == 12345);
}
// a mutable sparse array that can contain up to 10 nonzero values
fn example_mut_sparse_array(initial_nonzero_indices: [Field; 9], initial_nonzero_values: [Field; 9]) {
let sparse_array_size = 10000;
let mut array: MutSparseArray<10, Field> = MutSparseArray::create(nonzero_indices, nonzero_values, sparse_array_size);
// update element 1234 to contain value 9999
array.set(1234, 9999);
// error, array can only contain 10 nonzero values
array.ser(10, 888);
}
```
# Costs
Constructing arrays is proportional to the number of nonzero entries in the array and very small ~10 gates per element (plus the cost of initializing range tables if not already done so)
Reading from `SparseArray` is 14.5 gates
Reading and writing to `MutSparseArray` is ~30 gates