queue.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 QUEUE_H
  31. #define QUEUE_H
  32. #include <assert.h>
  33. #include <iostream>
  34. using namespace std;
  35. template<class T>
  36. class queue {
  37. private:
  38. T *data;
  39. int size;
  40. int head; // first valid location (if data)
  41. int tail; // next free location
  42. int len;
  43. void grow();
  44. public:
  45. queue(int size=4096);
  46. ~queue();
  47. bool enqueue(T &);
  48. bool dequeue(T *);
  49. bool peek(int offset, T *);
  50. bool isEmpty() const { return len==0; };
  51. //int length() const { return len; };
  52. unsigned int length() const { return (unsigned int)len; };
  53. };
  54. template<class T>
  55. queue<T>::queue(int vsize) : size(vsize) {
  56. if(size <= 0) size = 64; /* default */
  57. data = new T[size];
  58. head = 0;
  59. tail = 0;
  60. len = 0;
  61. }
  62. template<class T>
  63. queue<T>::~queue() {
  64. delete [] data;
  65. }
  66. template<class T>
  67. bool
  68. queue<T>::enqueue(T &elt) {
  69. if(len==size) grow();
  70. assert(len<size);
  71. data[tail] = elt;
  72. tail = (tail+1)%size;
  73. len++;
  74. return true;
  75. }
  76. template<class T>
  77. bool
  78. queue<T>::dequeue(T *elt) {
  79. if(len>0) {
  80. *elt = data[head];
  81. head = (head+1)%size;
  82. len--;
  83. return true;
  84. }
  85. return false;
  86. }
  87. template<class T>
  88. bool
  89. queue<T>::peek(int offset, T *elt) {
  90. if(len>offset) {
  91. int pos = (head+offset)%size;
  92. *elt = data[pos];
  93. return true;
  94. }
  95. return false;
  96. }
  97. template<class T>
  98. void
  99. queue<T>::grow() {
  100. T *data2 = new T[size*2];
  101. int k=head;
  102. for(int i=0; i<len; i++) {
  103. data2[i] = data[k];
  104. k = (k+1)%size;
  105. }
  106. head = 0;
  107. tail = len;
  108. delete [] data;
  109. data = data2;
  110. size *= 2;
  111. }
  112. #endif // QUEUE_H