empq_adaptive.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 __EMPQ_ADAPTIVE_H
  31. #define __EMPQ_ADAPTIVE_H
  32. #include "minmaxheap.h"
  33. #include "empq.h"
  34. #include "empq_impl.h"
  35. #define EMPQAD_DEBUG if(G_verbose() > G_verbose_std())
  36. enum regim_type {
  37. INMEM = 0,
  38. EXTMEM,
  39. EXTMEM_DEBUG
  40. };
  41. template<class T, class Key>
  42. class EMPQueueAdaptive {
  43. private:
  44. //dictates if the structure works in the internal/external memory regim;
  45. regim_type regim;
  46. MinMaxHeap<T> *im;
  47. em_pqueue<T,Key> *em;
  48. UnboundedMinMaxHeap<T> *dim; // debug, internal memory pq
  49. void initPQ(size_t);
  50. public:
  51. /* start in INMEM regim by allocating im of size precisely twice the
  52. size of the (pqueue within) the em_pqueue; */
  53. EMPQueueAdaptive(long N) : EMPQueueAdaptive() {};
  54. EMPQueueAdaptive();
  55. EMPQueueAdaptive(size_t inMem);
  56. ~EMPQueueAdaptive();
  57. void makeExternal();
  58. void makeExternalDebug();
  59. long maxlen() const; //return the maximum nb of elts that can fit
  60. bool is_empty() const; //return true if empty
  61. bool is_full() const; //return true if full
  62. bool min(T& elt); //return the element with min priority XXX
  63. //delete the element with minimum priority in the structure;
  64. //return false if pq is empty
  65. bool extract_min(T& elt);
  66. //extract all elts with min key, add them and return their sum XXX
  67. bool extract_all_min(T& elt);
  68. /* insert an element; if regim == INMEM, try insert it in im, and if
  69. it is full, extract_max pqsize/2 elements of im into a stream,
  70. switch to EXTMEM and insert the stream into em; if regim is
  71. EXTMEM, insert in em; */
  72. bool insert(const T& elt);
  73. long size() const; //return the nb of elements in the structure
  74. void clear(); /* delete all contents of pq */
  75. void verify();
  76. };
  77. #endif