sortutils.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. stats->recordLength("pre-sort", *str);
  32. rt_start(rt);
  33. /* let AMI_sort create its output stream and delete the inout stream */
  34. int eraseInputStream = 1;
  35. AMI_sort(*str,&sortedStr, &fo, eraseInputStream);
  36. rt_stop(rt);
  37. stats->recordLength("sort", sortedStr);
  38. stats->recordTime("sort", rt);
  39. sortedStr->seek(0);
  40. *str = sortedStr;
  41. }
  42. /* ********************************************************************** */
  43. /* warning - creates a new stream and returns it !! */
  44. template<class T, class FUN>
  45. AMI_STREAM<T> *
  46. sort(AMI_STREAM<T> *strIn, FUN fo) {
  47. Rtimer rt;
  48. AMI_STREAM<T> *strOut;
  49. stats->recordLength("pre-sort", strIn);
  50. rt_start(rt);
  51. AMI_sort(strIn, &strOut, &fo);
  52. assert(strOut);
  53. rt_stop(rt);
  54. stats->recordLength("sort", strOut);
  55. stats->recordTime("sort", rt);
  56. strOut->seek(0);
  57. return strOut;
  58. }
  59. #endif