avoid coping coords and unsafe accessing memory
diff --git a/README.md b/README.md index 20fff39..d9cf77e 100644 --- a/README.md +++ b/README.md
@@ -16,12 +16,16 @@ ```CPP #include "delaunator.h" #include <cstdio> - +using namespace std; //... int main(int, char* argv[]) { //... const vector<double> coords = {/* x0, y0, x1, y1, ... */}; - Delaunator delaunator(coords); //triangulation happens here + + //triangulation happens here + //note moving points to constructor + Delaunator delaunator(move(coords)); + for(long int i = 0; i < delaunator.triangles.size(); i+=3) { printf( "Triangle points: [[%f, %f], [%f, %f], [%f, %f]]\n",
diff --git a/src/delaunator.cpp b/src/delaunator.cpp index 8600b09..d37d5a5 100644 --- a/src/delaunator.cpp +++ b/src/delaunator.cpp
@@ -136,7 +136,7 @@ } } -Delaunator::Delaunator(const vector<double> &in_coords) { +Delaunator::Delaunator(vector<double> in_coords) { coords = move(in_coords); const long int n = coords.size() >> 1; double max_x = -1 * max_double;
diff --git a/src/delaunator.h b/src/delaunator.h index 2f3e705..a95b871 100644 --- a/src/delaunator.h +++ b/src/delaunator.h
@@ -16,7 +16,7 @@ class Delaunator{ public: - Delaunator(const std::vector<double> &in_coords); + Delaunator(std::vector<double> in_coords); std::vector<unsigned long int> triangles; std::vector<long int> halfedges; std::vector<double> coords;
diff --git a/src/triangulate.cpp b/src/triangulate.cpp index fba4dec..fa6d575 100644 --- a/src/triangulate.cpp +++ b/src/triangulate.cpp
@@ -45,7 +45,7 @@ return coords; } - const char* serialize_to_json(Delaunator &delaunator) { + const string serialize_to_json(Delaunator &delaunator) { rapidjson::StringBuffer sb; rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb); writer.StartObject(); @@ -93,7 +93,7 @@ } writer.EndArray(); writer.EndObject(); - return sb.GetString(); + return string(sb.GetString()); } } @@ -103,8 +103,8 @@ const char* output = argv[2]; string json = read_file(filename); const vector<double> coords = get_geo_json_points(json); - Delaunator delaunator(coords); - const char* out_json = serialize_to_json(delaunator); + Delaunator delaunator(move(coords)); + const char* out_json = serialize_to_json(delaunator).c_str(); if (output) { printf("Writing to file %s", output);