Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,44 @@ where
// for any valid index, we want to ensure the following is satified:
// keys[X] <= index <= keys[X+1]
// this requires us to sort the keys, and insert a startpoint and endpoint
let sorted_keys = sort_advanced(input_keys, __sort, assert_sorted);

// insert start and endpoints
keys[0] = 0;
for i in 0..M {
keys[i + 1] = sorted_keys.sorted[i];
}
keys[M + 1] = maximum;

// sort_indices[i] is the sorted position of input key i, so write each
// input value to its key's sorted slot
for i in 0..M {
values[sorted_keys.sort_indices[i] + 2] = input_values[i];
}
// `sort_advanced` does not accept empty arrays, and there is nothing else to insert
if M > 0 {
let sorted_keys = sort_advanced(input_keys, __sort, assert_sorted);

// insert values that map to our key start and endpoints
// if 0 is an input key then it is keys[1], and values[1] must equal values[2], so some
// conditional logic is required (same for `maximum` and keys[M])
let mut initial_value = T::default();
if (keys[1] == 0) {
initial_value = values[2];
}
let mut final_value = T::default();
if (keys[M] == maximum) {
final_value = values[M + 1];
}
values[1] = initial_value;
values[M + 2] = final_value;
for i in 0..M {
keys[i + 1] = sorted_keys.sorted[i];
}

// sort_indices[i] is the sorted position of input key i, so write each
// input value to its key's sorted slot
for i in 0..M {
values[sorted_keys.sort_indices[i] + 2] = input_values[i];
}

// insert values that map to our key start and endpoints
// if 0 is an input key then it is keys[1], and values[1] must equal values[2], so some
// conditional logic is required (same for `maximum` and keys[M])
let mut initial_value = T::default();
if (keys[1] == 0) {
initial_value = values[2];
}
let mut final_value = T::default();
if (keys[M] == maximum) {
final_value = values[M + 1];
}
values[1] = initial_value;
values[M + 2] = final_value;

// perform boundary checks!
// because the keys are sorted, checking the largest key does not exceed
// `maximum` validates every key
assert(maximum >= sorted_keys.sorted[M - 1]);
// perform boundary checks!
// because the keys are sorted, checking the largest key does not exceed
// `maximum` validates every key
assert(maximum >= sorted_keys.sorted[M - 1]);
}
}

/**
Expand Down Expand Up @@ -257,6 +262,15 @@ mod test {
assert(example.values[5] == 90);
}

#[test]
fn test_sparse_lookup_empty() {
let example: SparseArray<0, Field> = SparseArray::create([], [], 3);

for i in 0..3 {
assert(example.get(i) == 0);
}
}

#[test]
fn test_sparse_lookup_u32() {
let example = SparseArray::create(
Expand Down
20 changes: 20 additions & 0 deletions src/mut_sparse_array.nr
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ mod test {
assert(example.get(3) == 0);
}

#[test]
fn test_create_empty() {
let mut example: MutSparseArray<3, Field> = MutSparseArray::create([], [], 3);
for i in 0..3 {
assert(example.get(i) == 0);
}

example.set(1, 10);
example.set(0, 20);
example.set(2, 30);
assert(example.get(0) == 20);
assert(example.get(1) == 10);
assert(example.get(2) == 30);

// size 1, so 0 is also `maximum`
let mut example: MutSparseArray<0, Field> = MutSparseArray::create([], [], 1);
example.set(0, 40);
assert(example.get(0) == 40);
}

#[test]
fn test_maximum_not_in_input() {
let mut example: MutSparseArray<1, Field> = MutSparseArray::create([5], [50], 10);
Expand Down
Loading