pr_queue.h
上传用户:chinafayin
上传日期:2022-04-05
资源大小:153k
文件大小:4k
源码类别:

并行计算

开发平台:

Visual C++

  1. //----------------------------------------------------------------------
  2. // File: pr_queue.h
  3. // Programmer: Sunil Arya and David Mount
  4. // Description: Include file for priority queue and related
  5. //  structures.
  6. // Last modified: 01/04/05 (Version 1.0)
  7. //----------------------------------------------------------------------
  8. // Copyright (c) 1997-2005 University of Maryland and Sunil Arya and
  9. // David Mount.  All Rights Reserved.
  10. // 
  11. // This software and related documentation is part of the Approximate
  12. // Nearest Neighbor Library (ANN).  This software is provided under
  13. // the provisions of the Lesser GNU Public License (LGPL).  See the
  14. // file ../ReadMe.txt for further information.
  15. // 
  16. // The University of Maryland (U.M.) and the authors make no
  17. // representations about the suitability or fitness of this software for
  18. // any purpose.  It is provided "as is" without express or implied
  19. // warranty.
  20. //----------------------------------------------------------------------
  21. // History:
  22. // Revision 0.1  03/04/98
  23. // Initial release
  24. //----------------------------------------------------------------------
  25. #ifndef PR_QUEUE_H
  26. #define PR_QUEUE_H
  27. #include "../ANNx.h" // all ANN includes
  28. #include "../ANNperf.h" // performance evaluation
  29. //----------------------------------------------------------------------
  30. // Basic types.
  31. //----------------------------------------------------------------------
  32. typedef void *PQinfo; // info field is generic pointer
  33. typedef ANNdist PQkey; // key field is distance
  34. //----------------------------------------------------------------------
  35. // Priority queue
  36. // A priority queue is a list of items, along with associated
  37. // priorities.  The basic operations are insert and extract_minimum.
  38. //
  39. // The priority queue is maintained using a standard binary heap.
  40. // (Implementation note: Indexing is performed from [1..max] rather
  41. // than the C standard of [0..max-1].  This simplifies parent/child
  42. // computations.)  User information consists of a void pointer,
  43. // and the user is responsible for casting this quantity into whatever
  44. // useful form is desired.
  45. //
  46. // Because the priority queue is so central to the efficiency of
  47. // query processing, all the code is inline.
  48. //----------------------------------------------------------------------
  49. class ANNpr_queue {
  50. struct pq_node { // node in priority queue
  51. PQkey key; // key value
  52. PQinfo info; // info field
  53. };
  54. int n; // number of items in queue
  55. int max_size; // maximum queue size
  56. pq_node *pq; // the priority queue (array of nodes)
  57. public:
  58. ANNpr_queue(int max) // constructor (given max size)
  59. {
  60. n = 0; // initially empty
  61. max_size = max; // maximum number of items
  62. pq = new pq_node[max+1]; // queue is array [1..max] of nodes
  63. }
  64. ~ANNpr_queue() // destructor
  65. { delete [] pq; }
  66. ANNbool empty() // is queue empty?
  67. { if (n==0) return ANNtrue; else return ANNfalse; }
  68. ANNbool non_empty() // is queue nonempty?
  69. { if (n==0) return ANNfalse; else return ANNtrue; }
  70. void reset() // make existing queue empty
  71. { n = 0; }
  72. inline void insert( // insert item (inlined for speed)
  73. PQkey kv, // key value
  74. PQinfo inf) // item info
  75. {
  76. if (++n > max_size) annError("Priority queue overflow.", ANNabort);
  77. register int r = n;
  78. while (r > 1) { // sift up new item
  79. register int p = r/2;
  80. ANN_FLOP(1) // increment floating ops
  81. if (pq[p].key <= kv) // in proper order
  82. break;
  83. pq[r] = pq[p]; // else swap with parent
  84. r = p;
  85. }
  86. pq[r].key = kv; // insert new item at final location
  87. pq[r].info = inf;
  88. }
  89. inline void extr_min( // extract minimum (inlined for speed)
  90. PQkey &kv, // key (returned)
  91. PQinfo &inf) // item info (returned)
  92. {
  93. kv = pq[1].key; // key of min item
  94. inf = pq[1].info; // information of min item
  95. register PQkey kn = pq[n--].key;// last item in queue
  96. register int p = 1; // p points to item out of position
  97. register int r = p<<1; // left child of p
  98. while (r <= n) { // while r is still within the heap
  99. ANN_FLOP(2) // increment floating ops
  100. // set r to smaller child of p
  101. if (r < n  && pq[r].key > pq[r+1].key) r++;
  102. if (kn <= pq[r].key) // in proper order
  103. break;
  104. pq[p] = pq[r]; // else swap with child
  105. p = r; // advance pointers
  106. r = p<<1;
  107. }
  108. pq[p] = pq[n+1]; // insert last item in proper place
  109. }
  110. };
  111. #endif