1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::structure::graph::graph_trait::GraphWeightTrait;
use crate::structure::graph::Graph;
use std::ops::{Index, IndexMut};
impl<W: GraphWeightTrait, const NODES: usize> Index<(usize, usize)> for Graph<W, NODES> {
type Output = Option<W>;
/// Returns a reference to the edge value at the given (row, column) index.
///
/// # Arguments
///
/// * `index` - A tuple containing the row and column indices.
///
/// # Returns
///
/// * `&Self::Output` - A reference to the edge value, which is an `Option<W>`.
///
/// # Example
///
/// ```
/// use numberlab::structure::graph::Graph;
///
/// let graph = Graph::<f32, 6>::from_adjacency_matrix([
/// [None, Some(1.0), None, None, None, None],
/// [None, None, Some(1.0), None, None, Some(1.0)],
/// [None, None, None, None, None, Some(1.0)],
/// [None, None, Some(1.0), None, None, None],
/// [None, None, None, None, None, Some(1.0)],
/// [None, None, None, None, Some(1.0), None],
/// ]);
/// assert_eq!(graph[(0, 1)], Some(1.0));
/// assert_eq!(graph[(0, 2)], None);
/// ```
fn index(&self, index: (usize, usize)) -> &Self::Output {
&self.adjacency[index.0][index.1]
}
}
impl<W: GraphWeightTrait, const NODES: usize> IndexMut<(usize, usize)> for Graph<W, NODES> {
/// Returns a mutable reference to the edge value at the given (row, column) index.
///
/// # Arguments
///
/// * `index` - A tuple containing the row and column indices.
///
/// # Returns
///
/// * `&mut Self::Output` - A mutable reference to the edge value, which is an `Option<W>`.
///
/// # Example
///
/// ```
/// use numberlab::structure::graph::Graph;
///
/// let mut graph = Graph::<f32, 6>::from_adjacency_matrix([
/// [None, Some(1.0), None, None, None, None],
/// [None, None, Some(1.0), None, None, Some(1.0)],
/// [None, None, None, None, None, Some(1.0)],
/// [None, None, Some(1.0), None, None, None],
/// [None, None, None, None, None, Some(1.0)],
/// [None, None, None, None, Some(1.0), None],
/// ]);
/// graph[(0, 0)] = Some(1.0);
/// assert_eq!(graph[(0, 0)], Some(1.0));
/// ```
fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
&mut self.adjacency[index.0][index.1]
}
}