sortutils.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 SORTUTILS_H
  19. #define SORTUTILS_H
  20. #include <fstream>
  21. using namespace std;
  22. #include <grass/iostream/ami.h>
  23. #include "common.h"
  24. /* ********************************************************************** */
  25. /* deletes input stream *str and replaces it by the sorted stream */
  26. template<class T, class FUN>
  27. void
  28. sort(AMI_STREAM<T> **str, FUN fo) {
  29. Rtimer rt;
  30. AMI_STREAM<T> *sortedStr;
  31. if (stats)
  32. stats->recordLength("pre-sort", *str);
  33. rt_start(rt);
  34. /* let AMI_sort create its output stream and delete the inout stream */
  35. int eraseInputStream = 1;
  36. AMI_sort(*str,&sortedStr, &fo, eraseInputStream);
  37. rt_stop(rt);
  38. if (stats) {
  39. stats->recordLength("sort", sortedStr);
  40. stats->recordTime("sort", rt);
  41. }
  42. sortedStr->seek(0);
  43. *str = sortedStr;
  44. }
  45. /* ********************************************************************** */
  46. /* warning - creates a new stream and returns it !! */
  47. template<class T, class FUN>
  48. AMI_STREAM<T> *
  49. sort(AMI_STREAM<T> *strIn, FUN fo) {
  50. Rtimer rt;
  51. AMI_STREAM<T> *strOut;
  52. if (stats)
  53. stats->recordLength("pre-sort", strIn);
  54. rt_start(rt);
  55. AMI_sort(strIn, &strOut, &fo);
  56. assert(strOut);
  57. rt_stop(rt);
  58. if (stats) {
  59. stats->recordLength("sort", strOut);
  60. stats->recordTime("sort", rt);
  61. }
  62. strOut->seek(0);
  63. return strOut;
  64. }
  65. #endif