Yggdrasill  0.2
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ygg::WBTree< Node, NodeTraits, Options, Tag, Compare > Class Template Reference

The Weight Balanced Tree. More...

#include <wbtree.hpp>

Inheritance diagram for ygg::WBTree< Node, NodeTraits, Options, Tag, Compare >:
ygg::bst::BinarySearchTree< Node, Options, Tag, Compare >

Public Types

using MyClass = WBTree< Node, NodeTraits, Options, Tag, Compare >
 
using NB = WBTreeNodeBase< Node, Options, Tag >
 
using TB = bst::BinarySearchTree< Node, Options, Tag, Compare >
 
template<bool reverse>
using iterator = typename TB::template iterator< reverse >
 
template<bool reverse>
using const_iterator = typename TB::template const_iterator< reverse >
 

Public Member Functions

 WBTree () noexcept
 Create a new empty weight balanced tree.
 
 WBTree (MyClass &&other) noexcept
 Create a new weight balanced tree from a different weight balanced tree. More...
 
void insert (Node &node) CMP_NOEXCEPT(node)
 Inserts <node> into the tree. More...
 
void insert_left_leaning (Node &node) CMP_NOEXCEPT(node)
 
void insert_right_leaning (Node &node) CMP_NOEXCEPT(node)
 
template<class Comparable >
utilities::select_type_t< size_t, Node *, Options::stl_erase > erase (const Comparable &c) CMP_NOEXCEPT(c)
 Deletes a node that compares equally to from the tree. More...
 
template<bool reverse>
utilities::select_type_t< const iterator< reverse >, Node *, Options::stl_erase > erase (const iterator< reverse > &it) CMP_NOEXCEPT(*it)
 Deletes a node by iterator. More...
 
template<class Comparable >
Node * erase_optimistic (const Comparable &c) CMP_NOEXCEPT(c)
 
void remove (Node &node) CMP_NOEXCEPT(node)
 Removes <node> from the tree. More...
 
const_iterator< false > find (const Comparable &query) const CMP_NOEXCEPT(query)
 Finds an element in the tree. More...
 
iterator< false > find (const Comparable &query) CMP_NOEXCEPT(query)
 
iterator< false > find (const Comparable &query, Callbacks *cbs)
 
const_iterator< false > upper_bound (const Comparable &query) const CMP_NOEXCEPT(query)
 Upper-bounds an element. More...
 
iterator< false > upper_bound (const Comparable &query) CMP_NOEXCEPT(query)
 
const_iterator< false > lower_bound (const Comparable &query) const CMP_NOEXCEPT(query)
 Lower-bounds an element. More...
 
iterator< false > lower_bound (const Comparable &query) CMP_NOEXCEPT(query)
 
void dump_to_dot (const std::string &filename) const
 Debugging Method: Draw the Tree as a .dot file. More...
 
const_iterator< false > cbegin () const noexcept
 
const_iterator< false > cend () const noexcept
 
const_iterator< false > begin () const noexcept
 
iterator< false > begin () noexcept
 
const_iterator< false > end () const noexcept
 
iterator< false > end () noexcept
 
const_iterator< true > crbegin () const noexcept
 
const_iterator< true > crend () const noexcept
 
const_iterator< true > rbegin () const noexcept
 
iterator< true > rbegin () noexcept
 
const_iterator< true > rend () const noexcept
 
iterator< true > rend () noexcept
 
const_iterator< false > iterator_to (const Node &node) const noexcept
 
iterator< false > iterator_to (Node &node) noexcept
 
size_t size () const noexcept
 
void clear () noexcept
 Removes all elements from the tree. More...
 
bool empty () const noexcept
 Returns whether the tree is empty. More...
 
Node * get_root () const noexcept
 

Static Public Member Functions

static Node * get_parent (Node *n) noexcept
 
static Node * get_left_child (Node *n) noexcept
 
static Node * get_right_child (Node *n) noexcept
 

Protected Member Functions

template<bool fix_upward>
void remove_onepass (Node &node) CMP_NOEXCEPT(node)
 
void remove_to_leaf (Node &node) CMP_NOEXCEPT(node)
 
void fixup_after_delete (Node *parent, bool deleted_left) CMP_NOEXCEPT(*parent)
 
template<bool call_fixup>
bool remove_swap_and_remove_left (Node *node, Node *replacement) CMP_NOEXCEPT(*node)
 
template<bool call_fixup>
bool remove_swap_and_remove_right (Node *node, Node *replacement) CMP_NOEXCEPT(*node)
 
template<bool call_fixup>
void remove_leaf (Node *node) CMP_NOEXCEPT(*node)
 
template<bool on_equality_prefer_left>
void insert_leaf_base_twopass (Node &node, Node *start) CMP_NOEXCEPT(node)
 
void fixup_after_insert_twopass (Node *node) CMP_NOEXCEPT(*node)
 
