Debian Codegraph @

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