sortutils.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <grass/iostream/ami.h>
  22. #include "common.h"
  23. /* ********************************************************************** */
  24. /* deletes input stream *str and replaces it by the sorted stream */
  25. template<class T, class FUN>
  26. void
  27. sort(AMI_STREAM<T> **str, FUN fo) {
  28. Rtimer rt;
  29. AMI_STREAM<T> *sortedStr;
  30. if (stats)
  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. if (stats) {
  38. stats->recordLength("sort", sortedStr);
  39. stats->recordTime("sort", rt);
  40. }
  41. sortedStr->seek(0);
  42. *str = sortedStr;
  43. }
  44. /* ********************************************************************** */
  45. /* warning - creates a new stream and returns it !! */
  46. template<class T, class FUN>
  47. AMI_STREAM<T> *
  48. sort(AMI_STREAM<T> *strIn, FUN fo) {
  49. Rtimer rt;
  50. AMI_STREAM<T> *strOut;
  51. if (stats)
  52. stats->recordLength("pre-sort", strIn);
  53. rt_start(rt);
  54. AMI_sort(strIn, &strOut, &fo);
  55. assert(strOut);
  56. rt_stop(rt);
  57. if (stats) {
  58. stats->recordLength("sort", strOut);
  59. stats->recordTime("sort", rt);
  60. }
  61. strOut->seek(0);
  62. return strOut;
  63. }
  64. #endif