fuzzyhash/src/matrix.rs

62 lines
1.4 KiB
Rust
Raw Normal View History

2021-08-15 22:21:46 +00:00
use ff::PrimeField;
2021-08-14 17:57:08 +00:00
/// A dense matrix. Apple have said t and s are going to be on the order of 30 so I feel
/// you can probably always get away with a dense representation
2021-08-15 22:21:46 +00:00
pub struct Matrix<PF>
where
PF: PrimeField,
{
2021-08-14 17:57:08 +00:00
rows: usize,
cols: usize,
2021-08-15 22:21:46 +00:00
vals: Vec<Vec<PF>>,
2021-08-14 17:57:08 +00:00
}
2021-08-15 22:21:46 +00:00
impl<PF> Matrix<PF>
where
PF: PrimeField,
{
pub fn new(rows: usize, cols: usize) -> Matrix<PF> {
2021-08-14 17:57:08 +00:00
let mut vals = vec![];
for _i in 0..rows {
let mut row = vec![];
for _j in 0..cols {
2021-08-15 22:21:46 +00:00
row.push(PF::zero())
2021-08-14 17:57:08 +00:00
}
vals.push(row)
}
Matrix { rows, cols, vals }
}
pub fn rows(&self) -> usize {
self.rows
}
pub fn cols(&self) -> usize {
self.cols
}
2021-08-15 22:21:46 +00:00
pub fn update(&mut self, row: usize, col: usize, val: PF) {
2021-08-14 17:57:08 +00:00
self.vals[row][col] = val
}
2021-08-15 22:21:46 +00:00
pub fn at(&self, row: usize, col: usize) -> PF {
2021-08-14 17:57:08 +00:00
self.vals[row][col]
}
2021-08-15 22:21:46 +00:00
pub fn transpose(&self) -> Matrix<PF> {
2021-08-14 17:57:08 +00:00
let mut m = Matrix::new(self.cols, self.rows);
for i in 0..self.cols {
for j in 0..self.rows {
m.update(i, j, self.vals[j][i]);
}
}
m
}
pub fn swap_rows(&mut self, a: usize, b: usize) {
let tmp = self.vals[a].clone();
self.vals[a] = self.vals[b].clone();
self.vals[b] = tmp;
}
}