NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
priority_queue_inline.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 namespace nvbio {
29 
30 // constructor
31 //
32 template <typename Key, typename Container, typename Compare>
34  : m_size(0), m_queue(cont), m_cmp(cmp)
35 {
36  // TODO: heapify if not empty!
37  assert( m_queue.empty() == true );
38 }
39 
40 // is queue empty?
41 //
42 template <typename Key, typename Container, typename Compare>
44 {
45  return m_size == 0;
46 }
47 
48 // return queue size
49 //
50 template <typename Key, typename Container, typename Compare>
52 {
53  return m_size;
54 }
55 
56 // push an element
57 //
58 template <typename Key, typename Container, typename Compare>
60 {
61  // check whether the queue is full
62  m_size++;
63  m_queue.resize( m_size+1 ); // we need one more entry for things to work out
64 
65  uint32 r = m_size;
66  while (r > 1) // sift up new item
67  {
68  const uint32 p = r/2;
69  if (! m_cmp( m_queue[p], key )) // in proper order
70  break;
71 
72  m_queue[r] = m_queue[p]; // else swap with parent
73  r = p;
74  }
75  m_queue[r] = key; // insert new item at final location
76 }
77 
78 // pop an element
79 //
80 template <typename Key, typename Container, typename Compare>
82 {
83  Key dn = m_queue[m_size--]; // last item in queue
84  m_queue.resize( m_size+1 ); // we need one more entry for things to work out
85 
86  uint32 p = 1; // p points to item out of position
87  uint32 r = p << 1; // left child of p
88 
89  while (r <= m_size) // while r is still within the heap
90  {
91  // set r to smaller child of p
92  if (r < m_size && m_cmp( m_queue[r], m_queue[r+1] )) r++;
93  if (! m_cmp( dn, m_queue[r] )) // in proper order
94  break;
95 
96  m_queue[p] = m_queue[r]; // else swap with child
97  p = r; // advance pointers
98  r = p<<1;
99  }
100  m_queue[p] = m_queue[m_size+1]; // insert last item in proper place
101 }
102 
103 // top of the queue
104 //
105 template <typename Key, typename Container, typename Compare>
107 {
108  return m_queue[1];
109 }
110 
111 // top of the queue
112 //
113 template <typename Key, typename Container, typename Compare>
115 {
116  return m_queue[1];
117 }
118 
119 // return the i-th element in the heap
120 //
121 template <typename Key, typename Container, typename Compare>
123 {
124  return m_queue[1+i];
125 }
126 
127 // clear the queue
128 //
129 template <typename Key, typename Container, typename Compare>
131 {
132  m_size = 0;
133  m_queue.resize(0);
134 }
135 
136 namespace priqueue
137 {
138  // returns the index of the first node at the same level as node i
140  {
141  const uint32 msb = 1u << (sizeof(i) * 8u - 1u);
142  return msb >> lzc(i);
143  }
144 
145  // returns the width of the tree at the level of node i
147  {
148  return 1u << (sizeof(i) * 8u - lzc(i) - 1u);
149  }
150 
151  // returns the parent node of i
153  {
154  return i >> 1u;
155  }
156 }
157 
158 // locate the largest element v such that v <= x; return end() if no
159 // such element exists
160 //
161 template <typename Key, typename Container, typename Compare>
162 NVBIO_FORCEINLINE NVBIO_HOST_DEVICE priority_queue<Key,Container,Compare>::iterator
164 {
165  uint32 max_i = 0;
166  Key max;
167 #if 1
168  for (uint32 j = 1; j < size()+1; ++j)
169  {
170  if (!m_cmp( x, m_queue[j] )) // m_queue[j] <= x
171  {
172  if (max_i == 0 || !m_cmp( m_queue[j], max )) // m_queue[j] >= max
173  {
174  // found a new maximum
175  max = m_queue[j];
176  max_i = j;
177  }
178  }
179  }
180 
181  if (max_i == 0)
182  return end();
183 
184  return begin() + max_i-1;
185 #else
186  uint32 i;
187  bool stop;
188 
189  // start with the leftmost leaf node
190  i = priqueue::leftmost(m_size);
191  stop = false;
192 
193  while(!stop && i > 0)
194  {
195  const uint32 num_nodes = nvbio::min( priqueue::width(i), m_size - i );
196 
197  // visit all nodes at the same level of i
198  //stop = true;
199  stop = (num_nodes == priqueue::width(i) ? true : false);
200  for(uint32 j = i; j < i + num_nodes; j++)
201  {
202  if (!m_cmp( x, m_queue[j] )) // m_queue[j] <= x
203  {
204  // if at least one of the nodes at this level is <= x, then visit the level above
205  // (this is overly conservative: we can skip the parent if one of the children is > x)
206  stop = false;
207 
208  if (max_i == 0 || !m_cmp( m_queue[j], max )) // m_queue[j] >= max
209  {
210  // found a new maximum
211  if (j > max_i)
212  {
213  max = m_queue[j];
214  max_i = j;
215  }
216  }
217  }
218  }
219 
220  // go up one level
221  i = priqueue::parent(i);
222  }
223 
224  if (max_i == 0)
225  return end();
226 
227  return begin() + max_i-1;
228 #endif
229 }
230 
231 } // namespace nvbio