/**************************************************************************** * * MODULE: r.terraflow * * COPYRIGHT (C) 2007 Laura Toma * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * *****************************************************************************/ #ifndef CCFOREST_H #define CCFOREST_H #include #include using namespace std; #include #define DEBUG_CCFOREST if(0) template class keyvalue { private: T key, value; public: keyvalue() : key(-1), value(-1) {}; keyvalue(T vk, T vv) : key(vk), value(vv) {}; T getPriority() const { return key; }; T getValue() const { return value; }; T src() const { return key; }; T dst() const { return value; }; keyvalue operator =(const keyvalue &that) { key = that.key; value = that.value; return *this; }; int operator != (const keyvalue &e2) const { return (key != e2.key) || (value != e2.value); } int operator == (const keyvalue &e2) const { return (value == e2.value) && (key == e2.key); } int operator > (const keyvalue &e2) const { return (key > e2.key) || (key == e2.key && value > e2.value); } int operator >= (const keyvalue &e2) const { return (key > e2.key) || (key == e2.key && value >= e2.value); } int operator < (const keyvalue &e2) const { return (key < e2.key) || (key == e2.key && value < e2.value); } int operator <= (const keyvalue &e2) const { return (key <= e2.key) || (key == e2.key && value <= e2.value); } friend ostream& operator<<(ostream& s, const keyvalue &p) { return s << "(" << p.key << "," << p.value << ")"; } static int qscompare(const void *a, const void *b) { keyvalue *x = (keyvalue*)a; keyvalue *y = (keyvalue*)b; return compare(*x, *y); } static int compare(const keyvalue &x, const keyvalue &y) { return (x < y ? -1 : (x > y ? 1 : 0)); } }; /* laura: used in sort (instead of <); checkit */ template class keyCmpKeyvalueType { public: static int compare(const keyvalue &a, const keyvalue &b) { if (a.getPriority() < b.getPriority()) return -1; if (a.getPriority() > b.getPriority()) return 1; return 0; } }; /* ---------------------------------------------------------------------- */ /* laura: used in sort instead of valueCmp; checkit */ template class dstCmpKeyvalueType { public: static int compare (const keyvalue &a, const keyvalue &b) { if(a.dst() < b.dst()) return -1; if(a.dst() > b.dst()) return 1; if(a.src() < b.src()) return -1; if(a.src() > b.src()) return 1; return 0; } }; template class ccforest; template class ccforest { /* class cckeyvalue : public keyvalue {}; */ typedef keyvalue ccedge; typedef keyvalue cckeyvalue; private: AMI_STREAM *edgeStream; AMI_STREAM *rootStream; void findAllRoots(int depth=0); int rootCycles; ccforest *superTree; void removeDuplicates(T src, T parent, EMPQueueAdaptive &pq); int foundAllRoots; cckeyvalue savedRoot; int savedRootValid; public: ccforest(); ~ccforest(); void insert(const T& i, const T& j); /* insert edge (i,j) */ T findNextRoot(const T& i); /* find root where i >= prev i */ void printRootStream(); void printEdgeStream(); int size(); }; #endif /* CCFOREST_H */