datastructures 0.1.0
Loading...
Searching...
No Matches
_avl_common.hpp
Go to the documentation of this file.
1
12
13#pragma once
14
15#include <cstdint>
16#include <memory>
17#include <type_traits>
18
19namespace datastructures {
20namespace detail {
21
40inline int avl_calc_max_height(size_t n) {
41 int h = -1;
42 for (size_t a = 0, b = 1, t = 0; b <= n; t = a + b + 1, a = b, b = t, ++h)
43 ;
44 return h;
45}
46
50using avl_sign_type = int8_t;
51
58template <class TNode, class TAlloc>
60 std::allocator_traits<TAlloc>::template rebind_alloc<TNode>;
61
70template <class TNode, class TAlloc>
72 std::allocator_traits<avl_node_allocator_type<TNode, TAlloc>>::pointer;
73
75template <class TKey, class TItem, class TKeyComp, class TAlloc, class TNode>
77 public:
78 using key_type = TKey;
79 using item_type = TItem;
80 using comparator_type = TKeyComp;
81 using allocator_type = TAlloc;
82 using node_type = TNode;
83
86
91
95 using node_alloc_traits = std::allocator_traits<node_alloc_type>;
96
100 using size_type = typename node_alloc_traits::size_type;
101
102 protected:
104
107
108 // ==== Memory Management Functions ====
109
119 template <class T>
120 requires(!std::is_invocable_v<T>)
121 node_ptr create_node(T &&item);
122
131 template <class TCallback>
132 requires std::is_invocable_v<TCallback>
133 node_ptr create_node(TCallback &&callback);
134
136 void destroy_node(node_ptr node) noexcept;
137
138 public:
142
146
147 virtual ~avl_tree_base() = 0;
148
149 // ==== Allocator Getter ====
150
152 inline node_alloc_type get_allocator() const noexcept {
153 return allocator;
154 }
155
156 // ==== Access Operations ====
157
170 [[nodiscard]] static node_ptr min_from(node_ptr node) noexcept;
171
184 [[nodiscard]] static node_ptr max_from(node_ptr node) noexcept;
185};
186
187} // namespace detail
188} // namespace datastructures
189
190// ==== Implementation ====
191
192#include "_avl_common.ipp"
Implementation of the common AVL tree features.
Base class for avl trees.
Definition _avl_common.hpp:76
TKey key_type
Definition _avl_common.hpp:78
avl_node_allocator_type< node_type, allocator_type > node_alloc_type
The node allocator type.
Definition _avl_common.hpp:85
static node_ptr min_from(node_ptr node) noexcept
Finds the minimum value starting at the given node.
Definition _avl_common.ipp:72
avl_tree_base(comparator_type &&comparator, allocator_type &&allocator) noexcept
Definition _avl_common.hpp:143
static node_ptr max_from(node_ptr node) noexcept
Finds the maximum value starting at the given node.
Definition _avl_common.ipp:85
TNode node_type
Definition _avl_common.hpp:82
avl_sign_type sign_type
Definition _avl_common.hpp:103
TItem item_type
Definition _avl_common.hpp:79
avl_node_ptr< node_type, allocator_type > node_ptr
The node pointer type.
Definition _avl_common.hpp:90
TKeyComp comparator_type
Definition _avl_common.hpp:80
TAlloc allocator_type
Definition _avl_common.hpp:81
node_ptr create_node(T &&item)
Constructs a new node with the given item.
Definition _avl_common.ipp:28
comparator_type comparator
Definition _avl_common.hpp:105
void destroy_node(node_ptr node) noexcept
Destroys the node and frees the memory.
Definition _avl_common.ipp:62
avl_tree_base(const comparator_type &comparator, const allocator_type &allocator)
Definition _avl_common.hpp:139
node_alloc_type get_allocator() const noexcept
Returns the associated allocator.
Definition _avl_common.hpp:152
node_alloc_type allocator
Definition _avl_common.hpp:106
std::allocator_traits< node_alloc_type > node_alloc_traits
Node allocator traits.
Definition _avl_common.hpp:95
typename node_alloc_traits::size_type size_type
The size type.
Definition _avl_common.hpp:100
int8_t avl_sign_type
The type to use for the trinary sign value for each node.
Definition _avl_common.hpp:50
int avl_calc_max_height(size_t n)
Returns the maximum allowed height for an AVL tree of given size.
Definition _avl_common.hpp:40
std::allocator_traits< TAlloc >::template rebind_alloc< TNode > avl_node_allocator_type
An allocator to allocate nodes.
Definition _avl_common.hpp:60
std::allocator_traits< avl_node_allocator_type< TNode, TAlloc > >::pointer avl_node_ptr
A node pointer type.
Definition _avl_common.hpp:72
Declarations of the persistent map.
Definition _avl.hpp:14