Easy3D 2.6.1
Loading...
Searching...
No Matches
heap.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_CORE_HEAP_H
28#define EASY3D_CORE_HEAP_H
29
30#include <vector>
31#include <cassert>
32#include <iostream>
33
34
35namespace easy3d {
36
64
65 template<class HeapEntry, class HeapInterface>
66 class Heap : private std::vector<HeapEntry> {
67 public:
69
73 Heap() : HeapVector() {}
74
79 explicit Heap(const HeapInterface &i) : HeapVector(), interface_(i) {}
80
84 ~Heap() = default;
85
89 void clear() { HeapVector::clear(); }
90
95 bool empty() { return HeapVector::empty(); }
96
101 unsigned int size() { return static_cast<unsigned int>(HeapVector::size()); }
102
107 void reserve(unsigned int n) { HeapVector::reserve(n); }
108
113 void reset_heap_position(HeapEntry h) {
114 interface_.set_heap_position(h, -1);
115 }
116
122 bool is_stored(HeapEntry h) {
123 return interface_.get_heap_position(h) != -1;
124 }
125
130 void insert(HeapEntry h) {
131 This::push_back(h);
132 upheap(size() - 1);
133 }
134
139 HeapEntry front() {
140 assert(!empty());
141 return entry(0);
142 }
143
147 void pop_front() {
148 assert(!empty());
149 interface_.set_heap_position(entry(0), -1);
150 if (size() > 1) {
151 entry(0, entry(size() - 1));
152 HeapVector::resize(size() - 1);
153 downheap(0);
154 } else
155 HeapVector::resize(size() - 1);
156 }
157
162 void remove(HeapEntry h) {
163 const int pos = interface_.get_heap_position(h);
164 interface_.set_heap_position(h, -1);
165
166 assert(pos != -1);
167 assert(static_cast<unsigned int>(pos) < size());
168
169 // last item ?
170 if (static_cast<unsigned int>(pos) == size() - 1)
171 HeapVector::resize(size() - 1);
172
173 else {
174 entry(pos, entry(size() - 1)); // move last elem to pos
175 HeapVector::resize(size() - 1);
176 downheap(pos);
177 upheap(pos);
178 }
179 }
180
186 void update(HeapEntry h) {
187 const int pos = interface_.get_heap_position(h);
188 assert(pos != -1);
189 assert(static_cast<unsigned int>(pos) < size());
190 downheap(pos);
191 upheap(pos);
192 }
193
198 bool check() {
199 bool ok(true);
200 unsigned int j;
201 for (unsigned int i = 0; i < size(); ++i) {
202 if (((j = left(i)) < size()) &&
203 interface_.greater(entry(i), entry(j))) {
204 std::cerr << "Heap condition violated\n";
205 ok = false;
206 }
207 if (((j = right(i)) < size()) &&
208 interface_.greater(entry(i), entry(j))) {
209 std::cerr << "Heap condition violated\n";
210 ok = false;
211 }
212 }
213 return ok;
214 }
215
216 private:
217 // typedef
218 typedef std::vector<HeapEntry> HeapVector;
219
224 void upheap(unsigned int idx) {
225 HeapEntry h = entry(idx);
226 unsigned int parentIdx;
227
228 while ((idx > 0) && interface_.less(h, entry(parentIdx = parent(idx)))) {
229 entry(idx, entry(parentIdx));
230 idx = parentIdx;
231 }
232
233 entry(idx, h);
234 }
235
240 void downheap(unsigned int idx) {
241 HeapEntry h = entry(idx);
242 const unsigned int s = size();
243 while (idx < s) {
244 unsigned int childIdx = left(idx);
245 if (childIdx >= s)
246 break;
247
248 if ((childIdx + 1 < s) &&
249 (interface_.less(entry(childIdx + 1), entry(childIdx))))
250 ++childIdx;
251
252 if (interface_.less(h, entry(childIdx)))
253 break;
254
255 entry(idx, entry(childIdx));
256 idx = childIdx;
257 }
258
259 entry(idx, h);
260 }
261
267 HeapEntry entry(unsigned int idx) {
268 assert(idx < size());
269 return (This::operator[](idx));
270 }
271
277 void entry(unsigned int idx, HeapEntry h) {
278 assert(idx < size());
279 This::operator[](idx) = h;
280 interface_.set_heap_position(h, idx);
281 }
282
288 unsigned int parent(unsigned int i) { return (i - 1) >> 1; }
289
295 unsigned int left(unsigned int i) { return (i << 1) + 1; }
296
302 unsigned int right(unsigned int i) { return (i << 1) + 2; }
303
305 HeapInterface interface_;
306 };
307
308} // namespace easy3d
309
310#endif // EASY3D_CORE_HEAP_H
void remove(HeapEntry h)
Removes an entry from the heap.
Definition heap.h:162
bool empty()
Checks if the heap is empty.
Definition heap.h:95
HeapEntry front()
Retrieves the first entry in the heap.
Definition heap.h:139
void pop_front()
Deletes the first entry in the heap.
Definition heap.h:147
Heap()
Default constructor.
Definition heap.h:73
unsigned int size()
Returns the size of the heap.
Definition heap.h:101
void update(HeapEntry h)
Updates an entry in the heap.
Definition heap.h:186
void insert(HeapEntry h)
Inserts an entry into the heap.
Definition heap.h:130
~Heap()=default
Destructor.
void reset_heap_position(HeapEntry h)
Resets the heap position of an entry to -1 (not in heap).
Definition heap.h:113
void clear()
Clears the heap.
Definition heap.h:89
bool is_stored(HeapEntry h)
Checks if an entry is stored in the heap.
Definition heap.h:122
bool check()
Checks the heap condition.
Definition heap.h:198
void reserve(unsigned int n)
Reserves space for n entries.
Definition heap.h:107
Heap(const HeapInterface &i)
Constructs a heap with a given HeapInterface.
Definition heap.h:79
Heap< SurfaceMesh::Vertex, HeapInterface > This
Definition heap.h:68
Definition collider.cpp:182