* added utility class is_power_of_two
* added variable template is_power_of_two_v
This commit is contained in:
Michele Caini
2021-08-29 22:15:33 +02:00
parent 98f785d199
commit a595c6ec6f
2 changed files with 26 additions and 0 deletions

View File

@@ -73,6 +73,22 @@ constexpr void propagate_on_container_swap(Allocator &lhs, Allocator &rhs) ENTT_
}
/**
* @brief Utility class to check whether a value is a power of two or not.
* @tparam Value A value that may or may not be a power of two.
*/
template<std::size_t Value>
using is_power_of_two = std::bool_constant<Value && ((Value & (Value - 1)) == 0)>;
/**
* @brief Helper variable template.
* @tparam Value A value that may or may not be a power of two.
*/
template<std::size_t Value>
inline constexpr bool is_power_of_two_v = is_power_of_two<Value>::value;
}

View File

@@ -31,3 +31,13 @@ TEST(Memory, PoccaPocmaAndPocs) {
entt::propagate_on_container_move_assignment(lhs, rhs);
entt::propagate_on_container_swap(lhs, rhs);
}
TEST(Memory, IsPowerOfTwo) {
ASSERT_FALSE(entt::is_power_of_two_v<0u>);
ASSERT_TRUE(entt::is_power_of_two_v<1u>);
ASSERT_TRUE(entt::is_power_of_two_v<2u>);
ASSERT_TRUE(entt::is_power_of_two_v<4u>);
ASSERT_FALSE(entt::is_power_of_two_v<7u>);
ASSERT_TRUE(entt::is_power_of_two_v<128u>);
ASSERT_FALSE(entt::is_power_of_two_v<200u>);
}