queue.h 2.9 KB

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