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

Calculate the next power-of-two for the user in hash_map_init. #420

Merged
merged 4 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions include/rcutils/types/hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ rcutils_get_zero_initialized_hash_map();
* ```c
* rcutils_hash_map_t hash_map = rcutils_get_zero_initialized_hash_map();
* rcutils_ret_t ret =
* rcutils_hash_map_init(&hash_map, 10, rcutils_get_default_allocator());
* rcutils_hash_map_init(&hash_map, 2, rcutils_get_default_allocator());
* if (ret != RCUTILS_RET_OK) {
* // ... do error handling
* }
Expand All @@ -156,7 +156,8 @@ rcutils_get_zero_initialized_hash_map();
* ```
*
* \param[inout] hash_map rcutils_hash_map_t to be initialized
* \param[in] initial_capacity the amount of initial capacity for the hash_map - this must be greater than zero and a power of 2
* \param[in] initial_capacity the amount of initial capacity for the hash_map - this must be
* greater than zero and will be automatically rounded up to the next power of 2
* \param[in] key_size the size (in bytes) of the key used to index the data
* \param[in] data_size the size (in bytes) of the data being stored
* \param[in] key_hashing_func a function that returns a hashed value for a key
Expand Down
21 changes: 17 additions & 4 deletions src/hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ static rcutils_ret_t hash_map_check_and_grow_map(rcutils_hash_map_t * hash_map)
return ret;
}

// Modified from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
static size_t next_power_of_two(size_t v)
{
size_t shf;
v--;
for (size_t i = 0; i <= sizeof(size_t); ++i) {
shf = (1 << i);
v |= v >> shf;
}
v++;

return v > 1 ? v : 1;
}

rcutils_ret_t
rcutils_hash_map_init(
rcutils_hash_map_t * hash_map,
Expand All @@ -255,11 +269,10 @@ rcutils_hash_map_init(
return RCUTILS_RET_INVALID_ARGUMENT;
}

// Due to an optimization we use during lookup, we can currently only handle power-of-two
// capacities. Enforce that here.
// Due to an optimization we use during lookup, the capacity must be a power-of-two.
// If the user passed us something that is not power-of-two, round up to the next power-of-two
if ((initial_capacity & (initial_capacity - 1)) != 0) {
RCUTILS_SET_ERROR_MSG("This hashmap only works with power-of-two capacities");
return RCUTILS_RET_INVALID_ARGUMENT;
initial_capacity = next_power_of_two(initial_capacity);
}

hash_map->impl = allocator->allocate(sizeof(rcutils_hash_map_impl_t), allocator->state);
Expand Down
7 changes: 5 additions & 2 deletions test/test_hash_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ TEST_F(HashMapBaseTest, init_map_initial_capacity_zero_fails) {
EXPECT_EQ(RCUTILS_RET_INVALID_ARGUMENT, ret) << rcutils_get_error_string().str;
}

TEST_F(HashMapBaseTest, init_map_initial_capacity_not_power_of_two_fails) {
TEST_F(HashMapBaseTest, init_map_initial_capacity_not_power_of_two) {
rcutils_ret_t ret = rcutils_hash_map_init(
&map, 3, sizeof(uint32_t), sizeof(uint32_t),
test_hash_map_uint32_hash_func, test_uint32_cmp, &allocator);
EXPECT_EQ(RCUTILS_RET_INVALID_ARGUMENT, ret) << rcutils_get_error_string().str;
ASSERT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;
size_t current_capacity = 0;
ASSERT_EQ(RCUTILS_RET_OK, rcutils_hash_map_get_capacity(&map, &current_capacity));
EXPECT_EQ(4u, current_capacity);
}

TEST_F(HashMapBaseTest, init_map_key_size_zero_fails) {
Expand Down