Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make constants in PackedVector constexpr #6917

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 14 additions & 35 deletions include/util/packed_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> 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<WordT, BLOCK_ELEMENTS> initialize_lower_mask()
static constexpr std::array<WordT, BLOCK_ELEMENTS> initialize_lower_mask()
{
std::array<WordT, BLOCK_ELEMENTS> lower_mask;

Expand All @@ -170,7 +169,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
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;

Expand All @@ -194,7 +193,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
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;

Expand All @@ -209,7 +208,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
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;

Expand All @@ -232,7 +231,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
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;

Expand All @@ -246,28 +245,15 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> 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<WordT, BLOCK_ELEMENTS>
lower_mask /* = initialize_lower_mask()*/;
/* static constexpr */ std::array<WordT, BLOCK_ELEMENTS>
upper_mask /* = initialize_upper_mask()*/;
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS>
lower_offset /* = initialize_lower_offset()*/;
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS>
upper_offset /* = initialize_upper_offset()*/;
static constexpr std::array<WordT, BLOCK_ELEMENTS> lower_mask = initialize_lower_mask();
static constexpr std::array<WordT, BLOCK_ELEMENTS> upper_mask = initialize_upper_mask();
static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> 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
/* 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();

struct InternalIndex
Expand Down Expand Up @@ -378,35 +364,28 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack

PackedVector(std::initializer_list<T> 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);
}

PackedVector(util::ViewOrVector<WordT, Ownership> vec_, std::size_t num_elements)
: vec(std::move(vec_)), num_elements(num_elements)
{
initialize();
}

// forces the efficient read-only lookup
Expand Down
Loading