@@ -21,62 +21,69 @@ struct options_t;
2121template <typename T, size_t N>
2222class cache_block_t
2323{
24- std::array<std::unique_ptr<T>, N> arr ;
24+ std::array<std::unique_ptr<T>, N> m_arr ;
2525
2626public:
27- void set (size_t idx, T *ele) { arr [idx].reset (ele); }
27+ void set (size_t idx, T *ele) noexcept { m_arr [idx].reset (ele); }
2828
29- T const *get (size_t idx) const { return arr [idx].get (); }
29+ T const *get (size_t idx) const noexcept { return m_arr [idx].get (); }
3030};
3131
3232template <typename T, size_t BLOCK_SHIFT>
3333class elem_cache_t
3434{
35- constexpr static size_t per_block () { return 1 << BLOCK_SHIFT; }
36- constexpr static size_t num_blocks () { return 1 << (32 - BLOCK_SHIFT); }
35+ constexpr static size_t per_block () noexcept { return 1ULL << BLOCK_SHIFT; }
3736
38- constexpr static size_t id2block (osmid_t id)
37+ constexpr static size_t num_blocks () noexcept
38+ {
39+ return 1ULL << (32U - BLOCK_SHIFT);
40+ }
41+
42+ constexpr static size_t id2block (osmid_t id) noexcept
3943 {
4044 /* + NUM_BLOCKS/2 allows for negative IDs */
41- return (id >> BLOCK_SHIFT) + num_blocks () / 2 ;
45+ return (static_cast < size_t >(id) >> BLOCK_SHIFT) + num_blocks () / 2 ;
4246 }
4347
44- constexpr static size_t id2offset (osmid_t id)
48+ constexpr static size_t id2offset (osmid_t id) noexcept
4549 {
46- return id & (per_block () - 1 );
50+ return static_cast < size_t >(id) & (per_block () - 1U );
4751 }
4852
49- typedef cache_block_t <T, 1 << BLOCK_SHIFT> element_t ;
50- std::vector<std::unique_ptr<element_t >> arr;
53+ using element_t = cache_block_t <T, 1U << BLOCK_SHIFT>;
54+
55+ std::vector<std::unique_ptr<element_t >> m_blocks;
5156
5257public:
53- elem_cache_t () : arr (num_blocks()) {}
58+ elem_cache_t () : m_blocks (num_blocks()) {}
5459
5560 void set (osmid_t id, T *ele)
5661 {
57- const size_t block = id2block (id);
62+ size_t const block = id2block (id);
63+ assert (block < m_blocks.size ());
5864
59- if (!arr [block]) {
60- arr [block].reset (new element_t () );
65+ if (!m_blocks [block]) {
66+ m_blocks [block].reset (new element_t {} );
6167 }
6268
63- arr [block]->set (id2offset (id), ele);
69+ m_blocks [block]->set (id2offset (id), ele);
6470 }
6571
6672 T const *get (osmid_t id) const
6773 {
68- const size_t block = id2block (id);
74+ size_t const block = id2block (id);
75+ assert (block < m_blocks.size ());
6976
70- if (!arr [block]) {
77+ if (!m_blocks [block]) {
7178 return 0 ;
7279 }
7380
74- return arr [block]->get (id2offset (id));
81+ return m_blocks [block]->get (id2offset (id));
7582 }
7683
7784 void clear ()
7885 {
79- for (auto &ele : arr ) {
86+ for (auto &ele : m_blocks ) {
8087 ele.reset ();
8188 }
8289 }
0 commit comments