Skip to content

Commit

Permalink
Implement shrink_to_fit for MeshGraph.
Browse files Browse the repository at this point in the history
This change introduces a `shrink_to_fit` function for `MeshGraph`s. To
achieve this with little overhead, `Core` now exposes its storage fields
in the `graph` module, bypassing `AsStorage` and `Dispatch`. The
dispatch mechanism isn't needed for `MeshGraph`, which only ever
operates on an `OwnedCore` and known storage types.
  • Loading branch information
olson-sean-k committed Dec 9, 2020
1 parent d33539c commit 3f55673
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
12 changes: 12 additions & 0 deletions plexus/src/entity/storage/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ where
phantom: PhantomData<P>,
}

impl<E, R, P> HashStorage<E, R, P>
where
E: Entity,
InnerKey<E::Key>: Eq + Hash,
R: Default,
P: Mode,
{
pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to_fit();
}
}

impl<E> AsStorage<E> for HashStorage<E, (), Dynamic>
where
E: Entity<Storage = Self>,
Expand Down
8 changes: 4 additions & 4 deletions plexus/src/graph/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub struct Core<G, V = (), A = (), E = (), F = ()>
where
G: GraphData,
{
vertices: V,
arcs: A,
edges: E,
faces: F,
pub(in crate::graph) vertices: V,
pub(in crate::graph) arcs: A,
pub(in crate::graph) edges: E,
pub(in crate::graph) faces: F,
phantom: PhantomData<G>,
}

Expand Down
47 changes: 33 additions & 14 deletions plexus/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ where

/// Gets the number of vertices in the graph.
pub fn vertex_count(&self) -> usize {
self.as_storage_of::<Vertex<_>>().len()
self.core.vertices.len()
}

/// Gets an immutable view of the vertex with the given key.
Expand All @@ -580,7 +580,8 @@ where
// TODO: Return `Clone + Iterator`.
/// Gets an iterator of immutable views over the vertices in the graph.
pub fn vertices(&self) -> impl Iterator<Item = VertexView<&Self>> {
self.as_storage_of::<Vertex<_>>()
self.core
.vertices
.iter()
.map(|(key, _)| key)
.map(move |key| View::bind_unchecked(self, key))
Expand All @@ -589,15 +590,16 @@ where

/// Gets an iterator of orphan views over the vertices in the graph.
pub fn vertex_orphans(&mut self) -> impl Iterator<Item = VertexOrphan<G>> {
self.as_storage_mut_of::<Vertex<_>>()
self.core
.vertices
.iter_mut()
.map(|(key, data)| Orphan::bind_unchecked(data, key))
.map(From::from)
}

/// Gets the number of arcs in the graph.
pub fn arc_count(&self) -> usize {
self.as_storage_of::<Arc<_>>().len()
self.core.arcs.len()
}

/// Gets an immutable view of the arc with the given key.
Expand All @@ -613,7 +615,8 @@ where
// TODO: Return `Clone + Iterator`.
/// Gets an iterator of immutable views over the arcs in the graph.
pub fn arcs(&self) -> impl Iterator<Item = ArcView<&Self>> {
self.as_storage_of::<Arc<_>>()
self.core
.arcs
.iter()
.map(|(key, _)| key)
.map(move |key| View::bind_unchecked(self, key))
Expand All @@ -622,15 +625,16 @@ where

/// Gets an iterator of orphan views over the arcs in the graph.
pub fn arc_orphans(&mut self) -> impl Iterator<Item = ArcOrphan<G>> {
self.as_storage_mut_of::<Arc<_>>()
self.core
.arcs
.iter_mut()
.map(|(key, data)| Orphan::bind_unchecked(data, key))
.map(From::from)
}

/// Gets the number of edges in the graph.
pub fn edge_count(&self) -> usize {
self.as_storage_of::<Edge<_>>().len()
self.core.edges.len()
}

/// Gets an immutable view of the edge with the given key.
Expand All @@ -646,7 +650,8 @@ where
// TODO: Return `Clone + Iterator`.
/// Gets an iterator of immutable views over the edges in the graph.
pub fn edges(&self) -> impl Iterator<Item = EdgeView<&Self>> {
self.as_storage_of::<Edge<_>>()
self.core
.edges
.iter()
.map(|(key, _)| key)
.map(move |key| View::bind_unchecked(self, key))
Expand All @@ -655,15 +660,16 @@ where

/// Gets an iterator of orphan views over the edges in the graph.
pub fn edge_orphans(&mut self) -> impl Iterator<Item = EdgeOrphan<G>> {
self.as_storage_mut_of::<Edge<_>>()
self.core
.edges
.iter_mut()
.map(|(key, data)| Orphan::bind_unchecked(data, key))
.map(From::from)
}

/// Gets the number of faces in the graph.
pub fn face_count(&self) -> usize {
self.as_storage_of::<Face<_>>().len()
self.core.faces.len()
}

/// Gets an immutable view of the face with the given key.
Expand All @@ -679,7 +685,8 @@ where
// TODO: Return `Clone + Iterator`.
/// Gets an iterator of immutable views over the faces in the graph.
pub fn faces(&self) -> impl Iterator<Item = FaceView<&Self>> {
self.as_storage_of::<Face<_>>()
self.core
.faces
.iter()
.map(|(key, _)| key)
.map(move |key| View::bind_unchecked(self, key))
Expand All @@ -688,7 +695,8 @@ where

/// Gets an iterator of orphan views over the faces in the graph.
pub fn face_orphans(&mut self) -> impl Iterator<Item = FaceOrphan<G>> {
self.as_storage_mut_of::<Face<_>>()
self.core
.faces
.iter_mut()
.map(|(key, data)| Orphan::bind_unchecked(data, key))
.map(From::from)
Expand Down Expand Up @@ -746,7 +754,8 @@ where
// better than using `FaceView::triangulate` until triangulation
// is reworked.
let keys = self
.as_storage_of::<Face<_>>()
.core
.faces
.iter()
.map(|(key, _)| key)
.collect::<Vec<_>>();
Expand Down Expand Up @@ -890,7 +899,8 @@ where
/// ```
pub fn disjoint_subgraph_vertices(&self) -> impl ExactSizeIterator<Item = VertexView<&Self>> {
let keys = self
.as_storage_of::<Vertex<_>>()
.core
.vertices
.iter()
.map(|(key, _)| key)
.collect::<HashSet<_>>();
Expand All @@ -909,6 +919,15 @@ where
unimplemented!()
}

/// Shrinks the capacity of the graph's underlying storage as much as
/// possible.
pub fn shrink_to_fit(&mut self) {
self.core.vertices.shrink_to_fit();
self.core.arcs.shrink_to_fit();
self.core.edges.shrink_to_fit();
self.core.faces.shrink_to_fit();
}

/// Creates a [`Buildable`] mesh data structure from the graph.
///
/// The output is created from each unique vertex in the graph. No face data
Expand Down

0 comments on commit 3f55673

Please sign in to comment.