From 4be3491b6adde5b091164a123018ae6b0b0bb1df Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Sun, 1 Nov 2026 10:25:19 +0000 Subject: [PATCH] fix: support creating sparse arrays from empty inputs create() always sorted its input keys, and `sort_advanced` cannot be compiled for an empty array, so neither array could be created without initial entries, e.g. an initially empty MutSparseArray. Skip the sort and value insertion when there are no input keys. --- src/lib.nr | 66 +++++++++++++++++++++++++---------------- src/mut_sparse_array.nr | 20 +++++++++++++ 2 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/lib.nr b/src/lib.nr index 7b91d55..6946cb6 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -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]); + } } /** @@ -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( diff --git a/src/mut_sparse_array.nr b/src/mut_sparse_array.nr index 9a80310..cdc6194 100644 --- a/src/mut_sparse_array.nr +++ b/src/mut_sparse_array.nr @@ -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);