Skip to content

Commit

Permalink
Merge branch 'main' into vm-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Nov 12, 2024
2 parents 9b11fea + 7ec6146 commit 0f77f37
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crates/brainfuck_prover/src/components/memory/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 0f77f37

Please sign in to comment.