~frankencode/drycore/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 /*
  * Copyright (C) 2007-2013 Frank Mertens.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version
  * 2 of the License, or (at your option) any later version.
  */
#ifndef DRY_HEAP_HPP
#define DRY_HEAP_HPP

#include "containers.hpp"

namespace dry
{

template<class T, template<class T> class Order = Ascending>
class GenericHeap: public Container< T, GenericHeap<T, Order> >, public Order<T>
{
public:
	typedef T Item;

	~GenericHeap()
	{
		if (bufOwner_)
		{
			delete[] buf_;
			buf_ = 0;
		}
	}

	inline int size() const { return size_; }
	inline int fill() const { return fill_; }
	inline int length() const { return fill_; }
	inline bool isFull() const { return fill_ == size_; }
	inline bool isEmpty() const { return fill_ == 0; }

	inline void push(const T &item)
	{
		DRY_ASSERT(fill_ < size_);
		buf_[fill_] = item;
		++fill_;
		passUpLast();
	}

	inline void pop(T *item)
	{
		DRY_ASSERT(fill_ > 0);
		*item = buf_[0];
		--fill_;
		buf_[0] = buf_[fill_];
		passDownFromTop();
	}

	inline T pop() {
		T item;
		pop(&item);
		return item;
	}

	inline T top() { DRY_ASSERT(!isEmpty()); return buf_[0]; }

	inline void clear() { fill_ = 0; }

	inline static int parent(int i) { return (i - 1) / 2; }
	inline static int leftChild(int i) { return 2 * i + 1; }
	inline static int rightChild(int i) { return 2 * i + 2; }

	void xchg(int i, int j)
	{
		T h = buf_[i];
		buf_[i] = buf_[j];
		buf_[j] = h;
	}

	int min(int i, int j, int k)
	{
		int h = Order<T>::below(buf_[i], buf_[j]) ? i : j;
		return Order<T>::below(buf_[h], buf_[k]) ? h : k;
	}

	void passUpLast()
	{
		if (fill_ == 1) return;
		int i = fill_ - 1;
		while (i != 0) {
			int j;
			j = parent(i);
			if (Order<T>::below(buf_[j], buf_[i])) break;
			xchg(i, j);
			i = j;
		}
	}

	void passDownFromTop()
	{
		if (fill_ == 0) return;
		int i = 0;
		while (true) {
			int j, lc, rc;
			lc = leftChild(i);
			rc = rightChild(i);

			if (rc < fill_) {
				j = min(i, lc, rc);
				if (j == i) break;

				xchg(i, j);
				i = j;
			}
			else if (lc < fill_) {
				if (Order<T>::below(buf_[lc], buf_[i])) xchg(i, lc);
				break;
			}
			else
				break;
		}
	}

protected:
	GenericHeap(int size)
		: fill_(0),
		  size_(size),
		  bufOwner_(true),
		  buf_(new T[size])
	{}

	GenericHeap(T *buf, int size)
		: fill_(0),
		  size_(size),
		  bufOwner_(false),
		  buf_(buf)
	{}

	int fill_;    // current number of elements
	int size_;    // maximal number of elements
	bool bufOwner_;
	T *buf_;    // memory buffer used for storing elements
};

template<class T>
class Heap: public GenericHeap<T, FlexibleSortOrder>
{
public:
	typedef GenericHeap<T, FlexibleSortOrder> Super;

	inline static Ref<Heap> create(int size, int order = SortOrder::Ascending) {
		return new Heap(size, order);
	}
	inline static Ref<Heap> create(T *buf, int size, int order = SortOrder::Ascending) {
		return new Heap(buf, size, order);
	}

	void reset(int order) {
		Super::clear();
		Super::setSortOrder(order);
	}

private:
	Heap(int size, int order)
		: GenericHeap<T, FlexibleSortOrder>(size)
	{
		Super::setSortOrder(order);
	}
	Heap(T *buf, int size, int order)
		: GenericHeap<T, FlexibleSortOrder>(buf, size)
	{
		Super::setSortOrder(order);
	}
};

template<class T>
class MinHeap: public GenericHeap<T, Ascending>
{
public:
	inline static Ref<MinHeap> create(int size) {
		return new MinHeap(size);
	}
	inline static Ref<MinHeap> create(T *buf, int size) {
		return new MinHeap(buf, size);
	}
private:
	MinHeap(int size): GenericHeap<T, Ascending>(size) {}
	MinHeap(T *buf, int size): GenericHeap<T, Ascending>(buf, size) {}
};

template<class T>
class MaxHeap: public GenericHeap<T, Descending>
{
public:
	inline static Ref<MaxHeap> create(int size) {
		return new MaxHeap(size);
	}
	inline static Ref<MaxHeap> create(T *buf, int size) {
		return new MaxHeap(buf, size);
	}
private:
	MaxHeap(int size): GenericHeap<T, Descending>(size) {}
	MaxHeap(T *buf, int size): GenericHeap<T, Descending>(buf, size) {}
};

} // namespace dry

#endif // DRY_HEAP_H