11use crate:: {MutSparseArray , MutSparseArrayBase , U32RangeTraits };
22use dep::sort::sort_advanced ;
3- unconstrained fn __sort_field_as_u32 (lhs : Field , rhs : Field ) -> bool {
4- lhs as u32 < rhs as u32
3+ unconstrained fn __sort_field_as_u32 (lhs : u32 , rhs : u32 ) -> bool {
4+ lhs < rhs
55}
66
7- fn assert_sorted (lhs : Field , rhs : Field ) {
8- let result = (rhs - lhs - 1 );
9- result .assert_max_bit_size ::<32 >();
7+ fn assert_sorted (lhs : u32 , rhs : u32 ) {
8+ assert (lhs < rhs );
109}
1110
1211trait RangeTraits {
6261 ComparisonFuncs : RangeTraits ,
6362{
6463
65- pub (crate ) fn create <let M : u32 >(_keys : [Field ; M ], _values : [T ; M ], size : Field ) -> Self {
64+ pub (crate ) fn create <let M : u32 >(_keys : [u32 ; M ], _values : [T ; M ], size : u32 ) -> Self {
6665 assert (M <= N );
67- let _maximum = size - 1 ;
66+ let _maximum : u32 = size - 1 ;
6867 let mut r : Self = MutSparseArrayBase {
6968 keys : [0 ; N + 2 ],
7069 values : [T ::default (); N + 3 ],
8685 r .keys [M + 1 ] = _maximum ;
8786
8887 for i in 0 ..M + 2 {
89- r .linked_keys [i ] = i as Field + 1 ;
88+ r .linked_keys [i ] = i + 1 ;
9089 }
91- r .linked_keys [M + 1 ] = -1 ;
90+ // set the last linked key to 2^32 - 1
91+ r .linked_keys [M + 1 ] = 0xFFFFFFFF ;
9292
9393 // populate values based on the sorted keys
9494 // note: self.keys[i] maps to self.values[i+1]
@@ -117,17 +117,17 @@ where
117117 // because `self.keys` is sorted, we can simply validate that
118118 // sorted_keys.sorted[0] < 2^32
119119 // sorted_keys.sorted[N-1] < maximum
120- ComparisonFuncs ::assert_greater_than_or_equal (sorted_keys .sorted [0 ], 0 );
121- ComparisonFuncs ::assert_greater_than_or_equal (_maximum , 0 );
122- ComparisonFuncs ::assert_greater_than_or_equal (_maximum , sorted_keys .sorted [M - 1 ]);
120+ // ComparisonFuncs::assert_greater_than_or_equal(sorted_keys.sorted[0], 0);
121+ // ComparisonFuncs::assert_greater_than_or_equal(_maximum, 0);
122+ // ComparisonFuncs::assert_greater_than_or_equal(_maximum, sorted_keys.sorted[M - 1]);
123123
124124 // _maximum.assert_max_bit_size::<32>();
125125 // (_maximum - sorted_keys.sorted[M - 1]).assert_max_bit_size::<32>();
126- r .tail_ptr = M as Field + 2 ;
126+ r .tail_ptr = M + 2 ;
127127 r
128128 }
129129
130- unconstrained fn search_for_key (self , target : Field ) -> (Field , Field ) {
130+ unconstrained fn search_for_key (self , target : u32 ) -> (u32 , u32 ) {
131131 let mut found = false ;
132132 let mut found_index = 0 ;
133133 let mut previous_less_than_or_equal_to_target = false ;
@@ -137,11 +137,11 @@ where
137137 let current_less_than_or_equal_to_target = self .keys [iterator ] as u64 <= target as u64 ;
138138 if (self .keys [iterator ] == target ) {
139139 found = true ;
140- found_index = iterator as Field ;
140+ found_index = iterator ;
141141 break ;
142142 }
143143 if (previous_less_than_or_equal_to_target & !current_less_than_or_equal_to_target ) {
144- found_index = prev as Field ;
144+ found_index = prev ;
145145 break ;
146146 }
147147 previous_less_than_or_equal_to_target = current_less_than_or_equal_to_target ;
@@ -150,17 +150,17 @@ where
150150 // }
151151 }
152152
153- (found as Field , found_index )
153+ (found as u32 , found_index )
154154 }
155155
156- unconstrained fn __check_if_can_insert (self , found : Field ) {
156+ unconstrained fn __check_if_can_insert (self , found : u32 ) {
157157 assert (
158- (found == 1 ) | (self .tail_ptr as u32 < N + 2 ),
158+ (found == 1 ) | (self .tail_ptr < N + 2 ),
159159 "MutSparseArray::set exceeded maximum size of array" ,
160160 );
161161 }
162162
163- fn set (&mut self , idx : Field , value : T ) {
163+ fn set (&mut self , idx : u32 , value : T ) {
164164 let (found , found_index ) = unsafe { self .search_for_key (idx ) };
165165 assert (found * found == found );
166166
@@ -183,10 +183,10 @@ where
183183 // `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
184184 let lhs = self .keys [lhs_index ];
185185 let rhs = self .keys [rhs_index ];
186- let lhs_condition = idx - lhs - 1 + found ;
187- let rhs_condition = rhs - 1 + found - idx ;
188- ComparisonFuncs ::assert_greater_than_or_equal (lhs_condition , 0 );
189- ComparisonFuncs ::assert_greater_than_or_equal (rhs_condition , 0 );
186+ let lhs_condition = idx + found - lhs - 1 ;
187+ let rhs_condition = rhs + found - 1 - idx ;
188+ // ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
189+ // ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
190190
191191 // lhs_condition.assert_max_bit_size::<32>();
192192 // rhs_condition.assert_max_bit_size::<32>();
@@ -205,7 +205,7 @@ where
205205 }
206206 }
207207
208- fn get (self , idx : Field ) -> T {
208+ fn get (self , idx : u32 ) -> T {
209209 let (found , found_index ) = unsafe { self .search_for_key (idx ) };
210210 println (f"f { found} fi { found_index} " );
211211 assert (found * found == found );
@@ -225,10 +225,10 @@ where
225225 // `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
226226 let lhs = self .keys [lhs_index ];
227227 let rhs = self .keys [rhs_index ];
228- let lhs_condition = idx - lhs - 1 + found ;
229- let rhs_condition = rhs - 1 + found - idx ;
230- ComparisonFuncs ::assert_greater_than_or_equal (lhs_condition , 0 );
231- ComparisonFuncs ::assert_greater_than_or_equal (rhs_condition , 0 );
228+ let lhs_condition = idx + found - lhs - 1 ;
229+ let rhs_condition = rhs + found - 1 - idx ;
230+ // ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
231+ // ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
232232
233233 // lhs_condition.assert_max_bit_size::<32>();
234234 // rhs_condition.assert_max_bit_size::<32>();
@@ -241,19 +241,19 @@ impl<let N: u32, T> MutSparseArray<N, T>
241241where
242242 T : std::default::Default ,
243243{
244- pub (crate ) fn create <let M : u32 >(_keys : [Field ; M ], _values : [T ; M ], size : Field ) -> Self {
244+ pub (crate ) fn create <let M : u32 >(_keys : [u32 ; M ], _values : [T ; M ], size : u32 ) -> Self {
245245 Self { inner : MutSparseArrayBase ::create (_keys , _values , size ) }
246246 }
247247
248- fn get (self , idx : Field ) -> T {
248+ fn get (self , idx : u32 ) -> T {
249249 self .inner .get (idx )
250250 }
251251
252- fn set (&mut self , idx : Field , value : T ) {
252+ fn set (&mut self , idx : u32 , value : T ) {
253253 self .inner .set (idx , value );
254254 }
255255
256- fn length (self ) -> Field {
256+ fn length (self ) -> u32 {
257257 self .inner .maximum + 1
258258 }
259259}
@@ -276,7 +276,7 @@ mod test {
276276 assert (example .get (55 ) == 333 );
277277 for i in 0 ..100 {
278278 if ((i != 1 ) & (i != 5 ) & (i != 7 ) & (i != 55 ) & (i != 99 )) {
279- assert (example .get (i as Field ) == 0 );
279+ assert (example .get (i ) == 0 );
280280 }
281281 }
282282 }
@@ -350,7 +350,7 @@ mod test {
350350
351351 for i in 0 ..100 {
352352 if ((i != 1 ) & (i != 5 ) & (i != 7 ) & (i != 99 )) {
353- assert (example .get (i as Field ) == 0 );
353+ assert (example .get (i ) == 0 );
354354 }
355355 }
356356 }
@@ -386,7 +386,7 @@ mod test {
386386 assert (example .get (99 ) == values [1 ]);
387387 for i in 0 ..100 {
388388 if ((i != 1 ) & (i != 5 ) & (i != 7 ) & (i != 99 )) {
389- assert (example .get (i as Field ) == F ::default ());
389+ assert (example .get (i ) == F ::default ());
390390 }
391391 }
392392 }
0 commit comments