Fermat
priority_queue_inline.h
1 /*
2  * cugar
3  * Copyright (c) 2011-2018, 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 cugar {
29 
30 // constructor
31 //
32 template <typename Key, typename Container, typename Compare>
33 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE priority_queue<Key,Container,Compare>::priority_queue(const Container cont, const Compare cmp)
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>
43 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool priority_queue<Key,Container,Compare>::empty() const
44 {
45  return m_size == 0;
46 }
47 
48 // return queue size
49 //
50 template <typename Key, typename Container, typename Compare>
51 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE uint32 priority_queue<Key,Container,Compare>::size() const
52 {
53  return m_size;
54 }
55 
56 // push an element
57 //
58 template <typename Key, typename Container, typename Compare>
59 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void priority_queue<Key,Container,Compare>::push(const Key key)
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>
81 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void priority_queue<Key,Container,Compare>::pop()
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>
106 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Key& priority_queue<Key,Container,Compare>::top()
107 {
108  return m_queue[1];
109 }
110 
111 // top of the queue
112 //
113 template <typename Key, typename Container, typename Compare>
114 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Key priority_queue<Key,Container,Compare>::top() const
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>
122 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const Key& priority_queue<Key,Container,Compare>::operator[] (const uint32 i) const
123 {
124  return m_queue[1+i];
125 }
126 
127 // clear the queue
128 //
129 template <typename Key, typename Container, typename Compare>
130 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void priority_queue<Key,Container,Compare>::clear()
131 {
132  m_size = 0;
133  m_queue.resize(0);
134 }
135 
136 // locate the largest element v such that v <= x; return end() if no
137 // such element exists
138 //
139 template <typename Key, typename Container, typename Compare>
140 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE typename priority_queue<Key,Container,Compare>::iterator
142 {
143  uint32 max_i = 0;
144  Key max;
145 
146  for (uint32 j = 1; j < size()+1; ++j)
147  {
148  if (!m_cmp( x, m_queue[j] )) // m_queue[j] <= x
149  {
150  if (max_i == 0 || !m_cmp( m_queue[j], max )) // m_queue[j] >= max
151  {
152  // found a new maximum
153  max = m_queue[j];
154  max_i = j;
155  }
156  }
157  }
158 
159  if (max_i == 0)
160  return end();
161 
162  return begin() + max_i-1;
163 }
164 
165 } // namespace cugar
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_iterator begin() const
Definition: priority_queue.h:135
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE uint32 size() const
Definition: priority_queue_inline.h:51
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Key & top()
Definition: priority_queue_inline.h:106
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const Key & operator[](const uint32 i) const
Definition: priority_queue_inline.h:122
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE priority_queue(Container cont=Container(), const Compare cmp=Compare())
Definition: priority_queue_inline.h:33
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void push(const Key key)
Definition: priority_queue_inline.h:59
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_iterator end() const
Definition: priority_queue.h:145
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE iterator upper_bound(const Key &x)
Definition: priority_queue_inline.h:141
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool empty() const
Definition: priority_queue_inline.h:43
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void clear()
Definition: priority_queue_inline.h:130
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void pop()
Definition: priority_queue_inline.h:81