Skip to content

Commit 32eba04

Browse files
committed
Deprecate Matrix::reachable() for bfs_reachable() and dfs_reachable()
1 parent 63c2d94 commit 32eba04

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/matrix.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Matrix of an arbitrary type and utilities to rotate, transpose, etc.
22
33
use crate::directed::bfs::bfs_reach;
4+
use crate::directed::dfs::dfs_reach;
45
use crate::utils::uint_sqrt;
56
use itertools::iproduct;
67
use itertools::Itertools;
@@ -534,7 +535,37 @@ impl<C> Matrix<C> {
534535
/// to implement a flood-filling algorithm. Since the indices are collected
535536
/// into a vector, they can later be used without keeping a reference on the
536537
/// matrix itself, e.g., to modify the matrix.
538+
///
539+
/// This method calls the [`bfs_reachable()`](`Self::bfs_reachable`) method to
540+
/// do its work.
541+
#[deprecated(
542+
since = "3.0.7",
543+
note = "Use `bfs_reachable()` or `dfs_reachable()` methods instead"
544+
)]
537545
pub fn reachable<P>(
546+
&self,
547+
start: (usize, usize),
548+
diagonals: bool,
549+
predicate: P,
550+
) -> BTreeSet<(usize, usize)>
551+
where
552+
P: FnMut((usize, usize)) -> bool + Copy,
553+
{
554+
self.bfs_reachable(start, diagonals, predicate)
555+
}
556+
557+
/// Return a set of the indices reachable from a candidate starting point
558+
/// and for which the given predicate is valid. This can be used for example
559+
/// to implement a flood-filling algorithm. Since the indices are collected
560+
/// into a vector, they can later be used without keeping a reference on the
561+
/// matrix itself, e.g., to modify the matrix.
562+
///
563+
/// The search is done using a breadth first search (BFS) algorithm.
564+
///
565+
/// # See also
566+
///
567+
/// The [`dfs_reachable()`](`Self::dfs_reachable`) performs a DFS search instead.
568+
pub fn bfs_reachable<P>(
538569
&self,
539570
start: (usize, usize),
540571
diagonals: bool,
@@ -548,6 +579,32 @@ impl<C> Matrix<C> {
548579
})
549580
.collect()
550581
}
582+
583+
/// Return a set of the indices reachable from a candidate starting point
584+
/// and for which the given predicate is valid. This can be used for example
585+
/// to implement a flood-filling algorithm. Since the indices are collected
586+
/// into a vector, they can later be used without keeping a reference on the
587+
/// matrix itself, e.g., to modify the matrix.
588+
///
589+
/// The search is done using a depth first search (DFS) algorithm.
590+
///
591+
/// # See also
592+
///
593+
/// The [`bfs_reachable()`](`Self::bfs_reachable`) performs a BFS search instead.
594+
pub fn dfs_reachable<P>(
595+
&self,
596+
start: (usize, usize),
597+
diagonals: bool,
598+
mut predicate: P,
599+
) -> BTreeSet<(usize, usize)>
600+
where
601+
P: FnMut((usize, usize)) -> bool + Copy,
602+
{
603+
dfs_reach(start, |&n| {
604+
self.neighbours(n, diagonals).filter(move |&n| predicate(n))
605+
})
606+
.collect()
607+
}
551608
}
552609

553610
impl<C> Index<(usize, usize)> for Matrix<C> {

tests/matrix.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ fn empty_neighbours() {
380380
}
381381

382382
#[test]
383+
#[allow(deprecated)]
383384
fn reachable() {
384385
let m = matrix![[0, 1, 2], [3, 4, 5], [6, 7, 8]];
385386

@@ -396,6 +397,40 @@ fn reachable() {
396397
);
397398
}
398399

400+
#[test]
401+
fn bfs_reachable() {
402+
let m = matrix![[0, 1, 2], [3, 4, 5], [6, 7, 8]];
403+
404+
let indices = m.bfs_reachable((1, 0), false, |n| m[n] % 4 != 0);
405+
assert_eq!(
406+
indices.into_iter().collect::<Vec<_>>(),
407+
vec![(1, 0), (2, 0), (2, 1)]
408+
);
409+
410+
let indices = m.bfs_reachable((1, 0), true, |n| m[n] % 4 != 0);
411+
assert_eq!(
412+
indices.into_iter().collect::<Vec<_>>(),
413+
vec![(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
414+
);
415+
}
416+
417+
#[test]
418+
fn dfs_reachable() {
419+
let m = matrix![[0, 1, 2], [3, 4, 5], [6, 7, 8]];
420+
421+
let indices = m.dfs_reachable((1, 0), false, |n| m[n] % 4 != 0);
422+
assert_eq!(
423+
indices.into_iter().collect::<Vec<_>>(),
424+
vec![(1, 0), (2, 0), (2, 1)]
425+
);
426+
427+
let indices = m.dfs_reachable((1, 0), true, |n| m[n] % 4 != 0);
428+
assert_eq!(
429+
indices.into_iter().collect::<Vec<_>>(),
430+
vec![(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
431+
);
432+
}
433+
399434
#[test]
400435
fn bounds_test() {
401436
let mut m = matrix![[0, 1, 2], [3, 4, 5]];

0 commit comments

Comments
 (0)