streamutils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. using namespace std;
  22. #include <grass/iostream/ami.h>
  23. #include "types.h"
  24. #include "common.h"
  25. template<class T>
  26. void
  27. printStream(ostream &s, AMI_STREAM<T> *str) {
  28. T *elt;
  29. AMI_err ae;
  30. str->seek(0);
  31. while((ae = str->read_item(&elt)) == AMI_ERROR_NO_ERROR) {
  32. s << *elt << endl;
  33. }
  34. str->seek(0);
  35. }
  36. /* laura note: this works that class T has an empty contructor which
  37. initializes it to the nodata value */
  38. template<class T, class FUN>
  39. void
  40. printStream2Grid(AMI_STREAM<T> *str,
  41. dimension_type nrows, dimension_type ncols,
  42. const char *name,
  43. FUN fmt) {
  44. T *elt, nodata;
  45. AMI_err ae;
  46. ofstream fstrm(name);
  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. stats->recordLength("saving grid", str);
  77. fstrm << "rows=" << nrows << endl;
  78. fstrm << "cols=" << ncols << endl;
  79. assert(str->stream_len() == nrows * ncols);
  80. str->seek(0);
  81. for(dimension_type i=0; i<nrows; i++) {
  82. for(dimension_type j=0; j<ncols; j++) {
  83. ae = str->read_item(&elt);
  84. assert(ae == AMI_ERROR_NO_ERROR);
  85. fstrm << " " << fmt(*elt);
  86. }
  87. fstrm << endl;
  88. }
  89. str->seek(0);
  90. }
  91. /* ********************************************************************** */
  92. /* assume sorted... */
  93. template<class T, class FUN>
  94. AMI_STREAM<T> *
  95. removeDuplicates(AMI_STREAM<T> *str, FUN fo) {
  96. AMI_err ae;
  97. AMI_STREAM<T> *newStr = new AMI_STREAM<T>();
  98. if(str->stream_len() == 0) return newStr; /* empty stream */
  99. str->seek(0);
  100. T prev, *elp;
  101. ae = str->read_item(&elp);
  102. assert(ae == AMI_ERROR_NO_ERROR);
  103. prev = *elp;
  104. while((ae = str->read_item(&elp)) == AMI_ERROR_NO_ERROR) {
  105. if(fo.compare(*elp, prev)) { /* differ */
  106. newStr->write_item(prev);
  107. prev = *elp;
  108. } else {
  109. /* cout << "duplicate: " << *elp << " of " << prev << endl; */
  110. }
  111. }
  112. newStr->write_item(prev); /* last one */
  113. return newStr;
  114. }
  115. /* ********************************************************************** */
  116. template<class T, class FUN>
  117. void
  118. removeDuplicatesEx(AMI_STREAM<T> **str, FUN fo) {
  119. AMI_STREAM<T> *tmp = removeDuplicates(*str, fo);
  120. delete *str;
  121. *str = tmp;
  122. }
  123. /* ********************************************************************** */
  124. /*
  125. * merge a grid and a stream together to form an new grid of the original type
  126. * str should be sorted in ij order
  127. */
  128. template<class T, class TT, class FUN>
  129. AMI_STREAM<T> *
  130. mergeStream2Grid(AMI_STREAM<T> *grid,
  131. dimension_type rows, dimension_type cols,
  132. AMI_STREAM<TT> *str,
  133. FUN fo) {
  134. AMI_err ae, aeS;
  135. T *gep; /* grid element */
  136. TT *sep; /* stream element */
  137. AMI_STREAM<T> *mergeStr = new AMI_STREAM<T>();
  138. str->seek(0);
  139. grid->seek(0);
  140. aeS = str->read_item(&sep);
  141. assert(aeS == AMI_ERROR_NO_ERROR || aeS == AMI_ERROR_END_OF_STREAM);
  142. for(dimension_type i=0; i<rows; i++) {
  143. for(dimension_type j=0; j<cols; j++) {
  144. ae = grid->read_item(&gep);
  145. assert(ae == AMI_ERROR_NO_ERROR);
  146. if((aeS == AMI_ERROR_NO_ERROR) && (sep->i == i) && (sep->j == j)) {
  147. ae = mergeStr->write_item(fo(*sep));
  148. assert(ae == AMI_ERROR_NO_ERROR);
  149. aeS = str->read_item(&sep);
  150. assert(aeS == AMI_ERROR_NO_ERROR || aeS == AMI_ERROR_END_OF_STREAM);
  151. } else {
  152. ae = mergeStr->write_item(fo(*gep));
  153. assert(ae == AMI_ERROR_NO_ERROR);
  154. }
  155. }
  156. }
  157. return mergeStr;
  158. }
  159. #endif