Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
archeap.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
44# ifndef ARCHEAP_H
45# define ARCHEAP_H
46
47# include <tpl_binHeap.H>
48# include <tpl_fibonacci_heap.H>
49# include <tpl_graph_utils.H>
50
51template <class GT,
52 class Distance,
53 class Access_Heap_Node>
55 : public BinHeap<typename GT::Arc *, Distance_Compare<GT, Distance>>
56{
60
61public:
63 Distance &get_distance() { return dist; }
64
73
75 ArcHeap(const ArcHeap &) = delete;
76 ArcHeap & operator=(const ArcHeap &) = delete;
77
80 : Heap(std::move(other)), dist(std::move(other.dist)),
81 access_node(std::move(other.access_node)), dist_cmp(dist)
82 {}
83
86 {
87 if (this != &other)
88 {
89 empty();
90 Heap::operator=(std::move(other));
91 dist = std::move(other.dist);
92 access_node = std::move(other.access_node);
94 }
95 return *this;
96 }
97
98 typedef
100
101 typedef typename Heap::Node Node;
102
103 using handle_type = Node *;
104
107 void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
108 {
109 auto & handle_storage = access_node(tgt);
110 handle_type & heap_node =
111 reinterpret_cast<handle_type &>(handle_storage);
112
113 if (heap_node == nullptr) // Is there already an arc inserted into the heap?
114 { // No ==> create a new heap node and assign the arc
115 heap_node = new Node;
116 heap_node->get_key() = arc;
117 this->insert(heap_node);
118
119 return;
120 }
121
122 // two arcs with the same target ==> keep the smaller; discard the larger
123 typename GT::Arc *& arc_in_heap = heap_node->get_key();
124
125 // Does arc_in_heap have a shorter distance than arc?
126 if (dist_cmp(arc_in_heap, arc))
127 return; // old arc remains in the heap; new one is ignored
128
129 // arc_in_heap will be the newly inserted arc
130 arc_in_heap = arc;
131 this->update(heap_node);
132 }
133
136 typename GT::Arc * get_min_arc()
137 {
138 Node *heap_node = this->getMin();
139 typename GT::Arc *arc = heap_node->get_key();
140
141 // Select the node that the access functor maps to this heap node
142 auto *p = static_cast<typename GT::Node *>(arc->src_node);
143 auto & handle_src =
144 reinterpret_cast<handle_type &>(access_node(p));
145 if (handle_src != heap_node)
146 p = static_cast<typename GT::Node *>(arc->tgt_node);
147
148 auto & handle_ref =
149 reinterpret_cast<handle_type &>(access_node(p));
150 assert(handle_ref == heap_node);
151
152 handle_ref = nullptr;
153
154 delete heap_node;
155
156 return arc;
157 }
158
160 void empty()
161 {
162 this->remove_all_and_delete();
163 }
164
167 {
168 empty();
169 }
170};
171
172
173
174template <class GT,
175 class Distance,
176 class Access_Heap_Node>
178{
181
182public:
184
185private:
190
192 {
193 return reinterpret_cast<handle_type &>(access_node(p));
194 }
195
196public:
199
208
212
215 : dist(std::move(other.dist)), access_node(std::move(other.access_node)),
216 dist_cmp(dist), heap(std::move(other.heap))
217 {}
218
221 {
222 if (this != &other)
223 {
224 dist = std::move(other.dist);
225 access_node = std::move(other.access_node);
227 heap = std::move(other.heap);
228 }
229 return *this;
230 }
231
234 void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
235 {
236 auto & heap_node = handle_ref(tgt);
237
238 if (heap_node == nullptr) // Is there already an arc inserted into the heap?
239 { // No ==> create a new heap node and assign the arc
240 heap_node = heap.insert(arc);
241 return;
242 }
243
244 // two arcs with the same target ==> keep the smaller; discard the larger
245 typename GT::Arc *arc_in_heap = heap_node->data;
246
247 // Does arc_in_heap have a shorter distance than arc?
248 if (dist_cmp(arc_in_heap, arc))
249 return; // old arc remains in the heap; new one is ignored
250
251 heap.decrease_key(heap_node, arc);
252 }
253
256 typename GT::Arc * get_min_arc()
257 {
258 ah_underflow_error_if(heap.is_empty()) << "ArcFibonacciHeap is empty";
259
260 auto *heap_node = heap.get_min_node();
261 auto *arc = heap_node->data;
262
263 // Select the node that the access functor maps to this heap node
264 auto *p = static_cast<typename GT::Node *>(arc->src_node);
265 auto & handle_src = handle_ref(p);
266 if (handle_src != heap_node)
267 p = static_cast<typename GT::Node *>(arc->tgt_node);
268
269 auto & handle_entry = handle_ref(p);
270 assert(handle_entry == heap_node);
271
272 handle_entry = nullptr;
273
275
276 return arc;
277 }
278
280 bool is_empty() const noexcept { return heap.is_empty(); }
281
283 void empty()
284 {
285 heap.clear();
286 }
287
290 {
291 empty();
292 }
293};
294
295# endif // ARCHEAP_H
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:368
Key & get_key() noexcept
Node * get_min_node() const noexcept
Returns a pointer to the minimum node.
void clear() noexcept(std::is_nothrow_destructible_v< T >)
Removes all elements from the heap.
Node * insert(const T &val)
Inserts a new element (copy).
T extract_min()
Extracts and returns the minimum element.
void decrease_key(Node *x, const T &k)
Decreases the key of a node.
bool is_empty() const noexcept
Checks if the heap is empty.
void update(Node *p) noexcept
Actualiza prioridad de un nodo contenido en el heap.
Node * getMin()
Elimina del heap el nodo de menor prioridad.
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...
Node * insert(Node *p) noexcept
Inserta un nodo en un heap.
ArcFibonacciHeap & operator=(const ArcFibonacciHeap &)=delete
void empty()
Remove all heap nodes and delete their memory.
Definition archeap.H:283
Distance & get_distance()
Return the distance functor used to compare arcs in this heap.
Definition archeap.H:198
Compare dist_cmp
Definition archeap.H:188
typename Heap::handle_type handle_type
Definition archeap.H:183
Distance dist
Definition archeap.H:186
handle_type & handle_ref(typename GT::Node *p)
Definition archeap.H:191
~ArcFibonacciHeap()
Destructor. Clears all remaining heap nodes.
Definition archeap.H:289
bool is_empty() const noexcept
Return true if the heap is empty.
Definition archeap.H:280
ArcFibonacciHeap(const ArcFibonacciHeap &)=delete
Copying is disabled (heap manages internal node memory)
ArcFibonacciHeap(ArcFibonacciHeap &&other) noexcept
Move construction transfers ownership of heap nodes.
Definition archeap.H:214
ArcFibonacciHeap(Distance __dist=Distance(), Access_Heap_Node acc=Access_Heap_Node())
Construct an empty Fibonacci arc heap with the given distance functor and heap-node access policy.
Definition archeap.H:202
ArcFibonacciHeap & operator=(ArcFibonacciHeap &&other) noexcept
Move assignment transfers ownership of heap nodes.
Definition archeap.H:220
void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
Insert or update an arc associated with a target node, keeping the smallest-distance arc per node in ...
Definition archeap.H:234
Access_Heap_Node access_node
Definition archeap.H:187
Distance_Compare< GT, Distance > Compare
Definition archeap.H:179
GT::Arc * get_min_arc()
Extract the arc with minimum distance from the heap and clear its node-to-heap mapping.
Definition archeap.H:256
ArcHeap & operator=(ArcHeap &&other) noexcept
Move assignment transfers ownership of heap nodes.
Definition archeap.H:85
ArcHeap(Distance __dist=Distance(), Access_Heap_Node acc=Access_Heap_Node())
Construct an empty arc heap with the given distance functor and heap-node access policy.
Definition archeap.H:67
ArcHeap(const ArcHeap &)=delete
Copying is disabled (heap manages internal node memory)
Distance & get_distance()
Return the distance functor used to compare arcs in this heap.
Definition archeap.H:63
Distance dist
Definition archeap.H:57
ArcHeap(ArcHeap &&other) noexcept
Move construction transfers ownership of heap nodes.
Definition archeap.H:79
Access_Heap_Node access_node
Definition archeap.H:58
~ArcHeap()
Destructor. Clears all remaining heap nodes.
Definition archeap.H:166
void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
Insert or update an arc associated with a target node, keeping the smallest-distance arc per node in ...
Definition archeap.H:107
ArcHeap & operator=(const ArcHeap &)=delete
BinHeap< typename GT::Arc *, Distance_Compare< GT, Distance > > Heap
Definition archeap.H:99
Heap::Node Node
Definition archeap.H:101
Distance_Compare< GT, Distance > dist_cmp
Definition archeap.H:59
void empty()
Remove all heap nodes and delete their memory.
Definition archeap.H:160
GT::Arc * get_min_arc()
Extract the arc with minimum distance from the heap and clear its node-to-heap mapping.
Definition archeap.H:136
void * tgt_node
Please don't use.
Definition graph-dry.H:534
void * src_node
Definition graph-dry.H:533
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
DynList< T > maps(const C &c, Op op)
Classic map operation.
Node heap without virtual destructor.
Comparison functor for arc weights/distances.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Distance accessor.
Binary heap implementation using tree structure.
Fibonacci Heap implementation.
Utility algorithms and operations for graphs.