Skip to content

Commit 3f55673

Browse files
committed
Implement shrink_to_fit for MeshGraph.
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.
1 parent d33539c commit 3f55673

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

plexus/src/entity/storage/hash.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ where
2828
phantom: PhantomData<P>,
2929
}
3030

31+
impl<E, R, P> HashStorage<E, R, P>
32+
where
33+
E: Entity,
34+
InnerKey<E::Key>: Eq + Hash,
35+
R: Default,
36+
P: Mode,
37+
{
38+
pub fn shrink_to_fit(&mut self) {
39+
self.inner.shrink_to_fit();
40+
}
41+
}
42+
3143
impl<E> AsStorage<E> for HashStorage<E, (), Dynamic>
3244
where
3345
E: Entity<Storage = Self>,

plexus/src/graph/core.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ pub struct Core<G, V = (), A = (), E = (), F = ()>
4141
where
4242
G: GraphData,
4343
{
44-
vertices: V,
45-
arcs: A,
46-
edges: E,
47-
faces: F,
44+
pub(in crate::graph) vertices: V,
45+
pub(in crate::graph) arcs: A,
46+
pub(in crate::graph) edges: E,
47+
pub(in crate::graph) faces: F,
4848
phantom: PhantomData<G>,
4949
}
5050

plexus/src/graph/mod.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ where
564564

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

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

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

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

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

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

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

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

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

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

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

689696
/// Gets an iterator of orphan views over the faces in the graph.
690697
pub fn face_orphans(&mut self) -> impl Iterator<Item = FaceOrphan<G>> {
691-
self.as_storage_mut_of::<Face<_>>()
698+
self.core
699+
.faces
692700
.iter_mut()
693701
.map(|(key, data)| Orphan::bind_unchecked(data, key))
694702
.map(From::from)
@@ -746,7 +754,8 @@ where
746754
// better than using `FaceView::triangulate` until triangulation
747755
// is reworked.
748756
let keys = self
749-
.as_storage_of::<Face<_>>()
757+
.core
758+
.faces
750759
.iter()
751760
.map(|(key, _)| key)
752761
.collect::<Vec<_>>();
@@ -890,7 +899,8 @@ where
890899
/// ```
891900
pub fn disjoint_subgraph_vertices(&self) -> impl ExactSizeIterator<Item = VertexView<&Self>> {
892901
let keys = self
893-
.as_storage_of::<Vertex<_>>()
902+
.core
903+
.vertices
894904
.iter()
895905
.map(|(key, _)| key)
896906
.collect::<HashSet<_>>();
@@ -909,6 +919,15 @@ where
909919
unimplemented!()
910920
}
911921

922+
/// Shrinks the capacity of the graph's underlying storage as much as
923+
/// possible.
924+
pub fn shrink_to_fit(&mut self) {
925+
self.core.vertices.shrink_to_fit();
926+
self.core.arcs.shrink_to_fit();
927+
self.core.edges.shrink_to_fit();
928+
self.core.faces.shrink_to_fit();
929+
}
930+
912931
/// Creates a [`Buildable`] mesh data structure from the graph.
913932
///
914933
/// The output is created from each unique vertex in the graph. No face data

0 commit comments

Comments
 (0)