icentral_path_counts/
path_counts.rs1crate::ix!();
2
3
4pub const NO_PATHS: usize = 0;
5
6#[derive(Clone,Debug)]
7pub struct PathCounts {
8 name: String,
9 data: MaybeIndexedMap<usize>,
10}
11
12impl Default for PathCounts {
13 fn default() -> Self {
14 Self::empty_indexed("default_path_counts")
15 }
16}
17
18pub trait PathCountForNode {
19
20 fn path_count_for_node(&self, node: NodeId) -> usize;
21
22 fn path_count_for_node_ref(&self, node: NodeId) -> &usize;
23
24 fn path_count_for_node_mut(&mut self, node: NodeId) -> &mut usize;
25}
26
27impl PathCountForNode for PathCounts {
28
29 fn path_count_for_node(&self, node: NodeId) -> usize {
30 self.data[node]
31 }
32
33 fn path_count_for_node_ref(&self, node: NodeId) -> &usize {
34 &self.data[node]
35 }
36
37 fn path_count_for_node_mut(&mut self, node: NodeId) -> &mut usize {
38 &mut self.data[node]
39 }
40}
41
42impl PathCounts {
43
44 pub fn empty_indexed(name: &str) -> Self {
45
46 debug!("creating new empty_indexed PathCounts named {}", name);
47
48 Self {
49 name: name.to_owned(),
50 data: MaybeIndexedMap::empty_indexed()
51 }
52 }
53
54 pub fn empty_mapped(name: &str) -> Self {
55
56 debug!("creating new empty_mapped PathCounts named {}", name);
57
58 Self {
59 name: name.to_owned(),
60 data: MaybeIndexedMap::empty_mapped()
61 }
62 }
63
64 pub fn new(len: usize, name: &str) -> Self {
65
66 debug!("creating new PathCounts named {} of len: {}", name, len);
67
68 Self {
69 name: name.to_owned(),
70 data: MaybeIndexedMap::new(len,NO_PATHS),
71 }
72 }
73
74 pub fn len(&self) -> usize {
75 self.data.len()
76 }
77
78 pub fn increment_path_count_for_node(&mut self, node: NodeId, val: usize) {
79
80 debug!("in {}, increasing PathCount for node: {} by {}", self.name, node, val);
81
82 self.data[node] += val;
83 }
84
85 pub fn increment_path_count_for_node_from(
86 &mut self,
87 node: NodeId,
88 other: NodeId)
89 {
90 let other_path_count = self.data[other];
91 let node_path_count = self.data[node];
92
93 debug!(
94 "in {}, increasing PathCount for node: {} (currently {}) by the path count from the {} (currently {})",
95 self.name,
96 node,
97 node_path_count,
98 other,
99 other_path_count,
100 );
101
102 self.data[node] += other_path_count;
103 }
104
105 pub fn update_path_counts(
106 &mut self,
107 dst: NodeId,
108 src: NodeId)
109 {
110 self.set_path_count_for_node(dst, self.data[src]);
111 }
112
113 pub fn set_path_count_for_node(
114 &mut self,
115 node: NodeId,
116 count: usize)
117 {
118 debug!("in {}, setting path count for node {} to {}", self.name, node, count);
119
120 self.data[node] = count;
121 }
122
123 pub fn path_count_ratio(&self, v_p: NodeId, v_n: NodeId) -> f64 {
124
125 let ratio = self.path_count_for_node(v_p) as f64 / self.path_count_for_node(v_n) as f64;
126
127 debug!("in {}, computed path_count ratio {} for nodes {} and {}", self.name, ratio, v_p, v_n);
128
129 ratio
130 }
131
132 pub fn set_path_count_to_one(&mut self, source: NodeId) {
136
137 self.set_path_count_for_node(source,1);
138 }
139
140 pub fn set_path_count_to_zero(&mut self, source: NodeId) {
144
145 self.set_path_count_for_node(source,0);
146 }
147
148 pub fn reinit(&mut self, len: usize) {
149
150 debug!("reinitializing PathCounts {} to len: {}", self.name, len);
151
152 self.data.clear();
153
154 self.data.refill(
155 len,
156 NO_PATHS
157 );
158 }
159}