Skip to content

Commit

Permalink
Merge pull request #7686 from sloriot/SS-fix_size_after_remove
Browse files Browse the repository at this point in the history
Correct size() in case some points were removed
  • Loading branch information
lrineau committed Sep 6, 2023
2 parents a04ecc2 + f759a61 commit 7cd107a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Spatial_searching/include/CGAL/Kd_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Kd_tree {
mutable CGAL_MUTEX building_mutex;//mutex used to protect const calls inducing build()
#endif
bool built_;
bool removed_;
std::size_t removed_=0;

// protected copy constructor
Kd_tree(const Tree& tree)
Expand Down Expand Up @@ -278,13 +278,13 @@ class Kd_tree {
public:

Kd_tree(Splitter s = Splitter(),const SearchTraits traits=SearchTraits())
: traits_(traits),split(s), built_(false), removed_(false)
: traits_(traits),split(s), built_(false)
{}

template <class InputIterator>
Kd_tree(InputIterator first, InputIterator beyond,
Splitter s = Splitter(),const SearchTraits traits=SearchTraits())
: traits_(traits), split(s), pts(first, beyond), built_(false), removed_(false)
: traits_(traits), split(s), pts(first, beyond), built_(false)
{ }

bool empty() const {
Expand Down Expand Up @@ -324,7 +324,7 @@ class Kd_tree {
// must call invalidate_build() first.
CGAL_assertion(!is_built());
CGAL_assertion(!pts.empty());
CGAL_assertion(!removed_);
CGAL_assertion(removed_==0);
const Point_d& p = *pts.begin();
typename SearchTraits::Construct_cartesian_const_iterator_d ccci=traits_.construct_cartesian_const_iterator_d_object();
dim_ = static_cast<int>(std::distance(ccci(p), ccci(p,0)));
Expand Down Expand Up @@ -432,14 +432,14 @@ class Kd_tree {

void invalidate_build()
{
if(removed_){
if(removed_!=0){
// Walk the tree to collect the remaining points.
// Writing directly to pts would likely work, but better be safe.
std::vector<Point_d> ptstmp;
//ptstmp.resize(root()->num_items());
root()->tree_items(std::back_inserter(ptstmp));
pts.swap(ptstmp);
removed_=false;
removed_=0;
CGAL_assertion(is_built()); // the rest of the cleanup must happen
}
if(is_built()){
Expand All @@ -455,7 +455,7 @@ class Kd_tree {
{
invalidate_build();
pts.clear();
removed_ = false;
removed_ = 0;
}

void
Expand Down Expand Up @@ -512,8 +512,8 @@ class Kd_tree {
CGAL_assertion(success);

// Do not set the flag is the tree has been cleared.
if(is_built())
removed_ |= success;
if(is_built() && success)
++removed_;
}
private:
template<class Equal>
Expand Down Expand Up @@ -684,7 +684,7 @@ class Kd_tree {
size_type
size() const
{
return pts.size();
return pts.size()-removed_;
}

// Print statistics of the tree.
Expand Down

0 comments on commit 7cd107a

Please sign in to comment.