From 251ccb74fc17b7ad5f7cfccaf6bc4a88080104e9 Mon Sep 17 00:00:00 2001 From: Gaute Hope Date: Mon, 8 Jul 2024 08:00:39 +0200 Subject: [PATCH] geometry_graph: make Send (rm Rc + RefCell) --- geo/src/algorithm/relate/edge_end_builder.rs | 5 +++-- .../relate/geomgraph/geometry_graph.rs | 18 ++++++++-------- .../geomgraph/index/prepared_geometry.rs | 7 ++++--- .../geomgraph/index/segment_intersector.rs | 21 +++++++++---------- .../index/simple_edge_set_intersector.rs | 13 ++++++------ .../relate/geomgraph/planar_graph.rs | 15 ++++++------- geo/src/algorithm/relate/relate_operation.rs | 4 ++-- 7 files changed, 43 insertions(+), 40 deletions(-) diff --git a/geo/src/algorithm/relate/edge_end_builder.rs b/geo/src/algorithm/relate/edge_end_builder.rs index 4fcaf93c1d..fdaca8e8a7 100644 --- a/geo/src/algorithm/relate/edge_end_builder.rs +++ b/geo/src/algorithm/relate/edge_end_builder.rs @@ -2,7 +2,8 @@ use super::geomgraph::{Edge, EdgeEnd, EdgeIntersection}; use crate::GeoFloat; use std::cell::RefCell; -use std::rc::Rc; +// use std::rc::Rc; +use std::sync::Arc; /// Computes the [`EdgeEnd`]s which arise from an [`Edge`] who has had its `edge_intersections` /// populated with self and proper [`EdgeIntersection`]s. @@ -19,7 +20,7 @@ impl EdgeEndBuilder { } } - pub fn compute_ends_for_edges(&self, edges: &[Rc>>]) -> Vec> { + pub fn compute_ends_for_edges(&self, edges: &[Arc>>]) -> Vec> { let mut list = vec![]; for edge in edges { self.compute_ends_for_edge(&mut edge.borrow_mut(), &mut list); diff --git a/geo/src/algorithm/relate/geomgraph/geometry_graph.rs b/geo/src/algorithm/relate/geomgraph/geometry_graph.rs index 6746dc8ac3..44f925cdf6 100644 --- a/geo/src/algorithm/relate/geomgraph/geometry_graph.rs +++ b/geo/src/algorithm/relate/geomgraph/geometry_graph.rs @@ -10,8 +10,9 @@ use crate::HasDimensions; use crate::{Coord, GeoFloat, GeometryCow, Line, LineString, Point, Polygon}; use rstar::{RTree, RTreeNum}; -use std::cell::RefCell; -use std::rc::Rc; +// use std::cell::RefCell; +// use std::rc::Rc; +use std::sync::Arc; /// The computation of the [`IntersectionMatrix`](crate::algorithm::relate::IntersectionMatrix) relies on the use of a /// structure called a "topology graph". The topology graph contains nodes (CoordNode) and @@ -35,7 +36,7 @@ where { arg_index: usize, parent_geometry: GeometryCow<'a, F>, - tree: Option>>>, + tree: Option>>>, use_boundary_determination_rule: bool, has_computed_self_nodes: bool, planar_graph: PlanarGraph, @@ -49,14 +50,14 @@ impl GeometryGraph<'_, F> where F: GeoFloat, { - pub(crate) fn set_tree(&mut self, tree: Rc>>) { + pub(crate) fn set_tree(&mut self, tree: Arc>>) { self.tree = Some(tree); } - pub(crate) fn get_or_build_tree(&self) -> Rc>> { + pub(crate) fn get_or_build_tree(&self) -> Arc>> { self.tree .clone() - .unwrap_or_else(|| Rc::new(self.build_tree())) + .unwrap_or_else(|| Arc::new(self.build_tree())) } pub(crate) fn build_tree(&self) -> RTree> { @@ -65,7 +66,7 @@ where .iter() .enumerate() .flat_map(|(edge_idx, edge)| { - let edge = RefCell::borrow(edge); + // let edge = RefCell::borrow(edge); let start_of_final_segment: usize = edge.coords().len() - 1; (0..start_of_final_segment).map(move |segment_idx| { let p1 = edge.coords()[segment_idx]; @@ -105,7 +106,7 @@ where } } - pub(crate) fn edges(&self) -> &[Rc>>] { + pub(crate) fn edges(&self) -> &[Edge] { self.planar_graph.edges() } @@ -404,7 +405,6 @@ where let positions_and_intersections: Vec<(CoordPos, Vec>)> = self .edges() .iter() - .map(|cell| cell.borrow()) .map(|edge| { let position = edge .label() diff --git a/geo/src/algorithm/relate/geomgraph/index/prepared_geometry.rs b/geo/src/algorithm/relate/geomgraph/index/prepared_geometry.rs index 2fbc199e21..e5e54841a7 100644 --- a/geo/src/algorithm/relate/geomgraph/index/prepared_geometry.rs +++ b/geo/src/algorithm/relate/geomgraph/index/prepared_geometry.rs @@ -5,7 +5,8 @@ use crate::GeometryCow; use crate::{GeoFloat, Relate}; use std::cell::RefCell; -use std::rc::Rc; +// use std::rc::Rc; +use std::sync::Arc; use rstar::{RTree, RTreeNum}; @@ -38,7 +39,7 @@ mod conversions { Geometry, GeometryCollection, Line, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, }; - use std::rc::Rc; + use std::sync::Arc; impl<'a, F: GeoFloat> From> for PreparedGeometry<'a, F> { fn from(point: Point) -> Self { @@ -156,7 +157,7 @@ mod conversions { impl<'a, F: GeoFloat> From> for PreparedGeometry<'a, F> { fn from(geometry: GeometryCow<'a, F>) -> Self { let mut geometry_graph = GeometryGraph::new(0, geometry); - geometry_graph.set_tree(Rc::new(geometry_graph.build_tree())); + geometry_graph.set_tree(Arc::new(geometry_graph.build_tree())); // TODO: don't pass in line intersector here - in theory we'll want pluggable line intersectors // and the type (Robust) shouldn't be hard coded here. diff --git a/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs b/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs index 933281d022..868b5915ab 100644 --- a/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs +++ b/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs @@ -64,12 +64,12 @@ where fn is_trivial_intersection( &self, intersection: LineIntersection, - edge0: &RefCell>, + edge0: &Edge, segment_index_0: usize, - edge1: &RefCell>, + edge1: &Edge, segment_index_1: usize, ) -> bool { - if edge0.as_ptr() != edge1.as_ptr() { + if !std::ptr::eq(edge0, edge1) { return false; } @@ -81,7 +81,6 @@ where return true; } - let edge0 = edge0.borrow(); if edge0.is_closed() { // first and last coords in a ring are adjacent let max_segment_index = edge0.coords().len() - 1; @@ -97,23 +96,23 @@ where pub fn add_intersections( &mut self, - edge0: &RefCell>, + edge0: &Edge, segment_index_0: usize, - edge1: &RefCell>, + edge1: &Edge, segment_index_1: usize, ) { // avoid a segment spuriously "intersecting" with itself - if edge0.as_ptr() == edge1.as_ptr() && segment_index_0 == segment_index_1 { + if std::ptr::eq(edge0, edge1) && segment_index_0 == segment_index_1 { return; } let line_0 = Line::new( - edge0.borrow().coords()[segment_index_0], - edge0.borrow().coords()[segment_index_0 + 1], + edge0.coords()[segment_index_0], + edge0.coords()[segment_index_0 + 1], ); let line_1 = Line::new( - edge1.borrow().coords()[segment_index_1], - edge1.borrow().coords()[segment_index_1 + 1], + edge1.coords()[segment_index_1], + edge1.coords()[segment_index_1 + 1], ); let intersection = self.line_intersector.compute_intersection(line_0, line_1); diff --git a/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs b/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs index a735934465..15beefd8ee 100644 --- a/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs +++ b/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs @@ -3,7 +3,8 @@ use super::{EdgeSetIntersector, SegmentIntersector}; use crate::GeoFloat; use std::cell::RefCell; -use std::rc::Rc; +// use std::rc::Rc; +use std::sync::Arc; pub(crate) struct SimpleEdgeSetIntersector; @@ -14,8 +15,8 @@ impl SimpleEdgeSetIntersector { fn compute_intersects( &self, - edge0: &Rc>>, - edge1: &Rc>>, + edge0: &Arc>>, + edge1: &Arc>>, segment_intersector: &mut SegmentIntersector, ) { let edge0_coords_len = edge0.borrow().coords().len() - 1; @@ -31,13 +32,13 @@ impl SimpleEdgeSetIntersector { impl EdgeSetIntersector for SimpleEdgeSetIntersector { fn compute_intersections_within_set( &self, - graph: &GeometryGraph, + graph: &mut GeometryGraph, check_for_self_intersecting_edges: bool, segment_intersector: &mut SegmentIntersector, ) { let edges = graph.edges(); - for edge0 in edges.iter() { - for edge1 in edges.iter() { + for edge0 in edges.iter_mut() { + for edge1 in edges.iter_mut() { if check_for_self_intersecting_edges || edge0.as_ptr() != edge1.as_ptr() { self.compute_intersects(edge0, edge1, segment_intersector); } diff --git a/geo/src/algorithm/relate/geomgraph/planar_graph.rs b/geo/src/algorithm/relate/geomgraph/planar_graph.rs index 454e410f19..7d1c18dd71 100644 --- a/geo/src/algorithm/relate/geomgraph/planar_graph.rs +++ b/geo/src/algorithm/relate/geomgraph/planar_graph.rs @@ -4,8 +4,9 @@ use super::{ }; use crate::{Coord, GeoFloat}; -use std::cell::RefCell; -use std::rc::Rc; +// use std::cell::RefCell; +// use std::rc::Rc; +use std::sync::Arc; #[derive(Clone, PartialEq)] pub(crate) struct PlanarGraphNode; @@ -24,7 +25,7 @@ where #[derive(Clone, PartialEq)] pub(crate) struct PlanarGraph { pub(crate) nodes: NodeMap, - edges: Vec>>>, + edges: Vec>, } impl PlanarGraph { @@ -35,7 +36,7 @@ impl PlanarGraph { edges: self .edges .iter() - .map(|e| Rc::new(RefCell::new(e.borrow().clone()))) + .map(|e| e.clone()) .collect(), }; assert_eq!(from_arg_index, 0); @@ -50,7 +51,7 @@ impl PlanarGraph { node.swap_label_args(); } for edge in &mut self.edges { - edge.borrow_mut().swap_label_args(); + edge.swap_label_args(); } } @@ -59,7 +60,7 @@ impl PlanarGraph { assert_eq!(self.edges, other.edges); } - pub fn edges(&self) -> &[Rc>>] { + pub fn edges(&self) -> &[Edge] { &self.edges } @@ -79,7 +80,7 @@ impl PlanarGraph { } pub fn insert_edge(&mut self, edge: Edge) { - self.edges.push(Rc::new(RefCell::new(edge))); + self.edges.push(edge); } pub fn add_node_with_coordinate(&mut self, coord: Coord) -> &mut CoordNode { diff --git a/geo/src/algorithm/relate/relate_operation.rs b/geo/src/algorithm/relate/relate_operation.rs index 061bb29199..badce014ae 100644 --- a/geo/src/algorithm/relate/relate_operation.rs +++ b/geo/src/algorithm/relate/relate_operation.rs @@ -10,7 +10,7 @@ use crate::CoordinatePosition; use crate::{Coord, GeoFloat, GeometryCow}; use std::cell::RefCell; -use std::rc::Rc; +use std::sync::Arc; /// Computes an [`IntersectionMatrix`] describing the topological relationship between two /// Geometries. @@ -29,7 +29,7 @@ where graph_b: GeometryGraph<'a, F>, nodes: NodeMap, line_intersector: RobustLineIntersector, - isolated_edges: Vec>>>, + isolated_edges: Vec>>>, } #[derive(PartialEq)]