26 #include "../type_traits" 30 namespace cxx {
namespace Bits {
39 template<
typename Node,
typename Get_key,
typename Compare >
47 static Node *child(Node
const *n,
Direction d)
48 {
return Bst_node::next<Node>(n, d); }
50 static bool cmp(Node
const *l, Node
const *r)
51 {
return Compare()(Get_key::key_of(l), Get_key::key_of(r)); }
57 static Node *child(Node
const *n,
Direction d)
58 {
return Bst_node::next<Node>(n, !d); }
60 static bool cmp(Node
const *l, Node
const *r)
61 {
return Compare()(Get_key::key_of(r), Get_key::key_of(l)); }
77 typedef __Bst_iter<Node, Node, Fwd>
Iterator;
104 Node *
head()
const {
return static_cast<Node*
>(
_head); }
108 {
return Get_key::key_of(static_cast<Node const *>(n)); }
118 static Dir
dir(Key_param_type l, Key_param_type r)
136 {
return dir(l,
k(r)); }
139 static bool greater(Key_param_type l, Key_param_type r)
140 {
return Compare()(r, l); }
157 template<
typename FUNC>
166 callback(static_cast<Node *>(head));
231 Node *
find_node(Key_param_type key)
const;
247 Const_iterator
find(Key_param_type key)
const;
257 template<
typename FUNC>
273 template<
typename Node,
typename Get_key,
class Compare>
284 return static_cast<Node*
>(q);
289 template<
typename Node,
typename Get_key,
class Compare>
303 return static_cast<Node*
>(q);
305 return static_cast<Node*
>(r);
309 template<
typename Node,
typename Get_key,
class Compare>
321 return Iterator(static_cast<Node*>(q), static_cast<Node *>(r));
323 if (d !=
Dir::L && q == r)
static void remove_tree(Bst_node *head, FUNC &&callback)
Remove all elements in the subtree of head.
__Bst_iter< Node, Node, Rev > Rev_iterator
Backward iterator.
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
__Bst_iter< Node, Node const, Fwd > Const_iterator
Constant forward iterator.
Rev Rev_iter_ops
Helper for building reverse iterators for different wrapper classes.
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Node * head() const
Access the head node as object of type Node.
__Bst_iter< Node, Node const, Rev > Const_rev_iterator
Constant backward.
Node * lower_bound_node(Key_param_type key) const
find the first node with a key not less than the given key.
Const_iterator find(Key_param_type key) const
find the node with the given key.
static Dir dir(Key_param_type l, Bst_node const *r)
Get the direction to go from l to search for r.
static bool greater(Key_param_type l, Bst_node const *r)
Is l greater than r.
The direction to go in a binary search tree.
Get_key::Key_type Key_type
The type of key values used to generate the total order of the elements.
Node * find_node(Key_param_type key) const
find the node with the given key.
static Bst_node * next(Bst_node const *p, Direction d)
Get next node in direction d.
static Key_type k(Bst_node const *n)
Get the key value of n.
void remove_all(FUNC &&callback)
Clear the tree.
static Dir dir(Key_param_type l, Key_param_type r)
Get the direction to go from l to search for r.
Type_traits< Key_type >::Param_type Key_param_type
The type for key parameters.
static bool greater(Bst_node const *l, Bst_node const *r)
Is l greater than r.
Bst_node * _head
The head pointer of the tree.
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Bst()
Create an empty tree.
Basic binary search tree (BST).
Const_iterator end() const
Get the end marker for the constant forward iterator.
Basic type of a node in a binary search tree (BST).
static bool greater(Key_param_type l, Key_param_type r)
Is l greater than r.
__Bst_iter< Node, Node, Fwd > Iterator
Forward iterator.
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Fwd Fwd_iter_ops
Helper for building forward iterators for different wrapper classes.
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Iterator end()
Get the end marker for the mutable forward iterator.
Rev_iterator rend()
Get the end marker for the mutable backward iterator.