/
Service4.cs
244 lines (200 loc) · 8.76 KB
/
Service4.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System.Runtime.CompilerServices;
namespace PerfDemo.Services;
public class Service4
{
private readonly Dictionary<string, MasterPart?> _masterPartsByPartNumber;
public Service4(MasterPart[] masterParts, Part[] parts)
{
var masterPartsInfo = new MasterPartsInfo(masterParts);
var partsInfo = new PartsInfo(parts);
_masterPartsByPartNumber = BuildDictionary(masterPartsInfo, partsInfo);
}
public MasterPart? FindMatchedPart(string partNumber)
{
partNumber = partNumber.Trim();
if (partNumber.Length < 3) return null;
partNumber = partNumber.ToUpper();
return _masterPartsByPartNumber.GetValueOrDefault(partNumber);
}
private static Dictionary<string, MasterPart?> BuildDictionary(MasterPartsInfo masterPartsInfo, PartsInfo partsInfo)
{
var masterPartsByPartNumber = new Dictionary<string, MasterPart?>();
for (var i = 0; i < partsInfo.PartNumbers.Length; i++)
{
var partNumber = partsInfo.PartNumbers[i];
var match = FindMatchForPartNumber(partNumber, masterPartsInfo.SuffixesByLength);
match ??= FindMatchForPartNumber(partNumber, masterPartsInfo.SuffixesByNoHyphensLength);
if (match is not null)
{
masterPartsByPartNumber.TryAdd(partNumber, match);
}
}
for (var i = masterPartsInfo.MasterPartNumbers.Length - 1; i >= 0; i--)
{
var masterPart = masterPartsInfo.MasterPartNumbers[i];
if (partsInfo.SuffixesByLength.TryGetValue(masterPart.PartNumber.Length, out var suffixDictionary)
&& suffixDictionary is not null)
{
if (suffixDictionary.TryGetValue(masterPart.PartNumber, out var originalParts) && originalParts is not null)
{
for (var j = originalParts.Count - 1; j >= 0; j--)
{
masterPartsByPartNumber.TryAdd(originalParts[j], masterPart);
}
}
}
}
return masterPartsByPartNumber;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static MasterPart? FindMatchForPartNumber(
ReadOnlySpan<char> partNumber,
Dictionary<int, Dictionary<string, MasterPart>> suffixByLength)
{
if (suffixByLength.TryGetValue(partNumber.Length, out var masterPartBySuffix) && masterPartBySuffix != null)
{
masterPartBySuffix.TryGetValue(partNumber.ToString(), out var match);
return match;
}
return null;
}
private sealed class MasterPartsInfo
{
public MasterPart[] MasterPartNumbers { get; private set; } = default!;
public MasterPart[] MasterPartNumbersNoHyphens { get; private set; } = default!;
public Dictionary<int, Dictionary<string, MasterPart>> SuffixesByLength { get; private set; } = default!;
public Dictionary<int, Dictionary<string, MasterPart>> SuffixesByNoHyphensLength { get; private set; } = default!;
public MasterPartsInfo(MasterPart[] masterParts)
{
MasterPartNumbers = masterParts
.Where(x => x.PartNumber.Length > 2)
.OrderBy(x => x.PartNumber.Length)
.DistinctBy(x => x.PartNumber)
.ToArray();
MasterPartNumbersNoHyphens = masterParts
.Where(x => x.PartNumberNoHyphens.Length > 2)
.OrderBy(x => x.PartNumberNoHyphens.Length)
.DistinctBy(x => x.PartNumberNoHyphens)
.ToArray();
SuffixesByLength = GenerateDictionary(MasterPartNumbers, false);
SuffixesByNoHyphensLength = GenerateDictionary(MasterPartNumbersNoHyphens, true);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Dictionary<int, Dictionary<string, MasterPart>> GenerateDictionary(MasterPart[] masterPartNumbers, bool useNoHyphen)
{
var suffixesByLength = new Dictionary<int, Dictionary<string, MasterPart>>(51);
var startIndexByLength = GenerateStartIndexesByLengthDictionary(masterPartNumbers, useNoHyphen);
for (var length = 3; length <= 50; length++)
{
var tempDictionary = new Dictionary<string, MasterPart>();
if (startIndexByLength.TryGetValue(length, out var startIndex) && startIndex is not null)
{
for (var i = startIndex.Value; i < masterPartNumbers.Length; i++)
{
var suffix = useNoHyphen
? masterPartNumbers[i].PartNumberNoHyphens[^length..]
: masterPartNumbers[i].PartNumber[^length..];
tempDictionary.TryAdd(suffix, masterPartNumbers[i]);
}
}
suffixesByLength[length] = tempDictionary;
}
return suffixesByLength;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Dictionary<int, int?> GenerateStartIndexesByLengthDictionary(MasterPart[] masterPartNumbers, bool useNoHyphen)
{
var startIndexesByLength = new Dictionary<int, int?>(51);
for (var i = 0; i <= 50; i++)
{
startIndexesByLength[i] = null;
}
for (var i = 0; i < masterPartNumbers.Length; i++)
{
if (useNoHyphen)
{
var length = masterPartNumbers[i].PartNumberNoHyphens.Length;
if (startIndexesByLength[length] is null)
startIndexesByLength[length] = i;
}
else
{
var length = masterPartNumbers[i].PartNumber.Length;
if (startIndexesByLength[length] is null)
startIndexesByLength[length] = i;
}
}
BackwardFill(startIndexesByLength);
return startIndexesByLength;
}
}
private sealed class PartsInfo
{
public string[] PartNumbers { get; private set; } = default!;
public Dictionary<int, Dictionary<string, List<string>>> SuffixesByLength { get; private set; } = default!;
public PartsInfo(Part[] parts)
{
PartNumbers = parts
.Select(x => x.PartNumber.Trim().ToUpper())
.Where(x => x.Length > 2)
.OrderBy(x => x.Length)
.Distinct()
.ToArray();
SuffixesByLength = GenerateDictionary(PartNumbers);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Dictionary<int, Dictionary<string, List<string>>> GenerateDictionary(string[] partNumbers)
{
var suffixesByLength = new Dictionary<int, Dictionary<string, List<string>>>(51);
var startIndexesByLength = new Dictionary<int, int?>(51);
for (var i = 0; i <= 50; i++)
{
startIndexesByLength[i] = null;
}
for (var i = 0; i < partNumbers.Length; i++)
{
var length = partNumbers[i].Length;
if (startIndexesByLength[length] is null)
startIndexesByLength[length] = i;
}
BackwardFill(startIndexesByLength);
for (var length = 4; length <= 50; length++)
{
var tempDictionary = new Dictionary<string, List<string>>();
if (startIndexesByLength.TryGetValue(length + 1, out var startIndex) && startIndex is not null)
{
for (var i = startIndex.Value; i < partNumbers.Length; i++)
{
var suffix = partNumbers[i][^length..];
if (tempDictionary.TryGetValue(suffix, out var originalPartNumbers))
{
originalPartNumbers.Add(partNumbers[i]);
}
else
{
tempDictionary.TryAdd(suffix, new List<string>() { partNumbers[i] });
}
}
}
suffixesByLength[length] = tempDictionary;
}
return suffixesByLength;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void BackwardFill(Dictionary<int, int?> dictionary)
{
var temp = dictionary[50];
for (var i = 50; i >= 0; i--)
{
if (dictionary[i] is null)
{
dictionary[i] = temp;
}
else
{
temp = dictionary[i];
}
}
}
}