Skip to content

Commit 41e2ff1

Browse files
committed
Thread safe dictionary
Fixed a rare crash that could occur when two combinations could generate two identical solutions (classic dictionaries don't handle that very well)
1 parent 7006272 commit 41e2ff1

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

TFT Comp Creator 2/Form1.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TFT Comp Creator 2/Form1.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json.Linq;
22
using System;
33
using System.CodeDom.Compiler;
4+
using System.Collections.Concurrent;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Threading;
@@ -301,9 +302,9 @@ public void Creation()
301302

302303
int size = Convert.ToInt32(min_comp_size.Value);
303304

304-
305305

306-
Dictionary<List<string>, int> parallel_results = FindCombinations2(size, nodes, excluded_comp_champions, tempIncludeTrait, tempIncludeSpatula);
306+
307+
ConcurrentDictionary<List<string>, int> parallel_results = FindCombinations2(size, nodes, excluded_comp_champions, tempIncludeTrait, tempIncludeSpatula);
307308

308309
// Filter, sort, and take the top 10
309310
var top10 = parallel_results
@@ -322,6 +323,7 @@ public void Creation()
322323
}
323324
}
324325

326+
//Creation();
325327

326328
Print("done");
327329

TFT Comp Creator 2/Utility.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Newtonsoft.Json.Linq;
22
using System;
3+
using System.Collections.Concurrent;
34
using System.Collections.Generic;
45
using System.Linq;
56
using System.Threading;
@@ -400,13 +401,14 @@ public static List<string> GetTopTraits(List<string> championList, int depthLeve
400401
*/
401402

402403

403-
public static Dictionary<List<string>, int> FindCombinations2(int CompSize, List<string> items, List<string> excluded_comp_champions, List<string> tempIncludeTrait, List<string> tempIncludeSpatula)
404+
public static ConcurrentDictionary<List<string>, int> FindCombinations2(int CompSize, List<string> items, List<string> excluded_comp_champions, List<string> tempIncludeTrait, List<string> tempIncludeSpatula)
404405
{
405406
var combinations = GetCombs(items.Count(), CompSize);
406407

407408
int Synergy = 0;
408409

409-
Dictionary<List<string>, int> parallel_results = new Dictionary<List<string>, int>();
410+
// this new dictionary is thread safe, keep it or crash the whole program
411+
ConcurrentDictionary<List<string>, int> parallel_results = new ConcurrentDictionary<List<string>, int>();
410412

411413
CancellationTokenSource cts = new CancellationTokenSource();
412414
Parallel.ForEach(combinations, new ParallelOptions { CancellationToken = cts.Token }, combination =>
@@ -419,7 +421,12 @@ public static Dictionary<List<string>, int> FindCombinations2(int CompSize, List
419421

420422
if (CheckCompValidity(comp, excluded_comp_champions))
421423
{
422-
parallel_results.Add(comp, Synergy);
424+
// ensure we don't insert comp that has already been added by another thread
425+
if (!parallel_results.ContainsKey(comp))
426+
{
427+
parallel_results.TryAdd(comp, Synergy);
428+
}
429+
423430
}
424431

425432
});

0 commit comments

Comments
 (0)