CsrAdjacencyMatrix

Struct CsrAdjacencyMatrix 

Source
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

Source

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);
Source

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));
Source

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));
Source

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);
Source

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

Source§

fn clone(&self) -> CsrAdjacencyMatrix

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CsrAdjacencyMatrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CsrAdjacencyMatrix

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> IntoIterator for &'a CsrAdjacencyMatrix

Source§

type Item = (u64, u64)

The type of the elements being iterated over.
Source§

type IntoIter = CsrAdjacencyMatrixIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl MatrixRepresentation<u8> for CsrAdjacencyMatrix

Source§

fn dimension(&self) -> u64

Returns the dimension of the matrix. Read more
Source§

fn entry_count(&self) -> u64

Returns the number of non-zero entries in the matrix. Read more
Source§

fn get_entry(&self, col: u64, row: u64) -> u8

Returns the value contained at the specified entry in the matrix. If no entry exists, get_entry() will return zero. Read more
Source§

fn set_entry(&mut self, entry: u8, col: u64, row: u64)

Adds or overwrites the specified entry in the matrix with the the provided value. Read more
Source§

fn zero_entry(&mut self, col: u64, row: u64)

Sets the specified entry in the matrix to zero. Read more
Source§

impl ToString for CsrAdjacencyMatrix

Source§

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 ]

*/

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.