Refining packed_vector.hpp
* Rename to std::vector-like APIs (push_back, at) * Add size, reserve, capacity methods
This commit is contained in:
parent
46b58fba83
commit
1659bbf7b1
@ -25,7 +25,7 @@ class PackedVector
|
||||
public:
|
||||
PackedVector() = default;
|
||||
|
||||
void insert(OSMNodeID incoming_node_id)
|
||||
void push_back(OSMNodeID incoming_node_id)
|
||||
{
|
||||
std::uint64_t node_id = static_cast<std::uint64_t>(incoming_node_id);
|
||||
// mask incoming values, just in case they are > bitsize
|
||||
@ -61,7 +61,7 @@ class PackedVector
|
||||
num_elements++;
|
||||
}
|
||||
|
||||
OSMNodeID retrieve(const std::size_t &a_index) const
|
||||
OSMNodeID at(const std::size_t &a_index) const
|
||||
{
|
||||
BOOST_ASSERT(a_index < num_elements);
|
||||
|
||||
@ -102,6 +102,21 @@ class PackedVector
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
return num_elements;
|
||||
}
|
||||
|
||||
void reserve(std::size_t capacity)
|
||||
{
|
||||
vec.reserve(ceil(float(capacity) / ELEMSIZE) * BITSIZE);
|
||||
}
|
||||
|
||||
std::size_t capacity() const
|
||||
{
|
||||
return floor(float(vec.capacity()) / BITSIZE) * ELEMSIZE;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::uint64_t> vec;
|
||||
std::size_t num_elements = 0;
|
||||
|
@ -21,14 +21,27 @@ BOOST_AUTO_TEST_CASE(insert_and_retrieve_packed_test)
|
||||
{
|
||||
OSMNodeID r = static_cast<OSMNodeID>(rand() % 2147483647); // max 33-bit uint
|
||||
|
||||
packed_ids.insert(r);
|
||||
packed_ids.push_back(r);
|
||||
original_ids.push_back(r);
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < num_test_cases; i++)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(original_ids.at(i), packed_ids.retrieve(i));
|
||||
BOOST_CHECK_EQUAL(original_ids.at(i), packed_ids.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(packed_vector_capacity_test)
|
||||
{
|
||||
PackedVector packed_vec;
|
||||
const std::size_t original_size = packed_vec.capacity();
|
||||
std::vector<OSMNodeID> dummy_vec;
|
||||
|
||||
BOOST_CHECK_EQUAL(original_size, dummy_vec.capacity());
|
||||
|
||||
packed_vec.reserve(100);
|
||||
|
||||
BOOST_CHECK(packed_vec.capacity() >= 100);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
Loading…
Reference in New Issue
Block a user