Skip to content

Commit fcd447f

Browse files
Merge #305
305: Use tuples instead of tuples ref for matrix index r=samueltardieu a=samueltardieu This looks more natural this way: matrix[(1, 2)] instead of matrix[&(1, 2)] This is a semver incompatible change which requires bumping the major version. Co-authored-by: Samuel Tardieu <[email protected]>
2 parents 5b5840b + 81e2b05 commit fcd447f

File tree

5 files changed

+46
-46
lines changed

5 files changed

+46
-46
lines changed

src/directed/edmonds_karp.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl<C: Copy + Zero + Signed + Eq + Ord + Bounded> EdmondsKarp<C> for SparseCapa
371371
let mut result = Self::new(size, source, sink);
372372
for from in 0..size {
373373
for to in 0..size {
374-
let capacity = capacities[&(from, to)];
374+
let capacity = capacities[(from, to)];
375375
if capacity > Zero::zero() {
376376
result.set_capacity(from, to, capacity);
377377
}
@@ -514,11 +514,11 @@ impl<C: Copy + Zero + Signed + Ord + Bounded> EdmondsKarp<C> for DenseCapacity<C
514514
}
515515

516516
fn residual_capacity(&self, from: usize, to: usize) -> C {
517-
self.residuals[&(from, to)]
517+
self.residuals[(from, to)]
518518
}
519519

520520
fn flow(&self, from: usize, to: usize) -> C {
521-
self.flows[&(from, to)]
521+
self.flows[(from, to)]
522522
}
523523

524524
fn flows(&self) -> Vec<((usize, usize), C)> {
@@ -535,14 +535,14 @@ impl<C: Copy + Zero + Signed + Ord + Bounded> EdmondsKarp<C> for DenseCapacity<C
535535
}
536536

537537
fn add_flow(&mut self, from: usize, to: usize, capacity: C) {
538-
self.flows[&(from, to)] = self.flows[&(from, to)] + capacity;
539-
self.flows[&(to, from)] = self.flows[&(to, from)] - capacity;
540-
self.residuals[&(from, to)] = self.residuals[&(from, to)] - capacity;
541-
self.residuals[&(to, from)] = self.residuals[&(to, from)] + capacity;
538+
self.flows[(from, to)] = self.flows[(from, to)] + capacity;
539+
self.flows[(to, from)] = self.flows[(to, from)] - capacity;
540+
self.residuals[(from, to)] = self.residuals[(from, to)] - capacity;
541+
self.residuals[(to, from)] = self.residuals[(to, from)] + capacity;
542542
}
543543

544544
fn add_residual_capacity(&mut self, from: usize, to: usize, capacity: C) {
545-
self.residuals[&(from, to)] = self.residual_capacity(from, to) + capacity;
545+
self.residuals[(from, to)] = self.residual_capacity(from, to) + capacity;
546546
}
547547

548548
fn flows_from(&self, from: usize) -> Vec<usize> {

src/kuhn_munkres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<C: Copy> Weights<C> for Matrix<C> {
3939

4040
#[must_use]
4141
fn at(&self, row: usize, col: usize) -> C {
42-
self[&(row, col)]
42+
self[(row, col)]
4343
}
4444

4545
#[must_use]

src/matrix.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<C> Matrix<C> {
242242
/// let matrix = Matrix::from_rows(input.lines().map(|l| l.chars()))?;
243243
/// assert_eq!(matrix.rows, 2);
244244
/// assert_eq!(matrix.columns, 3);
245-
/// assert_eq!(matrix[&(1, 1)], 'e');
245+
/// assert_eq!(matrix[(1, 1)], 'e');
246246
/// # Ok::<_, MatrixFormatError>(())
247247
/// ```
248248
pub fn from_rows<IR, IC>(rows: IR) -> Result<Self, MatrixFormatError>
@@ -287,7 +287,7 @@ impl<C> Matrix<C> {
287287
///
288288
/// This function panics if the coordinates do not designated a cell.
289289
#[must_use]
290-
pub fn idx(&self, i: &(usize, usize)) -> usize {
290+
pub fn idx(&self, i: (usize, usize)) -> usize {
291291
assert!(
292292
i.0 < self.rows,
293293
"trying to access row {} (max {})",
@@ -474,17 +474,17 @@ impl<C> Matrix<C> {
474474
}
475475
}
476476

477-
impl<'a, C> Index<&'a (usize, usize)> for Matrix<C> {
477+
impl<C> Index<(usize, usize)> for Matrix<C> {
478478
type Output = C;
479479

480480
#[must_use]
481-
fn index(&self, index: &'a (usize, usize)) -> &C {
481+
fn index(&self, index: (usize, usize)) -> &C {
482482
&self.data[self.idx(index)]
483483
}
484484
}
485485

486-
impl<'a, C> IndexMut<&'a (usize, usize)> for Matrix<C> {
487-
fn index_mut(&mut self, index: &'a (usize, usize)) -> &mut C {
486+
impl<C> IndexMut<(usize, usize)> for Matrix<C> {
487+
fn index_mut(&mut self, index: (usize, usize)) -> &mut C {
488488
let i = self.idx(index);
489489
&mut self.data[i]
490490
}

tests/dijkstra-all.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn build_network(size: usize) -> Matrix<usize> {
77
for a in 0..size {
88
for b in 0..size {
99
if rng.gen_ratio(2, 3) {
10-
network[&(a, b)] = rng.gen::<u16>() as usize;
10+
network[(a, b)] = rng.gen::<u16>() as usize;
1111
}
1212
}
1313
}
@@ -17,7 +17,7 @@ fn build_network(size: usize) -> Matrix<usize> {
1717
fn neighbours(network: Matrix<usize>) -> impl FnMut(&usize) -> Vec<(usize, usize)> {
1818
move |&a| {
1919
(0..network.rows)
20-
.filter_map(|b| match network[&(a, b)] {
20+
.filter_map(|b| match network[(a, b)] {
2121
0 => None,
2222
p => Some((b, p)),
2323
})

tests/matrix.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ use pathfinding::{matrix, matrix::Matrix, utils::absdiff};
55
#[test]
66
fn sm() {
77
let mut m = Matrix::new(2, 2, 0usize);
8-
m[&(0, 0)] = 0;
9-
m[&(0, 1)] = 1;
10-
m[&(1, 0)] = 10;
11-
m[&(1, 1)] = 11;
12-
m[&(0, 1)] = 2;
13-
assert_eq!(m[&(0, 0)], 0);
14-
assert_eq!(m[&(0, 1)], 2);
15-
assert_eq!(m[&(1, 0)], 10);
16-
assert_eq!(m[&(1, 1)], 11);
8+
m[(0, 0)] = 0;
9+
m[(0, 1)] = 1;
10+
m[(1, 0)] = 10;
11+
m[(1, 1)] = 11;
12+
m[(0, 1)] = 2;
13+
assert_eq!(m[(0, 0)], 0);
14+
assert_eq!(m[(0, 1)], 2);
15+
assert_eq!(m[(1, 0)], 10);
16+
assert_eq!(m[(1, 1)], 11);
1717
m.fill(33);
18-
assert_eq!(m[&(0, 0)], 33);
19-
assert_eq!(m[&(0, 1)], 33);
20-
assert_eq!(m[&(1, 0)], 33);
21-
assert_eq!(m[&(1, 1)], 33);
18+
assert_eq!(m[(0, 0)], 33);
19+
assert_eq!(m[(0, 1)], 33);
20+
assert_eq!(m[(1, 0)], 33);
21+
assert_eq!(m[(1, 1)], 33);
2222
}
2323

2424
#[test]
@@ -27,12 +27,12 @@ fn from_vec() {
2727
assert_eq!(m.rows, 2);
2828
assert_eq!(m.columns, 3);
2929
assert!(!m.is_square());
30-
assert_eq!(m[&(0, 0)], 10);
31-
assert_eq!(m[&(0, 1)], 20);
32-
assert_eq!(m[&(0, 2)], 30);
33-
assert_eq!(m[&(1, 0)], 40);
34-
assert_eq!(m[&(1, 1)], 50);
35-
assert_eq!(m[&(1, 2)], 60);
30+
assert_eq!(m[(0, 0)], 10);
31+
assert_eq!(m[(0, 1)], 20);
32+
assert_eq!(m[(0, 2)], 30);
33+
assert_eq!(m[(1, 0)], 40);
34+
assert_eq!(m[(1, 1)], 50);
35+
assert_eq!(m[(1, 2)], 60);
3636
}
3737

3838
#[test]
@@ -43,10 +43,10 @@ fn from_vec_error() {
4343
#[test]
4444
fn to_vec() {
4545
let mut m = Matrix::new(2, 2, 0usize);
46-
m[&(0, 0)] = 0;
47-
m[&(0, 1)] = 1;
48-
m[&(1, 0)] = 10;
49-
m[&(1, 1)] = 11;
46+
m[(0, 0)] = 0;
47+
m[(0, 1)] = 1;
48+
m[(1, 0)] = 10;
49+
m[(1, 1)] = 11;
5050
assert_eq!(m.to_vec(), vec![0, 1, 10, 11]);
5151
}
5252

@@ -56,10 +56,10 @@ fn square_from_vec() {
5656
assert_eq!(m.rows, 2);
5757
assert_eq!(m.columns, 2);
5858
assert!(m.is_square());
59-
assert_eq!(m[&(0, 0)], 10);
60-
assert_eq!(m[&(0, 1)], 20);
61-
assert_eq!(m[&(1, 0)], 30);
62-
assert_eq!(m[&(1, 1)], 40);
59+
assert_eq!(m[(0, 0)], 10);
60+
assert_eq!(m[(0, 1)], 20);
61+
assert_eq!(m[(1, 0)], 30);
62+
assert_eq!(m[(1, 1)], 40);
6363
}
6464

6565
#[test]
@@ -255,7 +255,7 @@ fn empty_extend() {
255255
let mut i = 0;
256256
for row in 0..m.rows {
257257
for column in 0..m.columns {
258-
assert_eq!(m[&(row, column)], i);
258+
assert_eq!(m[(row, column)], i);
259259
i += 1;
260260
}
261261
}
@@ -276,7 +276,7 @@ fn matrix_macro() {
276276
let mut i = 0;
277277
for row in 0..m.rows {
278278
for column in 0..m.columns {
279-
assert_eq!(m[&(row, column)], i);
279+
assert_eq!(m[(row, column)], i);
280280
i += 1;
281281
}
282282
}

0 commit comments

Comments
 (0)