Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit 89a0af9

Browse files
author
Josh MacDonald
committed
Simplify btree_base_field_type<>
1 parent 9fe3bb3 commit 89a0af9

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

btree.h

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -316,38 +316,6 @@ static bool btree_compare_keys(
316316
return key_comparer::bool_compare(comp, x, y);
317317
}
318318

319-
// btree_is_node_big<> is a recursive template to determine whether a
320-
// node of TargetNodeSize bytes needs a larger base_fields type
321-
// (uint16, instead of uint8) to accomodate >= 256 values per node.
322-
template <int TargetNodeSize, int ValueSize>
323-
struct btree_is_node_big :
324-
btree_is_node_big<(TargetNodeSize / 2), (ValueSize / 2)> {
325-
};
326-
template <int TargetNodeSize>
327-
struct btree_is_node_big<TargetNodeSize, 1> {
328-
enum {
329-
// In the base case, TargetNodeSize corresponds to single-byte
330-
// entries and is the maximum number of values.
331-
is_big = base::integral_constant<bool, (TargetNodeSize >= 256)>::value,
332-
};
333-
};
334-
335-
// A helper for sizing the btree node's base_fields. The "type"
336-
// typedef in this struct is an integral type large enough to hold as
337-
// many ValueSize-values as will fit a node of TargetNodeSize bytes.
338-
template <int TargetNodeSize, int ValueSize>
339-
struct btree_base_field_type {
340-
enum {
341-
// "value_space" is the maximum leaf node count. leaf nodes have
342-
// a greatest maximum of the node types.
343-
value_space = TargetNodeSize - 2 * sizeof(void*),
344-
};
345-
typedef typename base::if_<
346-
btree_is_node_big<value_space, ValueSize>::is_big,
347-
uint16,
348-
uint8>::type type;
349-
};
350-
351319
template <typename Key, typename Compare,
352320
typename Alloc, int TargetNodeSize, int ValueSize>
353321
struct btree_common_params {
@@ -365,12 +333,21 @@ struct btree_common_params {
365333
typedef Key key_type;
366334
typedef ssize_t size_type;
367335
typedef ptrdiff_t difference_type;
368-
typedef typename btree_base_field_type<TargetNodeSize, ValueSize>::type
369-
base_field_type;
370336

371337
enum {
372338
kTargetNodeSize = TargetNodeSize,
339+
340+
// Available space for values. This is largest for leaf nodes,
341+
// which has overhead no fewer than two pointers.
342+
kNodeValueSpace = TargetNodeSize - 2 * sizeof(void*),
373343
};
344+
345+
// This is an integral type large enough to hold as many
346+
// ValueSize-values as will fit a node of TargetNodeSize bytes.
347+
typedef typename base::if_<
348+
(kNodeValueSpace / ValueSize) >= 256,
349+
uint16,
350+
uint8>::type node_count_type;
374351
};
375352

376353
// A parameters structure for holding the type parameters for a btree_map.
@@ -541,7 +518,7 @@ class btree_node {
541518
linear_search_type, binary_search_type>::type search_type;
542519

543520
struct base_fields {
544-
typedef typename Params::base_field_type field_type;
521+
typedef typename Params::node_count_type field_type;
545522

546523
// A boolean indicating whether the node is a leaf or not.
547524
bool leaf;
@@ -1479,6 +1456,10 @@ class btree : public Params::key_compare {
14791456
COMPILE_ASSERT(kNodeValues <
14801457
(1 << (8 * sizeof(typename base_fields::field_type))),
14811458
target_node_size_too_large);
1459+
1460+
// Test the assumption made in setting kNodeValueSpace.
1461+
COMPILE_ASSERT(sizeof(base_fields) >= 2 * sizeof(void*),
1462+
node_space_assumption_incorrect);
14821463
};
14831464

14841465
////

0 commit comments

Comments
 (0)