mem_stream.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 _MEM_STREAM_H
  31. #define _MEM_STREAM_H
  32. #include <stdlib.h>
  33. #include <assert.h>
  34. #include <cstring>
  35. #include <iostream>
  36. using namespace std;
  37. template<class T>
  38. class MEM_STREAM {
  39. private:
  40. T *data;
  41. T *curr;
  42. T *dataend;
  43. int len;
  44. public:
  45. MEM_STREAM(T *data, int len);
  46. ~MEM_STREAM(void);
  47. // Read and write elements.
  48. AMI_err read_item(T **elt);
  49. AMI_err write_item(const T &elt);
  50. // Return the number of items in the stream.
  51. off_t stream_len(void);
  52. // Return the path name of this stream.
  53. AMI_err name(char **stream_name);
  54. // Move to a specific item in the stream.
  55. AMI_err seek(off_t offset);
  56. char *sprint();
  57. };
  58. /**********************************************************************/
  59. template<class T>
  60. MEM_STREAM<T>::MEM_STREAM(T *datap, int lenv) {
  61. data = datap;
  62. dataend = data + lenv;
  63. curr = datap;
  64. len = lenv;
  65. };
  66. /**********************************************************************/
  67. // Return the number of items in the stream.
  68. template<class T>
  69. off_t MEM_STREAM<T>::stream_len(void) {
  70. return len;
  71. };
  72. /**********************************************************************/
  73. // Return the path name of this stream.
  74. template<class T>
  75. AMI_err MEM_STREAM<T>::name(char **stream_name) {
  76. const char *path = "dummy";
  77. *stream_name = new char [strlen(path) + 1];
  78. strcpy(*stream_name, path);
  79. return AMI_ERROR_NO_ERROR;
  80. };
  81. /**********************************************************************/
  82. // Move to a specific offset within the (sub)stream.
  83. template<class T>
  84. AMI_err MEM_STREAM<T>::seek(off_t offset) {
  85. assert(offset <= len);
  86. curr = data + offset;
  87. return AMI_ERROR_NO_ERROR;
  88. }
  89. /**********************************************************************/
  90. template<class T>
  91. MEM_STREAM<T>::~MEM_STREAM(void) {
  92. };
  93. /**********************************************************************/
  94. template<class T>
  95. AMI_err MEM_STREAM<T>::read_item(T **elt) {
  96. assert(data);
  97. if(curr == dataend) {
  98. return AMI_ERROR_END_OF_STREAM;
  99. }
  100. *elt = curr;
  101. curr++;
  102. return AMI_ERROR_NO_ERROR;
  103. };
  104. /**********************************************************************/
  105. template<class T>
  106. AMI_err MEM_STREAM<T>::write_item(const T &elt) {
  107. assert(data);
  108. if(curr == dataend) {
  109. return AMI_ERROR_END_OF_STREAM;
  110. }
  111. *curr = elt;
  112. curr++;
  113. return AMI_ERROR_NO_ERROR;
  114. };
  115. /**********************************************************************/
  116. // sprint()
  117. // Return a string describing the stream
  118. //
  119. // This function gives easy access to the file name, length.
  120. // It is not reentrant, but this should not be too much of a problem
  121. // if you are careful.
  122. template<class T>
  123. char *MEM_STREAM<T>::sprint() {
  124. static char buf[BUFSIZ];
  125. sprintf(buf, "[MEM_STREAM %d]", stream_len());
  126. return buf;
  127. };
  128. #endif // _MEM_STREAM_H