Make constants in PackedVector constexpr (#6917)

This commit is contained in:
Siarhei Fedartsou 2024-05-30 15:12:42 +02:00 committed by GitHub
parent 0ea757ed02
commit 1ff096ac5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 35 deletions

View File

@ -22,6 +22,7 @@
- NodeJS: - NodeJS:
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452) - CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
- Misc: - 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: 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 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) - CHANGED: Bump mapbox/variant to version 1.2.0 [#6898](https://github.com/Project-OSRM/osrm-backend/pull/6898)

View File

@ -153,8 +153,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
// number of words per block // number of words per block
static constexpr std::size_t BLOCK_WORDS = (Bits * BLOCK_ELEMENTS) / WORD_BITS; 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<WordT, BLOCK_ELEMENTS> initialize_lower_mask()
static /* constexpr */ std::array<WordT, BLOCK_ELEMENTS> initialize_lower_mask()
{ {
std::array<WordT, BLOCK_ELEMENTS> lower_mask; std::array<WordT, BLOCK_ELEMENTS> lower_mask;
@ -170,7 +169,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
return lower_mask; return lower_mask;
} }
static /* constexpr */ std::array<WordT, BLOCK_ELEMENTS> initialize_upper_mask() static constexpr std::array<WordT, BLOCK_ELEMENTS> initialize_upper_mask()
{ {
std::array<WordT, BLOCK_ELEMENTS> upper_mask; std::array<WordT, BLOCK_ELEMENTS> upper_mask;
@ -194,7 +193,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
return upper_mask; return upper_mask;
} }
static /* constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_lower_offset() static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_lower_offset()
{ {
std::array<std::uint8_t, WORD_BITS> lower_offset; std::array<std::uint8_t, WORD_BITS> lower_offset;
@ -209,7 +208,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
return lower_offset; return lower_offset;
} }
static /* constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_upper_offset() static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_upper_offset()
{ {
std::array<std::uint8_t, BLOCK_ELEMENTS> upper_offset; std::array<std::uint8_t, BLOCK_ELEMENTS> upper_offset;
@ -232,7 +231,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
return upper_offset; return upper_offset;
} }
static /* constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_word_offset() static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_word_offset()
{ {
std::array<std::uint8_t, BLOCK_ELEMENTS> word_offset; std::array<std::uint8_t, BLOCK_ELEMENTS> word_offset;
@ -246,28 +245,15 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
return word_offset; 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 // mask for the lower/upper word of a record
// TODO: With C++17 these could be constexpr static constexpr std::array<WordT, BLOCK_ELEMENTS> lower_mask = initialize_lower_mask();
/* static constexpr */ std::array<WordT, BLOCK_ELEMENTS> static constexpr std::array<WordT, BLOCK_ELEMENTS> upper_mask = initialize_upper_mask();
lower_mask /* = initialize_lower_mask()*/; static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> lower_offset =
/* static constexpr */ std::array<WordT, BLOCK_ELEMENTS> initialize_lower_offset();
upper_mask /* = initialize_upper_mask()*/; static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> upper_offset =
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_upper_offset();
lower_offset /* = initialize_lower_offset()*/;
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS>
upper_offset /* = initialize_upper_offset()*/;
// in which word of the block is the element // in which word of the block is the element
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> word_offset = static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> word_offset =
initialize_word_offset(); initialize_word_offset();
struct InternalIndex struct InternalIndex
@ -378,27 +364,21 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
PackedVector(std::initializer_list<T> list) PackedVector(std::initializer_list<T> list)
{ {
initialize();
reserve(list.size()); reserve(list.size());
for (const auto value : list) for (const auto value : list)
push_back(value); push_back(value);
} }
PackedVector() { initialize(); }; PackedVector(){};
PackedVector(const PackedVector &) = default; PackedVector(const PackedVector &) = default;
PackedVector(PackedVector &&) = default; PackedVector(PackedVector &&) = default;
PackedVector &operator=(const PackedVector &) = default; PackedVector &operator=(const PackedVector &) = default;
PackedVector &operator=(PackedVector &&) = default; PackedVector &operator=(PackedVector &&) = default;
PackedVector(std::size_t size) PackedVector(std::size_t size) { resize(size); }
{
initialize();
resize(size);
}
PackedVector(std::size_t size, T initial_value) PackedVector(std::size_t size, T initial_value)
{ {
initialize();
resize(size); resize(size);
fill(initial_value); fill(initial_value);
} }
@ -406,7 +386,6 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
PackedVector(util::ViewOrVector<WordT, Ownership> vec_, std::size_t num_elements) PackedVector(util::ViewOrVector<WordT, Ownership> vec_, std::size_t num_elements)
: vec(std::move(vec_)), num_elements(num_elements) : vec(std::move(vec_)), num_elements(num_elements)
{ {
initialize();
} }
// forces the efficient read-only lookup // forces the efficient read-only lookup