pqheap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /****************************************************************************
  2. *
  3. * MODULE: iostream
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. *
  8. * Iostream is a library that implements streams, external memory
  9. * sorting on streams, and an external memory priority queue on
  10. * streams. These are the fundamental components used in external
  11. * memory algorithms.
  12. * Credits: The library was developed by Laura Toma. The kernel of
  13. * class STREAM is based on the similar class existent in the GPL TPIE
  14. * project developed at Duke University. The sorting and priority
  15. * queue have been developed by Laura Toma based on communications
  16. * with Rajiv Wickremesinghe. The library was developed as part of
  17. * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
  18. * Rajiv Wickremesinghe as part of the Terracost project.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * General Public License for more details. *
  29. * **************************************************************************/
  30. #ifndef _PQHEAP_H
  31. #define _PQHEAP_H
  32. #include <assert.h>
  33. #include <stdlib.h>
  34. #define PQHEAP_MEM_DEBUG if(0)
  35. //HEAPSTATUS can be defined at compile time
  36. //this flag is currently off; we used it at some point for checking
  37. //how many times is each element in the heap accessed or something
  38. //like that
  39. #ifdef HEAPSTATUS
  40. static const int PAGESIZE = 1024;
  41. #endif
  42. // Helper functions for navigating through a binary heap.
  43. /* for simplicity the heap structure is slightly modified as:
  44. 0
  45. |
  46. 1
  47. /\
  48. 2 3
  49. /\ /\
  50. 4 5 6 7
  51. */
  52. // The children of an element of the heap.
  53. static inline unsigned int heap_lchild(unsigned int index) {
  54. return 2 * index;
  55. }
  56. static inline unsigned int heap_rchild(unsigned int index) {
  57. return 2 * index + 1;
  58. }
  59. // The parent of an element.
  60. static inline unsigned int heap_parent(unsigned int index) {
  61. return index >> 1;
  62. }
  63. // return minimum of two integers
  64. static unsigned int mymin(unsigned int a, unsigned int b) {
  65. return (a<=b)? a:b;
  66. }
  67. /**************************************************************
  68. ***************************************************************
  69. ***************************************************************
  70. Priority queue templated on a single type
  71. assume T to be a class with getPriority() and getValue() implemented;
  72. Supported operations: min, extract_min, insert in O(lg n)
  73. ***************************************************************
  74. ***************************************************************
  75. ***************************************************************/
  76. template <class T>
  77. class pqheap_t1 {
  78. // A pointer to an array of elements
  79. T* elements;
  80. // The number of elements currently in the queue.
  81. unsigned int cur_elts;
  82. // The maximum number the queue can hold.
  83. unsigned int max_elts;
  84. private:
  85. void heapify(unsigned int root);
  86. public:
  87. inline pqheap_t1(unsigned int size);
  88. //build heap from an array of elements; array a is REUSED, and NOT
  89. //COPIED, for efficiency; it'd better not be used after this
  90. //outside!!!
  91. inline pqheap_t1(T* a, unsigned int size);
  92. inline ~pqheap_t1(void);
  93. //build a heap from an array of elements;
  94. //if size > max_elts, insert first maxsize elements from array;
  95. //return nb of elements that did not fit;
  96. unsigned int fill(T* a, unsigned int size);
  97. // Is it full?
  98. inline bool full(void);
  99. //Is it empty?
  100. inline bool empty(void);
  101. inline bool is_empty() { return empty(); };
  102. // How many elements?
  103. inline unsigned int num_elts(void);
  104. // How many elements? sorry - i could never remember num_elts
  105. inline unsigned int size(void) const { return cur_elts; };
  106. // Min
  107. inline bool min(T& elt);
  108. T min();
  109. // Extract min and set elt = min
  110. inline bool extract_min(T& elt);
  111. //extract all elts with min key, add them and return their sum
  112. inline bool extract_all_min(T& elt);
  113. //delete min; same as extract_min, but ignore the value extracted
  114. inline bool delete_min();
  115. // Insert
  116. inline bool insert(const T& elt);
  117. //Delete the current minimum and insert the new item x;
  118. //the minimum item is lost (i.e. not returned to user);
  119. //needed to optimize merge
  120. inline void delete_min_and_insert(const T &x);
  121. //this function is a dirty way to allow building faster the heap
  122. //in case we build it from a sorted array; in that case we dont need
  123. //to 'insert' and then 'heapify', but it is enough to 'set'
  124. void set(long i, T& elt);
  125. //print
  126. inline friend ostream& operator<<(ostream& s, const pqheap_t1<T> &pq) {
  127. s << "PQ: "; s.flush();
  128. for (unsigned int i=0; i< mymin(10, pq.cur_elts); i++) {
  129. s << "["
  130. //<< pq.elements[i].getPriority() << ","
  131. //<< pq.elements[i].getValue()
  132. << pq.elements[i]
  133. << "]";
  134. }
  135. return s;
  136. }
  137. //print
  138. void print();
  139. //print
  140. void print_range();
  141. #ifdef HEAPSTATUS
  142. inline void heapstatus(int d);
  143. inline void heaptouch(unsigned int pos);
  144. unsigned int *numtouch;
  145. #endif
  146. };
  147. //************************************************************/
  148. template <class T>
  149. inline
  150. pqheap_t1<T>::pqheap_t1(unsigned int size) {
  151. elements = new T [size];
  152. cout << "pqheap_t1: register memory\n";
  153. cout.flush();
  154. PQHEAP_MEM_DEBUG cout << "pqheap_t1::pq_heap_t1: allocate\n";
  155. // PQHEAP_MEM_DEBUG MMmanager.print();
  156. if (!elements) {
  157. cerr << "could not allocate priority queue: insufficient memory..\n";
  158. exit(1);
  159. }
  160. assert(elements);
  161. max_elts = size;
  162. cur_elts = 0;
  163. #ifdef HEAPSTATUS
  164. numtouch = new unsigned int[size/PAGESIZE];
  165. assert(numtouch);
  166. for(int i=0; i<size/PAGESIZE; i++) {
  167. numtouch[i] = 0;
  168. }
  169. #endif
  170. }
  171. //************************************************************/
  172. /* (this constructor is a bit nasty) Build heap from an array of
  173. elements; array a is reused, and not copied, for efficiency; it'd
  174. better not be used after this outside!!! */
  175. template <class T>
  176. inline
  177. pqheap_t1<T>::pqheap_t1(T* a, unsigned int size) {
  178. {
  179. static int flag = 0;
  180. if(!flag) {
  181. cerr << "Using slow build in pqheap_t1" << endl;
  182. flag = 1;
  183. }
  184. }
  185. elements = a;
  186. max_elts = size;
  187. cur_elts = size;
  188. if (max_elts) {
  189. for (int i = heap_parent(max_elts-1); i>=0; i--) {
  190. //cout << "heapify i=" << i<<"\n";
  191. heapify(i);
  192. }
  193. }
  194. }
  195. //************************************************************/
  196. template <class T>
  197. inline
  198. pqheap_t1<T>::~pqheap_t1() {
  199. #ifdef HEAPSTATUS
  200. cout << endl << "pagesize = " << PAGESIZE << endl;
  201. cout << "max_elts = " << max_elts << endl;
  202. unsigned int n = max_elts / PAGESIZE;
  203. for(unsigned int i=0; i<n; i++) {
  204. cout << form("PQTEMP %d\t%d", i, numtouch[i]) << endl;
  205. }
  206. delete [] numtouch;
  207. #endif
  208. delete [] elements;
  209. cur_elts = 0;
  210. max_elts = 0;
  211. return;
  212. }
  213. //************************************************************/
  214. //build a heap from an array of elements;
  215. //if size > max_elts, insert first maxsize elements from array;
  216. //return nb of elements that did not fit;
  217. template <class T>
  218. inline unsigned int
  219. pqheap_t1<T>::fill(T* a, unsigned int size) {
  220. unsigned int i;
  221. assert(cur_elts == 0);
  222. for (i = 0; i<size; i++) {
  223. if (!insert(a[i])) {
  224. break;
  225. }
  226. }
  227. if (i < size) {
  228. assert(i == max_elts);
  229. return size - i;
  230. } else {
  231. return 0;
  232. }
  233. }
  234. //************************************************************/
  235. template <class T>
  236. inline bool
  237. pqheap_t1<T>::full(void) {
  238. return cur_elts == max_elts;
  239. }
  240. //************************************************************/
  241. template <class T>
  242. inline bool
  243. pqheap_t1<T>::empty(void) {
  244. return cur_elts == 0;
  245. }
  246. //************************************************************/
  247. template <class T>
  248. inline unsigned int
  249. pqheap_t1<T>::num_elts(void) {
  250. return cur_elts;
  251. }
  252. //************************************************************/
  253. template <class T>
  254. inline bool
  255. pqheap_t1<T>::min(T& elt) {
  256. if (!cur_elts) {
  257. return false;
  258. }
  259. elt = elements[0];
  260. return true;
  261. }
  262. //************************************************************/
  263. template <class T>
  264. T
  265. pqheap_t1<T>::min() {
  266. T elt;
  267. if(min(elt)) {
  268. return elt;
  269. } else {
  270. cerr << "unguarded min failed" << endl;
  271. assert(0);
  272. exit(1);
  273. }
  274. return elt;
  275. }
  276. //************************************************************/
  277. //this function is a dirty hack to allow building faster the heap
  278. //in case we build it from a sorted array; in thiat case we dont need
  279. //to 'insert' and then 'heapify', but it is enough to 'set'
  280. template <class T>
  281. inline void
  282. pqheap_t1<T>::set(long i, T& elt) {
  283. //must always set precisely the next element
  284. assert(i == cur_elts);
  285. elements[i] = elt;
  286. cur_elts++;
  287. }
  288. //************************************************************/
  289. #ifdef HEAPSTATUS
  290. template <class T>
  291. inline void pqheap_t1<T>::heaptouch(unsigned int pos) {
  292. numtouch[pos/PAGESIZE]++;
  293. assert(numtouch[pos/PAGESIZE] > 0);
  294. }
  295. #endif
  296. #ifdef HEAPSTATUS
  297. template <class T>
  298. inline void pqheap_t1<T>::heapstatus(int d) {
  299. static int count = 0;
  300. static int delta = 0;
  301. delta += d;
  302. count++;
  303. if((count % 10000) == 0) {
  304. cout << endl << form("PQHEAP %d\t%d", cur_elts, delta) << endl;
  305. count = 0;
  306. delta = 0;
  307. }
  308. }
  309. #endif
  310. //************************************************************/
  311. template <class T>
  312. inline bool
  313. pqheap_t1<T>::extract_min(T& elt) {
  314. if (!cur_elts) {
  315. return false;
  316. }
  317. elt = elements[0];
  318. elements[0] = elements[--cur_elts];
  319. heapify(0);
  320. #ifdef HEAPSTATUS
  321. heaptouch(cur_elts);
  322. heaptouch(0);
  323. heapstatus(-1);
  324. #endif
  325. return true;
  326. }
  327. //************************************************************/
  328. //extract all elts with min key, add them and return their sum
  329. template <class T>
  330. inline bool
  331. pqheap_t1<T>::extract_all_min(T& elt) {
  332. T next_elt;
  333. bool done = false;
  334. //extract first elt
  335. if (!extract_min(elt)) {
  336. return false;
  337. } else {
  338. while (!done) {
  339. //peek at the next min elt to see if matches
  340. if ((!min(next_elt)) ||
  341. !(next_elt.getPriority() == elt.getPriority())) {
  342. done = true;
  343. } else {
  344. extract_min(next_elt);
  345. elt = elt + next_elt;
  346. }
  347. }
  348. }
  349. return true;
  350. }
  351. //************************************************************/
  352. template <class T>
  353. inline bool
  354. pqheap_t1<T>::delete_min() {
  355. T dummy;
  356. return extract_min(dummy);
  357. }
  358. //************************************************************/
  359. template <class T>
  360. inline bool
  361. pqheap_t1<T>::insert(const T& elt) {
  362. unsigned int ii;
  363. if (full()) {
  364. return false;
  365. }
  366. for (ii = cur_elts++;
  367. ii && (elements[heap_parent(ii)].getPriority() > elt.getPriority());
  368. ii = heap_parent(ii)) {
  369. elements[ii] = elements[heap_parent(ii)];
  370. }
  371. elements[ii] = elt;
  372. #ifdef HEAPSTATUS
  373. heaptouch(ii);
  374. heapstatus(+1);
  375. #endif
  376. return true;
  377. }
  378. //************************************************************/
  379. template <class T>
  380. inline void
  381. pqheap_t1<T>::heapify(unsigned int root) {
  382. unsigned int min_index = root;
  383. unsigned int lc = heap_lchild(root);
  384. unsigned int rc = heap_rchild(root);
  385. #ifdef HEAPSTATUS
  386. // already did the root, so dont do it again
  387. if(lc < cur_elts) {
  388. heaptouch(lc);
  389. }
  390. if(rc < cur_elts) {
  391. heaptouch(rc);
  392. }
  393. #endif
  394. if ((lc < cur_elts) &&
  395. ((elements[lc].getPriority()) < elements[min_index].getPriority())) {
  396. min_index = lc;
  397. }
  398. if ((rc < cur_elts) &&
  399. ((elements[rc].getPriority()) < elements[min_index].getPriority())) {
  400. min_index = rc;
  401. }
  402. if (min_index != root) {
  403. T tmp_q = elements[min_index];
  404. elements[min_index] = elements[root];
  405. elements[root] = tmp_q;
  406. heapify(min_index);
  407. }
  408. }
  409. //************************************************************/
  410. //Delete the current minimum and insert the new item;
  411. //the minimum item is lost (i.e. not returned to user);
  412. //needed to optimize merge
  413. template <class T>
  414. inline void
  415. pqheap_t1<T>::delete_min_and_insert(const T &x) {
  416. assert(cur_elts);
  417. elements[0] = x;
  418. heapify(0);
  419. }
  420. /************************************************************/
  421. template <class T>
  422. void pqheap_t1<T>::print() {
  423. cout << "[";
  424. for (unsigned int i=0; i<cur_elts; i++) {
  425. cout << elements[i].getPriority().field1() <<",";
  426. }
  427. cout << "]";
  428. }
  429. /************************************************************/
  430. template <class T>
  431. void pqheap_t1<T>::print_range() {
  432. cout << "[";
  433. T a, b;
  434. min(a);
  435. max(b);
  436. if (cur_elts) {
  437. cout << a.getPriority().field1() << ".."
  438. << b.getPriority().field1();
  439. }
  440. cout << " (" << cur_elts << ")]";
  441. }
  442. #endif // _PQUEUE_HEAP_H