-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph.cs
More file actions
180 lines (163 loc) · 5.71 KB
/
Graph.cs
File metadata and controls
180 lines (163 loc) · 5.71 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace SimpleRoutingAnalyzer
{
class Graph
{
public static readonly int None = 0;
public int[] AdjencyMatrix { get; set; } = null;
public Point[] Points { get; set; } = null;
public bool[] Enabled { get; set; } = null;
public int Count { get => Points?.Length ?? 0; }
public Graph() { }
public Graph(int size)
{
AdjencyMatrix = new int[size * size];
Points = new Point[size];
Enabled = new bool[size];
for (int i = 0; i < Enabled.Length; i++)
Enabled[i] = true;
}
public Dictionary<string, string> Metadata { get; set; } = new Dictionary<string, string>();
public string this[string key]
{
get => Metadata[key];
set
{
if (Metadata.ContainsKey(key)) Metadata[key] = value;
else Metadata.Add(key, value);
}
}
public int this[int s, int d]
{
get => AdjencyMatrix[s * Count + d];
set => AdjencyMatrix[s * Count + d] = value;
}
public int[] this[int s]
{
get
{
if (!Enabled[s]) {
return new int[0];
}
var result = new List<int>(Count);
for (int d = 0; d < Count; d++) {
if (AdjencyMatrix[s * Count + d] != None && Enabled[d]) {
result.Add(d);
}
}
return result.ToArray();
}
}
public int[] GetStraightNodes(int s)
{
var result = new List<int>(Count);
for (int d = 0; d < Count; d++)
if (AdjencyMatrix[s * Count + d] != None)
result.Add(d);
return result.ToArray();
}
public int[] Connected(int s)
{
var result = new List<int>(Count);
for (int d = 0; d < Count; d++)
if (AdjencyMatrix[s * Count + d] != None)
result.Add(d);
return result.ToArray();
}
public int Available(int s)
{
int res = 0;
for (int d = 0; d < Count; d++)
if (AdjencyMatrix[s * Count + d] != None && Enabled[d])
res++;
return res;
}
public static Graph Parse(string str)
{
var lines = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length < 1) return null;
var line = lines[0].Split(new char[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
if (line.Length != lines.Length) return null;
var graph = new Graph(line.Length);
for (int y = 0; y < graph.Count; y++)
{
line = lines[y].Split(new char[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
if (line.Length != graph.Count) return null;
for (int x = 0; x < line.Length; x++)
{
int val;
if (int.TryParse(line[x], out val)) graph[x, y] = val;
else return null;
}
}
return graph;
}
public override string ToString()
{
var str = new StringBuilder(Count * Count * 4);
for (int y = 0; y < Count; y++)
{
for (int x = 0; x < Count; x++)
{
str.Append(AdjencyMatrix[y * Count + x]);
str.Append(" ");
}
str.Append(Environment.NewLine);
}
return str.ToString();
}
public void ParsePoints(string str)
{
var lines = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
for (int y = 0; y < lines.Length; y++)
{
var line = lines[y].Split(new char[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
if (line.Length != 2) return;
int px, py;
if (int.TryParse(line[0], out px) &&
int.TryParse(line[1], out py)) Points[y] = new Point(px, py);
else return;
}
}
public string PointsToString()
{
string str = "";
for (int y = 0; y < (Points?.Length ?? 0); y++)
str += Points[y].X + " " + Points[y].Y + Environment.NewLine;
return str;
}
public void ParseMetadata(string metadata)
{
Metadata.Clear();
var lines = metadata.Split(
new string[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries
);
foreach (var line in lines)
{
var parts = line.Split(new char[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2) return;
Metadata.Add(parts[0], parts[1]);
}
}
public string MetadataToString()
{
var builder = new StringBuilder();
foreach (var item in Metadata)
{
builder.Append(item.Key);
builder.Append(" ");
builder.Append(item.Value);
builder.AppendLine();
}
return builder.ToString();
}
}
}