hexagon_map/sparse_map/
iters.rs

1use super::*;
2
3use crate::HexPoint;
4use std::collections::btree_map::{Iter, IterMut};
5
6pub struct GetHexagonPoints<'i, T> {
7    map: Iter<'i, CubicPoint, T>,
8}
9
10pub struct MutGetHexagonPoints<'i, T> {
11    map: IterMut<'i, CubicPoint, T>,
12}
13
14impl<'i, T> Iterator for GetHexagonPoints<'i, T> {
15    type Item = (CubicPoint, &'i T);
16
17    fn next(&mut self) -> Option<Self::Item> {
18        let (p, v) = self.map.next()?;
19        Some((*p, v))
20    }
21}
22
23impl<'i, T> Iterator for MutGetHexagonPoints<'i, T> {
24    type Item = (CubicPoint, &'i T);
25
26    fn next(&mut self) -> Option<Self::Item> {
27        let (p, v) = self.map.next()?;
28        Some((*p, v))
29    }
30}
31
32impl<T> HexagonMap<T> {
33    pub fn points_all(&self) -> GetHexagonPoints<T> {
34        GetHexagonPoints { map: self.sparse.iter() }
35    }
36    pub fn points_mut(&mut self) -> MutGetHexagonPoints<T> {
37        MutGetHexagonPoints { map: self.sparse.iter_mut() }
38    }
39}
40
41pub struct GetHexagonAround<'i, T> {
42    map: &'i HexagonMap<T>,
43    current: IsometricLine,
44}
45
46pub struct MutHexagonAround<'i, T> {
47    map: &'i mut HexagonMap<T>,
48    current: IsometricLine,
49}
50
51impl<'i, T> Iterator for GetHexagonAround<'i, T> {
52    type Item = (CubicPoint, &'i T);
53
54    fn next(&mut self) -> Option<Self::Item> {
55        let p = self.current.next()?;
56        match self.map.sparse.get(&p) {
57            Some(s) => Some((p, s)),
58            None => self.next(),
59        }
60    }
61}
62
63impl<'i, T> Iterator for MutHexagonAround<'i, T> {
64    type Item = (CubicPoint, &'i mut T);
65
66    fn next(&mut self) -> Option<Self::Item> {
67        let p = self.current.next()?;
68        match self.map.sparse.get_mut(&p) {
69            Some(s) => Some((p, s)),
70            None => self.next(),
71        };
72        todo!()
73    }
74}
75
76impl<T> HexagonMap<T> {
77    /// Count all defined points in the map.
78    pub fn points_count(&self) -> usize {
79        self.sparse.len()
80    }
81    /// Find at most 6 points that are exists and adjacent to given point.
82    pub fn points_nearby(&self, source: CubicPoint) -> Vec<CubicPoint> {
83        let mut out = Vec::with_capacity(6);
84        for direction in Orientation::all() {
85            let target = source.go(direction);
86            if self.sparse.contains_key(&target) {
87                out.push(target);
88            }
89        }
90        out
91    }
92    /// Find at most 6 joints that are exists and adjacent to given point.
93    pub fn joints_nearby(&self, source: CubicPoint) -> Vec<Joint> {
94        let mut out = Vec::with_capacity(6);
95        for direction in Orientation::all() {
96            let target = source.go(direction);
97            if self.sparse.contains_key(&target) {
98                out.push(source.as_joint(direction));
99            }
100        }
101        out
102    }
103    /// Find all points that are within a certain distance of a point.
104    pub fn points_around(&self, source: CubicPoint, steps: usize) -> GetHexagonAround<T> {
105        GetHexagonAround { map: self, current: IsometricLine::new(source, steps) }
106    }
107}