ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
heap.h
Go to the documentation of this file.
1 #ifndef FLANN_UTIL_CUDA_HEAP_H
2 #define FLANN_UTIL_CUDA_HEAP_H
3 
4 /*
5  Copyright (c) 2011, Andreas Mützel <andreas.muetzel@gmx.net>
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15  * Neither the name of the <organization> nor the
16  names of its contributors may be used to endorse or promote products
17  derived from this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY Andreas Mützel <andreas.muetzel@gmx.net> ''AS IS'' AND ANY
20  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL Andreas Mützel <andreas.muetzel@gmx.net> BE LIABLE FOR ANY
23  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 namespace flann
32 {
33 namespace cuda
34 {
35 template <class T>
36 __device__ __host__ void swap( T& x, T& y )
37 {
38  T t=x;
39  x=y;
40  y=t;
41 }
42 
43 namespace heap
44 {
45 
48 template <class GreaterThan, class RandomAccessIterator>
49 __host__ __device__ void
50 sift_down( RandomAccessIterator array, size_t begin, size_t length, GreaterThan c = GreaterThan() )
51 {
52 
53  while( 2*begin+1 < length ) {
54  size_t left = 2*begin+1;
55  size_t right = 2*begin+2;
56  size_t largest=begin;
57  if((left < length)&& c(array[left], array[largest]) ) largest=left;
58 
59  if((right < length)&& c(array[right], array[largest]) ) largest=right;
60 
61  if( largest != begin ) {
62  cuda::swap( array[begin], array[largest] );
63  begin=largest;
64  }
65  else return;
66  }
67 }
68 
71 template <class GreaterThan, class RandomAccessIterator>
72 __host__ __device__ void
73 make_heap( RandomAccessIterator begin, size_t length, GreaterThan c = GreaterThan() )
74 {
75  int i=length/2-1;
76  while( i>=0 ) {
77  sift_down( begin, i, length, c );
78  i--;
79  }
80 }
81 
82 
85 template <class GreaterThan, class RandomAccessIterator>
86 __host__ __device__ bool
87 is_heap( RandomAccessIterator begin, size_t length, GreaterThan c = GreaterThan() )
88 {
89  for( unsigned i=0; i<length; i++ ) {
90  if((2*i+1 < length)&& c(begin[2*i+1],begin[i]) ) return false;
91  if((2*i+2 < length)&& c(begin[2*i+2],begin[i]) ) return false;
92  }
93  return true;
94 }
95 
96 
99 template <class GreaterThan, class RandomAccessIterator, class RandomAccessIterator2>
100 __host__ __device__ void
101 sift_down( RandomAccessIterator key, RandomAccessIterator2 value, size_t begin, size_t length, GreaterThan c = GreaterThan() )
102 {
103 
104  while( 2*begin+1 < length ) {
105  size_t left = 2*begin+1;
106  size_t right = 2*begin+2;
107  size_t largest=begin;
108  if((left < length)&& c(key[left], key[largest]) ) largest=left;
109 
110  if((right < length)&& c(key[right], key[largest]) ) largest=right;
111 
112  if( largest != begin ) {
113  cuda::swap( key[begin], key[largest] );
114  cuda::swap( value[begin], value[largest] );
115  begin=largest;
116  }
117  else return;
118  }
119 }
120 
123 template <class GreaterThan, class RandomAccessIterator, class RandomAccessIterator2>
124 __host__ __device__ void
125 make_heap( RandomAccessIterator key, RandomAccessIterator2 value, size_t length, GreaterThan c = GreaterThan() )
126 {
127  int i=length/2-1;
128  while( i>=0 ) {
129  sift_down( key, value, i, length, c );
130  i--;
131  }
132 }
133 
134 }
135 
136 }
137 }
138 
139 #endif
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
__host__ __device__ void sift_down(RandomAccessIterator array, size_t begin, size_t length, GreaterThan c=GreaterThan())
Definition: heap.h:50
__host__ __device__ void make_heap(RandomAccessIterator begin, size_t length, GreaterThan c=GreaterThan())
Definition: heap.h:73
__host__ __device__ bool is_heap(RandomAccessIterator begin, size_t length, GreaterThan c=GreaterThan())
Definition: heap.h:87
__device__ __host__ void swap(T &x, T &y)
Definition: heap.h:36