21 real(kind_real) :: dis
22 real(kind_real) :: sdis
66 integer :: heap_size = 0
81 function pq_create(results_in)
result(res)
100 nalloc =
size(results_in,1)
101 if (nalloc .lt. 1)
then 102 write (*,*)
'PQ_CREATE: error, input arrays must be allocated.' 104 res%elems => results_in
115 type(pq),
pointer :: a
116 integer,
intent(in) :: i_in
118 integer :: i, l, r, largest
120 real(kind_real) :: pri_i, pri_l, pri_r, pri_largest
123 type(kdtree2_result) :: temp
139 if (l .gt. a%heap_size)
then 143 pri_i = a%elems(i)%dis
144 pri_l = a%elems(l)%dis
145 if (
sup(pri_l,pri_i))
then 157 if (r .le. a%heap_size)
then 158 pri_r = a%elems(r)%dis
159 if (
sup(pri_r,pri_largest))
then 165 if (largest .ne. i)
then 169 a%elems(i) = a%elems(largest)
170 a%elems(largest) = temp
190 type(
pq),
pointer :: a
193 if (a%heap_size .gt. 0)
then 196 write (*,*)
'PQ_MAX: ERROR, heap_size < 1' 206 type(
pq),
pointer :: a
208 if (a%heap_size .gt. 0)
then 211 write (*,*)
'PQ_MAX_PRI: ERROR, heapsize < 1' 223 type(
pq),
pointer :: a
226 if (a%heap_size .ge. 1)
then 235 a%elems(1) = a%elems(a%heap_size)
236 a%heap_size = a%heap_size-1
240 write (*,*)
'PQ_EXTRACT_MAX: error, attempted to pop non-positive PQ' 247 real(kind_real) function pq_insert(a,dis,sdis,idx)
251 type(
pq),
pointer :: a
252 real(kind_real),
intent(in) :: dis
253 real(kind_real),
intent(in) :: sdis
254 integer,
intent(in) :: idx
257 integer :: i, isparent
258 real(kind_real) :: parentdis,parentsdis
265 a%heap_size = a%heap_size + 1
270 parentdis = a%elems(isparent)%dis
271 parentsdis = a%elems(isparent)%sdis
272 if (
sup(dis,parentdis))
then 274 a%elems(i)%dis = parentdis
275 a%elems(i)%sdis = parentsdis
276 a%elems(i)%idx = a%elems(isparent)%idx
285 a%elems(i)%sdis = sdis
300 type(
pq),
pointer :: a
301 real(kind_real),
intent(in) :: dis
302 real(kind_real),
intent(in) :: sdis
303 integer,
intent(in) :: idx
307 integer :: parent, child, n
308 real(kind_real) :: prichild, prichildp1
318 loop:
do while (child .le. n)
319 prichild = a%elems(child)%dis
326 if (child .lt. n)
then 327 prichildp1 = a%elems(child+1)%dis
328 if (
inf(prichild,prichildp1))
then 330 prichild = prichildp1
334 if (
supeq(dis,prichild))
then 340 a%elems(parent) = a%elems(child)
345 a%elems(parent)%dis = dis
346 a%elems(parent)%sdis = sdis
347 a%elems(parent)%idx = idx
351 a%elems(1)%sdis = sdis
372 type(
pq),
pointer :: a
375 if ((i .lt. 1) .or. (i .gt. a%heap_size))
then 376 write (*,*)
'PQ_DELETE: error, attempt to remove out of bounds element.' 382 a%elems(i) = a%elems(a%heap_size)
383 a%heap_size = a%heap_size - 1
integer, parameter, public kind_real