Aleph-w  1.9
General library for algorithms and data structures
archeap.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef ARCHEAP_H
28 # define ARCHEAP_H
29 
30 # include <tpl_binHeap.H>
31 # include <tpl_graph_utils.H>
32 
33  template <class GT,
34  class Distance,
35  class Access_Heap_Node>
36 class ArcHeap
37  : public BinHeap<typename GT::Arc*, Distance_Compare<GT, Distance>>
38 {
39  Distance dist;
40  Access_Heap_Node & access_node;
42 
43 public:
44 
45  Distance & get_distance() { return dist; }
46 
47  ArcHeap(Distance __dist = Distance(),
48  Access_Heap_Node && acc = Access_Heap_Node())
49  : dist(__dist), access_node(acc), dist_cmp(dist)
50  {
51  // empty
52  }
53 
54  typedef
56 
57  typedef typename Heap::Node Node;
58 
59  void put_arc(typename GT::Arc * arc, typename GT::Node * tgt)
60  {
61  Node *& heap_node = access_node(tgt);
62 
63  if (heap_node == nullptr) // ¿existe arco insertado en el heap?
64  { // No ==> crear nuevo nodo para el heap y asignarle arco
65  assert(heap_node == nullptr); // para ser consistente
66 
67  heap_node = new Node;
68  heap_node->get_key() = arc;
69  this->insert(heap_node);
70 
71  return;
72  }
73 
74  // dos arcos con igual destino ==> tomar menor; descartar mayor
75  typename GT::Arc *& arc_in_heap = heap_node->get_key();
76 
77  // ¿arc_in_heap tiene distancia menor que arc?
78  if (dist_cmp(arc_in_heap, arc))
79  return; // antiguo arco permanece en el heap; nuevo se ignora
80 
81  // arc_in_heap será el arco recién insertado arc
82  arc_in_heap = arc; // cambia el arco
83  this->update(heap_node); // actualiza el heap con el nuevo peso de arc
84  }
85 
86  typename GT::Arc * get_min_arc()
87  {
88  Node * heap_node = this->getMin();
89  typename GT::Arc * arc = heap_node->get_key();
90 
91  // seleccionar nodo que retorne la clase de acceso
92  typename GT::Node * p = (typename GT::Node*) arc->src_node;
93  if (access_node(p) != heap_node)
94  p = (typename GT::Node*) arc->tgt_node;
95 
96  assert(access_node(p) == heap_node);
97 
98  access_node(p) = nullptr;
99 
100  delete heap_node;
101 
102  return arc;
103  }
104 
105  void empty()
106  {
107  this->remove_all_and_delete();
108  }
109 
110  ~ArcHeap()
111  {
112  empty();
113  }
114 };
115 
116 # endif // ARCHEAP_H
117 
Node * insert(Node *p) noexcept
Definition: tpl_binHeap.H:587
Definition: tpl_graph_utils.H:1618
void update(Node *p) noexcept
Definition: tpl_binHeap.H:686
Node * getMin()
Definition: tpl_binHeap.H:663
Definition: tpl_binHeap.H:940
BinHeapNode< Key > Node
El tipo de nodo del heap.
Definition: tpl_binHeap.H:943
void remove_all_and_delete() noexcept
Definition: tpl_binHeap.H:730

Leandro Rabindranath León