Skip to content

Commit 2b07a34

Browse files
committed
Cleanup and test elem_cache_t
1 parent fa9b47d commit 2b07a34

2 files changed

Lines changed: 46 additions & 20 deletions

File tree

src/middle-ram.hpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,69 @@ struct options_t;
2121
template <typename T, size_t N>
2222
class cache_block_t
2323
{
24-
std::array<std::unique_ptr<T>, N> arr;
24+
std::array<std::unique_ptr<T>, N> m_arr;
2525

2626
public:
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

3232
template <typename T, size_t BLOCK_SHIFT>
3333
class 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

5257
public:
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
}

tests/test-middle.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ struct options_ram_flatnode
8888
}
8989
};
9090

91+
TEST_CASE("elem_cache_t")
92+
{
93+
elem_cache_t<int, 10> cache;
94+
95+
cache.set(3, new int{23});
96+
cache.set(5, new int{42});
97+
REQUIRE(*cache.get(3) == 23);
98+
REQUIRE(*cache.get(5) == 42);
99+
REQUIRE(cache.get(2) == nullptr);
100+
cache.set(2, new int{56});
101+
REQUIRE(*cache.get(2) == 56);
102+
cache.set(3, new int{0});
103+
REQUIRE(*cache.get(3) == 0);
104+
cache.clear();
105+
REQUIRE(cache.get(1) == nullptr);
106+
REQUIRE(cache.get(2) == nullptr);
107+
REQUIRE(cache.get(3) == nullptr);
108+
}
109+
91110
TEMPLATE_TEST_CASE("middle import", "", options_slim_default,
92111
options_slim_dense_cache, options_ram_optimized,
93112
options_ram_flatnode)

0 commit comments

Comments
 (0)