/
ConnectInfoJson.cs
146 lines (131 loc) · 4.6 KB
/
ConnectInfoJson.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
using System.Text.Json;
using Common.PieceInfo;
using JkwExtensions;
namespace MainCli;
internal class ConnectInfoJson : IMainRunner
{
JsonSerializerOptions serializeOption = new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new PointFJsonConverter(),
new PointArrayJsonConverter(),
new PointFArrayJsonConverter(),
},
};
public async Task Run()
{
var pieceInfoDirectory = @"../piece-info";
var connectionInfoDirectory = @"../connection-info";
if (!Directory.Exists(connectionInfoDirectory))
{
Directory.CreateDirectory(connectionInfoDirectory);
}
var pieceInfos = (await Directory.GetFiles(pieceInfoDirectory)
.OrderBy(x => x)
//.Take(100)
.Select(x => ReadFromFile(x))
.WhenAll())
.ToArray();
var connections = pieceInfos
.Select(info => new ConnectInfo
{
PieceName = info.Name,
Edges = info.Edges
.Select((x, i) => new ConnectEdge { Index = i, Connection = new() })
.ToArray(),
})
.ToList();
var arr = pieceInfos.Zip(connections)
.Select((x, i) =>
(
Index: i,
PieceInfo: x.First,
ConnectInfo: x.Second
))
.ToArray();
foreach (var (myIndex, myInfo, myConnectInfo) in arr)
{
Console.WriteLine(myInfo.Name);
var connection = arr
.Where(x => x.Index > myIndex)
.AsParallel()
.Select(other =>
{
var (otherIndex, otherInfo, otherConnectInfo) = other;
// Test is heavy operation
return Test(myInfo, otherInfo)
.Select(x =>
{
var (myEdge, otherEdge, value) = x;
return (
MyEdge: myEdge,
OtherInfo: otherInfo,
OtherConnectInfo: otherConnectInfo,
OtherEdge: otherEdge,
Value: value
);
})
.ToArray();
})
.SelectMany(x => x)
.ToList();
foreach (var (myEdge, otherInfo, otherConnectInfo, otherEdge, value) in connection)
{
myConnectInfo.Edges[myEdge].Connection.Add(new ConnectTarget
{
PieceName = otherInfo.Name,
EdgeIndex = otherEdge,
Value = value,
});
otherConnectInfo.Edges[otherEdge].Connection.Add(new ConnectTarget
{
PieceName = myInfo.Name,
EdgeIndex = myEdge,
Value = value,
});
}
}
foreach (var connectInfo in connections)
{
connectInfo.Edges = connectInfo.Edges
.Select(x => new ConnectEdge
{
Index = x.Index,
Connection = x.Connection
.OrderBy(x => x.Value)
.GroupBy(x => x.PieceName)
.Select(x => x.First())
.ToList(),
})
.ToArray();
}
foreach (var connectInfo in connections)
{
var connectInfoPath = Path.Join(connectionInfoDirectory, $"{connectInfo.PieceName}.json");
var jsonText = JsonSerializer.Serialize(connectInfo, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(connectInfoPath, jsonText);
}
}
public async Task<PieceInfo> ReadFromFile(string filepath)
{
var text = await File.ReadAllTextAsync(filepath);
var pieceInfo = JsonSerializer.Deserialize<PieceInfo>(text, serializeOption);
return pieceInfo;
}
private IEnumerable<(int MyEdge, int OtherEdge, float Value)> Test(PieceInfo me, PieceInfo other)
{
for (var i = 0; i < me.Edges.Count; i++)
{
for (var j = 0; j < other.Edges.Count; j++)
{
var (result, value) = me.Edges[i].Test(other.Edges[j]);
if (result)
{
yield return (i, j, value);
}
}
}
}
}