From 1ff096ac5c3aa11f1e98cd375e1fdf117f1b8ccc Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Thu, 30 May 2024 15:12:42 +0200 Subject: [PATCH] Make constants in PackedVector constexpr (#6917) --- CHANGELOG.md | 1 + include/util/packed_vector.hpp | 49 ++++++++++------------------------ 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a22841b00..cbeaf91df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - NodeJS: - CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452) - Misc: + - CHANGED: Make constants in PackedVector constexpr. [#6917](https://github.com/Project-OSRM/osrm-backend/pull/6917) - CHANGED: Use std::variant instead of mapbox::util::variant. [#6903](https://github.com/Project-OSRM/osrm-backend/pull/6903) - CHANGED: Bump rapidjson to version f9d53419e912910fd8fa57d5705fa41425428c35 [#6906](https://github.com/Project-OSRM/osrm-backend/pull/6906) - CHANGED: Bump mapbox/variant to version 1.2.0 [#6898](https://github.com/Project-OSRM/osrm-backend/pull/6898) diff --git a/include/util/packed_vector.hpp b/include/util/packed_vector.hpp index 243f423e5..6e03421ee 100644 --- a/include/util/packed_vector.hpp +++ b/include/util/packed_vector.hpp @@ -153,8 +153,7 @@ template class Pack // number of words per block static constexpr std::size_t BLOCK_WORDS = (Bits * BLOCK_ELEMENTS) / WORD_BITS; - // C++14 does not allow operator[] to be constexpr, this is fixed in C++17. - static /* constexpr */ std::array initialize_lower_mask() + static constexpr std::array initialize_lower_mask() { std::array lower_mask; @@ -170,7 +169,7 @@ template class Pack return lower_mask; } - static /* constexpr */ std::array initialize_upper_mask() + static constexpr std::array initialize_upper_mask() { std::array upper_mask; @@ -194,7 +193,7 @@ template class Pack return upper_mask; } - static /* constexpr */ std::array initialize_lower_offset() + static constexpr std::array initialize_lower_offset() { std::array lower_offset; @@ -209,7 +208,7 @@ template class Pack return lower_offset; } - static /* constexpr */ std::array initialize_upper_offset() + static constexpr std::array initialize_upper_offset() { std::array upper_offset; @@ -232,7 +231,7 @@ template class Pack return upper_offset; } - static /* constexpr */ std::array initialize_word_offset() + static constexpr std::array initialize_word_offset() { std::array word_offset; @@ -246,28 +245,15 @@ template class Pack return word_offset; } - // For now we need to call these on object creation - void initialize() - { - lower_mask = initialize_lower_mask(); - upper_mask = initialize_upper_mask(); - lower_offset = initialize_lower_offset(); - upper_offset = initialize_upper_offset(); - word_offset = initialize_word_offset(); - } - // mask for the lower/upper word of a record - // TODO: With C++17 these could be constexpr - /* static constexpr */ std::array - lower_mask /* = initialize_lower_mask()*/; - /* static constexpr */ std::array - upper_mask /* = initialize_upper_mask()*/; - /* static constexpr */ std::array - lower_offset /* = initialize_lower_offset()*/; - /* static constexpr */ std::array - upper_offset /* = initialize_upper_offset()*/; + static constexpr std::array lower_mask = initialize_lower_mask(); + static constexpr std::array upper_mask = initialize_upper_mask(); + static constexpr std::array lower_offset = + initialize_lower_offset(); + static constexpr std::array upper_offset = + initialize_upper_offset(); // in which word of the block is the element - /* static constexpr */ std::array word_offset = + static constexpr std::array word_offset = initialize_word_offset(); struct InternalIndex @@ -378,27 +364,21 @@ template class Pack PackedVector(std::initializer_list list) { - initialize(); reserve(list.size()); for (const auto value : list) push_back(value); } - PackedVector() { initialize(); }; + PackedVector(){}; PackedVector(const PackedVector &) = default; PackedVector(PackedVector &&) = default; PackedVector &operator=(const PackedVector &) = default; PackedVector &operator=(PackedVector &&) = default; - PackedVector(std::size_t size) - { - initialize(); - resize(size); - } + PackedVector(std::size_t size) { resize(size); } PackedVector(std::size_t size, T initial_value) { - initialize(); resize(size); fill(initial_value); } @@ -406,7 +386,6 @@ template class Pack PackedVector(util::ViewOrVector vec_, std::size_t num_elements) : vec(std::move(vec_)), num_elements(num_elements) { - initialize(); } // forces the efficient read-only lookup