/
graph.cs
158 lines (125 loc) · 4.27 KB
/
graph.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;
namespace Graphite.Core {
public abstract class Shape {
public Shape () {
}
public abstract void Render (object shouldBeVertex, object shouldBeCanvas);
public abstract bool IsUnder (object shouldBeVertex, Point pt);
public abstract string name ();
public abstract string alias ();
}
public class Edge: Graphite.Abstract.IEdge {
public Vertex From {get; protected set;}
public Vertex To {get; protected set;}
public int Weight {get; protected set;}
protected bool _marked;
public Edge (Vertex fromv, Vertex tov, int weight) {
From = fromv;
To = tov;
Weight = weight;
_marked = false;
}
public bool Connected (Vertex v) {
return To == v;
}
public bool Matches (Edge e) {
return e.From == this.To && e.To == this.From;
}
public static implicit operator string (Edge e) {
return String.Format ("#{0}:{1}", e.To.Id, e.Weight);
}
public void Save (System.Xml.XmlWriter w) {
w.WriteStartElement ("edge");
w.WriteAttributeString ("to", To.Id.ToString());
w.WriteAttributeString ("weight", Weight.ToString());
w.WriteEndElement ();
}
// IEdge interface implementation
public Graphite.Abstract.IVertex vfrom () {
return From;
}
public Graphite.Abstract.IVertex vto () {
return To;
}
public int weight () {
return Weight;
}
// IMarkable interface implementation
public void setMark (bool marked) {
_marked = marked;
}
public bool marked () {
return _marked;
}
}
public class Vertex: Graphite.Abstract.IVertex {
public int Id {get; protected set;}
public Point Position;
public Shape VertexShape;
protected List<Edge> _edges;
protected bool _marked;
protected object _private;
public Vertex (int id, Point pos) {
Id = id;
Position = pos;
_edges = new List<Edge>();
_marked = false;
}
public void Connect (Vertex v, int weight = 0) {
if (!Connected (v))
_edges.Add (new Edge (this, v, weight));
}
public void Disconnect (Vertex v) {
Edge e = _edges.First (x => x.Connected (v));
_edges.Remove (e);
}
public bool Connected (Vertex v) {
return _edges.Any (x => x.Connected (v));
}
public Edge[] Edges () {
return _edges.ToArray();
}
public static implicit operator string (Vertex v) {
StringWriter writer = new StringWriter ();
writer.Write ("Vertex #{0}: ", v.Id);
foreach (Edge each in v._edges)
writer.Write ("{0} ", (string) each);
return writer.ToString();
}
public void Save (System.Xml.XmlWriter w) {
w.WriteStartElement ("vertex");
w.WriteAttributeString ("id", Id.ToString());
w.WriteAttributeString ("shape", VertexShape.alias ());
/* TODO: Introduce a Position class */ {
w.WriteStartElement ("position");
w.WriteAttributeString ("x", Position.X.ToString ());
w.WriteAttributeString ("y", Position.Y.ToString ());
w.WriteEndElement ();
}
foreach (Edge each in _edges)
each.Save (w);
w.WriteEndElement ();
}
// IVertex interface implementation
public Graphite.Abstract.IEdge [] edges () {
return Edges();
}
public void setPrivateData (object data) {
_private = data;
}
public object getPrivateData () {
return _private;
}
// IMarkable interface implementation
public void setMark (bool marked) {
_marked = marked;
}
public bool marked () {
return _marked;
}
}
}