lib_jsl_geometry/
tables.rs1use lib_jsl_core::ErrorsJSL;
9use crate::common::{GeometricPrimitive, IsGeometryKey, IsGeometryTable};
10use std::cell::RefCell;
11use std::collections::HashMap;
12use std::rc::Rc;
13
14pub type SharedGeometryTable<K, V> = Rc<RefCell<HashGeometryTable<K, V>>>;
19
20#[derive(Debug, Clone)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize))]
25pub struct HashGeometryTable<K, V> {
26 entries: HashMap<K, V>,
27}
28
29impl<K, V> Default for HashGeometryTable<K, V> {
30 fn default() -> Self {
31 Self {
32 entries: HashMap::new(),
33 }
34 }
35}
36
37impl<K, V> HashGeometryTable<K, V> {
38 pub fn new() -> Self {
40 Self::default()
41 }
42}
43
44impl<K, V> IsGeometryTable for HashGeometryTable<K, V>
45where
46 K: IsGeometryKey,
47 V: GeometricPrimitive,
48{
49 type Key = K;
50 type Item = V;
51
52 fn get(&self, key: &Self::Key) -> Option<Self::Item> {
53 self.entries.get(key).cloned()
54 }
55
56 fn insert(&mut self, key: Self::Key, value: Self::Item) -> Result<(), ErrorsJSL> {
57 self.entries.insert(key, value);
58 Ok(())
59 }
60
61 fn remove(&mut self, key: &Self::Key) -> Option<Self::Item> {
62 self.entries.remove(key)
63 }
64
65 fn contains_key(&self, key: &Self::Key) -> bool {
66 self.entries.contains_key(key)
67 }
68
69 fn size(&self) -> usize {
70 self.entries.len()
71 }
72
73 fn union(&self, other: &Self) -> Self {
74 let mut entries = self.entries.clone();
75 entries.extend(other.entries.clone());
76 Self { entries }
77 }
78
79 fn intersection(&self, other: &Self) -> Self {
80 let entries = self
81 .entries
82 .iter()
83 .filter(|(key, _)| other.entries.contains_key(*key))
84 .map(|(key, value)| (*key, value.clone()))
85 .collect();
86 Self { entries }
87 }
88
89 fn iter(&self) -> Box<dyn Iterator<Item = (Self::Key, Self::Item)> + '_> {
90 Box::new(self.entries.iter().map(|(key, value)| (*key, value.clone())))
91 }
92
93 #[cfg(feature = "serde")]
94 fn to_file(&self, path: &str) -> Result<(), ErrorsJSL> {
95 let serialized = serde_json::to_string_pretty(self).map_err(|err| err.to_string())?;
96 std::fs::write(std::path::Path::new(path), serialized).map_err(|err| err.to_string())
97 }
98
99 #[cfg(not(feature = "serde"))]
100 fn to_file(&self, _path: &str) -> Result<(), ErrorsJSL> {
101 Err(ErrorsJSL::Message("writing geometry tables requires the `serde` feature".to_string()))
102 }
103
104 fn from_file(path: &str) -> Result<Self, ErrorsJSL>
105 where
106 Self: Sized,
107 {
108 let _ = path;
109 Err(ErrorsJSL::NotImplementedYet)
110 }
111}
112
113impl<K, V> IsGeometryTable for SharedGeometryTable<K, V>
114where
115 K: IsGeometryKey,
116 V: GeometricPrimitive,
117{
118 type Key = K;
119 type Item = V;
120
121 fn get(&self, key: &Self::Key) -> Option<Self::Item> {
122 self.borrow().get(key)
123 }
124
125 fn insert(&mut self, key: Self::Key, value: Self::Item) -> Result<(), ErrorsJSL> {
126 self.borrow_mut().insert(key, value)
127 }
128
129 fn remove(&mut self, key: &Self::Key) -> Option<Self::Item> {
130 self.borrow_mut().remove(key)
131 }
132
133 fn contains_key(&self, key: &Self::Key) -> bool {
134 self.borrow().contains_key(key)
135 }
136
137 fn size(&self) -> usize {
138 self.borrow().size()
139 }
140
141 fn union(&self, other: &Self) -> Self {
142 Rc::new(RefCell::new(self.borrow().union(&other.borrow())))
143 }
144
145 fn intersection(&self, other: &Self) -> Self {
146 Rc::new(RefCell::new(self.borrow().intersection(&other.borrow())))
147 }
148
149 fn iter(&self) -> Box<dyn Iterator<Item = (Self::Key, Self::Item)> + '_> {
150 let items: Vec<_> = self.borrow().iter().collect();
151 Box::new(items.into_iter())
152 }
153
154 fn to_file(&self, path: &str) -> Result<(), ErrorsJSL> {
155 self.borrow().to_file(path)
156 }
157
158 fn from_file(path: &str) -> Result<Self, ErrorsJSL>
159 where
160 Self: Sized,
161 {
162 HashGeometryTable::from_file(path).map(|table| Rc::new(RefCell::new(table)))
163 }
164}