Aleph-w 3.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 <ah-concepts.H>
54# include <ahDry.H>
55# include <ah-args-ctor.H>
56# include <htlist.H>
57# include <ah-dry.H>
58# include <tpl_binNodeUtils.H>
59# include <tpl_binHeap.H>
60
61using namespace Aleph;
62
63namespace Aleph {
64
74 template <class T, class Compare = Aleph::less<T>>
76 class DynBinHeap : public BinHeap<T, Compare>,
77 public LocateFunctions<DynBinHeap<T, Compare>, T>,
78 public FunctionalMethods<DynBinHeap<T, Compare>, T>,
79 public GenericKeys<DynBinHeap<T, Compare>, T>,
80 public EqualToMethod<DynBinHeap<T, Compare>>,
81 public StlAlephIterator<DynBinHeap<T, Compare>>
82 {
83 public:
84
86
87 using Item_Type = T;
88
89 using Key_Type = T;
90
91 private:
92
94
96
97 T & __insert(Node * p) noexcept
98 {
99 return BinHeap<T, Compare>::insert(p)->get_key();
100 }
101
102 void copy(const DynBinHeap & src)
103 {
104 src.for_each_in_preorder([this] (Node * p)
105 {
106 __insert(new Node (p->get_key()));
107 });
108 }
109
110 public:
111
112 DynBinHeap(Compare & cmp) noexcept : Base(cmp) { /* empty */ }
113
114 DynBinHeap(Compare && cmp = Compare()) noexcept : BinHeap<T, Compare>(cmp)
115 { /* empty */ }
116
118 {
119 copy(h);
120 }
121
123 {
124 this->swap(h);
125 }
126
128
130
132 {
133 if (this == &h)
134 return *this;
135
136 empty();
137
138 copy(h);
139
140 return *this;
141 }
142
144 {
145 this->swap(h);
146 return *this;
147 }
148
155 T & insert(const T & item)
156 {
157 return __insert(new Node (item));
158 }
159
160 T & insert(T && item)
161 {
162 return __insert(new Node (std::forward<T>(item)));
163 }
164
165 T & append(const T & item)
166 {
167 return __insert(new Node (item));
168 }
169
170 T & append(T && item)
171 {
172 return __insert(new Node (std::forward<T>(item)));
173 }
174
176 T & put(const T & item)
177 {
178 return insert(item);
179 }
180
181 T & put(T && item)
182 {
183 return insert(std::forward<T>(item));
184 }
185
192 {
194
195 T return_value = std::move(node->get_key());
196
197 delete node;
198
199 return return_value;
200 }
201
204 {
205 return getMin();
206 }
207
210 {
211 return getMin();
212 }
213
219 void update(T & data) noexcept
220 {
221 Node * node = Node::key_to_node(data);
223 }
224
233 bool remove(T & data) noexcept
234 {
235 if (this->is_empty())
236 return false;
237
240 if (removed == nullptr)
241 return false;
242
243 delete removed;
244 return true;
245 }
246
248 bool erase(T & data) noexcept
249 {
250 return remove(data);
251 }
252
254 T & top() const
255 {
256 return BinHeap<T, Compare>::top()->get_key();
257 }
258
261 {
262 this->remove_all_and_delete();
263 }
264
271 void clear() noexcept { empty(); }
272
275 {
276 empty();
277 }
278
279 template <class Operation>
281 {
282 return this->preorder_traverse([&op] (Node * p)
283 { return op(p->get_key()); });
284 }
285
286 template <class Operation>
288 {
289 return traverse(op);
290 }
291
292 template <class Operation>
293 bool traverse(Operation & op) const
294 {
295 return this->preorder_traverse([&op] (Node * p)
296 { return op(p->get_key()); });
297 }
298
299 template <class Operation>
300 bool traverse(Operation && op = Operation()) const
301 {
302 return traverse<Operation>(op);
303 }
304
305 struct Iterator : public Base::Iterator
306 {
307 using Item_Type = T;
313 {
314 return KEY(Base::Iterator::get_curr_ne());
315 }
316 const T & get_curr() const { return KEY(Base::Iterator::get_curr()); }
317 };
318 };
319
320} // end namespace Aleph
321
322# endif // TPL_DYNBINHEAP_H
323
324
325
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:113
@ KEY
Definition btreepic.C:169
long double h
Definition btreepic.C:154
static BinHeapNode * key_to_node(Key &__key) noexcept
Dynamic heap of elements of type T ordered by a comparison functor.
T & top() const
Return a reference to the smallest element.
T get()
Alias for getMin().
DynBinHeap(const DynBinHeap &h)
bool traverse(Operation &&op=Operation())
bool erase(T &data) noexcept
Alias for remove().
T & put(T &&item)
DynBinHeap(Compare &cmp) noexcept
bool traverse(Operation &&op=Operation()) const
void clear() noexcept
Removes all elements from the heap.
~DynBinHeap()
Destructor.
T & insert(T &&item)
bool traverse(Operation &op) const
T getMin()
Remove the minimum element (according to Compare) and return it.
void copy(const DynBinHeap &src)
void update(T &data) noexcept
Adjust the position of an element after mutating its priority.
void empty() noexcept
Remove every element.
bool remove(T &data) noexcept
Remove an arbitrary element belonging to the heap.
T & put(const T &item)
Synonym of insert().
T & __insert(Node *p) noexcept
typename BinHeap< T, Compare >::Node Node
T & append(T &&item)
DynBinHeap(DynBinHeap &&h)
BinHeap< T, Compare > Base
bool traverse(Operation &op)
DynBinHeap & operator=(const DynBinHeap &h)
T & insert(const T &item)
Insert a copy of item into the heap.
DynBinHeap(Compare &&cmp=Compare()) noexcept
T & append(const T &item)
bool preorder_traverse(Node *p, Operation op) const
void update(Node *p) noexcept
Updates the priority of a node contained in the heap.
bool is_empty() const noexcept
Node * getMin()
Removes the node with the lowest priority from 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 * insert(Node *p) noexcept
Inserts a node into a heap.
Node * top()
Returns the node with the lowest priority according to the comparison criterion specified in the decl...
Equality test for containers.
Definition ah-dry.H:1959
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:658
Common sequential searching methods on containers.
Definition ah-dry.H:200
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:121
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
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:1846
Binary heap implementation using tree structure.
Utility functions for binary tree operations.