Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sort to memory table #38

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}