00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef EDGE_H
00013 #define EDGE_H
00014
00015 #include <iostream>
00016
00017 typedef unsigned int NodeID;
00018 typedef unsigned int EdgeWeight;
00019 typedef unsigned long long int EdgeWeightBig;
00020
00025 class EdgeWithoutSource
00026 {
00028 friend bool operator<(const EdgeWithoutSource &e1,
00029 const EdgeWithoutSource &e2) {
00030 return e1._weight<e2._weight;
00031 }
00032
00037 friend bool operator==(const EdgeWithoutSource &e1,
00038 const EdgeWithoutSource &e2) {
00039 return (e1._target == e2._target) && (e1._weight == e2._weight);
00040 }
00041
00043 friend std::ostream& operator<<( std::ostream& os, const EdgeWithoutSource &e ) {
00044 os << e.target() << " " << e.weight();
00045 return os;
00046 }
00047
00048 public:
00050 static EdgeWithoutSource minWeight() {return EdgeWithoutSource(0,0);}
00051
00053 static EdgeWithoutSource maxWeight() {return EdgeWithoutSource(0,0xffffffff);}
00054
00056 EdgeWithoutSource( NodeID target = 0, EdgeWeight weight = 0 )
00057 : _target( target ), _weight( weight )
00058 {}
00059
00061 NodeID target() const {return _target;}
00062
00064 EdgeWeight weight() const {return _weight;}
00065
00066 protected:
00068 void setTarget(NodeID target) {_target = target;}
00069
00070 private:
00071 NodeID _target;
00072 EdgeWeight _weight;
00073 };
00074
00078 class Edge : public EdgeWithoutSource
00079 {
00081 friend bool operator==(const Edge &e1, const Edge &e2) {
00082 return ( e1.source()==e2.source() ) && ( e1.target()==e2.target() );
00083 }
00084
00086 friend std::ostream& operator<<( std::ostream& os, const Edge &e ) {
00087 os << e.source() << " " << e.target() << " " << e.weight();
00088 return os;
00089 }
00090
00091 public:
00093 typedef EdgeWeight key_type;
00094
00095
00097 static Edge minWeight() {return Edge(0,0,0);}
00098
00100 static Edge maxWeight() {return Edge(0,0,0xffffffff);}
00101
00102
00104 Edge(NodeID source = 0, NodeID target = 0, EdgeWeight weight = 0)
00105 : _source( source ), EdgeWithoutSource( target, weight )
00106 {}
00107
00109 NodeID source() const {return _source;}
00110
00112 void swap() {NodeID tmp = _source; _source = target(); setTarget(tmp);}
00113
00118 bool isSelfLoop() const {return (source() == target());}
00119
00120
00121 private:
00122 NodeID _source;
00123 };
00124
00125
00126
00131 template <typename EdgeType = Edge>
00132 class GetWeight
00133 {
00134 public:
00135 typedef Edge::key_type key_type;
00136 key_type operator() (const EdgeType & obj)
00137 {
00138 return obj.weight();
00139 }
00140 static EdgeType min_value(){ return EdgeType::minWeight(); }
00141 static EdgeType max_value(){ return EdgeType::maxWeight(); }
00142 };
00143
00144
00145
00151 template <typename EdgeType = Edge>
00152 class WeightOrdering
00153 {
00154 public:
00155 bool operator() (const EdgeType &e1, const EdgeType &e2) const
00156 {
00157 if (e1.weight() < e2.weight()) return true;
00158 return false;
00159 }
00160 static EdgeType min_value(){ return EdgeType::minWeight(); }
00161 static EdgeType max_value(){ return EdgeType::maxWeight(); }
00162 };
00163
00164
00170 class SourceTargetWeightOrdering
00171 {
00172 public:
00173 bool operator() (const Edge &e1, const Edge &e2) const
00174 {
00175
00176 if (e1.source() < e2.source()) return true;
00177 if (e1.source() > e2.source()) return false;
00178
00179
00180 if (e1.target() < e2.target()) return true;
00181 if (e1.target() > e2.target()) return false;
00182
00183
00184 if (e1.weight() < e2.weight()) return true;
00185 return false;
00186 }
00187
00188 static Edge min_value() {return Edge(0,0,0);}
00189 static Edge max_value() {return Edge(0xffffffff,0xffffffff,0xffffffff);}
00190 };
00191
00192
00200 template <typename EdgeType = Edge>
00201 class SourceWeightOrdering
00202 {
00203 public:
00204 bool operator() (const EdgeType &e1, const EdgeType &e2) const
00205 {
00206
00207 if (e1.source() < e2.source()) return true;
00208 if (e1.source() > e2.source()) return false;
00209
00210
00211 if (e1.weight() > e2.weight()) return true;
00212 return false;
00213 }
00214
00215 static EdgeType min_value(){ return EdgeType::minWeight(); }
00216 static EdgeType max_value(){ return EdgeType::maxWeight(); }
00217 };
00218
00219 #endif // EDGE_H