toolbox_rs/cell.rs
1use crate::graph::NodeID;
2
3/// A Cell represents a partition of a graph with border nodes and their distance matrix.
4///
5/// The Cell struct maintains a collection of border nodes (nodes that connect this cell to other cells)
6/// and a distance matrix that stores the shortest distances between these border nodes within the cell.
7///
8/// # Examples
9///
10/// ```
11/// use toolbox_rs::cell::Cell;
12///
13/// let border_nodes = vec![0, 1, 2]; // Three border nodes
14/// let distances = vec![
15/// 0, 5, 7, // distances from node 0 to others
16/// 5, 0, 3, // distances from node 1 to others
17/// 7, 3, 0 // distances from node 2 to others
18/// ];
19/// let cell = Cell::new(border_nodes, distances, 42);
20///
21/// assert_eq!(cell.get_distance(0, 1), 5); // Distance from node 0 to 1
22/// assert_eq!(cell.id(), 42);
23/// assert_eq!(cell.border_nodes(), &[0, 1, 2]);
24/// ```
25#[derive(Clone, Debug, Default)]
26pub struct Cell {
27 border_nodes: Vec<NodeID>,
28 distance_matrix: Vec<usize>,
29 id: usize,
30}
31
32impl Cell {
33 /// Creates a new Cell with the specified border nodes, distance matrix, and ID.
34 ///
35 /// # Arguments
36 ///
37 /// * `border_nodes` - Vector of node IDs that represent the border nodes of this cell
38 /// * `distance_matrix` - A flattened matrix of distances between border nodes
39 /// * `id` - Unique identifier for this cell
40 ///
41 /// # Examples
42 ///
43 /// ```
44 /// use toolbox_rs::cell::Cell;
45 ///
46 /// let cell = Cell::new(vec![0, 1], vec![0, 4, 4, 0], 1);
47 /// assert_eq!(cell.get_distance(0, 1), 4);
48 /// ```
49 pub fn new(border_nodes: Vec<NodeID>, distance_matrix: Vec<usize>, id: usize) -> Self {
50 Self {
51 border_nodes,
52 distance_matrix,
53 id,
54 }
55 }
56
57 /// Returns the distance between two border nodes within the cell.
58 ///
59 /// # Arguments
60 ///
61 /// * `source` - Index of the source node in the border_nodes list
62 /// * `target` - Index of the target node in the border_nodes list
63 ///
64 /// # Examples
65 ///
66 /// ```
67 /// use toolbox_rs::cell::Cell;
68 ///
69 /// let cell = Cell::new(
70 /// vec![10, 20, 30], // border nodes
71 /// vec![0, 5, 8, 5, 0, 3, 8, 3, 0], // 3x3 distance matrix
72 /// 1
73 /// );
74 /// assert_eq!(cell.get_distance(0, 1), 5); // Distance from first to second border node
75 /// assert_eq!(cell.get_distance(1, 2), 3); // Distance from second to third border node
76 /// ```
77 pub fn get_distance(&self, source: usize, target: usize) -> usize {
78 self.distance_matrix[source * self.border_nodes.len() + target]
79 }
80
81 /// Returns the unique identifier of this cell.
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use toolbox_rs::cell::Cell;
87 ///
88 /// let cell = Cell::new(vec![0], vec![0], 42);
89 /// assert_eq!(cell.id(), 42);
90 /// ```
91 pub fn id(&self) -> usize {
92 self.id
93 }
94
95 /// Returns a slice containing all border nodes of this cell.
96 ///
97 /// # Examples
98 ///
99 /// ```
100 /// use toolbox_rs::cell::Cell;
101 ///
102 /// let cell = Cell::new(vec![1, 2, 3], vec![0, 1, 1, 1, 0, 1, 1, 1, 0], 1);
103 /// assert_eq!(cell.border_nodes(), &[1, 2, 3]);
104 /// ```
105 pub fn border_nodes(&self) -> &[NodeID] {
106 &self.border_nodes
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn test_new_cell() {
116 let border_nodes = vec![1, 2, 3];
117 let distance_matrix = vec![0, 1, 2, 1, 0, 3, 2, 3, 0];
118 let id = 1;
119 let cell = Cell::new(border_nodes.clone(), distance_matrix.clone(), id);
120
121 assert_eq!(cell.border_nodes, border_nodes);
122 assert_eq!(cell.distance_matrix, distance_matrix);
123 assert_eq!(cell.id, id);
124 }
125
126 #[test]
127 fn test_get_distance() {
128 let cell = Cell::new(vec![1, 2, 3], vec![0, 4, 7, 4, 0, 2, 7, 2, 0], 1);
129
130 assert_eq!(cell.get_distance(0, 1), 4);
131 assert_eq!(cell.get_distance(1, 2), 2);
132 assert_eq!(cell.get_distance(0, 2), 7);
133 assert_eq!(cell.get_distance(2, 0), 7);
134 }
135
136 #[test]
137 fn test_border_nodes() {
138 let nodes = vec![1, 2, 3, 4];
139 let cell = Cell::new(
140 nodes.clone(),
141 vec![0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
142 1,
143 );
144
145 assert_eq!(cell.border_nodes(), &nodes);
146 }
147
148 #[test]
149 fn test_cell_id() {
150 let cell = Cell::new(vec![1], vec![0], 42);
151 assert_eq!(cell.id(), 42);
152 }
153
154 #[test]
155 fn test_cell_clone() {
156 let original = Cell::new(vec![1, 2], vec![0, 1, 1, 0], 1);
157 let cloned = original.clone();
158
159 assert_eq!(original.border_nodes(), cloned.border_nodes());
160 assert_eq!(original.id(), cloned.id());
161 assert_eq!(original.get_distance(0, 1), cloned.get_distance(0, 1));
162 }
163}