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
170 void clear() { empty(); }
171
174 {
175 empty();
176 }
177};
178
179
180
181template <class GT,
182 class Distance,
183 class Access_Heap_Node>
185{
188
189public:
191
192private:
197
199 {
200 return reinterpret_cast<handle_type &>(access_node(p));
201 }
202
203public:
206
215
219
222 : dist(std::move(other.dist)), access_node(std::move(other.access_node)),
223 dist_cmp(dist), heap(std::move(other.heap))
224 {}
225
228 {
229 if (this != &other)
230 {
231 dist = std::move(other.dist);
232 access_node = std::move(other.access_node);
234 heap = std::move(other.heap);
235 }
236 return *this;
237 }
238
241 void put_arc(typename GT::Arc *arc, typename GT::Node *tgt)
242 {
243 auto & heap_node = handle_ref(tgt);
244
245 if (heap_node == nullptr) // Is there already an arc inserted into the heap?
246 { // No ==> create a new heap node and assign the arc
247 heap_node = heap.insert(arc);
248 return;
249 }
250
251 // two arcs with the same target ==> keep the smaller; discard the larger
252 typename GT::Arc *arc_in_heap = heap_node->data;
253
254 // Does arc_in_heap have a shorter distance than arc?
255 if (dist_cmp(arc_in_heap, arc))
256 return; // old arc remains in the heap; new one is ignored
257
258 heap.decrease_key(heap_node, arc);
259 }
260
263 typename GT::Arc * get_min_arc()
264 {
265 ah_underflow_error_if(heap.is_empty()) << "ArcFibonacciHeap is empty";
266
267 auto *heap_node = heap.get_min_node();
268 auto *arc = heap_node->data;
269
270 // Select the node that the access functor maps to this heap node
271 auto *p = static_cast<typename GT::Node *>(arc->src_node);
272 auto & handle_src = handle_ref(p);
273 if (handle_src != heap_node)
274 p = static_cast<typename GT::Node *>(arc->tgt_node);
275
276 auto & handle_entry = handle_ref(p);
277 assert(handle_entry == heap_node);
278
279 handle_entry = nullptr;
280
282
283 return arc;
284 }
285
287 bool is_empty() const noexcept { return heap.is_empty(); }
288
290 void empty()
291 {
292 heap.clear();
293 }
294
300 void clear() { empty(); }
301
304 {
305 empty();
306 }
307};
308
309# endif // ARCHEAP_H
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:368
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.
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:290
Distance & get_distance()
Return the distance functor used to compare arcs in this heap.
Definition archeap.H:205
Compare dist_cmp
Definition archeap.H:195
void clear()
Empties the container.
Definition archeap.H:300
typename Heap::handle_type handle_type
Definition archeap.H:190
Distance dist
Definition archeap.H:193
handle_type & handle_ref(typename GT::Node *p)
Definition archeap.H:198
~ArcFibonacciHeap()
Destructor. Clears all remaining heap nodes.
Definition archeap.H:303
bool is_empty() const noexcept
Return true if the heap is empty.
Definition archeap.H:287
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:221
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:209
ArcFibonacciHeap & operator=(ArcFibonacciHeap &&other) noexcept
Move assignment transfers ownership of heap nodes.
Definition archeap.H:227
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:241
Access_Heap_Node access_node
Definition archeap.H:194
Distance_Compare< GT, Distance > Compare
Definition archeap.H:186
GT::Arc * get_min_arc()
Extract the arc with minimum distance from the heap and clear its node-to-heap mapping.
Definition archeap.H:263
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
void clear()
Empties the container.
Definition archeap.H:170
~ArcHeap()
Destructor. Clears all remaining heap nodes.
Definition archeap.H:173
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
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.