2
* Copyright (C) 2007-2013 Frank Mertens.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version
7
* 2 of the License, or (at your option) any later version.
12
#include "containers.hpp"
17
template<class T, template<class T> class Order = Ascending>
18
class GenericHeap: public Container< T, GenericHeap<T, Order> >, public Order<T>
32
inline int size() const { return size_; }
33
inline int fill() const { return fill_; }
34
inline int length() const { return fill_; }
35
inline bool isFull() const { return fill_ == size_; }
36
inline bool isEmpty() const { return fill_ == 0; }
38
inline void push(const T &item)
40
DRY_ASSERT(fill_ < size_);
46
inline void pop(T *item)
48
DRY_ASSERT(fill_ > 0);
51
buf_[0] = buf_[fill_];
61
inline T top() { DRY_ASSERT(!isEmpty()); return buf_[0]; }
63
inline void clear() { fill_ = 0; }
65
inline static int parent(int i) { return (i - 1) / 2; }
66
inline static int leftChild(int i) { return 2 * i + 1; }
67
inline static int rightChild(int i) { return 2 * i + 2; }
69
void xchg(int i, int j)
76
int min(int i, int j, int k)
78
int h = Order<T>::below(buf_[i], buf_[j]) ? i : j;
79
return Order<T>::below(buf_[h], buf_[k]) ? h : k;
84
if (fill_ == 1) return;
89
if (Order<T>::below(buf_[j], buf_[i])) break;
95
void passDownFromTop()
97
if (fill_ == 0) return;
111
else if (lc < fill_) {
112
if (Order<T>::below(buf_[lc], buf_[i])) xchg(i, lc);
121
GenericHeap(int size)
128
GenericHeap(T *buf, int size)
135
int fill_; // current number of elements
136
int size_; // maximal number of elements
138
T *buf_; // memory buffer used for storing elements
142
class Heap: public GenericHeap<T, FlexibleSortOrder>
145
typedef GenericHeap<T, FlexibleSortOrder> Super;
147
inline static Ref<Heap> create(int size, int order = SortOrder::Ascending) {
148
return new Heap(size, order);
150
inline static Ref<Heap> create(T *buf, int size, int order = SortOrder::Ascending) {
151
return new Heap(buf, size, order);
154
void reset(int order) {
156
Super::setSortOrder(order);
160
Heap(int size, int order)
161
: GenericHeap<T, FlexibleSortOrder>(size)
163
Super::setSortOrder(order);
165
Heap(T *buf, int size, int order)
166
: GenericHeap<T, FlexibleSortOrder>(buf, size)
168
Super::setSortOrder(order);
173
class MinHeap: public GenericHeap<T, Ascending>
176
inline static Ref<MinHeap> create(int size) {
177
return new MinHeap(size);
179
inline static Ref<MinHeap> create(T *buf, int size) {
180
return new MinHeap(buf, size);
183
MinHeap(int size): GenericHeap<T, Ascending>(size) {}
184
MinHeap(T *buf, int size): GenericHeap<T, Ascending>(buf, size) {}
188
class MaxHeap: public GenericHeap<T, Descending>
191
inline static Ref<MaxHeap> create(int size) {
192
return new MaxHeap(size);
194
inline static Ref<MaxHeap> create(T *buf, int size) {
195
return new MaxHeap(buf, size);
198
MaxHeap(int size): GenericHeap<T, Descending>(size) {}
199
MaxHeap(T *buf, int size): GenericHeap<T, Descending>(buf, size) {}