@@ -32,14 +32,18 @@ impl<T, const N: usize> Storage<T, N> {
3232 self . insertion_buffer . force_push ( value) ;
3333 }
3434
35- /// Folds over the elements in the storage using the provided function.
36- pub fn fold < U , F > ( & self , init : U , mut func : F ) -> U
35+ /// Folds over the elements in the storage in reverse order using the provided function.
36+ pub fn rfold < U , F > ( & self , init : U , mut func : F ) -> U
3737 where
3838 F : FnMut ( U , & T ) -> U ,
3939 {
40- let mut acc = init ;
40+ let mut items = Vec :: new ( ) ;
4141 while let Some ( value) = self . insertion_buffer . pop ( ) {
42- acc = func ( acc, & value) ;
42+ items. push ( value) ;
43+ }
44+ let mut acc = init;
45+ for value in items. iter ( ) . rev ( ) {
46+ acc = func ( acc, value) ;
4347 }
4448 acc
4549 }
@@ -59,18 +63,18 @@ mod tests {
5963 let storage = Storage :: < i32 , 10 > :: new ( ) ;
6064 storage. insert ( 7 ) ;
6165
62- let sum = storage. fold ( 0 , |acc, & x| acc + x) ;
66+ let sum = storage. rfold ( 0 , |acc, & x| acc + x) ;
6367 assert_eq ! ( sum, 7 , "The sum of the elements should be equal to the inserted value." ) ;
6468 }
6569
6670 #[ test]
67- fn test_fold_functionality ( ) {
71+ fn test_rfold_functionality ( ) {
6872 let storage = Storage :: < i32 , 5 > :: new ( ) ;
6973 storage. insert ( 1 ) ;
7074 storage. insert ( 2 ) ;
7175 storage. insert ( 3 ) ;
7276
73- let sum = storage. fold ( 0 , |acc, & x| acc + x) ;
77+ let sum = storage. rfold ( 0 , |acc, & x| acc + x) ;
7478 assert_eq ! (
7579 sum, 6 ,
7680 "The sum of the elements should be equal to the sum of inserted values."
@@ -84,13 +88,13 @@ mod tests {
8488 storage. insert ( 2 ) ;
8589 storage. insert ( 5 ) ;
8690
87- let first_sum = storage. fold ( 0 , |acc, & x| acc + x) ;
91+ let first_sum = storage. rfold ( 0 , |acc, & x| acc + x) ;
8892 assert_eq ! ( first_sum, 8 , "The sum of the elements should be equal to the inserted values." ) ;
8993
9094 storage. insert ( 30 ) ;
9195 storage. insert ( 22 ) ;
9296
93- let second_sum = storage. fold ( 0 , |acc, & x| acc + x) ;
97+ let second_sum = storage. rfold ( 0 , |acc, & x| acc + x) ;
9498 assert_eq ! (
9599 second_sum, 52 ,
96100 "The sum of the elements should be equal to the inserted values."
@@ -103,7 +107,7 @@ mod tests {
103107 storage. insert ( 1 ) ;
104108 // This value should overwrite the previously inserted value (1).
105109 storage. insert ( 4 ) ;
106- let sum = storage. fold ( 0 , |acc, & x| acc + x) ;
110+ let sum = storage. rfold ( 0 , |acc, & x| acc + x) ;
107111 assert_eq ! ( sum, 4 , "The sum of the elements should be equal to the inserted values." ) ;
108112 }
109113
@@ -128,7 +132,24 @@ mod tests {
128132 thread. join ( ) . expect ( "Thread should finish without panicking" ) ;
129133 }
130134
131- let count = storage. fold ( 0 , |acc, _| acc + 1 ) ;
135+ let count = storage. rfold ( 0 , |acc, _| acc + 1 ) ;
132136 assert_eq ! ( count, 100 , "Storage should be filled to its limit with concurrent insertions." ) ;
133137 }
138+
139+ #[ test]
140+ fn test_rfold_order ( ) {
141+ let storage = Storage :: < i32 , 5 > :: new ( ) ;
142+ storage. insert ( 1 ) ;
143+ storage. insert ( 2 ) ;
144+ storage. insert ( 3 ) ;
145+
146+ let mut result = Vec :: new ( ) ;
147+ storage. rfold ( ( ) , |_, & x| result. push ( x) ) ;
148+
149+ assert_eq ! (
150+ result,
151+ vec![ 3 , 2 , 1 ] ,
152+ "Elements should be processed in reverse order of insertion"
153+ ) ;
154+ }
134155}
0 commit comments