template<bool on_equality_prefer_left>
void insert_leaf_onepass (Node &node) CMP_NOEXCEPT(node)
 
void rotate_left (Node *parent) noexcept
 
void rotate_right (Node *parent) noexcept
 
void swap_nodes (Node *n1, Node *n2) noexcept
 
void replace_node (Node *to_be_replaced, Node *replace_with) noexcept
 
void swap_unrelated_nodes (Node *n1, Node *n2) noexcept
 
void swap_neighbors (Node *parent, Node *child) noexcept
 
void verify_sizes () const
 
Node * get_first_equal (Node *n) noexcept
 
Node * get_smallest () const noexcept
 
Node * get_largest () const noexcept
 
Node * get_uncle (Node *node) const noexcept
 
void dump_to_dot_base (const std::string &filename, NodeNameGetter name_getter) const
 
void output_node_base (const Node *node, std::ofstream &out, NodeNameGetter name_getter) const
 

Protected Attributes

Node * root
 
Compare cmp
 
SizeHolder< Options::constant_time_size > s
 

Detailed Description

template<class Node, class NodeTraits, class Options = DefaultOptions, class Tag = int, class Compare = ygg::utilities::flexible_less>
class ygg::WBTree< Node, NodeTraits, Options, Tag, Compare >

The Weight Balanced Tree.

TODO change this This is the main Weight Balanced Tree class.

Template Parameters
NodeThe node class for this tree. It must be derived from WBTreeNodeBase.
NodeTraitsA class implementing various hooks and functions on your node class. See DOCTODO for details.
OptionsThe TreeOptions class specifying the parameters of this WBTree. See the TreeOptions and TreeFlags classes for details.
TagAn class tag that identifies this tree. Can be used to insert the same nodes into multiple trees. See DOCTODO for details. Can be any class, the class can be empty.
CompareA compare class. The Red-Black Tree follows STL semantics for 'Compare'. Defaults to ygg::utilities::flexible_less. Implement operator<(const Node & lhs, const Node & rhs) if you want to use it.

Constructor & Destructor Documentation

◆ WBTree()

template<class Node , class NodeTraits , class Options = DefaultOptions, class Tag = int, class Compare = ygg::utilities::flexible_less>
ygg::WBTree< Node, NodeTraits, Options, Tag, Compare >::WBTree ( MyClass &&  other)
noexcept

Create a new weight balanced tree from a different weight balanced tree.

The other weight balanced tree is moved into this one, i.e., using it afterwards is undefined behavior.

Parameters
otherThe weight balanced tree that this one is constructed from

Member Function Documentation

◆ begin()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::begin ( ) const
noexceptinherited

Returns an iterator pointing to the smallest element in the tree.

◆ cbegin()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::cbegin ( ) const
noexceptinherited

Returns an iterator pointing to the smallest element in the tree.

◆ cend()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::cend ( ) const
noexceptinherited

Returns an iterator pointing after the largest element in the tree.

◆ clear()

void ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::clear ( )
noexceptinherited

Removes all elements from the tree.

Removes all elements from the tree.

◆ crbegin()

const_iterator<true> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::crbegin ( ) const
noexceptinherited

Returns an reverse iterator pointing to the largest element in the tree.

◆ crend()

const_iterator<true> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::crend ( ) const
noexceptinherited

Returns an reverse iterator pointing before the smallest element in the tree.

◆ dump_to_dot()

void ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::dump_to_dot ( const std::string &  filename) const
inherited

Debugging Method: Draw the Tree as a .dot file.

Outputs the current tree as a .dot file which can be drawn using graphviz.

Parameters
filenameThe file path where to write the .dot file.

◆ empty()

bool ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::empty ( ) const
noexceptinherited

Returns whether the tree is empty.

This method runs in O(1).

Returns
true if the tree is empty, false otherwise

◆ end()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::end ( ) const
noexceptinherited

Returns an iterator pointing after the largest element in the tree.

◆ erase() [1/2]

template<class Node , class NodeTraits , class Options = DefaultOptions, class Tag = int, class Compare = ygg::utilities::flexible_less>
template<class Comparable >
utilities::select_type_t<size_t, Node *, Options::stl_erase> ygg::WBTree< Node, NodeTraits, Options, Tag, Compare >::erase ( const Comparable &  c)

Deletes a node that compares equally to from the tree.

Warning
The behavior of this method strongly depends on whether the STL_ERASE option is set or not!

Removes a node that compares equally to from the tree.

If STL_ERASE is set, this method removes all nodes that compare equally to c from the tree and returns the number of nodes removed.

If STL_ERASE is not set, it removes only one node and returns a pointer to the removed node.

Parameters
cAnything comparable to a node. A node (resp. all nodes, see above) that compares equally will be removed
Returns
If STL_ERASE is unset: A pointer to the node that has been removed, or nullptr if no node was removed. If STL_ERASE is set: The number of erased nodes.

