26 using System.Collections.Generic;
30 using System.Text.RegularExpressions;
31 using System.Globalization;
74 public IList<Dictionary<Arc, string>> Extensions {
get;
private set; }
77 public int StartIndex {
get;
set; }
81 Extensions =
new List<Dictionary<Arc,string>>();
94 buildableGraph.
Clear();
97 var whitespaces =
new Regex(
@"\s+");
100 tokens = whitespaces.Split(reader.ReadLine());
101 int nodeCount =
int.Parse(tokens[0], CultureInfo.InvariantCulture);
102 int arcCount =
int.Parse(tokens[1], CultureInfo.InvariantCulture);
105 for (
int i = 0; i < nodeCount; i++) nodes[i] = buildableGraph.
AddNode();
109 for (
int i = 0; i < arcCount; i++)
111 tokens = whitespaces.Split(reader.ReadLine());
112 int a = (int)(
long.Parse(tokens[0], CultureInfo.InvariantCulture) - StartIndex);
113 int b = (int)(
long.Parse(tokens[1], CultureInfo.InvariantCulture) - StartIndex);
115 Arc arc = buildableGraph.
AddArc(nodes[a], nodes[b], directedness);
117 int extensionCount = tokens.Length - 2;
118 for (
int j = 0; j < extensionCount - Extensions.Count; j++)
119 Extensions.Add(
new Dictionary<Arc, string>());
120 for (
int j = 0; j < extensionCount; j++)
121 Extensions[j][arc] = tokens[2 + j];
130 using (var reader =
new StreamReader(filename))
131 return Load(reader, directedness);
136 public void Save(TextWriter writer)
138 var whitespace =
new Regex(
@"\s");
140 writer.WriteLine(Graph.NodeCount() +
" " + Graph.ArcCount());
141 Dictionary<Node, long> index =
new Dictionary<Node,long>();
142 long indexFactory = StartIndex;
143 foreach (var arc
in Graph.Arcs())
145 Node u = Graph.U(arc);
147 if (!index.TryGetValue(u, out uindex)) index[u] = uindex = indexFactory++;
149 Node v = Graph.V(arc);
151 if (!index.TryGetValue(v, out vindex)) index[v] = vindex = indexFactory++;
153 writer.Write(uindex +
" " + vindex);
154 foreach (var ext
in Extensions)
157 ext.TryGetValue(arc, out value);
158 if (
string.IsNullOrEmpty(value) || whitespace.IsMatch(value))
159 throw new ArgumentException(
"Extension value is empty or contains whitespaces.");
160 writer.Write(
' ' + ext[arc]);
167 public void Save(
string filename)
169 using (var writer =
new StreamWriter(filename))
188 public Dictionary<string, Dictionary<Node, string>> NodeMaps {
get;
private set; }
190 public Dictionary<string, Dictionary<Arc, string>> ArcMaps {
get;
private set; }
192 public Dictionary<string, string> Attributes {
get;
private set; }
196 NodeMaps =
new Dictionary<string, Dictionary<Node, string>>();
197 ArcMaps =
new Dictionary<string, Dictionary<Arc, string>>();
198 Attributes =
new Dictionary<string, string>();
201 private static string Escape(
string s)
203 StringBuilder result =
new StringBuilder();
208 case '\n': result.Append(
"\\n");
break;
209 case '\r': result.Append(
"\\r");
break;
210 case '\t': result.Append(
"\\t");
break;
211 case '"': result.Append(
"\\\"");
break;
212 case '\\': result.Append(
"\\\\");
break;
213 default: result.Append(c);
break;
216 return result.ToString();
219 private static string Unescape(
string s)
221 StringBuilder result =
new StringBuilder();
222 bool escaped =
false;
229 case 'n': result.Append(
'\n');
break;
230 case 'r': result.Append(
'\r');
break;
231 case 't': result.Append(
'\t');
break;
232 default: result.Append(c);
break;
238 escaped = (c ==
'\\');
239 if (!escaped) result.Append(c);
242 return result.ToString();
256 buildableGraph.
Clear();
259 var nodeFromLabel =
new Dictionary<string,Node>();
263 Regex splitRegex =
new Regex(
@"\s*((""(\""|.)*"")|(\S+))\s*", RegexOptions.Compiled);
266 bool prevHeader =
false;
267 List<string> columnNames = null;
268 int labelColumnIndex = -1;
272 string line = reader.ReadLine();
273 if (line == null)
break;
275 if (line ==
"" || line[0] ==
'#')
continue;
276 List<string> tokens = splitRegex.Matches(line).Cast<Match>()
279 string s = m.Groups[1].Value;
280 if (s ==
"")
return s;
281 if (s[0] ==
'"' && s[s.Length-1] ==
'"')
282 s = Unescape(s.Substring(1, s.Length-2));
285 string first = tokens.First();
290 section = first.Substring(1);
299 case "nodes":
case "red_nodes":
case "blue_nodes":
303 columnNames = tokens;
304 for (
int i = 0; i < columnNames.Count; i++)
306 string column = columnNames[i];
307 if (column ==
"label") labelColumnIndex = i;
308 if (!NodeMaps.ContainsKey(column))
309 NodeMaps[column] =
new Dictionary<Node, string>();
315 for (
int i = 0; i < tokens.Count; i++)
317 NodeMaps[columnNames[i]][node] = tokens[i];
318 if (i == labelColumnIndex) nodeFromLabel[tokens[i]] = node;
328 columnNames = tokens;
329 foreach (var column
in columnNames)
330 if (!ArcMaps.ContainsKey(column))
331 ArcMaps[column] =
new Dictionary<Arc, string>();
335 Node u = nodeFromLabel[tokens[0]];
336 Node v = nodeFromLabel[tokens[1]];
337 Arc arc = buildableGraph.
AddArc(u, v, currDir);
338 for (
int i = 2; i < tokens.Count; i++)
339 ArcMaps[columnNames[i-2]][arc] = tokens[i];
345 Attributes[tokens[0]] = tokens[1];
355 using (var reader =
new StreamReader(filename))
356 Load(reader, directedness);
363 public void Save(TextWriter writer, IEnumerable<string> comment = null)
365 if (comment != null)
foreach (var line
in comment) writer.WriteLine(
"# " + line);
368 writer.WriteLine(
"@nodes");
369 writer.Write(
"label");
370 foreach (var kv
in NodeMaps)
if (kv.Key !=
"label") writer.Write(
' '+kv.Key);
372 foreach (var node
in Graph.Nodes())
374 writer.Write(node.Id);
375 foreach (var kv
in NodeMaps)
if (kv.Key !=
"label")
378 if (!kv.Value.TryGetValue(node, out value)) value =
"";
379 writer.Write(
" \"" + Escape(value) +
'"');
386 for (
int i = 0; i < 2; i++)
388 var arcs = (i == 0 ? Graph.Arcs().Where(arc => !Graph.IsEdge(arc)) : Graph.Arcs(
ArcFilter.Edge));
389 writer.WriteLine(i == 0 ?
"@arcs" :
"@edges");
390 if (ArcMaps.Count == 0) writer.WriteLine(
'-');
393 foreach (var kv
in ArcMaps) writer.Write(kv.Key +
' ');
396 foreach (var arc
in arcs)
398 writer.Write(Graph.U(arc).Id +
' ' + Graph.V(arc).Id);
399 foreach (var kv
in ArcMaps)
402 if (!kv.Value.TryGetValue(arc, out value)) value =
"";
403 writer.Write(
" \"" + Escape(value) +
'"');
411 if (Attributes.Count > 0)
413 writer.WriteLine(
"@attributes");
414 foreach (var kv
in Attributes)
415 writer.WriteLine(
'"' + Escape(kv.Key) +
"\" \"" + Escape(kv.Value) +
'"');
421 public void Save(
string filename, IEnumerable<string> comment = null)
423 using (var writer =
new StreamWriter(filename))
424 Save(writer, comment);