Skip to content

Commit b770f2b

Browse files
committed
rewrite sort factory for optimization
1 parent d5c5782 commit b770f2b

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

DTD.Sort.Net.Main/Sortfactory.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,29 @@ namespace DTD.Sort.Net.Main
1212
{
1313
internal class SortFactory<T> where T : IComparable<T>
1414
{
15-
public readonly List<ISort<T>> SortLibrary;
1615

16+
public readonly Dictionary<SortType,ISort<T>> SortLibrary;
1717
public SortFactory()
1818
{
19-
SortLibrary = new List<ISort<T>>();
19+
20+
SortLibrary = new Dictionary<SortType, ISort<T>>();
2021
Assembly algorithmAssembly = Assembly.Load("DTD.Sort.Net.Algorithms");
2122

2223
Type[] algorithms = algorithmAssembly.GetTypes();
2324
foreach (Type algorithm in algorithms)
2425
{
2526

2627
var genericType = algorithm.MakeGenericType(typeof(T));
27-
var instance = algorithmAssembly.CreateInstance(genericType.FullName);
28-
SortLibrary.Add((ISort<T>)instance);
28+
var instance = algorithmAssembly.CreateInstance(genericType.FullName!);
29+
var iSortInstance = (ISort<T>) instance;
30+
31+
if (iSortInstance != null) SortLibrary[iSortInstance.Type] = iSortInstance;
2932
}
3033

3134
}
3235

33-
public ISort<T> GetSort(SortType type) => SortLibrary.First(r => r.Type == type);
36+
37+
public ISort<T> GetSort(SortType type) => SortLibrary[type];
3438

3539
}
3640
}

DTD.Sort.Net.Tests/ProductTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ProductTest
1414

1515

1616
private static IEnumerable<ISort<Product>> TestAssistant =>
17-
new SortFactory<Product>().SortLibrary;
17+
new SortFactory<Product>().SortLibrary.Values;
1818

1919

2020
[SetUp]

0 commit comments

Comments
 (0)