From 7dbbe1b3f9d1569275115968c0ba6390f99431c9 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sun, 23 Aug 2020 11:32:18 +0200 Subject: [PATCH 1/2] Remove ndarray dependency from SMAWK unit tests --- src/lib.rs | 133 ++++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 63 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5fd67e9..c7f5b5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -376,120 +376,127 @@ pub fn online_column_minima #[cfg(test)] mod tests { use super::*; - use ndarray::arr2; #[test] fn smawk_1x1() { - let matrix = arr2(&[[2]]); - let minima = vec![0]; - assert_eq!(smawk_row_minima(&matrix), minima); - assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima); + let matrix = vec![vec![2]]; + assert_eq!(smawk_row_minima(&matrix), vec![0]); + assert_eq!(smawk_column_minima(&matrix), vec![0]); } #[test] fn smawk_2x1() { - let matrix = arr2(&[[3], [2]]); - let minima = vec![0, 0]; - assert_eq!(smawk_row_minima(&matrix), minima); - assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima); + let matrix = vec![ + vec![3], // + vec![2], + ]; + assert_eq!(smawk_row_minima(&matrix), vec![0, 0]); + assert_eq!(smawk_column_minima(&matrix), vec![1]); } #[test] fn smawk_1x2() { - let matrix = arr2(&[[2, 1]]); - let minima = vec![1]; - assert_eq!(smawk_row_minima(&matrix), minima); - assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima); + let matrix = vec![vec![2, 1]]; + assert_eq!(smawk_row_minima(&matrix), vec![1]); + assert_eq!(smawk_column_minima(&matrix), vec![0, 0]); } #[test] fn smawk_2x2() { - let matrix = arr2(&[[3, 2], [2, 1]]); - let minima = vec![1, 1]; - assert_eq!(smawk_row_minima(&matrix), minima); - assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima); + let matrix = vec![ + vec![3, 2], // + vec![2, 1], + ]; + assert_eq!(smawk_row_minima(&matrix), vec![1, 1]); + assert_eq!(smawk_column_minima(&matrix), vec![1, 1]); } #[test] fn smawk_3x3() { - let matrix = arr2(&[[3, 4, 4], [3, 4, 4], [2, 3, 3]]); - let minima = vec![0, 0, 0]; - assert_eq!(smawk_row_minima(&matrix), minima); - assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima); + let matrix = vec![ + vec![3, 4, 4], // + vec![3, 4, 4], + vec![2, 3, 3], + ]; + assert_eq!(smawk_row_minima(&matrix), vec![0, 0, 0]); + assert_eq!(smawk_column_minima(&matrix), vec![2, 2, 2]); } #[test] fn smawk_4x4() { - let matrix = arr2(&[[4, 5, 5, 5], [2, 3, 3, 3], [2, 3, 3, 3], [2, 2, 2, 2]]); - let minima = vec![0, 0, 0, 0]; - assert_eq!(smawk_row_minima(&matrix), minima); - assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima); + let matrix = vec![ + vec![4, 5, 5, 5], // + vec![2, 3, 3, 3], + vec![2, 3, 3, 3], + vec![2, 2, 2, 2], + ]; + assert_eq!(smawk_row_minima(&matrix), vec![0, 0, 0, 0]); + assert_eq!(smawk_column_minima(&matrix), vec![1, 3, 3, 3]); } #[test] fn smawk_5x5() { - let matrix = arr2(&[ - [3, 2, 4, 5, 6], - [2, 1, 3, 3, 4], - [2, 1, 3, 3, 4], - [3, 2, 4, 3, 4], - [4, 3, 2, 1, 1], - ]); - let minima = vec![1, 1, 1, 1, 3]; - assert_eq!(smawk_row_minima(&matrix), minima); - assert_eq!(smawk_column_minima(&matrix.reversed_axes()), minima); + let matrix = vec![ + vec![3, 2, 4, 5, 6], + vec![2, 1, 3, 3, 4], + vec![2, 1, 3, 3, 4], + vec![3, 2, 4, 3, 4], + vec![4, 3, 2, 1, 1], + ]; + assert_eq!(smawk_row_minima(&matrix), vec![1, 1, 1, 1, 3]); + assert_eq!(smawk_column_minima(&matrix), vec![1, 1, 4, 4, 4]); } #[test] fn online_1x1() { - let matrix = arr2(&[[0]]); + let matrix = vec![vec![0]]; let minima = vec![(0, 0)]; - assert_eq!( - online_column_minima(0, 1, |_, i, j| matrix[[i, j]],), - minima - ); + assert_eq!(online_column_minima(0, 1, |_, i, j| matrix[i][j]), minima); } #[test] fn online_2x2() { - let matrix = arr2(&[[0, 2], [0, 0]]); + let matrix = vec![ + vec![0, 2], // + vec![0, 0], + ]; let minima = vec![(0, 0), (0, 2)]; - assert_eq!( - online_column_minima(0, 2, |_, i, j| matrix[[i, j]],), - minima - ); + assert_eq!(online_column_minima(0, 2, |_, i, j| matrix[i][j]), minima); } #[test] fn online_3x3() { - let matrix = arr2(&[[0, 4, 4], [0, 0, 4], [0, 0, 0]]); + let matrix = vec![ + vec![0, 4, 4], // + vec![0, 0, 4], + vec![0, 0, 0], + ]; let minima = vec![(0, 0), (0, 4), (0, 4)]; - assert_eq!( - online_column_minima(0, 3, |_, i, j| matrix[[i, j]],), - minima - ); + assert_eq!(online_column_minima(0, 3, |_, i, j| matrix[i][j]), minima); } #[test] fn online_4x4() { - let matrix = arr2(&[[0, 5, 5, 5], [0, 0, 3, 3], [0, 0, 0, 3], [0, 0, 0, 0]]); + let matrix = vec![ + vec![0, 5, 5, 5], // + vec![0, 0, 3, 3], + vec![0, 0, 0, 3], + vec![0, 0, 0, 0], + ]; let minima = vec![(0, 0), (0, 5), (1, 3), (1, 3)]; - assert_eq!( - online_column_minima(0, 4, |_, i, j| matrix[[i, j]],), - minima - ); + assert_eq!(online_column_minima(0, 4, |_, i, j| matrix[i][j]), minima); } #[test] fn online_5x5() { - let matrix = arr2(&[ - [0, 2, 4, 6, 7], - [0, 0, 3, 4, 5], - [0, 0, 0, 3, 4], - [0, 0, 0, 0, 4], - [0, 0, 0, 0, 0], - ]); + let matrix = vec![ + vec![0, 2, 4, 6, 7], + vec![0, 0, 3, 4, 5], + vec![0, 0, 0, 3, 4], + vec![0, 0, 0, 0, 4], + vec![0, 0, 0, 0, 0], + ]; let minima = vec![(0, 0), (0, 2), (1, 3), (2, 3), (2, 4)]; - assert_eq!(online_column_minima(0, 5, |_, i, j| matrix[[i, j]]), minima); + assert_eq!(online_column_minima(0, 5, |_, i, j| matrix[i][j]), minima); } } From 4cf68fd67133440e9e31586e6eef4fecb14c14eb Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sun, 23 Aug 2020 16:00:16 +0200 Subject: [PATCH 2/2] Make ndarray an optional dependency --- Cargo.toml | 2 +- benches/comparison.rs | 1 + src/brute_force.rs | 6 ++++++ src/lib.rs | 10 ++++++++++ src/monge.rs | 6 ++++++ src/recursive.rs | 6 ++++++ tests/agreement.rs | 2 ++ tests/complexity.rs | 2 ++ 8 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d4bbc9c..fbb0642 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ appveyor = { repository = "mgeisler/smawk" } codecov = { repository = "mgeisler/smawk" } [dependencies] -ndarray = "0.13" +ndarray = { version = "0.13", optional = true } num-traits = "0.2" rand = "0.7" diff --git a/benches/comparison.rs b/benches/comparison.rs index 5bc814e..5ca9a2e 100644 --- a/benches/comparison.rs +++ b/benches/comparison.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "ndarray")] #![feature(test)] extern crate test; diff --git a/src/brute_force.rs b/src/brute_force.rs index b2162a1..bcd0a30 100644 --- a/src/brute_force.rs +++ b/src/brute_force.rs @@ -1,4 +1,10 @@ //! Brute-force algorithm for finding column minima. +//! +//! The functions here are mostly meant to be used for testing +//! correctness of the SMAWK implementation. +//! +//! **Note: this module is only available if you enable the `ndarray` +//! Cargo feature.** use ndarray::{Array2, ArrayView1}; diff --git a/src/lib.rs b/src/lib.rs index c7f5b5f..ac6ef1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,8 +90,11 @@ #![doc(html_root_url = "https://docs.rs/smawk/0.2.0")] +#[cfg(feature = "ndarray")] pub mod brute_force; +#[cfg(feature = "ndarray")] pub mod monge; +#[cfg(feature = "ndarray")] pub mod recursive; /// Minimal matrix trait for two-dimensional arrays. @@ -101,6 +104,9 @@ pub mod recursive; /// elements. Modeled after /// [`ndarray::Array2`](https://docs.rs/ndarray/latest/ndarray/type.Array2.html) /// from the [ndarray crate ](https://crates.io/crates/ndarray). +/// +/// Enable the `ndarray` Cargo feature if you want to use it with +/// `ndarray::Array2`. pub trait Matrix { /// Return the number of rows. fn nrows(&self) -> usize; @@ -129,6 +135,10 @@ impl Matrix for Vec> { } /// Adapting `ndarray::Array2` to the `Matrix` trait. +/// +/// **Note: this implementation is only available if you enable the +/// `ndarray` Cargo feature.** +#[cfg(feature = "ndarray")] impl Matrix for ndarray::Array2 { #[inline] fn nrows(&self) -> usize { diff --git a/src/monge.rs b/src/monge.rs index 0f2176e..a19767b 100644 --- a/src/monge.rs +++ b/src/monge.rs @@ -1,4 +1,10 @@ //! Functions for generating and checking Monge arrays. +//! +//! The functions here are mostly meant to be used for testing +//! correctness of the SMAWK implementation. +//! +//! **Note: this module is only available if you enable the `ndarray` +//! Cargo feature.** use ndarray::{s, Array2}; use num_traits::{PrimInt, WrappingAdd}; diff --git a/src/recursive.rs b/src/recursive.rs index b49168d..eb38265 100644 --- a/src/recursive.rs +++ b/src/recursive.rs @@ -1,4 +1,10 @@ //! Recursive algorithm for finding column minima. +//! +//! The functions here are mostly meant to be used for testing +//! correctness of the SMAWK implementation. +//! +//! **Note: this module is only available if you enable the `ndarray` +//! Cargo feature.** use ndarray::{s, Array2, ArrayView2, Axis}; diff --git a/tests/agreement.rs b/tests/agreement.rs index ed8993f..efdcf52 100644 --- a/tests/agreement.rs +++ b/tests/agreement.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "ndarray")] + use ndarray::{s, Array2}; use rand::SeedableRng; use rand_chacha::ChaCha20Rng; diff --git a/tests/complexity.rs b/tests/complexity.rs index 7e10de9..0283e08 100644 --- a/tests/complexity.rs +++ b/tests/complexity.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "ndarray")] + use ndarray::{Array1, Array2}; use rand::SeedableRng; use rand_chacha::ChaCha20Rng;