Skip to content

Commit 1ff096a

Browse files
Make constants in PackedVector constexpr (#6917)
1 parent 0ea757e commit 1ff096a

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- NodeJS:
2323
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
2424
- Misc:
25+
- CHANGED: Make constants in PackedVector constexpr. [#6917](https://github.com/Project-OSRM/osrm-backend/pull/6917)
2526
- CHANGED: Use std::variant instead of mapbox::util::variant. [#6903](https://github.com/Project-OSRM/osrm-backend/pull/6903)
2627
- CHANGED: Bump rapidjson to version f9d53419e912910fd8fa57d5705fa41425428c35 [#6906](https://github.com/Project-OSRM/osrm-backend/pull/6906)
2728
- CHANGED: Bump mapbox/variant to version 1.2.0 [#6898](https://github.com/Project-OSRM/osrm-backend/pull/6898)

include/util/packed_vector.hpp

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
153153
// number of words per block
154154
static constexpr std::size_t BLOCK_WORDS = (Bits * BLOCK_ELEMENTS) / WORD_BITS;
155155

156-
// C++14 does not allow operator[] to be constexpr, this is fixed in C++17.
157-
static /* constexpr */ std::array<WordT, BLOCK_ELEMENTS> initialize_lower_mask()
156+
static constexpr std::array<WordT, BLOCK_ELEMENTS> initialize_lower_mask()
158157
{
159158
std::array<WordT, BLOCK_ELEMENTS> lower_mask;
160159

@@ -170,7 +169,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
170169
return lower_mask;
171170
}
172171

173-
static /* constexpr */ std::array<WordT, BLOCK_ELEMENTS> initialize_upper_mask()
172+
static constexpr std::array<WordT, BLOCK_ELEMENTS> initialize_upper_mask()
174173
{
175174
std::array<WordT, BLOCK_ELEMENTS> upper_mask;
176175

@@ -194,7 +193,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
194193
return upper_mask;
195194
}
196195

197-
static /* constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_lower_offset()
196+
static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_lower_offset()
198197
{
199198
std::array<std::uint8_t, WORD_BITS> lower_offset;
200199

@@ -209,7 +208,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
209208
return lower_offset;
210209
}
211210

212-
static /* constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_upper_offset()
211+
static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_upper_offset()
213212
{
214213
std::array<std::uint8_t, BLOCK_ELEMENTS> upper_offset;
215214

@@ -232,7 +231,7 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
232231
return upper_offset;
233232
}
234233

235-
static /* constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_word_offset()
234+
static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> initialize_word_offset()
236235
{
237236
std::array<std::uint8_t, BLOCK_ELEMENTS> word_offset;
238237

@@ -246,28 +245,15 @@ template <typename T, std::size_t Bits, storage::Ownership Ownership> class Pack
246245
return word_offset;
247246
}
248247

249-
// For now we need to call these on object creation
250-
void initialize()
251-
{
252-
lower_mask = initialize_lower_mask();
253-
upper_mask = initialize_upper_mask();
254-
lower_offset = initialize_lower_offset();
255-
upper_offset = initialize_upper_offset();
256-
word_offset = initialize_word_offset();
257-
}
258-
259248
// mask for the lower/upper word of a record
260-
// TODO: With C++17 these could be constexpr
261-
/* static constexpr */ std::array<WordT, BLOCK_ELEMENTS>
262-
lower_mask /* = initialize_lower_mask()*/;
263-
/* static constexpr */ std::array<WordT, BLOCK_ELEMENTS>
264-
upper_mask /* = initialize_upper_mask()*/;
265-
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS>
266-
lower_offset /* = initialize_lower_offset()*/;
267-
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS>
268-
upper_offset /* = initialize_upper_offset()*/;
249+
static constexpr std::array<WordT, BLOCK_ELEMENTS> lower_mask = initialize_lower_mask();
250+
static constexpr std::array<WordT, BLOCK_ELEMENTS> upper_mask = initialize_upper_mask();
251+
static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> lower_offset =
252+
initialize_lower_offset();
253+
static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> upper_offset =
254+
initialize_upper_offset();
269255
// in which word of the block is the element
270-
/* static constexpr */ std::array<std::uint8_t, BLOCK_ELEMENTS> word_offset =
256+
static constexpr std::array<std::uint8_t, BLOCK_ELEMENTS> word_offset =
271257
initialize_word_offset();
272258

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

379365
PackedVector(std::initializer_list<T> list)
380366
{
381-
initialize();
382367
reserve(list.size());
383368
for (const auto value : list)
384369
push_back(value);
385370
}
386371

387-
PackedVector() { initialize(); };
372+
PackedVector(){};
388373
PackedVector(const PackedVector &) = default;
389374
PackedVector(PackedVector &&) = default;
390375
PackedVector &operator=(const PackedVector &) = default;
391376
PackedVector &operator=(PackedVector &&) = default;
392377

393-
PackedVector(std::size_t size)
394-
{
395-
initialize();
396-
resize(size);
397-
}
378+
PackedVector(std::size_t size) { resize(size); }
398379

399380
PackedVector(std::size_t size, T initial_value)
400381
{
401-
initialize();
402382
resize(size);
403383
fill(initial_value);
404384
}
405385

406386
PackedVector(util::ViewOrVector<WordT, Ownership> vec_, std::size_t num_elements)
407387
: vec(std::move(vec_)), num_elements(num_elements)
408388
{
409-
initialize();
410389
}
411390

412391
// forces the efficient read-only lookup

0 commit comments

Comments
 (0)