| 1 |
//! Functions for generating and checking Monge arrays. |
| 2 |
//! |
| 3 |
//! The functions here are mostly meant to be used for testing |
| 4 |
//! correctness of the SMAWK implementation. |
| 5 |
|
| 6 |
use crate::Matrix; |
| 7 |
use core::num::Wrapping; |
| 8 |
use core::ops::Add; |
| 9 |
|
| 10 |
/// Verify that a matrix is a Monge matrix. |
| 11 |
/// |
| 12 |
/// A [Monge matrix] \(or array) is a matrix where the following |
| 13 |
/// inequality holds: |
| 14 |
/// |
| 15 |
/// ```text |
| 16 |
/// M[i, j] + M[i', j'] <= M[i, j'] + M[i', j] for all i < i', j < j' |
| 17 |
/// ``` |
| 18 |
/// |
| 19 |
/// The inequality says that the sum of the main diagonal is less than |
| 20 |
/// the sum of the antidiagonal. Checking this condition is done by |
| 21 |
/// verifying all adjacent 2 × 2 submatrices, so the running time is |
| 22 |
/// O(*mn*) where *m* is the number of rows and *n* is the number of |
| 23 |
/// columns. |
| 24 |
/// |
| 25 |
/// [Monge matrix]: https://en.wikipedia.org/wiki/Monge_array |
| 26 |
pub fn is_monge<T: Ord + Copy, M: Matrix<T>>(matrix: &M) -> bool |
| 27 |
where |
| 28 |
Wrapping<T>: Add<Output = Wrapping<T>>, |
| 29 |
{ |
| 30 |
/// Returns `Ok(a + b)` if the computation can be done without |
| 31 |
/// overflow, otherwise `Err(a + b - T::MAX - 1)` is returned. |
| 32 |
fn checked_add<T: Ord + Copy>(a: Wrapping<T>, b: Wrapping<T>) -> Result<T, T> |
| 33 |
where |
| 34 |
Wrapping<T>: Add<Output = Wrapping<T>>, |
| 35 |
{ |
| 36 |
let sum = a + b; |
| 37 |
if sum < a { |
| 38 |
Err(sum.0) |
| 39 |
} else { |
| 40 |
Ok(sum.0) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
(0..matrix.nrows() - 1) |
| 45 |
.flat_map(|row| (0..matrix.ncols() - 1).map(move |col| (row, col))) |
| 46 |
.all(|(row, col)| { |
| 47 |
let top_left = Wrapping(matrix.index(row, col)); |
| 48 |
let top_right = Wrapping(matrix.index(row, col + 1)); |
| 49 |
let bot_left = Wrapping(matrix.index(row + 1, col)); |
| 50 |
let bot_right = Wrapping(matrix.index(row + 1, col + 1)); |
| 51 |
|
| 52 |
match ( |
| 53 |
checked_add(top_left, bot_right), |
| 54 |
checked_add(bot_left, top_right), |
| 55 |
) { |
| 56 |
(Ok(a), Ok(b)) => a <= b, // No overflow. |
| 57 |
(Err(a), Err(b)) => a <= b, // Double overflow. |
| 58 |
(Ok(_), Err(_)) => true, // Anti-diagonal overflow. |
| 59 |
(Err(_), Ok(_)) => false, // Main diagonal overflow. |
| 60 |
} |
| 61 |
}) |
| 62 |
} |
| 63 |
|
| 64 |
#[cfg(test)] |
| 65 |
mod tests { |
| 66 |
use super::*; |
| 67 |
use alloc::vec; |
| 68 |
use alloc::vec::Vec; |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn is_monge_handles_overflow() { |
| 72 |
// The x + y <= z + w computations will overflow for an u8 |
| 73 |
// matrix unless is_monge is careful. |
| 74 |
let matrix: Vec<Vec<u8>> = vec![ |
| 75 |
vec![200, 200, 200, 200], |
| 76 |
vec![200, 200, 200, 200], |
| 77 |
vec![200, 200, 200, 200], |
| 78 |
]; |
| 79 |
assert!(is_monge(&matrix)); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn monge_constant_rows() { |
| 84 |
let matrix = vec![ |
| 85 |
vec![42, 42, 42, 42], |
| 86 |
vec![0, 0, 0, 0], |
| 87 |
vec![100, 100, 100, 100], |
| 88 |
vec![1000, 1000, 1000, 1000], |
| 89 |
]; |
| 90 |
assert!(is_monge(&matrix)); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn monge_constant_cols() { |
| 95 |
let matrix = vec![ |
| 96 |
vec![42, 0, 100, 1000], |
| 97 |
vec![42, 0, 100, 1000], |
| 98 |
vec![42, 0, 100, 1000], |
| 99 |
vec![42, 0, 100, 1000], |
| 100 |
]; |
| 101 |
assert!(is_monge(&matrix)); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn monge_upper_right() { |
| 106 |
let matrix = vec![ |
| 107 |
vec![10, 10, 42, 42, 42], |
| 108 |
vec![10, 10, 42, 42, 42], |
| 109 |
vec![10, 10, 10, 10, 10], |
| 110 |
vec![10, 10, 10, 10, 10], |
| 111 |
]; |
| 112 |
assert!(is_monge(&matrix)); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn monge_lower_left() { |
| 117 |
let matrix = vec![ |
| 118 |
vec![10, 10, 10, 10, 10], |
| 119 |
vec![10, 10, 10, 10, 10], |
| 120 |
vec![42, 42, 42, 10, 10], |
| 121 |
vec![42, 42, 42, 10, 10], |
| 122 |
]; |
| 123 |
assert!(is_monge(&matrix)); |
| 124 |
} |
| 125 |
} |