From 7ec6146da2ae3813800e9ebf388338c744296961 Mon Sep 17 00:00:00 2001 From: malatrax <71888134+zmalatrax@users.noreply.github.com> Date: Fri, 8 Nov 2024 21:53:48 +0100 Subject: [PATCH] feat: add sort to memory table (#38) --- .../src/components/memory/table.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/brainfuck_prover/src/components/memory/table.rs b/crates/brainfuck_prover/src/components/memory/table.rs index 45933fb..f0c8ca8 100644 --- a/crates/brainfuck_prover/src/components/memory/table.rs +++ b/crates/brainfuck_prover/src/components/memory/table.rs @@ -105,6 +105,11 @@ impl MemoryTable { pub fn get_row(&self, row: &MemoryTableRow) -> Option<&MemoryTableRow> { self.table.iter().find(|r| *r == row) } + + /// Sorts in-place the existing [`MemoryTableRow`] rows in the Memory Table by `mp`. + pub fn sort(&mut self) { + self.table.sort_by_key(|x| (x.mp, x.clk)); + } } #[cfg(test)] @@ -247,4 +252,20 @@ mod tests { // Check that the retrieved row is None assert!(retrieved.is_none(), "Should return None for a non-existing row."); } + + #[test] + fn test_sort() { + let mut memory_table = MemoryTable::new(); + let row1 = MemoryTableRow::new(BaseField::zero(), BaseField::zero(), BaseField::zero()); + let row2 = MemoryTableRow::new(BaseField::one(), BaseField::zero(), BaseField::zero()); + let row3 = MemoryTableRow::new(BaseField::zero(), BaseField::one(), BaseField::zero()); + memory_table.add_rows(vec![row3.clone(), row1.clone(), row2.clone()]); + + let mut expected_memory_table = MemoryTable::new(); + expected_memory_table.add_rows(vec![row1, row2, row3]); + + memory_table.sort(); + + assert_eq!(memory_table, expected_memory_table); + } }