|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Top-down splay tree - Self-adjusting BST with amortized O(log n) operations. More...
#include <tpl_splay_tree.H>
Classes | |
| struct | Iterator |
| Iterator over the nodes. More... | |
Public Types | |
| using | Node = NodeType< Key > |
| using | key_type = Key |
| The key type stored in the node. | |
Public Member Functions | |
| Compare & | key_comp () noexcept |
| Returns a reference to the comparison criteria. | |
| Compare & | get_compare () noexcept |
See key_comp(). | |
| void | splay (const Key &key) noexcept |
Search key within the tree and splay that node; if key is not found, the last node of the search is splayed. | |
| GenTdSplayTree () noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >) | |
| Builds an empty tree with a comparison criteria built by default. | |
| GenTdSplayTree (Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >) | |
Builds an empty tree with the comparison criteria __cmp. | |
| GenTdSplayTree (const GenTdSplayTree &)=delete | |
| Not copyable: the tree does not own its nodes, and a copy would hold the same ones. | |
| GenTdSplayTree & | operator= (const GenTdSplayTree &)=delete |
| Not copyable, for the same reason as the copy constructor. | |
| GenTdSplayTree (GenTdSplayTree &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >) | |
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again. | |
| void | swap (GenTdSplayTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >) |
Swaps in constant time the nodes and the comparison criteria of this and tree. | |
| virtual | ~GenTdSplayTree ()=default |
| Destructor. | |
| Node * | insert (Node *p) noexcept |
| Inserts a node in a top-down splay tree. | |
| Node * | insert_dup (Node *p) noexcept |
| Inserts a node in a top-down splay tree, even if its key is already in the tree. | |
| Node * | search (const Key &key) noexcept |
| Searches a key in a top-down splay tree. | |
| Node * | search_or_insert (Node *p) noexcept |
Searches the key of p and, if it is not in the tree, inserts p. | |
| Node * | remove (const Key &key) noexcept |
| Remove a key from a top down splay tree. | |
| Node *& | getRoot () noexcept |
| Get the top-down splay tree's root. | |
| bool | verify () const |
Private Member Functions | |
| signed char | splay_impl (const Key &key) noexcept |
| Node * | __insert (Node *p) noexcept |
Static Private Member Functions | |
| static Node * | splay_max (Node *r) noexcept |
Brings the maximum of the subtree of root r to its root, with the zig-zig rotations of a splay. | |
Private Attributes | |
| Node | headnode |
| Node * | head |
| Node *& | root |
| Compare | cmp |
Static Private Attributes | |
| static constexpr signed char | CmpLess = -1 |
| static constexpr signed char | CmpEqual = 0 |
| static constexpr signed char | CmpGreater = 1 |
Top-down splay tree - Self-adjusting BST with amortized O(log n) operations.
GenTdSplayTree implements a splay tree, a self-adjusting binary search tree where every access operation (search, insert, delete) moves the accessed node to the root through a sequence of tree rotations called "splaying".
These rotations ensure amortized efficiency by reducing tree depth.
| NodeType | Node template (BinNode or BinNodeVtl). |
| Key | The type of keys stored in the tree. |
| Compare | Comparison functor for ordering keys. |
noexcept, and if it throws the program ends (std::terminate()). A splay relinks the tree as it compares, so a comparison that threw in the middle would leave nodes hanging from a local header, out of the tree. verify(), which does not splay, lets what the comparison throws reach the caller. Key throw: that local header is a whole node, whose key each splay builds by default inside noexcept. In a DynMapTree, whose keys are pairs, that is the default construction of its Data. Definition at line 146 of file tpl_splay_tree.H.
| using GenTdSplayTree< NodeType, Key, Compare >::key_type = Key |
The key type stored in the node.
Definition at line 250 of file tpl_splay_tree.H.
| using GenTdSplayTree< NodeType, Key, Compare >::Node = NodeType<Key> |
Definition at line 149 of file tpl_splay_tree.H.
|
inlinenoexcept |
Builds an empty tree with a comparison criteria built by default.
It is a constructor apart from the one that takes the comparison, and not a default argument of that one: GCC and Clang do not count a default argument in the noexcept of the implicit default constructor of a tree that inherits these, and what the construction of the comparison threw would end the program there.
| what | the default construction of the key of a header node or of the comparison, or the copy of the comparison, throws; nothing when they are noexcept. |
Definition at line 287 of file tpl_splay_tree.H.
|
inlinenoexcept |
Builds an empty tree with the comparison criteria __cmp.
| [in] | __cmp | the comparison criteria, which the tree copies. |
| what | the default construction of the key of a header node or the copy of __cmp throws; nothing when both are noexcept. The copy is from a Compare that is not const, which a comparison may have apart from the one from a const one. |
Definition at line 304 of file tpl_splay_tree.H.
|
delete |
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
The copy that the compiler wrote did worse: the copy used the header node of the tree that it copied, so that the two were one tree, and std::move() of a tree copied it too. Move the tree, which leaves the other one empty, or copy its nodes (copyRec()).
|
inlinenoexcept |
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
It builds no key, if it can: the key of the header node is built by default when that cannot throw; else it is that of the header node of tree, moved, when the move of a key cannot throw (see bin_node_detail::header_from()).
| [in,out] | tree | the tree to move from. It keeps its comparison criteria, which are copied: from a const one, or from one that is not const, if they have only that copy. Criteria that cannot be copied are moved, and tree keeps criteria that were moved from. |
| what | the copy of the comparison criteria throws, or their move if they cannot be copied; and what the default construction of the key of the header node throws, with a key whose move may throw too. Then tree keeps its nodes. Nothing when they are noexcept. |
Definition at line 342 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::root.
|
virtualdefault |
Destructor.
|
inlineprivatenoexcept |
Definition at line 372 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::cmp, KEY, LLINK, RLINK, and GenTdSplayTree< NodeType, Key, Compare >::root.
Referenced by GenTdSplayTree< NodeType, Key, Compare >::insert(), GenTdSplayTree< NodeType, Key, Compare >::insert_dup(), and GenTdSplayTree< NodeType, Key, Compare >::search_or_insert().
|
inlinenoexcept |
See key_comp().
Definition at line 256 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::key_comp().
|
inlinenoexcept |
Get the top-down splay tree's root.
Definition at line 535 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::root.
Referenced by main(), TEST(), and write_splay().
|
inlinenoexcept |
Inserts a node in a top-down splay tree.
| p | a pointer to the node to be inserted |
std::terminate()). noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree. Definition at line 403 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::__insert(), Aleph::and, GenTdSplayTree< NodeType, Key, Compare >::CmpEqual, KEY, LLINK, RLINK, GenTdSplayTree< NodeType, Key, Compare >::root, and GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
Referenced by main(), and write_splay().
|
inlinenoexcept |
Inserts a node in a top-down splay tree, even if its key is already in the tree.
| p | a pointer to the node to be inserted |
p std::terminate()). noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree. Definition at line 433 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::__insert(), Aleph::and, KEY, LLINK, RLINK, GenTdSplayTree< NodeType, Key, Compare >::root, and GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
|
inlinenoexcept |
Returns a reference to the comparison criteria.
Definition at line 253 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::cmp.
Referenced by GenTdSplayTree< NodeType, Key, Compare >::get_compare().
|
delete |
Not copyable, for the same reason as the copy constructor.
|
inlinenoexcept |
Remove a key from a top down splay tree.
Searches a key in a top down splay tree and remove the containing the key if this is found.
| key | to search |
std::terminate()). noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree. Definition at line 506 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::CmpEqual, LLINK, RLINK, GenTdSplayTree< NodeType, Key, Compare >::root, GenTdSplayTree< NodeType, Key, Compare >::splay_impl(), and GenTdSplayTree< NodeType, Key, Compare >::splay_max().
|
inlinenoexcept |
Searches a key in a top-down splay tree.
| key | key to search |
std::terminate()). noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree. Definition at line 458 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::CmpEqual, GenTdSplayTree< NodeType, Key, Compare >::root, and GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
Referenced by main(), TEST(), and write_splay().
|
inlinenoexcept |
Searches the key of p and, if it is not in the tree, inserts p.
| p | a pointer to the node to search or insert |
p, or p if it was inserted std::terminate()). noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree. Definition at line 480 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::__insert(), GenTdSplayTree< NodeType, Key, Compare >::CmpEqual, KEY, GenTdSplayTree< NodeType, Key, Compare >::root, and GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
|
inlinenoexcept |
Search key within the tree and splay that node; if key is not found, the last node of the search is splayed.
| [in] | key | the key to search |
std::terminate()). noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree. Definition at line 269 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
|
inlineprivatenoexcept |
Definition at line 159 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::cmp, GenTdSplayTree< NodeType, Key, Compare >::CmpEqual, GenTdSplayTree< NodeType, Key, Compare >::CmpGreater, GenTdSplayTree< NodeType, Key, Compare >::CmpLess, KEY, l, LLINK, r, RLINK, GenTdSplayTree< NodeType, Key, Compare >::root, Aleph::rotate_to_left(), and Aleph::rotate_to_right().
Referenced by GenTdSplayTree< NodeType, Key, Compare >::insert(), GenTdSplayTree< NodeType, Key, Compare >::insert_dup(), GenTdSplayTree< NodeType, Key, Compare >::remove(), GenTdSplayTree< NodeType, Key, Compare >::search(), GenTdSplayTree< NodeType, Key, Compare >::search_or_insert(), and GenTdSplayTree< NodeType, Key, Compare >::splay().
|
inlinestaticprivatenoexcept |
Brings the maximum of the subtree of root r to its root, with the zig-zig rotations of a splay.
The new root has no right child.
Definition at line 223 of file tpl_splay_tree.H.
References l, LLINK, r, RLINK, and Aleph::rotate_to_left().
Referenced by GenTdSplayTree< NodeType, Key, Compare >::remove().
|
inlinenoexcept |
Swaps in constant time the nodes and the comparison criteria of this and tree.
| [in,out] | tree | the tree to swap with. |
| what | the swap of the comparison criteria throws, which goes first: then the nodes stay where they were. |
Definition at line 360 of file tpl_splay_tree.H.
References GenTdSplayTree< NodeType, Key, Compare >::cmp, and GenTdSplayTree< NodeType, Key, Compare >::root.
|
inline |
Definition at line 540 of file tpl_splay_tree.H.
References Aleph::check_bst(), GenTdSplayTree< NodeType, Key, Compare >::cmp, and GenTdSplayTree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
private |
Definition at line 154 of file tpl_splay_tree.H.
Referenced by GenTdSplayTree< NodeType, Key, Compare >::__insert(), GenTdSplayTree< NodeType, Key, Compare >::key_comp(), GenTdSplayTree< NodeType, Key, Compare >::splay_impl(), GenTdSplayTree< NodeType, Key, Compare >::swap(), and GenTdSplayTree< NodeType, Key, Compare >::verify().
|
staticconstexprprivate |
Definition at line 156 of file tpl_splay_tree.H.
Referenced by GenTdSplayTree< NodeType, Key, Compare >::insert(), GenTdSplayTree< NodeType, Key, Compare >::remove(), GenTdSplayTree< NodeType, Key, Compare >::search(), GenTdSplayTree< NodeType, Key, Compare >::search_or_insert(), and GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
|
staticconstexprprivate |
Definition at line 157 of file tpl_splay_tree.H.
Referenced by GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
|
staticconstexprprivate |
Definition at line 155 of file tpl_splay_tree.H.
Referenced by GenTdSplayTree< NodeType, Key, Compare >::splay_impl().
|
private |
Definition at line 153 of file tpl_splay_tree.H.
|
private |
Definition at line 152 of file tpl_splay_tree.H.
|
private |
Definition at line 153 of file tpl_splay_tree.H.
Referenced by GenTdSplayTree< NodeType, Key, Compare >::GenTdSplayTree(), GenTdSplayTree< NodeType, Key, Compare >::__insert(), GenTdSplayTree< NodeType, Key, Compare >::getRoot(), GenTdSplayTree< NodeType, Key, Compare >::insert(), GenTdSplayTree< NodeType, Key, Compare >::insert_dup(), GenTdSplayTree< NodeType, Key, Compare >::remove(), GenTdSplayTree< NodeType, Key, Compare >::search(), GenTdSplayTree< NodeType, Key, Compare >::search_or_insert(), GenTdSplayTree< NodeType, Key, Compare >::splay_impl(), GenTdSplayTree< NodeType, Key, Compare >::swap(), and GenTdSplayTree< NodeType, Key, Compare >::verify().