11using Newtonsoft . Json . Linq ;
22using System ;
3+ using System . Collections . Concurrent ;
34using System . Collections . Generic ;
45using System . Linq ;
56using 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