From 9a5b0467249919e2e6393141252452329eb63742 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Tue, 31 Oct 2023 23:35:33 +0000 Subject: [PATCH] mesh: Use a `.map()` instead of custom iterator `struct`s --- src/mesh.rs | 112 +++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 66 deletions(-) diff --git a/src/mesh.rs b/src/mesh.rs index 3c083d0..ba89fd0 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -98,7 +98,7 @@ impl Mesh { impl visit::GraphBase for Mesh { type NodeId = VertexIndex; - type EdgeId = FixedDirectedEdgeHandle; + type EdgeId = (VertexIndex, VertexIndex); } pub struct MeshVisitMap { @@ -160,25 +160,22 @@ impl visit::Data for Mesh { } #[derive(Clone, Copy)] -pub struct MeshEdgeReference<'a> { - handle: DirectedEdgeHandle<'a, Vertex, (), (), ()>, +pub struct MeshEdgeReference { + from: VertexIndex, + to: VertexIndex, } -impl<'a> visit::EdgeRef for MeshEdgeReference<'a> { +impl<'a> visit::EdgeRef for MeshEdgeReference { type NodeId = VertexIndex; - type EdgeId = FixedDirectedEdgeHandle; + type EdgeId = (VertexIndex, VertexIndex); type Weight = (); fn source(&self) -> Self::NodeId { - VertexIndex { - handle: self.handle.from().fix(), - } + self.from } fn target(&self) -> Self::NodeId { - VertexIndex { - handle: self.handle.to().fix(), - } + self.to } fn weight(&self) -> &Self::Weight { @@ -186,78 +183,61 @@ impl<'a> visit::EdgeRef for MeshEdgeReference<'a> { } fn id(&self) -> Self::EdgeId { - self.handle.fix() - } -} - -pub struct MeshEdgeReferences<'a> { - iter: DirectedEdgeIterator<'a, Vertex, (), (), ()>, -} - -impl<'a> Iterator for MeshEdgeReferences<'a> { - type Item = MeshEdgeReference<'a>; - - fn next(&mut self) -> Option { - let handle = self.iter.next()?; - Some(MeshEdgeReference { handle }) + (self.from, self.to) } } impl<'a> visit::IntoEdgeReferences for &'a Mesh { - type EdgeRef = MeshEdgeReference<'a>; - type EdgeReferences = MeshEdgeReferences<'a>; + type EdgeRef = MeshEdgeReference; + type EdgeReferences = Box + 'a>; fn edge_references(self) -> Self::EdgeReferences { - MeshEdgeReferences { - iter: self.triangulation.directed_edges(), - } - } -} - -pub struct MeshNeighbors<'a> { - iter: Box> + 'a>, -} - -impl<'a> Iterator for MeshNeighbors<'a> { - type Item = VertexIndex; - - fn next(&mut self) -> Option { - let handle = self.iter.next()?; - Some(VertexIndex { - handle: handle.to().fix(), - }) + Box::new( + self.triangulation + .directed_edges() + .map(|edge| MeshEdgeReference { + from: VertexIndex { + handle: edge.from().fix(), + }, + to: VertexIndex { + handle: edge.to().fix(), + }, + }), + ) } } impl<'a> visit::IntoNeighbors for &'a Mesh { - type Neighbors = MeshNeighbors<'a>; + type Neighbors = Box + 'a>; fn neighbors(self, a: Self::NodeId) -> Self::Neighbors { - MeshNeighbors { - iter: Box::new(self.triangulation.vertex(a.handle).out_edges()), - } - } -} - -pub struct MeshEdges<'a> { - iter: Box> + 'a>, -} - -impl<'a> Iterator for MeshEdges<'a> { - type Item = MeshEdgeReference<'a>; - - fn next(&mut self) -> Option { - let handle = self.iter.next()?; - Some(MeshEdgeReference { handle }) + Box::new( + self.triangulation + .vertex(a.handle) + .out_edges() + .map(|handle| VertexIndex { + handle: handle.to().fix(), + }), + ) } } impl<'a> visit::IntoEdges for &'a Mesh { - type Edges = MeshEdges<'a>; + type Edges = Box + 'a>; fn edges(self, a: Self::NodeId) -> Self::Edges { - MeshEdges { - iter: Box::new(self.triangulation.vertex(a.handle).out_edges()), - } + Box::new( + self.triangulation + .vertex(a.handle) + .out_edges() + .map(|edge| MeshEdgeReference { + from: VertexIndex { + handle: edge.from().fix(), + }, + to: VertexIndex { + handle: edge.to().fix(), + }, + }), + ) } }