-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapping.cs
53 lines (43 loc) · 1.47 KB
/
Mapping.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
using System;
using System.Collections.Generic;
using System.Linq;
using QuikGraph;
namespace GraphRewriteEngine
{
public class Mapping<T> {
public Dictionary<T, T> M; //make private later
public IEnumerable<T> Keys() {
return M.Keys;
}
public IEnumerable<T> Values() {
return M.Values;
}
//Empty starting mapping
public Mapping() {
M = new Dictionary<T, T>();
}
public Mapping(Dictionary<T, T> m) {
M = new Dictionary<T, T>(m);
}
public bool IsSubset(IEnumerable<T> A, IEnumerable<T> B) {
return !A.Except(B).Any();
}
public Mapping<T> Compose(Mapping<T> m) {
if (IsSubset(this.Values(), m.Keys())) {
var composition = new Dictionary<T, T>();
foreach (T key in this.Keys()) {
composition[key] = m.M[this.M[key]];
}
return new Mapping<T>(composition);
}
throw new NotSupportedException();
}
public override string ToString() {
string output = "";
foreach (KeyValuePair<T, T> entry in M) {
output += $"{entry.Key.ToString()} -> {entry.Value.ToString()}\n";
}
return output;
}
}
}