Debian Codegraph @

debian/rust-vendor/smawk/src/brute_force.rs

raw
1 //! Brute-force algorithm for finding column minima.
2 //!
3 //! The functions here are mostly meant to be used for testing
4 //! correctness of the SMAWK implementation.
5 //!
6 //! **Note: this module is only available if you enable the `ndarray`
7 //! Cargo feature.**
8
9 use alloc::vec::Vec;
10 use ndarray::{Array2, ArrayView1};
11
12 /// Compute lane minimum by brute force.
13 ///
14 /// This does a simple scan through the lane (row or column).
15 #[inline]
16 pub fn lane_minimum<T: Ord>(lane: ArrayView1<'_, T>) -> usize {
17 lane.iter()
18 .enumerate()
19 .min_by_key(|&(idx, elem)| (elem, idx))
20 .map(|(idx, _)| idx)
21 .expect("empty lane in matrix")
22 }
23
24 /// Compute row minima by brute force in O(*mn*) time.
25 ///
26 /// This function implements a simple brute-force approach where each
27 /// matrix row is scanned completely. This means that the function
28 /// works on all matrices, not just Monge matrices.
29 ///
30 /// # Examples
31 ///
32 /// ```
33 /// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
34 /// [5, 3, 5, 3],
35 /// [5, 3, 3, 1]]);
36 /// assert_eq!(smawk::brute_force::row_minima(&matrix),
37 /// vec![1, 1, 3]);
38 /// ```
39 ///
40 /// # Panics
41 ///
42 /// It is an error to call this on a matrix with zero columns.
43 pub fn row_minima<T: Ord>(matrix: &Array2<T>) -> Vec<usize> {
44 matrix.rows().into_iter().map(lane_minimum).collect()
45 }
46
47 /// Compute column minima by brute force in O(*mn*) time.
48 ///
49 /// This function implements a simple brute-force approach where each
50 /// matrix column is scanned completely. This means that the function
51 /// works on all matrices, not just Monge matrices.
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
57 /// [5, 3, 5, 3],
58 /// [5, 3, 3, 1]]);
59 /// assert_eq!(smawk::brute_force::column_minima(&matrix),
60 /// vec![0, 0, 2, 2]);
61 /// ```
62 ///
63 /// # Panics
64 ///
65 /// It is an error to call this on a matrix with zero rows.
66 pub fn column_minima<T: Ord>(matrix: &Array2<T>) -> Vec<usize> {
67 matrix.columns().into_iter().map(lane_minimum).collect()
68 }
69
70 #[cfg(test)]
71 mod tests {
72 use super::*;
73 use alloc::vec;
74 use ndarray::arr2;
75
76 #[test]
77 fn brute_force_1x1() {
78 let matrix = arr2(&[[2]]);
79 let minima = vec![0];
80 assert_eq!(row_minima(&matrix), minima);
81 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
82 }
83
84 #[test]
85 fn brute_force_2x1() {
86 let matrix = arr2(&[
87 [3], //
88 [2],
89 ]);
90 let minima = vec![0, 0];
91 assert_eq!(row_minima(&matrix), minima);
92 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
93 }
94
95 #[test]
96 fn brute_force_1x2() {
97 let matrix = arr2(&[[2, 1]]);
98 let minima = vec![1];
99 assert_eq!(row_minima(&matrix), minima);
100 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
101 }
102
103 #[test]
104 fn brute_force_2x2() {
105 let matrix = arr2(&[
106 [3, 2], //
107 [2, 1],
108 ]);
109 let minima = vec![1, 1];
110 assert_eq!(row_minima(&matrix), minima);
111 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
112 }
113
114 #[test]
115 fn brute_force_3x3() {
116 let matrix = arr2(&[
117 [3, 4, 4], //
118 [3, 4, 4],
119 [2, 3, 3],
120 ]);
121 let minima = vec![0, 0, 0];
122 assert_eq!(row_minima(&matrix), minima);
123 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
124 }
125
126 #[test]
127 fn brute_force_4x4() {
128 let matrix = arr2(&[
129 [4, 5, 5, 5], //
130 [2, 3, 3, 3],
131 [2, 3, 3, 3],
132 [2, 2, 2, 2],
133 ]);
134 let minima = vec![0, 0, 0, 0];
135 assert_eq!(row_minima(&matrix), minima);
136 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
137 }
138
139 #[test]
140 fn brute_force_5x5() {
141 let matrix = arr2(&[
142 [3, 2, 4, 5, 6],
143 [2, 1, 3, 3, 4],
144 [2, 1, 3, 3, 4],
145 [3, 2, 4, 3, 4],
146 [4, 3, 2, 1, 1],
147 ]);
148 let minima = vec![1, 1, 1, 1, 3];
149 assert_eq!(row_minima(&matrix), minima);
150 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
151 }
152 }