ccforest.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 CCFOREST_H
  19. #define CCFOREST_H
  20. #include <assert.h>
  21. #include <iostream>
  22. #include <grass/iostream/ami.h>
  23. #define DEBUG_CCFOREST if(0)
  24. template<class T>
  25. class keyvalue {
  26. private:
  27. T key, value;
  28. public:
  29. keyvalue() : key(-1), value(-1) {};
  30. keyvalue(T vk, T vv) : key(vk), value(vv) {};
  31. T getPriority() const { return key; };
  32. T getValue() const { return value; };
  33. T src() const { return key; };
  34. T dst() const { return value; };
  35. keyvalue operator =(const keyvalue &that) {
  36. key = that.key;
  37. value = that.value;
  38. return *this;
  39. };
  40. int operator != (const keyvalue &e2) const {
  41. return (key != e2.key) || (value != e2.value);
  42. }
  43. int operator == (const keyvalue &e2) const {
  44. return (value == e2.value) && (key == e2.key);
  45. }
  46. int operator > (const keyvalue &e2) const {
  47. return (key > e2.key) || (key == e2.key && value > e2.value);
  48. }
  49. int operator >= (const keyvalue &e2) const {
  50. return (key > e2.key) || (key == e2.key && value >= e2.value);
  51. }
  52. int operator < (const keyvalue &e2) const {
  53. return (key < e2.key) || (key == e2.key && value < e2.value);
  54. }
  55. int operator <= (const keyvalue &e2) const {
  56. return (key <= e2.key) || (key == e2.key && value <= e2.value);
  57. }
  58. friend ostream& operator<<(ostream& s, const keyvalue &p) {
  59. return s << "(" << p.key << "," << p.value << ")";
  60. }
  61. static int qscompare(const void *a, const void *b) {
  62. keyvalue<T> *x = (keyvalue<T>*)a;
  63. keyvalue<T> *y = (keyvalue<T>*)b;
  64. return compare(*x, *y);
  65. }
  66. static int compare(const keyvalue<T> &x, const keyvalue<T> &y) {
  67. return (x < y ? -1 : (x > y ? 1 : 0));
  68. }
  69. };
  70. /* laura: used in sort (instead of <); checkit */
  71. template<class T>
  72. class keyCmpKeyvalueType {
  73. public:
  74. static int compare(const keyvalue<T> &a, const keyvalue<T> &b) {
  75. if (a.getPriority() < b.getPriority()) return -1;
  76. if (a.getPriority() > b.getPriority()) return 1;
  77. return 0;
  78. }
  79. };
  80. /* ---------------------------------------------------------------------- */
  81. /* laura: used in sort instead of valueCmp; checkit */
  82. template<class T>
  83. class dstCmpKeyvalueType {
  84. public:
  85. static int compare (const keyvalue<T> &a, const keyvalue<T> &b) {
  86. if(a.dst() < b.dst()) return -1;
  87. if(a.dst() > b.dst()) return 1;
  88. if(a.src() < b.src()) return -1;
  89. if(a.src() > b.src()) return 1;
  90. return 0;
  91. }
  92. };
  93. template<class T> class ccforest;
  94. template<class T>
  95. class ccforest {
  96. /* class cckeyvalue : public keyvalue<T> {}; */
  97. typedef keyvalue<T> ccedge;
  98. typedef keyvalue<T> cckeyvalue;
  99. private:
  100. AMI_STREAM<ccedge> *edgeStream;
  101. AMI_STREAM<cckeyvalue> *rootStream;
  102. void findAllRoots(int depth=0);
  103. int rootCycles;
  104. ccforest<T> *superTree;
  105. void removeDuplicates(T src, T parent,
  106. EMPQueueAdaptive<cckeyvalue,T> &pq);
  107. int foundAllRoots;
  108. cckeyvalue savedRoot;
  109. int savedRootValid;
  110. public:
  111. ccforest();
  112. ~ccforest();
  113. void insert(const T& i, const T& j); /* insert edge (i,j) */
  114. T findNextRoot(const T& i); /* find root where i >= prev i */
  115. void printRootStream();
  116. void printEdgeStream();
  117. int size();
  118. };
  119. #endif /* CCFOREST_H */