hypergraph_rs/geometry/
positions.rs1use indexmap::IndexMap;
4
5use super::Point;
6
7#[derive(Clone, Debug, Default, PartialEq)]
11pub struct Positions {
12 map: IndexMap<String, Point>,
13}
14
15impl Positions {
16 #[must_use]
18 pub fn new() -> Self {
19 Self::default()
20 }
21
22 pub fn insert(&mut self, id: impl Into<String>, at: Point) -> Option<Point> {
24 self.map.insert(id.into(), at)
25 }
26
27 #[must_use]
29 pub fn get(&self, id: &str) -> Option<Point> {
30 self.map.get(id).copied()
31 }
32
33 #[must_use]
35 pub fn len(&self) -> usize {
36 self.map.len()
37 }
38
39 #[must_use]
41 pub fn is_empty(&self) -> bool {
42 self.map.is_empty()
43 }
44
45 pub fn iter(&self) -> impl Iterator<Item = (&str, Point)> + '_ {
47 self.map.iter().map(|(id, at)| (id.as_str(), *at))
48 }
49
50 pub(crate) fn point_mut(&mut self, index: usize) -> &mut Point {
52 self.map
53 .get_index_mut(index)
54 .expect("index in range of positions")
55 .1
56 }
57
58 pub(crate) fn id_at(&self, index: usize) -> &str {
60 self.map
61 .get_index(index)
62 .expect("index in range of positions")
63 .0
64 .as_str()
65 }
66}
67
68impl FromIterator<(String, Point)> for Positions {
69 fn from_iter<I: IntoIterator<Item = (String, Point)>>(iter: I) -> Self {
70 Self {
71 map: iter.into_iter().collect(),
72 }
73 }
74}
75
76#[derive(Clone, Debug, Default, PartialEq)]
78pub struct Placement {
79 pub positions: Positions,
81 pub unplaced: Vec<String>,
83}