jhasse / poly2tri

2D constrained Delaunay triangulation library
BSD 3-Clause "New" or "Revised" License
429 stars 89 forks source link

Return by reference and const overloads #49

Open pr0g opened 1 year ago

pr0g commented 1 year ago

This PR adds a const overload of GetPoint (that returns a const Point*) and updates GetTriangles and GetMap to return a const reference to the underlying data structure as opposed to returning by value (and forcing a copy).

A copy can still be made by not using a const ref variable.

e.g.

std::vector<p2t::Triangle*> triangles = cdt.GetTriangles(); // copy
const std::vector<p2t::Triangle*>& triangles = cdt.GetTriangles(); // no copy

I held off making any further changes to keep the PR small and simple. There are a few more spots that could benefit from const but it's not critical by any means.

Also to reduce duplication in the const/non-const overloads, I used the solution popularized by Scott Meyers in Effective C++ (Item 3) - see https://stackoverflow.com/a/123995/1947066 for details (I'm happy to change this to duplicate the logic in both overloads if you think that is clearer, or add a comment to explain what is being done and why).

Great library and I hope this incredibly minor contribution is helpful,

Thanks!