From ea542763c56075ae77e9a5cb78bf47feb8d868c3 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 4 Jan 2020 23:04:23 +0800 Subject: [PATCH] include/cpp-btree: use "constexpr inline" for constants in C++17, it's a more general way to define constants. and these constants are correctly typed. Signed-off-by: Kefu Chai --- src/include/cpp-btree/btree.h | 52 ++++++++++++++++------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/include/cpp-btree/btree.h b/src/include/cpp-btree/btree.h index 7e03ef88329c1..df2415867dcec 100644 --- a/src/include/cpp-btree/btree.h +++ b/src/include/cpp-btree/btree.h @@ -139,16 +139,14 @@ struct common_params { // True if this is a multiset or multimap. using is_multi_container = std::integral_constant; - enum { - kTargetNodeSize = TargetNodeSize, - kValueSize = ValueSize, - // Upper bound for the available space for values. This is largest for leaf - // nodes, which have overhead of at least a pointer + 3 bytes (for storing - // 3 field_types) + paddings. if alignof(key_type) is 1, the size of padding - // would be 0. - kNodeValueSpace = - TargetNodeSize - /*minimum overhead=*/(sizeof(void *) + 4), - }; + constexpr static int kTargetNodeSize = TargetNodeSize; + constexpr static int kValueSize = ValueSize; + // Upper bound for the available space for values. This is largest for leaf + // nodes, which have overhead of at least a pointer + 3 bytes (for storing + // 3 field_types) + paddings. if alignof(key_type) is 1, the size of padding + // would be 0. + constexpr static int kNodeValueSpace = + TargetNodeSize - /*minimum overhead=*/(sizeof(void *) + 4); // This is an integral type large enough to hold as many // ValueSize-values as will fit a node of TargetNodeSize bytes. @@ -409,20 +407,18 @@ class btree_node { : NodeTargetValues((begin + end) / 2 + 1, end); } - enum { - kValueSize = params_type::kValueSize, - kTargetNodeSize = params_type::kTargetNodeSize, - kNodeTargetValues = NodeTargetValues(0, params_type::kTargetNodeSize), + constexpr static int kValueSize = params_type::kValueSize; + constexpr static int kTargetNodeSize = params_type::kTargetNodeSize; + constexpr static int kNodeTargetValues = NodeTargetValues(0, kTargetNodeSize); - // We need a minimum of 3 values per internal node in order to perform - // splitting (1 value for the two nodes involved in the split and 1 value - // propagated to the parent as the delimiter for the split). - kNodeValues = kNodeTargetValues >= 3 ? kNodeTargetValues : 3, + // We need a minimum of 3 values per internal node in order to perform + // splitting (1 value for the two nodes involved in the split and 1 value + // propagated to the parent as the delimiter for the split). + constexpr static size_type kNodeValues = std::max(kNodeTargetValues, 3); - // The node is internal (i.e. is not a leaf node) if and only if `max_count` - // has this value. - kInternalNodeMaxCount = 0, - }; + // The node is internal (i.e. is not a leaf node) if and only if `max_count` + // has this value. + constexpr static size_type kInternalNodeMaxCount = 0; struct base_fields { // A pointer to the node's parent. @@ -893,11 +889,9 @@ class btree { return const_cast(&empty_node); } - enum { - kNodeValues = node_type::kNodeValues, - kMinNodeValues = kNodeValues / 2, - kValueSize = node_type::kValueSize, - }; + constexpr static int kNodeValues = node_type::kNodeValues; + constexpr static int kMinNodeValues = kNodeValues / 2; + constexpr static int kValueSize = node_type::kValueSize; // A helper class to get the empty base class optimization for 0-size // allocators. Base is allocator_type. @@ -2079,7 +2073,7 @@ auto btree

::erase(iterator begin, iterator end) const size_type remaining_to_erase = size_ - target_size; const size_type remaining_in_node = begin.node->count() - begin.position; begin = erase_from_leaf_node( - begin, (std::min)(remaining_to_erase, remaining_in_node)); + begin, std::min(remaining_to_erase, remaining_in_node)); } else { begin = erase(begin); } @@ -2413,7 +2407,7 @@ inline auto btree

::internal_emplace(iterator iter, Args &&... args) // size. Simply grow the size of the root node. assert(iter.node == root()); iter.node = - new_leaf_root_node((std::min)(kNodeValues, 2 * max_count)); + new_leaf_root_node(std::min(kNodeValues, 2 * max_count)); iter.node->swap(root(), mutable_allocator()); delete_leaf_node(root()); mutable_root() = iter.node; -- 2.47.3