Merge pull request #4493 from sgiraudot/Properties-Fix_capacity-GF

[Surface Mesh / Point Set] Fix capacity handling in properties
This commit is contained in:
Laurent Rineau 2020-01-29 16:27:44 +01:00
commit c950da11b0
1 changed files with 13 additions and 3 deletions

View File

@ -236,7 +236,7 @@ class Property_container
public: public:
// default constructor // default constructor
Property_container() : size_(0) {} Property_container() : size_(0), capacity_(0) {}
// destructor (deletes all property arrays) // destructor (deletes all property arrays)
virtual ~Property_container() { clear(); } virtual ~Property_container() { clear(); }
@ -252,6 +252,7 @@ public:
clear(); clear();
parrays_.resize(_rhs.n_properties()); parrays_.resize(_rhs.n_properties());
size_ = _rhs.size(); size_ = _rhs.size();
capacity_ = _rhs.capacity();
for (std::size_t i=0; i<parrays_.size(); ++i) for (std::size_t i=0; i<parrays_.size(); ++i)
parrays_[i] = _rhs.parrays_[i]->clone(); parrays_[i] = _rhs.parrays_[i]->clone();
} }
@ -287,6 +288,7 @@ public:
continue; continue;
parrays_.push_back (_rhs.parrays_[i]->empty_clone()); parrays_.push_back (_rhs.parrays_[i]->empty_clone());
parrays_.back()->reserve(capacity_);
parrays_.back()->resize(size_); parrays_.back()->resize(size_);
} }
} }
@ -305,6 +307,9 @@ public:
// returns the current size of the property arrays // returns the current size of the property arrays
size_t size() const { return size_; } size_t size() const { return size_; }
// returns the current capacity of the property arrays
size_t capacity() const { return capacity_; }
// returns the number of property arrays // returns the number of property arrays
size_t n_properties() const { return parrays_.size(); } size_t n_properties() const { return parrays_.size(); }
@ -353,6 +358,7 @@ public:
// otherwise add the property // otherwise add the property
Property_array<T>* p = new Property_array<T>(name, t); Property_array<T>* p = new Property_array<T>(name, t);
p->reserve(capacity_);
p->resize(size_); p->resize(size_);
parrays_.push_back(p); parrays_.push_back(p);
return std::make_pair(Pmap(p), true); return std::make_pair(Pmap(p), true);
@ -430,10 +436,11 @@ public:
// reserve memory for n entries in all arrays // reserve memory for n entries in all arrays
void reserve(size_t n) const void reserve(size_t n)
{ {
for (std::size_t i=0; i<parrays_.size(); ++i) for (std::size_t i=0; i<parrays_.size(); ++i)
parrays_[i]->reserve(n); parrays_[i]->reserve(n);
capacity_ = std::max(n, capacity_);
} }
// resize all arrays to size n // resize all arrays to size n
@ -445,10 +452,11 @@ public:
} }
// free unused space in all arrays // free unused space in all arrays
void shrink_to_fit() const void shrink_to_fit()
{ {
for (std::size_t i=0; i<parrays_.size(); ++i) for (std::size_t i=0; i<parrays_.size(); ++i)
parrays_[i]->shrink_to_fit(); parrays_[i]->shrink_to_fit();
capacity_ = size_;
} }
// add a new element to each vector // add a new element to each vector
@ -457,6 +465,7 @@ public:
for (std::size_t i=0; i<parrays_.size(); ++i) for (std::size_t i=0; i<parrays_.size(); ++i)
parrays_[i]->push_back(); parrays_[i]->push_back();
++size_; ++size_;
capacity_ = (std::max(size_, capacity_));
} }
// reset element to its default property values // reset element to its default property values
@ -483,6 +492,7 @@ public:
private: private:
std::vector<Base_property_array*> parrays_; std::vector<Base_property_array*> parrays_;
size_t size_; size_t size_;
size_t capacity_;
}; };
/// @endcond /// @endcond