Skip to main content

polyhedron/
data_container.rs

1use std::fmt::Debug;
2
3/// Abstract storage interface.
4pub trait StorageBuffer: Clone + Debug + Default
5{
6    /// Underlying element stored in this collection.
7    type Element: Clone + Debug + Default;
8
9    /// Read the element at the specified index.
10    fn read(&self, idx: u64) -> &Self::Element;
11    /// Update the element at the specified index.
12    fn update<F: FnMut(&mut Self::Element)>(&mut self, idx: u64, update: F);
13
14    /// Add an element to the collection.
15    fn append(&mut self, value: Self::Element);
16    /// Remove the element at the specified index from the collection.
17    fn discard(&mut self, idx: u64) -> Self::Element;
18    /// Remove elements for which `keep` returns false.
19    fn filter<F: FnMut(&Self::Element) -> bool>(&mut self, keep: F);
20    /// Cardinality of the collection.
21    fn len(&self) -> usize;
22    /// Iterate over the collection.
23    fn iter_ref(&self) -> impl Iterator<Item = &Self::Element>;
24}
25
26impl<T: Default + Clone + Debug> StorageBuffer for Vec<T>
27{
28    type Element = T;
29
30    fn read(&self, idx: u64) -> &Self::Element { &self[idx as usize] }
31
32    fn update<F: FnMut(&mut Self::Element)>(&mut self, idx: u64, mut update: F)
33    {
34        update(&mut self[idx as usize])
35    }
36
37    fn append(&mut self, value: Self::Element) { self.push(value) }
38
39    fn discard(&mut self, idx: u64) -> Self::Element { self.remove(idx as usize) }
40
41    fn filter<F: FnMut(&Self::Element) -> bool>(&mut self, keep: F) { self.retain(keep) }
42
43    fn len(&self) -> usize { self.len() }
44
45    fn iter_ref(&self) -> impl Iterator<Item = &Self::Element> { self.iter() }
46}
47
48impl StorageBuffer for ()
49{
50    type Element = ();
51
52    fn read(&self, _idx: u64) -> &Self::Element { self }
53
54    fn update<F: FnMut(&mut Self::Element)>(&mut self, _idx: u64, mut _update: F) {}
55
56    fn append(&mut self, _value: Self::Element) {}
57
58    fn discard(&mut self, _idx: u64) -> Self::Element {}
59
60    fn filter<F: FnMut(&Self::Element) -> bool>(&mut self, _f: F) {}
61
62    fn iter_ref(&self) -> impl Iterator<Item = &Self::Element> { std::iter::empty() }
63
64    fn len(&self) -> usize { 0 }
65}
66
67/// Defines container layout for mesh elements.
68pub trait MeshStorage
69{
70    /// Sorage for vertex data.
71    type VertStorage: StorageBuffer;
72    /// Sorage for edge data.
73    type EdgeStorage: StorageBuffer;
74    /// Sorage for face data.
75    type FaceStorage: StorageBuffer;
76}
77
78/// Helper type accessor for vertex data.
79pub type VertData<M> = <<M as MeshStorage>::VertStorage as StorageBuffer>::Element;
80/// Helper type accessor for edge data.
81pub type EdgeData<M> = <<M as MeshStorage>::EdgeStorage as StorageBuffer>::Element;
82/// Helper type accessor for face data.
83pub type FaceData<M> = <<M as MeshStorage>::FaceStorage as StorageBuffer>::Element;
84
85impl<V, E, F> MeshStorage for (V, E, F)
86where
87    V: StorageBuffer,
88    E: StorageBuffer,
89    F: StorageBuffer,
90{
91    type EdgeStorage = E;
92    type FaceStorage = F;
93    type VertStorage = V;
94}