pub struct CsrAdjacencyMatrix { /* private fields */ }Expand description
A matrix in CSR (Compressed Sparse Row) format. The matrix uses a HashMap to map columns to a HashSet of rows. Any matrix entry not contained in a HashSet is assumed to be zero.
use graphrox::matrix::{CsrAdjacencyMatrix, MatrixRepresentation};
let mut matrix = CsrAdjacencyMatrix::new();
matrix.set_entry(1, 0, 0);
matrix.set_entry(1, 1, 2);
assert_eq!(matrix.get_entry(0, 0), 1);
assert_eq!(matrix.get_entry(1, 2), 1);
assert_eq!(matrix.get_entry(2, 2), 0);Implementations§
Source§impl CsrAdjacencyMatrix
impl CsrAdjacencyMatrix
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty CsrAdjacencyMatrix.
use graphrox::matrix::{CsrAdjacencyMatrix, MatrixRepresentation};
let matrix = CsrAdjacencyMatrix::new();
assert_eq!(matrix.dimension(), 0);
assert_eq!(matrix.entry_count(), 0);Sourcepub fn get_sparse_col_vector(&self, col: u64) -> Vec<u64>
pub fn get_sparse_col_vector(&self, col: u64) -> Vec<u64>
Returns a list of non-zero entries in a column.
use graphrox::matrix::{CsrAdjacencyMatrix, MatrixRepresentation};
let mut matrix = CsrAdjacencyMatrix::new();
matrix.set_entry(1, 5, 2);
matrix.set_entry(1, 5, 10);
matrix.set_entry(1, 5, 11);
matrix.set_entry(1, 9, 5);
let col_vector = matrix.get_sparse_col_vector(5);
assert_eq!(col_vector.len(), 3);
assert!(col_vector.contains(&2));
assert!(col_vector.contains(&10));
assert!(col_vector.contains(&11));Sourcepub fn get_sparse_row_vector(&self, row: u64) -> Vec<u64>
pub fn get_sparse_row_vector(&self, row: u64) -> Vec<u64>
Returns a list of non-zero entries in a row.
§Performance
Because the matrix is represented sparsely in memory by a HashMap mapping a column index to a HashSet of row indices, obtaining the sparse row vector requires iterating through all the columns in the matrix and checking whether the row is in the set corresponding to each column. Obtaining the row vector is a much more computationally expensive operation than finding a column vector.
use graphrox::matrix::{CsrAdjacencyMatrix, MatrixRepresentation};
let mut matrix = CsrAdjacencyMatrix::new();
matrix.set_entry(1, 2, 5);
matrix.set_entry(1, 10, 5);
matrix.set_entry(1, 11, 5);
matrix.set_entry(1, 5, 9);
let row_vector = matrix.get_sparse_row_vector(5);
assert_eq!(row_vector.len(), 3);
assert!(row_vector.contains(&2));
assert!(row_vector.contains(&10));
assert!(row_vector.contains(&11));Sourcepub fn col_nonzero_entry_count(&self, col: u64) -> u64
pub fn col_nonzero_entry_count(&self, col: u64) -> u64
Returns a count of the non-zero entries in a column.
use graphrox::matrix::{CsrAdjacencyMatrix, MatrixRepresentation};
let mut matrix = CsrAdjacencyMatrix::new();
matrix.set_entry(1, 5, 2);
matrix.set_entry(1, 5, 10);
matrix.set_entry(1, 5, 11);
matrix.set_entry(1, 9, 5);
assert_eq!(matrix.col_nonzero_entry_count(5), 3);Sourcepub fn row_nonzero_entry_count(&self, row: u64) -> u64
pub fn row_nonzero_entry_count(&self, row: u64) -> u64
Returns a count of the non-zero entries in a row.
§Performance
Because the matrix is represented sparsely in memory by a HashMap mapping a column index to a HashSet of row indices, counting a row’s entries requires iterating through all the columns in the matrix and checking whether the row is in the set corresponding to each column. Obtaining the entry count in a row is a much more computationally expensive operation than finding the entry count for a column.
use graphrox::matrix::{CsrAdjacencyMatrix, MatrixRepresentation};
let mut matrix = CsrAdjacencyMatrix::new();
matrix.set_entry(1, 2, 5);
matrix.set_entry(1, 10, 5);
matrix.set_entry(1, 11, 5);
matrix.set_entry(1, 5, 9);
assert_eq!(matrix.row_nonzero_entry_count(5), 3);Trait Implementations§
Source§impl Clone for CsrAdjacencyMatrix
impl Clone for CsrAdjacencyMatrix
Source§fn clone(&self) -> CsrAdjacencyMatrix
fn clone(&self) -> CsrAdjacencyMatrix
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CsrAdjacencyMatrix
impl Debug for CsrAdjacencyMatrix
Source§impl Default for CsrAdjacencyMatrix
impl Default for CsrAdjacencyMatrix
Source§impl<'a> IntoIterator for &'a CsrAdjacencyMatrix
impl<'a> IntoIterator for &'a CsrAdjacencyMatrix
Source§impl MatrixRepresentation<u8> for CsrAdjacencyMatrix
impl MatrixRepresentation<u8> for CsrAdjacencyMatrix
Source§fn entry_count(&self) -> u64
fn entry_count(&self) -> u64
Source§fn get_entry(&self, col: u64, row: u64) -> u8
fn get_entry(&self, col: u64, row: u64) -> u8
get_entry() will return zero. Read moreSource§impl ToString for CsrAdjacencyMatrix
impl ToString for CsrAdjacencyMatrix
Source§fn to_string(&self) -> String
fn to_string(&self) -> String
Generates a string representation of a CsrAdjacencyMatrix.
use graphrox::matrix::{CsrAdjacencyMatrix, MatrixRepresentation};
let mut matrix = CsrAdjacencyMatrix::new();
matrix.set_entry(1, 0, 0);
matrix.set_entry(1, 1, 2);
matrix.set_entry(1, 2, 1);
matrix.set_entry(1, 1, 0);
println!("{}", matrix.to_string());
/* Output:
[ 1, 1, 0 ]
[ 0, 0, 1 ]
[ 0, 1, 0 ]
*/