28 # ifndef TPL_ARRAYHEAP_H 29 # define TPL_ARRAYHEAP_H 31 # include <ahFunction.H> 34 # include <ahAssert.H> 35 # include <array_it.H> 37 # include <tpl_dynDlist.H> 38 # include <ah-args-ctor.H> 42 using namespace Aleph;
47 template <
typename T,
class Compare>
inline 48 T & sift_up(T * ptr,
const size_t l,
const size_t r, Compare & cmp)
51 for (
size_t p; i > l; i = p)
54 if (cmp(ptr[p], ptr[i]))
57 std::swap(ptr[p], ptr[i]);
63 template <
typename T,
class Compare>
inline 64 void sift_down(T * ptr,
const size_t l,
const size_t r, Compare & cmp)
74 if (cmp (ptr[c + 1], ptr[c]))
77 if (cmp (ptr[i], ptr[c]))
80 std::swap(ptr[c], ptr[i]);
85 template <
typename T,
class Compare>
inline 86 void sift_down_up(T * ptr,
const size_t l,
const size_t i,
const size_t r,
89 sift_down <T, Compare> (ptr, i, r, cmp);
90 sift_up <T, Compare> (ptr, l, i, cmp);
114 template <
typename T,
class Compare = Aleph::less<T>>
inline 115 void heapsort(T * array,
const size_t n, Compare cmp = Compare())
120 for (
size_t i = 2; i <= n; ++i)
122 for (
size_t i = n; i > 1; --i)
124 std::swap(array[1], array[i]);
125 sift_down<T,Aleph::Inversed_Compare<T,Compare>>(array, 1, i - 1, inv_cmp);
150 template <
typename T,
class Compare = Aleph::less<T>>
inline 151 void faster_heapsort(T * array,
const size_t n, Compare cmp = Compare())
156 for (
size_t i = n/2; i >= 1; --i)
157 sift_down(array, i, n, inv_cmp);
158 for (
size_t i = n; i > 1; --i)
160 std::swap(array[1], array[i]);
161 sift_down(array, 1, i - 1, inv_cmp);
167 template <
typename T,
class Compare = Aleph::less<T>>
168 bool valid_heap(T * array,
const size_t l,
const size_t r,
169 Compare cmp = Compare())
172 for (i = l_index(l) ; i <= r; i++)
173 if (cmp(array[i], array[u_index (i)]))
191 template <
typename T,
class Compare = Aleph::less<T>>
196 public StlAlephIterator<ArrayHeap<T, Compare>>
200 size_t num_items = 0;
202 mutable bool array_allocated =
true;
206 static size_t r_index(
const size_t & i)
215 std::swap(array, h.array);
216 std::swap(dim, h.dim);
217 std::swap(num_items, h.num_items);
218 std::swap(array_allocated, h.array_allocated);
219 std::swap(cmp, h.cmp);
231 ArrayHeap(
const size_t d = 1024, Compare __cmp = Compare())
232 : array(nullptr), dim(d), num_items(0), array_allocated(false), cmp(__cmp)
234 array =
new T [d + 1];
235 array_allocated =
true;
239 ArrayHeap(T * ptr,
const size_t & d, Compare __cmp = Compare())
240 : array(ptr), dim(d), num_items(0), array_allocated(false), cmp(__cmp)
246 : dim(h.dim), num_items(h.num_items), cmp(h.cmp)
248 array =
new T [dim + 1];
249 for (
size_t i = 1; i <= num_items; ++i)
250 array[i] = h.array[i];
254 : array_allocated(
false), cmp(h.cmp)
266 T * ptr =
new T [h.dim + 1];
270 array_allocated =
true;
274 for (
size_t i = 1; i <= h.num_items; ++i)
275 array[i] = h.array[i];
276 num_items = h.num_items;
291 if (array_allocated and array !=
nullptr)
299 throw std::underflow_error(
"Heap is empty");
304 T & insert_ne(
const T & key) noexcept
306 array[++num_items] = key;
307 return sift_up(array, 1, num_items, cmp);
310 T & insert_ne(T && key) noexcept
312 array[++num_items] = std::move(key);
313 return sift_up(array, 1, num_items, cmp);
327 if (num_items >= dim)
328 throw std::overflow_error(
"Heap out of capacity");
329 return insert_ne(key);
334 if (num_items >= dim)
335 throw std::overflow_error(
"Heap out of capacity");
336 return insert_ne(std::forward<T>(key));
339 T & put(
const T & key) {
return insert(key); }
341 T & append(
const T & key) {
return insert(key); }
343 T & put(T && key) {
return insert(std::forward<T>(key)); }
345 T & append(T && key) {
return insert(std::forward<T>(key)); }
359 throw std::underflow_error(
"Heap is empty");
361 T ret_val = array[1];
362 array[1] = array[num_items--];
363 sift_down(array, 1, num_items, cmp);
380 const size_t &
size()
const {
return num_items; }
404 assert(&data >= &array[0] and &data <= &array[dim]);
406 const size_t i = &data - array;
407 sift_down_up(array, 1, i, num_items, cmp);
408 sift_up(array, i, num_items, cmp);
411 void remove(T & item)
413 item = array[num_items--];
433 template <
class Operation>
434 bool __traverse(Operation & operation)
436 for (
size_t i = 1; i <= num_items; ++i)
437 if (not operation(array[i]))
445 template <
class Operation>
446 bool traverse(Operation & operation)
const 448 return const_cast<ArrayHeap&
>(*this).__traverse(operation);
451 template <
class Operation>
452 bool traverse(Operation & operation)
454 return __traverse(operation);
457 template <
class Operation>
458 bool traverse(Operation && operation = Operation())
const 460 return const_cast<ArrayHeap&
>(*this).__traverse(operation);
463 template <
class Operation>
464 bool traverse(Operation && operation = Operation())
466 return traverse(operation);
void update(T &data)
Definition: tpl_arrayHeap.H:402
Definition: htlist.H:1290
T getMax()
Definition: tpl_arrayHeap.H:374
Definition: tpl_arrayHeap.H:192
bool is_empty() const
Retorna true si el heap está vacÃo.
Definition: tpl_arrayHeap.H:383
T getMin()
Definition: tpl_arrayHeap.H:356
virtual ~ArrayHeap()
Destructor.
Definition: tpl_arrayHeap.H:289
T & operator[](const size_t i)
Retorna la i-ésima entrada del heap.
Definition: tpl_arrayHeap.H:418
ArrayHeap(T *ptr, const size_t &d, Compare __cmp=Compare())
Constructor con arreglo y dimensión.
Definition: tpl_arrayHeap.H:239
Definition: array_it.H:45
T & top()
Retorna el menor elemento del heap.
Definition: tpl_arrayHeap.H:296
ArrayHeap(const size_t d=1024, Compare __cmp=Compare())
Constructor con dimensión por omisión.
Definition: tpl_arrayHeap.H:231
const size_t & size() const
Retorna la cantidad de elementos.
Definition: tpl_arrayHeap.H:380
T & insert(const T &key)
Definition: tpl_arrayHeap.H:325
Definition: ahFunction.H:969
Definition: htlist.H:1323