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 <ahDry.H>
54# include <ah-args-ctor.H>
55# include <htlist.H>
56# include <ah-dry.H>
57# include <tpl_binNodeUtils.H>
58# include <tpl_binHeap.H>
59
60using namespace Aleph;
61
62namespace Aleph {
63
73 template <class T, class Compare = Aleph::less<T>>
74 class DynBinHeap : public BinHeap<T, Compare>,
75 public LocateFunctions<DynBinHeap<T, Compare>, T>,
76 public FunctionalMethods<DynBinHeap<T, Compare>, T>,
77 public GenericKeys<DynBinHeap<T, Compare>, T>,
78 public EqualToMethod<DynBinHeap<T, Compare>>,
79 public StlAlephIterator<DynBinHeap<T, Compare>>
80 {
81 public:
82
84
85 using Item_Type = T;
86
87 using Key_Type = T;
88
89 private:
90
92
94
95 T & __insert(Node * p) noexcept
96 {
97 return BinHeap<T, Compare>::insert(p)->get_key();
98 }
99
100 void copy(const DynBinHeap & src)
101 {
102 src.for_each_in_preorder([this] (Node * p)
103 {
104 __insert(new Node (p->get_key()));
105 });
106 }
107
108 public:
109
110 DynBinHeap(Compare & cmp) noexcept : Base(cmp) { /* empty */ }
111
112 DynBinHeap(Compare && cmp = Compare()) noexcept : BinHeap<T, Compare>(cmp)
113 { /* empty */ }
114
116 {
117 copy(h);
118 }
119
121 {
122 this->swap(h);
123 }
124
126
128
130 {
131 if (this == &h)
132 return *this;
133
134 empty();
135
136 copy(h);
137
138 return *this;
139 }
140
142 {
143 this->swap(h);
144 return *this;
145 }
146
153 T & insert(const T & item)
154 {
155 return __insert(new Node (item));
156 }
157
158 T & insert(T && item)
159 {
160 return __insert(new Node (std::forward<T>(item)));
161 }
162
163 T & append(const T & item)
164 {
165 return __insert(new Node (item));
166 }
167
168 T & append(T && item)
169 {
170 return __insert(new Node (std::forward<T>(item)));
171 }
172
174 T & put(const T & item)
175 {
176 return insert(item);
177 }
178
179 T & put(T && item)
180 {
181 return insert(std::forward<T>(item));
182 }
183
190 {
192
193 T return_value = std::move(node->get_key());
194
195 delete node;
196
197 return return_value;
198 }
199
202 {
203 return getMin();
204 }
205
208 {
209 return getMin();
210 }
211
217 void update(T & data) noexcept
218 {
219 Node * node = Node::key_to_node(data);
221 }
222
231 bool remove(T & data) noexcept
232 {
233 if (this->is_empty())
234 return false;
235
238 if (removed == nullptr)
239 return false;
240
241 delete removed;
242 return true;
243 }
244
246 bool erase(T & data) noexcept
247 {
248 return remove(data);
249 }
250
252 T & top() const
253 {
254 return BinHeap<T, Compare>::top()->get_key();
255 }
256
259 {
260 this->remove_all_and_delete();
261 }
262
265 {
266 empty();
267 }
268
269 template <class Operation>
271 {
272 return this->preorder_traverse([&op] (Node * p)
273 { return op(p->get_key()); });
274 }
275
276 template <class Operation>
278 {
279 return traverse(op);
280 }
281
282 template <class Operation>
283 bool traverse(Operation & op) const
284 {
285 return this->preorder_traverse([&op] (Node * p)
286 { return op(p->get_key()); });
287 }
288
289 template <class Operation>
290 bool traverse(Operation && op = Operation()) const
291 {
292 return traverse<Operation>(op);
293 }
294
295 struct Iterator : public Base::Iterator
296 {
297 using Item_Type = T;
303 {
304 return KEY(Base::Iterator::get_curr_ne());
305 }
306 const T & get_curr() const { return KEY(Base::Iterator::get_curr()); }
307 };
308 };
309
310} // end namespace Aleph
311
312# endif // TPL_DYNBINHEAP_H
313
314
315
Variadic constructor macros for containers.
#define Args_Ctor(Name, Type)
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
~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
Actualiza prioridad de un nodo contenido en el heap.
bool is_empty() const noexcept
Node * getMin()
Elimina del heap el nodo de menor prioridad.
Node * remove(Node *node)
Elimina del heap el nodo node.
void remove_all_and_delete() noexcept
Borra todos los nodos del heap, invoca a los destructores de los nodos eliminados y libera toda la me...
void for_each_in_preorder(Operation &operation) const
Node * insert(Node *p) noexcept
Inserta un nodo en un heap.
Node * top()
Retorna el nodo con menor prioridad según el criterio de comparación especificado en la declaración.
Equality test for containers.
Definition ah-dry.H:1534
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:548
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
Common sequential searching methods on containers.
Definition ah-dry.H:164
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
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:107
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:1501
Binary heap implementation using tree structure.
Utility functions for binary tree operations.