Browse Source

memory:
* added utility class is_power_of_two
* added variable template is_power_of_two_v

Michele Caini 4 years ago
parent
commit
a595c6ec6f
2 changed files with 26 additions and 0 deletions
  1. 16 0
      src/entt/core/memory.hpp
  2. 10 0
      test/entt/core/memory.cpp

+ 16 - 0
src/entt/core/memory.hpp

@@ -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;
+
+
 }
 }
 
 
 
 

+ 10 - 0
test/entt/core/memory.cpp

@@ -31,3 +31,13 @@ TEST(Memory, PoccaPocmaAndPocs) {
     entt::propagate_on_container_move_assignment(lhs, rhs);
     entt::propagate_on_container_move_assignment(lhs, rhs);
     entt::propagate_on_container_swap(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>);
+}