Aleph-w  1.9
General library for algorithms and data structures
tpl_dynBinHeap.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 TPL_DYNBINHEAP_H
28 # define TPL_DYNBINHEAP_H
29 
30 # include <ahDry.H>
31 # include <ah-args-ctor.H>
32 # include <htlist.H>
33 # include <ah-dry.H>
34 # include <tpl_binNodeUtils.H>
35 # include <tpl_binHeap.H>
36 
37 using namespace Aleph;
38 
39 namespace Aleph {
40 
54  template <class T, class Compare = Aleph::less<T>>
55  class DynBinHeap : public BinHeap<T, Compare>,
56  public LocateFunctions<DynBinHeap<T, Compare>, T>,
57  public FunctionalMethods<DynBinHeap<T, Compare>, T>,
58  public GenericKeys<DynBinHeap<T, Compare>, T>,
59  public EqualToMethod<DynBinHeap<T, Compare>>,
60  public StlAlephIterator<DynBinHeap<T, Compare>>
61  {
62  public:
63 
66 
68  using Item_Type = T;
69 
70  using Key_Type = T;
71 
72  private:
73 
74  using Base = BinHeap<T, Compare>;
75 
76  using Node = typename BinHeap<T, Compare>::Node;
77 
78  T & __insert(Node * p) noexcept
79  {
80  return BinHeap<T, Compare>::insert(p)->get_key();
81  }
82 
83  void copy(const DynBinHeap & src)
84  {
85  src.for_each_in_preorder([this] (Node * p)
86  {
87  __insert(new Node (p->get_key()));
88  });
89  }
90 
91  public:
92 
93  DynBinHeap(Compare & cmp) noexcept : Base(cmp) { /* empty */ }
94 
95  DynBinHeap(Compare && cmp = Compare()) noexcept : BinHeap<T, Compare>(cmp)
96  { /* empty */ }
97 
98  DynBinHeap(const DynBinHeap & h) : Base(h.cmp)
99  {
100  copy(h);
101  }
102 
103  DynBinHeap(DynBinHeap && h) : Base(h.cmp)
104  {
105  this->swap(h);
106  }
107 
108  Special_Ctors(DynBinHeap, T);
109 
110  Args_Ctor(DynBinHeap, T);
111 
112  DynBinHeap & operator = (const DynBinHeap & h)
113  {
114  if (this == &h)
115  return *this;
116 
117  empty();
118 
119  copy(h);
120 
121  return *this;
122  }
123 
124  DynBinHeap & operator = (DynBinHeap && h) noexcept
125  {
126  this->swap(h);
127  return *this;
128  }
129 
139  T & insert(const T & item)
140  {
141  return __insert(new Node (item));
142  }
143 
144  T & insert(T && item)
145  {
146  return __insert(new Node (std::forward<T>(item)));
147  }
148 
149  T & append(const T & item)
150  {
151  return __insert(new Node (item));
152  }
153 
154  T & append(T && item)
155  {
156  return __insert(new Node (std::forward<T>(item)));
157  }
158 
168  T & put(const T & item)
169  {
170  return insert(item);
171  }
172 
173  T & put(T && item)
174  {
175  return insert(std::forward<T>(item));
176  }
177 
184  T getMin()
185  {
186  Node * node = BinHeap<T, Compare>::getMin();
187 
188  T return_value = std::move(node->get_key());
189 
190  delete node;
191 
192  return return_value;
193  }
194 
196  T getMax()
197  {
198  return getMin();
199  }
200 
207  T get()
208  {
209  return getMin();
210  }
211 
225  void update(T & data) noexcept
226  {
227  Node * node = Node::key_to_node(data);
229  }
230 
246  void remove(T & data) noexcept
247  {
248  Node * node = BinHeap<T, Compare>::Node::key_to_node(data);
250  }
251 
253  void erase(T & data) noexcept
254  {
255  remove(data);
256  }
257 
261  T & top() const
262  {
263  return BinHeap<T, Compare>::top()->get_key();
264  }
265 
267  void empty() noexcept
268  {
269  this->remove_all_and_delete();
270  }
271 
274  {
275  empty();
276  }
277 
278  template <class Operation>
279  bool traverse(Operation & op) noexcept(noexcept(op))
280  {
281  return this->preorder_traverse([&op] (Node * p)
282  { return op(p->get_key()); });
283  }
284 
285  template <class Operation>
286  bool traverse(Operation && op = Operation()) noexcept(noexcept(op))
287  {
288  return traverse(op);
289  }
290 
291  template <class Operation>
292  bool traverse(Operation & op) const noexcept(noexcept(op))
293  {
294  return this->preorder_traverse([&op] (Node * p)
295  { return op(p->get_key()); });
296  }
297 
298  template <class Operation>
299  bool traverse(Operation && op = Operation()) const noexcept(noexcept(op))
300  {
301  return traverse<Operation>(op);
302  }
303 
304  struct Iterator : public Base::Iterator
305  {
306  using Item_Type = T;
307  using Base = typename Base::Iterator;
308  Iterator(const DynBinHeap & h) : Base(h) {}
309  const T & get_curr_ne() const noexcept
310  {
311  return KEY(Base::get_curr_ne());
312  }
313  const T & get_curr() const { return KEY(Base::get_curr()); }
314  };
315  };
316 
317 } // end namespace Aleph
318 
319 # endif // TPL_DYNBINHEAP_H
320 
321 
322 
Definition: htlist.H:450
Definition: htlist.H:1290
T & put(const T &item)
Definition: tpl_dynBinHeap.H:168
Definition: htlist.H:133
T & insert(const T &item)
Definition: tpl_dynBinHeap.H:139
T getMin()
Definition: tpl_dynBinHeap.H:184
void update(T &data) noexcept
Definition: tpl_dynBinHeap.H:225
Node * insert(Node *p) noexcept
Definition: tpl_binHeap.H:587
Node * top()
Definition: tpl_binHeap.H:754
T getMax()
Definition: tpl_dynBinHeap.H:196
~DynBinHeap()
Destructor.
Definition: tpl_dynBinHeap.H:273
T Item_Type
El tipo de elemento que retorna get_curr().
Definition: tpl_dynBinHeap.H:68
Node::Key_Type & KEY(Node *p) noexcept
Definition: tpl_binNode.H:318
Definition: tpl_dynBinHeap.H:55
void erase(T &data) noexcept
Sinónimo de remove.
Definition: tpl_dynBinHeap.H:253
Definition: ah-comb.H:35
void empty() noexcept
Vacía todos los elementos del heap dinámico.
Definition: tpl_dynBinHeap.H:267
void update(Node *p) noexcept
Definition: tpl_binHeap.H:686
T & top() const
Definition: tpl_dynBinHeap.H:261
Node * getMin()
Definition: tpl_binHeap.H:663
Definition: tpl_binHeap.H:940
void remove_all_and_delete() noexcept
Definition: tpl_binHeap.H:730
Node * remove(Node *node)
Definition: tpl_binHeap.H:701
Definition: htlist.H:1323

Leandro Rabindranath León