1 //
2 // edge.cc
3 //
4
5 #include <iostream>
6 #include <stdexcept>
7 #include "edge.h"
8 #include "vertex.h"
9
10
11
Edge(Vertex * a,Vertex * b,bool dir,int wt)12 Edge::Edge (Vertex *a, Vertex *b, bool dir, int wt) : v (a), w (b),
13 flow (0), weight (wt), directed (dir), selected (false)
14 {
15 if (a == b)
16 throw std::invalid_argument ("Loops not yet supported.");
17 }
18
Edge(const Edge & other)19 Edge::Edge (const Edge &other) : v (other.v), w (other.w),
20 flow (other.flow), weight (other.weight),
21 directed (other.directed), selected (false)
22 {
23 }
24
cycle_orientations()25 void Edge::cycle_orientations ()
26 {
27 // NOTE: this relies on vertex comparability
28 if (!directed)
29 directed = true;
30 else {
31 if (v->label < w->label)
32 directed = false;
33 Vertex *tmp = v;
34 v = w;
35 w = tmp;
36 }
37 }
38
operator =(const Edge & other)39 Edge &Edge::operator= (const Edge &other)
40 {
41 if (this == &other)
42 return *this;
43
44 v = other.v;
45 w = other.w;
46 flow = other.flow;
47 weight = other.weight;
48 directed = other.directed;
49
50 return *this;
51 }
52
operator ==(const Edge & other) const53 bool Edge::operator== (const Edge &other) const
54 {
55 if (!directed)
56 return (((v == other.v) && (w == other.w)) ||
57 ((v == other.w) && (w == other.v)));
58 else
59 return ((v == other.v) && (w == other.w));
60 }
61
operator <<(std::ostream & o,const Edge & e)62 std::ostream &operator<< (std::ostream &o, const Edge &e)
63 {
64 o << "edge \"" << e.v->label.mb_str (wxConvUTF8) << "\" " <<
65 (e.directed ? "-> \"" : "-- \"")
66 << e.w->label.mb_str (wxConvUTF8) << "\"";
67
68 if (e.weight != 1)
69 o << " with weight " << e.weight;
70
71 o << '\n';
72
73 return o;
74 }
75
incident_to(const Vertex * v1) const76 bool Edge::incident_to (const Vertex *v1) const
77 {
78 return ((v == v1) || (w == v1));
79 }
80
incident_to(const Vertex * v1,const Vertex * v2) const81 bool Edge::incident_to (const Vertex *v1, const Vertex *v2) const
82 {
83 return (incident_to (v1) && incident_to (v2));
84 }
85