ccforest.h 3.8 KB

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