Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynBinHeap.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
50# ifndef TPL_DYNBINHEAP_H
51# define TPL_DYNBINHEAP_H
52
53# include <memory>
54# include <type_traits>
55
56# include <ah-concepts.H>
57# include <ahDry.H>
58# include <ah-args-ctor.H>
59# include <htlist.H>
60# include <ah-dry.H>
61# include <tpl_binNodeUtils.H>
62# include <tpl_binHeap.H>
63
64using namespace Aleph;
65
66namespace Aleph {
67
77 template <class T, class Compare = Aleph::less<T>>
78 requires StrictWeakOrder<Compare, T>
79 class DynBinHeap : public BinHeap<T, Compare>,
80 public LocateFunctions<DynBinHeap<T, Compare>, T>,
81 public FunctionalMethods<DynBinHeap<T, Compare>, T>,
82 public GenericKeys<DynBinHeap<T, Compare>, T>,
83 public StlAlephIterator<DynBinHeap<T, Compare>>
84 {
85 public:
86
88
89 using Item_Type = T;
90
91 using Key_Type = T;
92
93 private:
94
96
98
99 // Takes the node p. What the comparison throws leaves p out of the
100 // heap, which is as it was, so p is freed
102 {
103 std::unique_ptr<Node> node(p);
105 return node.release()->get_key();
106 }
107
108 void copy(const DynBinHeap & src)
109 {
110 src.for_each_in_preorder([this] (Node * p)
111 {
112 __insert(new Node (p->get_key()));
113 });
114 }
115
116 public:
117
124 DynBinHeap(Compare & cmp) noexcept(std::is_nothrow_constructible_v<Base, Compare &>)
125 : Base(cmp) { /* empty */ }
126
128 DynBinHeap(Compare && cmp = Compare())
129 noexcept(std::is_nothrow_constructible_v<Base, Compare &>)
130 : BinHeap<T, Compare>(cmp)
131 { /* empty */ }
132
134 {
135 try
136 {
137 copy(h);
138 }
139 catch (...)
140 { // the destructor does not run for an object that is not built
141 empty();
142 throw;
143 }
144 }
145
147 {
148 this->swap(h);
149 }
150
152
154
156 {
157 if (this == &h)
158 return *this;
159
160 empty();
161
162 copy(h);
163
164 return *this;
165 }
166
168 {
169 this->swap(h);
170 return *this;
171 }
172
181 T & insert(const T & item)
182 {
183 return __insert(new Node (item));
184 }
185
186 T & insert(T && item)
187 {
188 return __insert(new Node (std::forward<T>(item)));
189 }
190
191 T & append(const T & item)
192 {
193 return __insert(new Node (item));
194 }
195
196 T & append(T && item)
197 {
198 return __insert(new Node (std::forward<T>(item)));
199 }
200
202 T & put(const T & item)
203 {
204 return insert(item);
205 }
206
207 T & put(T && item)
208 {
209 return insert(std::forward<T>(item));
210 }
211
220 {
221 // If Base::getMin() throws, the heap is as it was and there is no
222 // node to free
223 std::unique_ptr<Node> node(Base::getMin());
224 return std::move(node->get_key());
225 }
226
229 {
230 return getMin();
231 }
232
235 {
236 return getMin();
237 }
238
247 void update(T & data) noexcept(Base::nothrow_cmp)
248 {
249 Node * node = Node::key_to_node(data);
251 }
252
263 bool remove(T & data) noexcept(Base::nothrow_cmp)
264 {
265 if (this->is_empty())
266 return false;
267
268 // If Base::remove() throws, the node is still in the heap
269 delete Base::remove(Node::key_to_node(data));
270 return true;
271 }
272
274 bool erase(T & data) noexcept(Base::nothrow_cmp)
275 {
276 return remove(data);
277 }
278
280 const T & top() const
281 {
282 return BinHeap<T, Compare>::top()->get_key();
283 }
284
286 void empty() noexcept
287 {
288 this->remove_all_and_delete();
289 }
290
297 void clear() noexcept { empty(); }
298
301 {
302 empty();
303 }
304
305 template <class Operation>
307 bool traverse(Operation & op)
308 {
309 return this->preorder_traverse([&op] (Node * p)
310 { return op(p->get_key()); });
311 }
312
313 template <class Operation>
315 bool traverse(Operation && op = Operation())
316 {
317 return traverse(op);
318 }
319
320 template <class Operation>
322 bool traverse(Operation & op) const
323 {
324 return this->preorder_traverse([&op] (Node * p)
325 { return op(static_cast<const T &>(p->get_key())); });
326 }
327
328 template <class Operation>
330 bool traverse(Operation && op = Operation()) const
331 {
332 return traverse<Operation>(op);
333 }
334
335 struct Iterator : public Base::Iterator
336 {
337 using Item_Type = T;
339 Iterator() noexcept = default;
341 : Base::Iterator(h) {}
342 const T & get_curr_ne() const noexcept
343 {
344 return KEY(Base::Iterator::get_curr_ne());
345 }
346 const T & get_curr() const { return KEY(Base::Iterator::get_curr()); }
347 };
348 };
349
350} // end namespace Aleph
351
352# endif // TPL_DYNBINHEAP_H
353
354
355
Variadic constructor macros for containers.
#define Args_Ctor(Name, Type)
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Container traversal and functional operation mixins.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
@ KEY
Definition btreepic.C:169
long double h
Definition btreepic.C:154
Dynamic heap of elements of type T ordered by a comparison functor.
bool traverse(Operation &op) const
T get()
Alias for getMin().
bool traverse(Operation &&op=Operation()) const
bool traverse(Operation &op)
T getMin()
Remove the minimum element (according to Compare) and return it.
DynBinHeap(const DynBinHeap &h)
T & put(T &&item)
void clear() noexcept
Removes all elements from the heap.
~DynBinHeap()
Destructor.
T & insert(T &&item)
void copy(const DynBinHeap &src)
void empty() noexcept
Remove every element.
bool remove(T &data) noexcept(Base::nothrow_cmp)
Remove an arbitrary element belonging to the heap.
bool traverse(Operation &&op=Operation())
T & put(const T &item)
Synonym of insert().
T getMax()
See getMin(); to use if the order of priority is reversed.
typename BinHeap< T, Compare >::Node Node
void update(T &data) noexcept(Base::nothrow_cmp)
Adjust the position of an element after mutating its priority.
DynBinHeap(Compare &cmp) noexcept(std::is_nothrow_constructible_v< Base, Compare & >)
Builds an empty heap with the comparison cmp.
T & __insert(Node *p) noexcept(Base::nothrow_cmp)
T & append(T &&item)
DynBinHeap(DynBinHeap &&h)
DynBinHeap & operator=(const DynBinHeap &h)
bool erase(T &data) noexcept(Base::nothrow_cmp)
Alias for remove().
T & insert(const T &item)
Insert a copy of item into the heap.
const T & top() const
Return a reference to the smallest element.
DynBinHeap(Compare &&cmp=Compare()) noexcept(std::is_nothrow_constructible_v< Base, Compare & >)
See DynBinHeap(Compare & cmp).
T & append(const T &item)
bool preorder_traverse(Node *p, Operation op) const
Node * insert(Node *p) noexcept(nothrow_cmp)
Inserts a node into a heap.
bool is_empty() const noexcept
void update(Node *p) noexcept(nothrow_cmp)
Updates the priority of a node contained in the heap.
Node * remove(Node *node)
Removes node from the heap.
void remove_all_and_delete() noexcept
Deletes all the nodes of the heap, invokes the destructors of the removed nodes, and frees all the me...
void for_each_in_preorder(Operation &operation) const
Node * top()
Returns the node with the lowest priority according to the comparison criterion specified in the decl...
Node * getMin()
Removes the node with the lowest priority from the heap.
static constexpr bool nothrow_cmp
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
Common sequential searching methods on containers.
Definition ah-dry.H:555
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
f(args...) is a valid call whose result is usable as a condition.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
STL namespace.
Node heap without virtual destructor.
const T & get_curr_ne() const noexcept
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Generic list of items stored in a container.
Definition ah-dry.H:2896
Binary heap implementation using tree structure.
Utility functions for binary tree operations.