Skip to content

Commit

Permalink
Add examples to all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeisler committed Sep 17, 2023
1 parent 539050a commit ce5ec7c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/brute_force.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ pub fn lane_minimum<T: Ord>(lane: ArrayView1<'_, T>) -> usize {

/// Compute row minima by brute force in O(*mn*) time.
///
/// This function implements a simple brute-force approach where each
/// matrix row is scanned completely. This means that the function
/// works on all matrices, not just Monge matrices.
///
/// # Examples
///
/// ```
/// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
/// [5, 3, 5, 3],
/// [5, 3, 3, 1]]);
/// assert_eq!(smawk::brute_force::row_minima(&matrix),
/// vec![1, 1, 3]);
/// ```
///
/// # Panics
///
/// It is an error to call this on a matrix with zero columns.
Expand All @@ -31,6 +45,20 @@ pub fn row_minima<T: Ord>(matrix: &Array2<T>) -> Vec<usize> {

/// Compute column minima by brute force in O(*mn*) time.
///
/// This function implements a simple brute-force approach where each
/// matrix column is scanned completely. This means that the function
/// works on all matrices, not just Monge matrices.
///
/// # Examples
///
/// ```
/// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
/// [5, 3, 5, 3],
/// [5, 3, 3, 1]]);
/// assert_eq!(smawk::brute_force::column_minima(&matrix),
/// vec![0, 0, 2, 2]);
/// ```
///
/// # Panics
///
/// It is an error to call this on a matrix with zero rows.
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ impl<T: Copy> Matrix<T> for ndarray::Array2<T> {
///
/// Running time on an *m* ✕ *n* matrix: O(*m* + *n*).
///
/// # Examples
///
/// ```
/// use smawk::{Matrix, smawk_row_minima};
/// let matrix = vec![vec![4, 2, 4, 3],
/// vec![5, 3, 5, 3],
/// vec![5, 3, 3, 1]];
/// assert_eq!(smawk_row_minima(&matrix),
/// vec![1, 1, 3]);
/// ```
///
/// # Panics
///
/// It is an error to call this on a matrix with zero columns.
Expand Down Expand Up @@ -196,6 +207,17 @@ pub fn smawk_row_minima<T: PartialOrd + Copy, M: Matrix<T>>(matrix: &M) -> Vec<u
///
/// Running time on an *m* ✕ *n* matrix: O(*m* + *n*).
///
/// # Examples
///
/// ```
/// use smawk::{Matrix, smawk_column_minima};
/// let matrix = vec![vec![4, 2, 4, 3],
/// vec![5, 3, 5, 3],
/// vec![5, 3, 3, 1]];
/// assert_eq!(smawk_column_minima(&matrix),
/// vec![0, 0, 2, 2]);
/// ```
///
/// # Panics
///
/// It is an error to call this on a matrix with zero rows.
Expand Down
30 changes: 30 additions & 0 deletions src/recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ use ndarray::{s, Array2, ArrayView2, Axis};

/// Compute row minima in O(*m* + *n* log *m*) time.
///
/// This function computes row minima in a totally monotone matrix
/// using a recursive algorithm.
///
/// Running time on an *m* ✕ *n* matrix: O(*m* + *n* log *m*).
///
/// # Examples
///
/// ```
/// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
/// [5, 3, 5, 3],
/// [5, 3, 3, 1]]);
/// assert_eq!(smawk::recursive::row_minima(&matrix),
/// vec![1, 1, 3]);
/// ```
///
/// # Panics
///
/// It is an error to call this on a matrix with zero columns.
Expand All @@ -21,6 +36,21 @@ pub fn row_minima<T: Ord>(matrix: &Array2<T>) -> Vec<usize> {

/// Compute column minima in O(*n* + *m* log *n*) time.
///
/// This function computes column minima in a totally monotone matrix
/// using a recursive algorithm.
///
/// Running time on an *m* ✕ *n* matrix: O(*n* + *m* log *n*).
///
/// # Examples
///
/// ```
/// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
/// [5, 3, 5, 3],
/// [5, 3, 3, 1]]);
/// assert_eq!(smawk::recursive::column_minima(&matrix),
/// vec![0, 0, 2, 2]);
/// ```
///
/// # Panics
///
/// It is an error to call this on a matrix with zero rows.
Expand Down

0 comments on commit ce5ec7c

Please sign in to comment.