From 9fa067cb4d531d12a24d593a43c527425c9c50b2 Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Fri, 24 Jan 2025 19:36:57 +0100 Subject: [PATCH] recycle the constraints indices --- .../Polyline_constraint_hierarchy_2.h | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h b/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h index 7b186736ff2..40a59450325 100644 --- a/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h +++ b/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -470,9 +471,10 @@ private: #endif {} - Compare comp; - Sc_to_c_map sc_to_c_map; - Constraints_set constraints_set; + Compare comp; + Sc_to_c_map sc_to_c_map; + std::queue free_ids; + Constraints_set constraints_set; } priv; public: Polyline_constraint_hierarchy_2(const Compare& comp) : priv(comp) {} @@ -587,13 +589,20 @@ private: // // then the uses of `constraints_set` Constraint_id create_new_constraint() { - auto id{number_of_constraints() == 0 ? 0 : priv.constraints_set.rbegin()->index() + 1}; + size_type id; // uninitialized + if(priv.free_ids.empty()) { + id = priv.constraints_set.size(); + } else { + id = priv.free_ids.front(); + priv.free_ids.pop(); + } Constraint_id cid{new Vertex_list_with_info{this}, id}; priv.constraints_set.insert(cid); return cid; } void erase_constraint(Constraint_id cid) { + priv.free_ids.push(cid.index()); priv.constraints_set.erase(cid); cid.destroy(); } @@ -719,6 +728,7 @@ swap(Polyline_constraint_hierarchy_2& ch) { using std::swap; swap(priv.comp, ch.priv.comp); + priv.free_ids.swap(ch.priv.free_ids); priv.constraints_set.swap(ch.priv.constraints_set); priv.sc_to_c_map.swap(ch.priv.sc_to_c_map); } @@ -1164,8 +1174,7 @@ clear() cl_ptr->clear(); delete cl_ptr; } - priv.sc_to_c_map.clear(); - priv.constraints_set.clear(); + priv = Priv(priv.comp); }