Aleph-w 7.10.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 <memory>
48
49# include <tpl_binHeap.H>
50# include <tpl_fibonacci_heap.H>
51# include <tpl_graph_utils.H>
52
53template <class GT,
54 class Distance,
55 class Access_Heap_Node>
57 : public BinHeap<typename GT::Arc *, Distance_Compare<GT, Distance>>
58{
59 Access_Heap_Node access_node;
60
61public:
65 Distance &get_distance() { return this->get_compare().dist; }
66
69 ArcHeap(Distance __dist = Distance(),
70 Access_Heap_Node acc = Access_Heap_Node())
72 {
73 // empty
74 }
75
77 ArcHeap(const ArcHeap &) = delete;
78 ArcHeap & operator=(const ArcHeap &) = delete;
79
83 ArcHeap(ArcHeap && other) noexcept
84 : Heap(other.get_compare()), access_node(std::move(other.access_node))
85 {
86 this->swap(other);
87 }
88
91 ArcHeap & operator=(ArcHeap && other) noexcept
92 {
93 if (this != &other)
94 {
95 empty();
96 this->swap(other);
97 access_node = std::move(other.access_node);
98 }
99 return *this;
100 }
101
102 typedef
104
105 typedef typename Heap::Node Node;
106
107 using handle_type = Node *;
108
109private:
110 // The handle that the access functor keeps for heap_node: that of the
111 // node of its arc whose handle it is
113 {
114 typename GT::Arc *arc = heap_node->get_key();
115 auto *p = static_cast<typename GT::Node *>(arc->src_node);
116 auto & handle_src = reinterpret_cast<handle_type &>(access_node(p));
117 if (handle_src == heap_node)
118 return handle_src;
119
120 p = static_cast<typename GT::Node *>(arc->tgt_node);
121 auto & handle_tgt = reinterpret_cast<handle_type &>(access_node(p));
122 assert(handle_tgt == heap_node);
123 return handle_tgt;
124 }
125
126public:
127
131 void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
132 {
133 auto & handle_storage = access_node(tgt);
134 handle_type & heap_node =
135 reinterpret_cast<handle_type &>(handle_storage);
136
137 if (heap_node == nullptr) // Is there already an arc inserted into the heap?
138 { // No ==> create a new heap node and assign the arc
139 std::unique_ptr<Node> node(new Node);
140 node->get_key() = arc;
141 this->insert(node.get()); // if it throws, the node is not in the heap
142 heap_node = node.release();
143
144 return;
145 }
146
147 // two arcs with the same target ==> keep the smaller; discard the larger
148 typename GT::Arc *& arc_in_heap = heap_node->get_key();
149
150 // Does arc_in_heap have a shorter distance than arc?
151 if (this->get_compare()(arc_in_heap, arc))
152 return; // old arc remains in the heap; new one is ignored
153
154 // arc_in_heap will be the newly inserted arc
155 typename GT::Arc *old_arc = arc_in_heap;
156 arc_in_heap = arc;
157 try
158 {
159 this->update(heap_node);
160 }
161 catch (...)
162 { // update() moved no node: with its arc back, the heap is as it was
163 arc_in_heap = old_arc;
164 throw;
165 }
166 }
167
171 typename GT::Arc * get_min_arc()
172 {
173 Node *top = this->top();
174 handle_type & handle = handle_of(top);
175
176 std::unique_ptr<Node> heap_node(this->getMin_ne()); // changes nothing if it throws
177 assert(heap_node.get() == top);
178 handle = nullptr;
179
180 return heap_node->get_key();
181 }
182
191 void empty()
192 {
193 this->for_each_in_preorder([this](Node *p) { handle_of(p) = nullptr; });
194 this->remove_all_and_delete();
195 }
196
202 void clear() { empty(); }
203
206 {
207 empty();
208 }
209};
210
211
212
213template <class GT,
214 class Distance,
215 class Access_Heap_Node>
217{
220
221public:
223
224private:
225 Access_Heap_Node access_node;
227
229 {
230 return reinterpret_cast<handle_type &>(access_node(p));
231 }
232
233 // The handle that the access functor keeps for heap_node: that of the
234 // node of its arc whose handle it is
236 {
237 auto *arc = heap_node->data;
238 auto & handle_src =
239 handle_ref(static_cast<typename GT::Node *>(arc->src_node));
240 if (handle_src == heap_node)
241 return handle_src;
242
243 auto & handle_tgt =
244 handle_ref(static_cast<typename GT::Node *>(arc->tgt_node));
245 assert(handle_tgt == heap_node);
246 return handle_tgt;
247 }
248
249 // Clears the handles of the heap nodes in the circular list of first and
250 // in the lists of their children. It goes down by child and up by parent,
251 // without recursion, since a tree can be as high as the heap is large
253 {
254 handle_type p = first;
255 while (p != nullptr)
256 {
257 handle_of(p) = nullptr;
258 if (p->child != nullptr)
259 {
260 p = p->child;
261 continue;
262 }
263
264 // p has no children: go to the next node of its list or, if p is
265 // the last one, to the next one of the list of its parent
266 while (p != nullptr)
267 {
268 handle_type head = p->parent == nullptr ? first : p->parent->child;
269 if (p->right != head)
270 {
271 p = p->right;
272 break;
273 }
274 p = p->parent;
275 }
276 }
277 }
278
279public:
284
288 Access_Heap_Node acc = Access_Heap_Node())
289 : access_node(acc), heap(Compare(__dist))
290 {
291 // empty
292 }
293
297
300 : access_node(std::move(other.access_node)), heap(std::move(other.heap))
301 {}
302
306 {
307 if (this != &other)
308 {
309 empty();
310 access_node = std::move(other.access_node);
311 heap = std::move(other.heap);
312 }
313 return *this;
314 }
315
319 void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
320 {
321 auto & heap_node = handle_ref(tgt);
322
323 if (heap_node == nullptr) // Is there already an arc inserted into the heap?
324 { // No ==> create a new heap node and assign the arc
325 heap_node = heap.insert(arc);
326 return;
327 }
328
329 // two arcs with the same target ==> keep the smaller; discard the larger
330 typename GT::Arc *arc_in_heap = heap_node->data;
331
332 // Does arc_in_heap have a shorter distance than arc?
333 if (heap.get_compare()(arc_in_heap, arc))
334 return; // old arc remains in the heap; new one is ignored
335
336 heap.decrease_key(heap_node, arc);
337 }
338
342 typename GT::Arc * get_min_arc()
343 {
344 ah_underflow_error_if(heap.is_empty()) << "ArcFibonacciHeap is empty";
345
346 auto *heap_node = heap.get_min_node();
347 auto *arc = heap_node->data;
348 handle_type & handle = handle_of(heap_node);
349
350 (void) heap.extract_min(); // if it throws, the node is in the heap
351 handle = nullptr;
352
353 return arc;
354 }
355
357 bool is_empty() const noexcept { return heap.is_empty(); }
358
367 void empty()
368 {
370 heap.clear();
371 }
372
378 void clear() { empty(); }
379
382 {
383 empty();
384 }
385};
386
387# endif // ARCHEAP_H
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
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.
Compare & get_compare() noexcept
Returns the comparison functor that orders the heap.
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.
Node * insert(Node *p) noexcept(nothrow_cmp)
Inserts a node into a heap.
Compare & get_compare() noexcept
void update(Node *p) noexcept(nothrow_cmp)
Updates the priority of a node contained in 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...
Node * getMin_ne() noexcept(nothrow_cmp)
Removes the node with the lowest priority from a heap that is not empty.
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...
ArcFibonacciHeap & operator=(const ArcFibonacciHeap &)=delete
void empty()
Remove all heap nodes and delete their memory, after clearing the handles that the access functor kee...
Definition archeap.H:367
Distance & get_distance()
Return the distance functor of the comparison that orders the arcs of this heap and chooses between t...
Definition archeap.H:283
handle_type & handle_of(handle_type heap_node)
Definition archeap.H:235
void clear()
Empties the container.
Definition archeap.H:378
typename Heap::handle_type handle_type
Definition archeap.H:222
handle_type & handle_ref(typename GT::Node *p)
Definition archeap.H:228
~ArcFibonacciHeap()
Destructor. Clears all remaining heap nodes, as empty() does.
Definition archeap.H:381
bool is_empty() const noexcept
Return true if the heap is empty.
Definition archeap.H:357
ArcFibonacciHeap(const ArcFibonacciHeap &)=delete
Copying is disabled (heap manages internal node memory)
ArcFibonacciHeap(ArcFibonacciHeap &&other) noexcept
Move construction transfers ownership of heap nodes and the distance.
Definition archeap.H:299
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:287
ArcFibonacciHeap & operator=(ArcFibonacciHeap &&other) noexcept
Move assignment transfers ownership of heap nodes and the distance, after emptying this heap as empty...
Definition archeap.H:305
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:319
void clear_handles(handle_type first)
Definition archeap.H:252
Access_Heap_Node access_node
Definition archeap.H:225
GT::Arc * get_min_arc()
Extract the arc with minimum distance from the heap and clear its node-to-heap mapping.
Definition archeap.H:342
ArcHeap & operator=(ArcHeap &&other) noexcept
Move assignment transfers ownership of heap nodes and the distance, after emptying this heap as empty...
Definition archeap.H:91
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:69
ArcHeap(const ArcHeap &)=delete
Copying is disabled (heap manages internal node memory)
Distance & get_distance()
Return the distance functor of the comparison that orders the arcs of this heap and chooses between t...
Definition archeap.H:65
ArcHeap(ArcHeap &&other) noexcept
Move construction transfers ownership of heap nodes and the distance.
Definition archeap.H:83
handle_type & handle_of(Node *heap_node)
Definition archeap.H:112
Access_Heap_Node access_node
Definition archeap.H:59
void clear()
Empties the container.
Definition archeap.H:202
~ArcHeap()
Destructor. Clears all remaining heap nodes, as empty() does.
Definition archeap.H:205
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:131
ArcHeap & operator=(const ArcHeap &)=delete
BinHeap< typename GT::Arc *, Distance_Compare< GT, Distance > > Heap
Definition archeap.H:103
Heap::Node Node
Definition archeap.H:105
void empty()
Remove all heap nodes and delete their memory, after clearing the handles that the access functor kee...
Definition archeap.H:191
GT::Arc * get_min_arc()
Extract the arc with minimum distance from the heap and clear its node-to-heap mapping.
Definition archeap.H:171
void * tgt_node
Please don't use.
Definition graph-dry.H:793
void * src_node
Definition graph-dry.H:792
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
Node heap without virtual destructor.
Comparison functor for arc weights/distances.
Distance accessor.
Binary heap implementation using tree structure.
Fibonacci Heap implementation.
Utility algorithms and operations for graphs.