streamutils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #ifndef STREAMUTILS_H
  19. #define STREAMUTILS_H
  20. #include <fstream>
  21. #include <grass/iostream/ami.h>
  22. #include "types.h"
  23. #include "common.h"
  24. template<class T>
  25. void
  26. printStream(ostream &s, AMI_STREAM<T> *str) {
  27. T *elt;
  28. AMI_err ae;
  29. str->seek(0);
  30. while((ae = str->read_item(&elt)) == AMI_ERROR_NO_ERROR) {
  31. s << *elt << endl;
  32. }
  33. str->seek(0);
  34. }
  35. /* laura note: this works that class T has an empty contructor which
  36. initializes it to the nodata value */
  37. template<class T, class FUN>
  38. void
  39. printStream2Grid(AMI_STREAM<T> *str,
  40. dimension_type nrows, dimension_type ncols,
  41. const char *name,
  42. FUN fmt) {
  43. T *elt, nodata;
  44. AMI_err ae;
  45. ofstream fstrm(name);
  46. if (stats)
  47. stats->comment("saving grid: ", name);
  48. fstrm << "rows=" << nrows << endl;
  49. fstrm << "cols=" << ncols << endl;
  50. str->seek(0);
  51. ae = str->read_item(&elt);
  52. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  53. for(dimension_type i=0; i<nrows; i++) {
  54. for(dimension_type j=0; j<ncols; j++) {
  55. if(ae == AMI_ERROR_NO_ERROR && elt->i == i && elt->j == j) {
  56. fstrm << " " << fmt(*elt);
  57. ae = str->read_item(&elt);
  58. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  59. } else {
  60. fstrm << " " << fmt(nodata);
  61. }
  62. } /* for j */
  63. fstrm << endl;
  64. }
  65. assert(ae == AMI_ERROR_END_OF_STREAM); /* stream must have finished */
  66. str->seek(0);
  67. }
  68. template<class T, class FUN>
  69. void printGridStream(AMI_STREAM<T> *str,
  70. dimension_type nrows, dimension_type ncols,
  71. char *name,
  72. FUN fmt) {
  73. T *elt;
  74. AMI_err ae;
  75. ofstream fstrm(name);
  76. if (stats)
  77. stats->recordLength("saving grid", str);
  78. fstrm << "rows=" << nrows << endl;
  79. fstrm << "cols=" << ncols << endl;
  80. assert(str->stream_len() == (off_t)nrows * ncols);
  81. str->seek(0);
  82. for(dimension_type i=0; i<nrows; i++) {
  83. for(dimension_type j=0; j<ncols; j++) {
  84. ae = str->read_item(&elt);
  85. assert(ae == AMI_ERROR_NO_ERROR);
  86. fstrm << " " << fmt(*elt);
  87. }
  88. fstrm << endl;
  89. }
  90. str->seek(0);
  91. }
  92. /* ********************************************************************** */
  93. /* assume sorted... */
  94. template<class T, class FUN>
  95. AMI_STREAM<T> *
  96. removeDuplicates(AMI_STREAM<T> *str, FUN fo) {
  97. AMI_err ae;
  98. AMI_STREAM<T> *newStr = new AMI_STREAM<T>();
  99. if(str->stream_len() == 0) return newStr; /* empty stream */
  100. str->seek(0);
  101. T prev, *elp;
  102. ae = str->read_item(&elp);
  103. assert(ae == AMI_ERROR_NO_ERROR);
  104. prev = *elp;
  105. while((ae = str->read_item(&elp)) == AMI_ERROR_NO_ERROR) {
  106. if(fo.compare(*elp, prev)) { /* differ */
  107. newStr->write_item(prev);
  108. prev = *elp;
  109. } else {
  110. /* cout << "duplicate: " << *elp << " of " << prev << endl; */
  111. }
  112. }
  113. newStr->write_item(prev); /* last one */
  114. return newStr;
  115. }
  116. /* ********************************************************************** */
  117. template<class T, class FUN>
  118. void
  119. removeDuplicatesEx(AMI_STREAM<T> **str, FUN fo) {
  120. AMI_STREAM<T> *tmp = removeDuplicates(*str, fo);
  121. delete *str;
  122. *str = tmp;
  123. }
  124. /* ********************************************************************** */
  125. /*
  126. * merge a grid and a stream together to form an new grid of the original type
  127. * str should be sorted in ij order
  128. */
  129. template<class T, class TT, class FUN>
  130. AMI_STREAM<T> *
  131. mergeStream2Grid(AMI_STREAM<T> *grid,
  132. dimension_type rows, dimension_type cols,
  133. AMI_STREAM<TT> *str,
  134. FUN fo) {
  135. AMI_err ae, aeS;
  136. T *gep; /* grid element */
  137. TT *sep; /* stream element */
  138. AMI_STREAM<T> *mergeStr = new AMI_STREAM<T>();
  139. str->seek(0);
  140. grid->seek(0);
  141. aeS = str->read_item(&sep);
  142. assert(aeS == AMI_ERROR_NO_ERROR || aeS == AMI_ERROR_END_OF_STREAM);
  143. for(dimension_type i=0; i<rows; i++) {
  144. for(dimension_type j=0; j<cols; j++) {
  145. ae = grid->read_item(&gep);
  146. assert(ae == AMI_ERROR_NO_ERROR);
  147. if((aeS == AMI_ERROR_NO_ERROR) && (sep->i == i) && (sep->j == j)) {
  148. ae = mergeStr->write_item(fo(*sep));
  149. assert(ae == AMI_ERROR_NO_ERROR);
  150. aeS = str->read_item(&sep);
  151. assert(aeS == AMI_ERROR_NO_ERROR || aeS == AMI_ERROR_END_OF_STREAM);
  152. } else {
  153. ae = mergeStr->write_item(fo(*gep));
  154. assert(ae == AMI_ERROR_NO_ERROR);
  155. }
  156. }
  157. }
  158. return mergeStr;
  159. }
  160. #endif