-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.rs
37 lines (32 loc) · 1.02 KB
/
path.rs
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
use std::collections::HashMap;
use crate::block::{CityBlock, Heat};
pub(crate) struct CityMapPath {
map: HashMap::<CityBlock,(Heat, Option<CityBlock>)>,
total_heat_loss: Heat,
target: CityBlock
}
impl CityMapPath {
pub(crate) fn total_heat_loss(&self) -> Heat { self.total_heat_loss }
pub(crate) fn new(map: HashMap::<CityBlock,(Heat, Option<CityBlock>)>, target: CityBlock) -> CityMapPath {
let total_heat_loss = map[&target].0;
CityMapPath { map, total_heat_loss, target}
}
pub(crate) fn iter(&self) -> PathIter {
PathIter { path: self, current: Some(self.target) }
}
}
pub(crate) struct PathIter<'a> {
path: &'a CityMapPath,
current: Option<CityBlock>
}
impl Iterator for PathIter<'_> {
type Item = (Heat, CityBlock);
fn next(&mut self) -> Option<Self::Item> {
self.current
.map(|current|{
let (heat, parent) = self.path.map[¤t];
self.current = parent;
(heat,current)
})
}
}