ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
heap.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * THE BSD LICENSE
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *************************************************************************/
30 
31 #ifndef FLANN_HEAP_H_
32 #define FLANN_HEAP_H_
33 
34 #include <algorithm>
35 #include <vector>
36 
37 namespace flann
38 {
39 
47 template <typename T>
48 class Heap
49 {
50 
55  std::vector<T> heap;
56  int length;
57 
61  int count;
62 
63 
64 
65 public:
73  Heap(int size)
74  {
75  length = size;
76  heap.reserve(length);
77  count = 0;
78  }
79 
84  int size()
85  {
86  return count;
87  }
88 
94  bool empty()
95  {
96  return size()==0;
97  }
98 
102  void clear()
103  {
104  heap.clear();
105  count = 0;
106  }
107 
108  // fix std::binary_function unrecognized error when set c++17
109  // struct CompareT : public std::binary_function<T,T,bool>
110  struct CompareT
111  {
112  bool operator()(const T& t_1, const T& t_2) const
113  {
114  return t_2 < t_1;
115  }
116  };
117 
127  void insert(const T& value)
128  {
129  /* If heap is full, then return without adding this element. */
130  if (count == length) {
131  return;
132  }
133 
134  heap.push_back(value);
135  static CompareT compareT;
136  std::push_heap(heap.begin(), heap.end(), compareT);
137  ++count;
138  }
139 
140 
141 
149  bool popMin(T& value)
150  {
151  if (count == 0) {
152  return false;
153  }
154 
155  value = heap[0];
156  static CompareT compareT;
157  std::pop_heap(heap.begin(), heap.end(), compareT);
158  heap.pop_back();
159  --count;
160 
161  return true; /* Return old last node. */
162  }
163 };
164 
165 
166 template <typename T>
168 {
169  struct Interval
170  {
171  T left;
172  T right;
173  };
174 
179  std::vector<Interval> heap;
180  size_t capacity_;
181  size_t size_;
182 
183 public:
191  IntervalHeap(int capacity) : capacity_(capacity), size_(0)
192  {
193  heap.resize(capacity/2 + capacity%2 + 1); // 1-based indexing
194  }
195 
199  size_t size()
200  {
201  return size_;
202  }
203 
208  bool empty()
209  {
210  return size_==0;
211  }
212 
216  void clear()
217  {
218  size_ = 0;
219  }
220 
221  void insert(const T& value)
222  {
223  /* If heap is full, then return without adding this element. */
224  if (size_ == capacity_) {
225  return;
226  }
227 
228  // insert into the root
229  if (size_<2) {
230  if (size_==0) {
231  heap[1].left = value;
232  heap[1].right = value;
233  }
234  else {
235  if (value<heap[1].left) {
236  heap[1].left = value;
237  }
238  else {
239  heap[1].right = value;
240  }
241  }
242  ++size_;
243  return;
244  }
245 
246  size_t last_pos = size_/2 + size_%2;
247  bool min_heap;
248 
249  if (size_%2) { // odd number of elements
250  min_heap = (value<heap[last_pos].left)? true : false;
251  }
252  else {
253  ++last_pos;
254  min_heap = (value<heap[last_pos/2].left)? true : false;
255  }
256 
257  if (min_heap) {
258  size_t pos = last_pos;
259  size_t par = pos/2;
260  while (pos>1 && value < heap[par].left) {
261  heap[pos].left = heap[par].left;
262  pos = par;
263  par = pos/2;
264  }
265  heap[pos].left = value;
266  ++size_;
267 
268  if (size_%2) { // duplicate element in last position if size is odd
269  heap[last_pos].right = heap[last_pos].left;
270  }
271  }
272  else {
273  size_t pos = last_pos;
274  size_t par = pos/2;
275  while (pos>1 && heap[par].right < value) {
276  heap[pos].right = heap[par].right;
277  pos = par;
278  par = pos/2;
279  }
280  heap[pos].right = value;
281  ++size_;
282 
283  if (size_%2) { // duplicate element in last position if size is odd
284  heap[last_pos].left = heap[last_pos].right;
285  }
286  }
287  }
288 
289 
295  bool popMin(T& value)
296  {
297  if (size_ == 0) {
298  return false;
299  }
300 
301  value = heap[1].left;
302  size_t last_pos = size_/2 + size_%2;
303  T elem = heap[last_pos].left;
304 
305  if (size_ % 2) { // odd number of elements
306  --last_pos;
307  }
308  else {
309  heap[last_pos].left = heap[last_pos].right;
310  }
311  --size_;
312  if (size_<2) return true;
313 
314  size_t crt=1; // root node
315  size_t child = crt*2;
316 
317  while (child <= last_pos) {
318  if (child < last_pos && heap[child+1].left < heap[child].left) ++child; // pick the child with min
319 
320  if (!(heap[child].left<elem)) break;
321 
322  heap[crt].left = heap[child].left;
323  if (heap[child].right<elem) {
324  std::swap(elem, heap[child].right);
325  }
326 
327  crt = child;
328  child *= 2;
329  }
330  heap[crt].left = elem;
331  return true;
332  }
333 
334 
340  bool popMax(T& value)
341  {
342  if (size_ == 0) {
343  return false;
344  }
345 
346  value = heap[1].right;
347  size_t last_pos = size_/2 + size_%2;
348  T elem = heap[last_pos].right;
349 
350  if (size_%2) { // odd number of elements
351  --last_pos;
352  }
353  else {
354  heap[last_pos].right = heap[last_pos].left;
355  }
356  --size_;
357  if (size_<2) return true;
358 
359  size_t crt=1; // root node
360  size_t child = crt*2;
361 
362  while (child <= last_pos) {
363  if (child < last_pos && heap[child].right < heap[child+1].right) ++child; // pick the child with max
364 
365  if (!(elem < heap[child].right)) break;
366 
367  heap[crt].right = heap[child].right;
368  if (elem<heap[child].left) {
369  std::swap(elem, heap[child].left);
370  }
371 
372  crt = child;
373  child *= 2;
374  }
375  heap[crt].right = elem;
376  return true;
377  }
378 
379 
380  bool getMin(T& value)
381  {
382  if (size_==0) {
383  return false;
384  }
385  value = heap[1].left;
386  return true;
387  }
388 
389 
390  bool getMax(T& value)
391  {
392  if (size_==0) {
393  return false;
394  }
395  value = heap[1].right;
396  return true;
397  }
398 };
399 
400 
401 template <typename T>
403 {
404  IntervalHeap<T> interval_heap_;
405  size_t capacity_;
406 public:
407  BoundedHeap(size_t capacity) : interval_heap_(capacity), capacity_(capacity)
408  {
409 
410  }
411 
415  int size()
416  {
417  return interval_heap_.size();
418  }
419 
424  bool empty()
425  {
426  return interval_heap_.empty();
427  }
428 
432  void clear()
433  {
434  interval_heap_.clear();
435  }
436 
437  void insert(const T& value)
438  {
439  if (interval_heap_.size()==capacity_) {
440  T max;
441  interval_heap_.getMax(max);
442  if (max<value) return;
443  interval_heap_.popMax(max);
444  }
445  interval_heap_.insert(value);
446  }
447 
448  bool popMin(T& value)
449  {
450  return interval_heap_.popMin(value);
451  }
452 };
453 
454 
455 
456 }
457 
458 #endif //FLANN_HEAP_H_
int count
int64_t size_
Definition: FilePLY.cpp:37
BoundedHeap(size_t capacity)
Definition: heap.h:407
bool popMin(T &value)
Definition: heap.h:448
void insert(const T &value)
Definition: heap.h:437
bool empty()
Definition: heap.h:424
void clear()
Definition: heap.h:432
bool empty()
Definition: heap.h:94
void clear()
Definition: heap.h:102
int size()
Definition: heap.h:84
Heap(int size)
Definition: heap.h:73
void insert(const T &value)
Definition: heap.h:127
bool popMin(T &value)
Definition: heap.h:149
size_t size()
Definition: heap.h:199
bool empty()
Definition: heap.h:208
void clear()
Definition: heap.h:216
bool getMax(T &value)
Definition: heap.h:390
bool popMax(T &value)
Definition: heap.h:340
bool popMin(T &value)
Definition: heap.h:295
void insert(const T &value)
Definition: heap.h:221
bool getMin(T &value)
Definition: heap.h:380
IntervalHeap(int capacity)
Definition: heap.h:191
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
int max(int a, int b)
Definition: cutil_math.h:48
void swap(cloudViewer::core::SmallVectorImpl< T > &LHS, cloudViewer::core::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1370
bool operator()(const T &t_1, const T &t_2) const
Definition: heap.h:112