Skip to content

Commit

Permalink
PreparedLineString: Use thread-safe lazy init
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Mar 1, 2023
1 parent 93f5d06 commit 3cfed69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
7 changes: 5 additions & 2 deletions include/geos/geom/prep/PreparedLineString.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ namespace prep { // geos::geom::prep
*/
class PreparedLineString : public BasicPreparedGeometry {
private:
std::unique_ptr<noding::FastSegmentSetIntersectionFinder> segIntFinder;
mutable std::unique_ptr<noding::FastSegmentSetIntersectionFinder> segIntFinder;
mutable noding::SegmentString::ConstVect segStrings;
mutable std::unique_ptr<operation::distance::IndexedFacetDistance> indexedDistance;

mutable std::once_flag segIntFinderFlag;
mutable std::once_flag indexedDistanceFlag;

protected:
public:
PreparedLineString(const Geometry* geom)
Expand All @@ -54,7 +57,7 @@ class PreparedLineString : public BasicPreparedGeometry {

~PreparedLineString() override;

noding::FastSegmentSetIntersectionFinder* getIntersectionFinder();
noding::FastSegmentSetIntersectionFinder* getIntersectionFinder() const;

bool intersects(const geom::Geometry* g) const override;
std::unique_ptr<geom::CoordinateSequence> nearestPoints(const geom::Geometry* g) const override;
Expand Down
20 changes: 11 additions & 9 deletions src/geom/prep/PreparedLineString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <geos/noding/FastSegmentSetIntersectionFinder.h>
#include <geos/operation/distance/IndexedFacetDistance.h>

#include <mutex>

namespace geos {
namespace geom { // geos.geom
namespace prep { // geos.geom.prep
Expand All @@ -44,12 +46,12 @@ PreparedLineString::~PreparedLineString()
}

noding::FastSegmentSetIntersectionFinder*
PreparedLineString::getIntersectionFinder()
PreparedLineString::getIntersectionFinder() const
{
if(! segIntFinder) {
std::call_once(segIntFinderFlag, [this]() {
noding::SegmentStringUtil::extractSegmentStrings(&getGeometry(), segStrings);
segIntFinder.reset(new noding::FastSegmentSetIntersectionFinder(&segStrings));
}
segIntFinder = detail::make_unique<noding::FastSegmentSetIntersectionFinder>(&segStrings);
});

return segIntFinder.get();
}
Expand All @@ -68,12 +70,12 @@ PreparedLineString::intersects(const geom::Geometry* g) const

/* public */
operation::distance::IndexedFacetDistance*
PreparedLineString::
getIndexedFacetDistance() const
PreparedLineString::getIndexedFacetDistance() const
{
if(! indexedDistance ) {
indexedDistance.reset(new operation::distance::IndexedFacetDistance(&getGeometry()));
}
std::call_once(indexedDistanceFlag, [this]() {
indexedDistance = detail::make_unique<operation::distance::IndexedFacetDistance>(&getGeometry());
});

return indexedDistance.get();
}

Expand Down

0 comments on commit 3cfed69

Please sign in to comment.