mem_stream.h 4.1 KB

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