◆ erase() [2/2]

template<class Node , class NodeTraits , class Options = DefaultOptions, class Tag = int, class Compare = ygg::utilities::flexible_less>
template<bool reverse>
utilities::select_type_t<const iterator<reverse>, Node *, Options::stl_erase> ygg::WBTree< Node, NodeTraits, Options, Tag, Compare >::erase ( const iterator< reverse > &  it)

Deletes a node by iterator.

Warning
The return type of this method depends on whether the STL_ERASE option is set or not!

Removes a node that is pointed to by an iterator.

Parameters
cAnything comparable to a node. A node (resp. all nodes, see above) that compares equally will be removed
Returns
If STL_ERASE is unset: A pointer to the node that has been removed. If STL_ERASE is set: An iterator to the node after the removed node (or end()).

◆ find()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::find ( const Comparable &  query) const
inherited

Finds an element in the tree.

Returns an iterator to the first element that compares equally to <query>. Note that <query> does not have to be a Node, but can be anything that can be compared to a Node, i.e., for which Compare()(const Node &, const Comparable &) and Compare()(const Comparable &, const Node &) are defined and implemented. In the case of using the default ygg::utilities::flexible_less as Compare, that means you have to implement operator<() for both types.

Warning
Not available for explicitly ordered trees
Parameters
queryAn object comparing equally to the element that should be found.
Returns
An iterator to the first element comparing equally to <query>, or end() if no such element exists

◆ insert()

template<class Node , class NodeTraits , class Options = DefaultOptions, class Tag = int, class Compare = ygg::utilities::flexible_less>
void ygg::WBTree< Node, NodeTraits, Options, Tag, Compare >::insert ( Node &  node)

Inserts <node> into the tree.

Inserts <node> into the tree.

Warning: Please note that after calling insert() on a node (and before removing that node again), that node may not move in memory. A common pitfall is to store nodes in a std::vector (or other STL container), which reallocates (and thereby moves objecs around).

Warning
Not available for explicitly ordered trees
Parameters
NodeThe node to be inserted.

◆ iterator_to()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::iterator_to ( const Node &  node) const
noexceptinherited

Returns an iterator pointing to the entry held in node.

Parameters
nodeThe node the iterator should point to.

◆ lower_bound()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::lower_bound ( const Comparable &  query) const
inherited

Lower-bounds an element.

Returns an iterator to the first element that is not less that <query>, i.e., that does not have to go before <query>.

Note that <query> does not have to be a Node, but can be anything that can be compared to a Node, i.e., for which Compare()(const Node &, const Comparable &) and Compare()(const Comparable &, const Node &) are defined and implemented. In the case of using the default ygg::utilities::flexible_less as Compare, that means you have to implement operator<() for both types.

Warning
Not available for explicitly ordered trees
Parameters
queryAn object comparable to Node that should be lower-bounded
Returns
An iterator to the first element comparing greater-or-equally to <query>, or end() if no such element exists

◆ rbegin()

const_iterator<true> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::rbegin ( ) const
noexceptinherited

Returns an reverse iterator pointing to the largest element in the tree.

◆ remove()

template<class Node , class NodeTraits , class Options = DefaultOptions, class Tag = int, class Compare = ygg::utilities::flexible_less>
void ygg::WBTree< Node, NodeTraits, Options, Tag, Compare >::remove ( Node &  node)

Removes <node> from the tree.

Removes <node> from the tree.

Parameters
NodeThe node to be removed.

◆ rend()

const_iterator<true> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::rend ( ) const
noexceptinherited

Returns an reverse iterator pointing before the smallest element in the tree.

◆ size()

size_t ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::size ( ) const
noexceptinherited

Return the number of elements in the tree.

This method runs in O(1).

Warning
This method is only available if CONSTANT_TIME_SIZE is set as option!
Returns
The number of elements in the tree.

◆ upper_bound()

const_iterator<false> ygg::bst::BinarySearchTree< Node, Options, Tag, Compare, DefaultParentContainer<Node> >::upper_bound ( const Comparable &  query) const
inherited

Upper-bounds an element.

Returns an iterator to the smallest element to which <query> compares as "less", i.e. the smallest element that is considered go strictly after <query>.

Note that <query> does not have to be a Node, but can be anything that can be compared to a Node, i.e., for which Compare()(const Node &, const Comparable &) and Compare()(const Comparable &, const Node &) are defined and implemented. In the case of using the default ygg::utilities::flexible_less as Compare, that means you have to implement operator<() for both types.

Warning
Not available for explicitly ordered trees
Parameters
queryAn object comparable to Node that should be upper-bounded
Returns
An iterator to the first element comparing "greater" to <query>, or end() if no such element exists

The documentation for this class was generated from the following file: