2 # ifndef TPL_ARRAYHEAP_H
3 # define TPL_ARRAYHEAP_H
5 # include <ahFunction.H>
14 template <
typename T,
class Compare>
inline
15 T & sift_up(T * ptr,
const size_t l,
const size_t r)
18 for (
size_t p; i > l; i = p)
21 if (Compare () (ptr[p], ptr[i]))
24 std::swap(ptr[p], ptr[i]);
30 template <
typename T,
class Compare>
inline
31 void sift_down(T * ptr,
const size_t l,
const size_t r)
41 if (Compare () (ptr[c + 1], ptr[c]))
44 if (Compare () (ptr[i], ptr[c]))
47 std::swap(ptr[c], ptr[i]);
52 template <
typename T,
class Compare>
inline
53 void sift_down_up(T * ptr,
const size_t l,
const size_t i,
const size_t r)
55 sift_down <T, Compare> (ptr, i, r);
56 sift_up <T, Compare> (ptr, l, i);
80 template <
typename T,
class Compare = Aleph::less<T> >
84 for (
int i = 2; i <= n; ++i)
86 for (
int i = n; i > 1; --i)
88 std::swap(array[1], array[i]);
89 sift_down<T,Aleph::Inversed_Compare<T,Compare> >(array, 1, i - 1);
114 template <
typename T,
class Compare = Aleph::less<T> >
118 for (
int i = n/2; i >= 1; --i)
120 for (
int i = n; i > 1; --i)
122 std::swap(array[1], array[i]);
123 sift_down<T,Aleph::Inversed_Compare<T,Compare> >(array, 1, i - 1);
129 template <
typename T,
class Compare>
130 bool valid_heap(T * array,
const size_t l,
const size_t r)
133 for (i = l_index(l) ; i <= r; i++)
134 if (Compare () (array[i], array[u_index (i)]))
152 template <
typename T,
class Compare = Aleph::less<T>>
159 static size_t r_index(
const size_t & i)
164 mutable bool array_allocated;
170 : array(NULL), dim(d), num_items(0), array_allocated(false)
172 array =
new T [d + 1];
173 array_allocated =
true;
178 : array(ptr), dim(d), num_items(0), array_allocated(false)
186 if (array_allocated and array != NULL)
191 T &
top() throw(std::exception, std::underflow_error)
194 throw std::underflow_error(
"Heap is empty");
208 T &
insert(
const T & key)
throw(std::exception, std::overflow_error)
210 if (num_items >= dim)
211 throw std::overflow_error(
"Heap out of capacity");
213 array[++num_items] = key;
214 return sift_up<T,Compare>(array, 1, num_items);
226 T
getMin() throw(std::exception, std::underflow_error)
229 throw std::underflow_error(
"Heap is empty");
231 T ret_val = array[1];
232 array[1] = array[num_items--];
233 sift_down<T, Compare>(array, 1, num_items);
238 T
get()
throw(std::exception, std::underflow_error)
244 T
getMax() throw(std::exception, std::underflow_error)
250 const size_t &
size()
const {
return num_items; }
274 I(&data >= &array[0] and &data <= &array[dim]);
276 const size_t i = &data - array;
277 sift_down_up <T, Compare> (array, 1, i, num_items);
278 sift_up <T, Compare> (array, i, num_items);
281 void remove(T & item)
283 item = array[num_items--];
296 template <
class Operation>
297 bool __traverse(Operation & operation)
299 for (
size_t i = 1; i <= num_items; ++i)
300 if (not operation(array[i]))
308 template <
class Operation>
309 bool traverse(Operation & operation)
const
311 return const_cast<ArrayHeap&
>(*this).__traverse(operation);
314 template <
class Operation>
315 bool traverse(Operation & operation)
317 return __traverse(operation);
320 template <
class Operation>
321 bool traverse(Operation && operation = Operation())
const
323 return const_cast<ArrayHeap&
>(*this).__traverse(operation);
326 template <
class Operation>
327 bool traverse(Operation && operation = Operation())
329 return traverse(operation);
332 Functional_Methods(T);
void update(T &data)
Definition: tpl_arrayHeap.H:272
T getMax()
Definition: tpl_arrayHeap.H:244
Definition: ahFunction.H:942
Definition: tpl_arrayHeap.H:153
T getMin()
Definition: tpl_arrayHeap.H:226
virtual ~ArrayHeap()
Destructor.
Definition: tpl_arrayHeap.H:184
void heapsort(T *array, const size_t n)
Definition: tpl_arrayHeap.H:81
const size_t & size() const
Retorna la cantidad de elementos.
Definition: tpl_arrayHeap.H:250
void faster_heapsort(T *array, const size_t n)
Definition: tpl_arrayHeap.H:115
T & operator[](const size_t i)
Retorna la i-ésima entrada del heap.
Definition: tpl_arrayHeap.H:288
ArrayHeap(T *ptr, const size_t &d)
Constructor con arreglo y dimensión.
Definition: tpl_arrayHeap.H:177
T & top()
Retorna el menor elemento del heap.
Definition: tpl_arrayHeap.H:191
T & insert(const T &key)
Definition: tpl_arrayHeap.H:208
bool is_empty() const
Retorna true si el heap está vacío.
Definition: tpl_arrayHeap.H:253
ArrayHeap(const size_t &d=1024)
Constructor con dimensión por omisión.
Definition: tpl_arrayHeap.H:169