From 18bcebae984c92d09139d5bdeac250880b25fbce Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Tue, 20 Apr 2021 15:30:28 -0600 Subject: [PATCH] Simplify control flow in AlignedVector::resize{,_fast}. --- include/deal.II/base/aligned_vector.h | 102 ++++++++++++++++++-------- 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/include/deal.II/base/aligned_vector.h b/include/deal.II/base/aligned_vector.h index f2a046dd65..8d21c971df 100644 --- a/include/deal.II/base/aligned_vector.h +++ b/include/deal.II/base/aligned_vector.h @@ -833,22 +833,36 @@ inline void AlignedVector::resize_fast(const size_type new_size) { const size_type old_size = size(); - if (std::is_trivial::value == false && new_size < old_size) + + if (new_size == 0) + clear(); + else if (new_size == old_size) + {} // nothing to do here + else if (new_size < old_size) { // call destructor on fields that are released. doing it backward // releases the elements in reverse order as compared to how they were // created - while (used_elements_end != elements.get() + new_size) - (--used_elements_end)->~T(); + if (std::is_trivial::value == false) + { + while (used_elements_end != elements.get() + new_size) + (--used_elements_end)->~T(); + } + else + used_elements_end = elements.get() + new_size; + } + else // new_size > old_size + { + // Allocate more space, and claim that space as used + reserve(new_size); + used_elements_end = elements.get() + new_size; + + // need to still set the values in case the class is non-trivial because + // virtual classes etc. need to run their (default) constructor + if (std::is_trivial::value == false) + dealii::internal::AlignedVectorDefaultInitialize( + new_size - old_size, elements.get() + old_size); } - reserve(new_size); - used_elements_end = elements.get() + new_size; - - // need to still set the values in case the class is non-trivial because - // virtual classes etc. need to run their (default) constructor - if (std::is_trivial::value == false && new_size > old_size) - dealii::internal::AlignedVectorDefaultInitialize( - new_size - old_size, elements.get() + old_size); } @@ -858,21 +872,34 @@ inline void AlignedVector::resize(const size_type new_size) { const size_type old_size = size(); - if (std::is_trivial::value == false && new_size < old_size) + + if (new_size == 0) + clear(); + else if (new_size == old_size) + {} // nothing to do here + else if (new_size < old_size) { // call destructor on fields that are released. doing it backward // releases the elements in reverse order as compared to how they were // created - while (used_elements_end != elements.get() + new_size) - (--used_elements_end)->~T(); + if (std::is_trivial::value == false) + { + while (used_elements_end != elements.get() + new_size) + (--used_elements_end)->~T(); + } + else + used_elements_end = elements.get() + new_size; } - reserve(new_size); - used_elements_end = elements.get() + new_size; + else // new_size > old_size + { + // Allocate more space, and claim that space as used + reserve(new_size); + used_elements_end = elements.get() + new_size; - // finally set the desired init values - if (new_size > old_size) - dealii::internal::AlignedVectorDefaultInitialize( - new_size - old_size, elements.get() + old_size); + // finally set the values to the default initializer + dealii::internal::AlignedVectorDefaultInitialize( + new_size - old_size, elements.get() + old_size); + } } @@ -882,22 +909,35 @@ inline void AlignedVector::resize(const size_type new_size, const T &init) { const size_type old_size = size(); - if (std::is_trivial::value == false && new_size < old_size) + + if (new_size == 0) + clear(); + else if (new_size == old_size) + {} // nothing to do here + else if (new_size < old_size) { // call destructor on fields that are released. doing it backward // releases the elements in reverse order as compared to how they were // created - while (used_elements_end != elements.get() + new_size) - (--used_elements_end)->~T(); + if (std::is_trivial::value == false) + { + while (used_elements_end != elements.get() + new_size) + (--used_elements_end)->~T(); + } + else + used_elements_end = elements.get() + new_size; + } + else // new_size > old_size + { + // Allocate more space, and claim that space as used + reserve(new_size); + used_elements_end = elements.get() + new_size; + + // finally set the desired init values + dealii::internal::AlignedVectorSet(new_size - old_size, + init, + elements.get() + old_size); } - reserve(new_size); - used_elements_end = elements.get() + new_size; - - // finally set the desired init values - if (new_size > old_size) - dealii::internal::AlignedVectorSet(new_size - old_size, - init, - elements.get() + old_size); } -- 2.39.5