https://github.com/noir-lang/sparse_array
https://github.com/noir-lang/sparse_array
Last synced: 5 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 (7 months ago)
- Default Branch: master
- Last Pushed: 2024-11-14T14:03:19.000Z (5 months ago)
- Last Synced: 2024-11-14T15:19:06.006Z (5 months ago)
- Language: Noir
- Size: 18.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.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)
## 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