Skip to content

Commit bde0362

Browse files
Merge #329 #330 #331
329: Add Matrix::map() r=samueltardieu a=samueltardieu 330: grid r=samueltardieu a=samueltardieu - Add Matrix::map() - Add Grid equality - Add Matrix<bool> to Grid conversion 331: Do not run tests systematically on pull request r=samueltardieu a=samueltardieu Co-authored-by: Samuel Tardieu <sam@rfc1149.net>
4 parents a5238df + 2fe5768 + 5e81869 + c9791d0 commit bde0362

5 files changed

Lines changed: 103 additions & 3 deletions

File tree

.github/workflows/tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ on:
33
branches:
44
- staging
55
- trying
6-
pull_request:
76

87
name: Continuous integration
98

src/grid.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Rectangular grid in which vertices can be added or removed, with or
22
//! without diagonal links.
33
4+
use super::matrix::Matrix;
5+
use super::utils::absdiff;
46
use crate::directed::bfs::bfs_reach;
57
use indexmap::IndexSet;
68
use itertools::iproduct;
79
use std::collections::BTreeSet;
810
use std::fmt;
911
use std::iter::{FromIterator, FusedIterator};
1012

11-
use super::utils::absdiff;
12-
1313
#[derive(Clone)]
1414
/// Representation of a rectangular grid in which vertices can be added
1515
/// or removed. Edges are automatically created between adjacent vertices.
@@ -560,3 +560,30 @@ impl fmt::Debug for Grid {
560560
Ok(())
561561
}
562562
}
563+
564+
impl From<&Matrix<bool>> for Grid {
565+
fn from(matrix: &Matrix<bool>) -> Self {
566+
let mut grid = Grid::new(matrix.columns, matrix.rows);
567+
for ((r, c), &v) in matrix.indices().zip(matrix.values()) {
568+
if v {
569+
grid.add_vertex((c, r));
570+
}
571+
}
572+
grid
573+
}
574+
}
575+
576+
impl From<Matrix<bool>> for Grid {
577+
fn from(matrix: Matrix<bool>) -> Self {
578+
Grid::from(&matrix)
579+
}
580+
}
581+
582+
impl PartialEq for Grid {
583+
fn eq(&self, other: &Self) -> bool {
584+
self.vertices_len() == other.vertices_len()
585+
&& self.iter().zip(other.iter()).all(|(a, b)| a == b)
586+
}
587+
}
588+
589+
impl Eq for Grid {}

src/matrix.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ impl<C: Clone> Matrix<C> {
166166
}
167167
Ok(())
168168
}
169+
170+
/// Transform the matrix into another matrix with the same shape
171+
/// after applying a transforming function to every elements.
172+
pub fn map<O, F>(self, transform: F) -> Matrix<O>
173+
where
174+
F: FnMut(C) -> O,
175+
{
176+
Matrix {
177+
rows: self.rows,
178+
columns: self.columns,
179+
data: self.data.into_iter().map(transform).collect(),
180+
}
181+
}
169182
}
170183

171184
impl<C: Copy> Matrix<C> {

tests/grid.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,48 @@ fn debug() {
470470
)
471471
);
472472
}
473+
474+
#[test]
475+
fn from_matrix() {
476+
let m = pathfinding::prelude::Matrix::square_from_vec(vec![
477+
true, true, true, false, false, false, true, false, true,
478+
])
479+
.unwrap();
480+
let g = Grid::from(&m);
481+
let g2 = Grid::from(m);
482+
assert_eq!(g, g2);
483+
let mut vertices = g.into_iter().collect::<Vec<_>>();
484+
vertices.sort_unstable();
485+
assert_eq!(vertices, vec![(0, 0), (0, 2), (1, 0), (2, 0), (2, 2)]);
486+
}
487+
488+
#[test]
489+
fn test_equality() {
490+
let g = [
491+
(0, 0),
492+
(1, 0),
493+
(2, 0),
494+
(3, 0),
495+
(4, 0),
496+
(0, 1),
497+
(4, 1),
498+
(0, 2),
499+
(4, 2),
500+
(0, 3),
501+
(4, 3),
502+
(0, 4),
503+
(1, 4),
504+
(2, 4),
505+
(3, 4),
506+
(4, 4),
507+
]
508+
.into_iter()
509+
.collect::<Grid>();
510+
assert_eq!(g, g);
511+
let mut g2 = g.clone();
512+
assert_eq!(g, g2);
513+
g2.remove_vertex((0, 0));
514+
assert_ne!(g, g2);
515+
g2.add_vertex((0, 0));
516+
assert_eq!(g, g2);
517+
}

tests/matrix.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,19 @@ fn uninit() {
590590
}
591591
}
592592
}
593+
594+
#[test]
595+
fn map() {
596+
let m = Matrix::new(3, 3, 10);
597+
let m = m.map({
598+
let mut counter = 0;
599+
move |x| {
600+
counter += 1;
601+
x + counter
602+
}
603+
});
604+
assert_eq!(
605+
m,
606+
Matrix::square_from_vec(vec![11, 12, 13, 14, 15, 16, 17, 18, 19]).unwrap()
607+
);
608+
}

0 commit comments

Comments
 (0)