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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::ops::{Add, AddAssign};
pub trait DijkstraPerformanceData {
fn add_iteration(&mut self);
fn add_unnecessary_heap_element(&mut self);
fn record_heap_size(&mut self, heap_size: usize);
fn record_distance_array_size(&mut self, distance_array_size: usize);
fn finish_dijkstra(&mut self);
fn iterations(&self) -> Option<u64>;
fn unnecessary_heap_elements(&self) -> Option<u64>;
fn max_max_heap_size(&self) -> Option<usize>;
fn max_max_distance_array_size(&self) -> Option<usize>;
fn average_max_heap_size(&self) -> Option<f64>;
fn average_max_distance_array_size(&self) -> Option<f64>;
}
#[derive(Default, Debug, Clone, Eq, PartialEq)]
pub struct DijkstraPerformanceCounter {
pub iterations: u64,
pub unnecessary_heap_elements: u64,
max_heap_size: usize,
max_distance_array_size: usize,
max_max_heap_size: usize,
max_max_distance_array_size: usize,
sum_max_heap_size: u128,
sum_max_distance_array_size: u128,
total_invocations: u64,
}
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
pub struct NoopDijkstraPerformanceCounter;
impl DijkstraPerformanceData for DijkstraPerformanceCounter {
fn add_iteration(&mut self) {
self.iterations += 1;
}
fn add_unnecessary_heap_element(&mut self) {
self.unnecessary_heap_elements += 1;
}
fn record_heap_size(&mut self, heap_size: usize) {
self.max_heap_size = self.max_heap_size.max(heap_size);
}
fn record_distance_array_size(&mut self, distance_array_size: usize) {
self.max_distance_array_size = self.max_distance_array_size.max(distance_array_size);
}
fn finish_dijkstra(&mut self) {
self.max_max_heap_size = self.max_max_heap_size.max(self.max_heap_size);
self.max_max_distance_array_size = self
.max_max_distance_array_size
.max(self.max_distance_array_size);
self.sum_max_heap_size += self.max_heap_size as u128;
self.sum_max_distance_array_size += self.max_distance_array_size as u128;
self.max_heap_size = 0;
self.max_distance_array_size = 0;
self.total_invocations += 1;
}
fn iterations(&self) -> Option<u64> {
Some(self.iterations)
}
fn unnecessary_heap_elements(&self) -> Option<u64> {
Some(self.unnecessary_heap_elements)
}
fn max_max_heap_size(&self) -> Option<usize> {
Some(self.max_max_heap_size)
}
fn max_max_distance_array_size(&self) -> Option<usize> {
Some(self.max_max_distance_array_size)
}
fn average_max_heap_size(&self) -> Option<f64> {
Some(self.sum_max_heap_size as f64 / self.total_invocations as f64)
}
fn average_max_distance_array_size(&self) -> Option<f64> {
Some(self.sum_max_distance_array_size as f64 / self.total_invocations as f64)
}
}
impl DijkstraPerformanceData for NoopDijkstraPerformanceCounter {
fn add_iteration(&mut self) {}
fn add_unnecessary_heap_element(&mut self) {}
fn record_heap_size(&mut self, _heap_size: usize) {}
fn record_distance_array_size(&mut self, _distance_array_size: usize) {}
fn finish_dijkstra(&mut self) {}
fn iterations(&self) -> Option<u64> {
None
}
fn unnecessary_heap_elements(&self) -> Option<u64> {
None
}
fn max_max_heap_size(&self) -> Option<usize> {
None
}
fn max_max_distance_array_size(&self) -> Option<usize> {
None
}
fn average_max_heap_size(&self) -> Option<f64> {
None
}
fn average_max_distance_array_size(&self) -> Option<f64> {
None
}
}
impl Add for DijkstraPerformanceCounter {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
iterations: self.iterations + rhs.iterations,
unnecessary_heap_elements: self.unnecessary_heap_elements
+ rhs.unnecessary_heap_elements,
max_heap_size: self.max_heap_size.max(rhs.max_heap_size),
max_distance_array_size: self
.max_distance_array_size
.max(rhs.max_distance_array_size),
max_max_heap_size: self.max_max_heap_size.max(rhs.max_max_heap_size),
max_max_distance_array_size: self
.max_max_distance_array_size
.max(rhs.max_max_distance_array_size),
sum_max_heap_size: self.sum_max_heap_size + rhs.sum_max_heap_size,
sum_max_distance_array_size: self.sum_max_distance_array_size
+ rhs.sum_max_distance_array_size,
total_invocations: self.total_invocations + rhs.total_invocations,
}
}
}
impl Add for NoopDijkstraPerformanceCounter {
type Output = Self;
fn add(self, _rhs: Self) -> Self::Output {
Self
}
}
impl AddAssign for DijkstraPerformanceCounter {
fn add_assign(&mut self, rhs: Self) {
*self = self.clone() + rhs;
}
}
impl AddAssign for NoopDijkstraPerformanceCounter {
fn add_assign(&mut self, _rhs: Self) {
}
}