14
14
using System . Globalization ;
15
15
using System . Linq ;
16
16
using System . Reflection ;
17
+ using System . Text ;
17
18
using OpenRA . Primitives ;
18
19
using OpenRA . Support ;
19
20
using OpenRA . Traits ;
@@ -432,15 +433,26 @@ public static Dictionary<TKey, TSource> ToDictionaryWithConflictLog<TSource, TKe
432
433
public static Dictionary < TKey , TElement > ToDictionaryWithConflictLog < TSource , TKey , TElement > (
433
434
this IEnumerable < TSource > source , Func < TSource , TKey > keySelector , Func < TSource , TElement > elementSelector ,
434
435
string debugName , Func < TKey , string > logKey = null , Func < TElement , string > logValue = null )
436
+ {
437
+ var output = new Dictionary < TKey , TElement > ( ) ;
438
+ IntoDictionaryWithConflictLog ( source , keySelector , elementSelector , debugName , output , logKey , logValue ) ;
439
+ return output ;
440
+ }
441
+
442
+ public static void IntoDictionaryWithConflictLog < TSource , TKey , TElement > (
443
+ this IEnumerable < TSource > source , Func < TSource , TKey > keySelector , Func < TSource , TElement > elementSelector ,
444
+ string debugName , Dictionary < TKey , TElement > output ,
445
+ Func < TKey , string > logKey = null , Func < TElement , string > logValue = null )
435
446
{
436
447
// Fall back on ToString() if null functions are provided:
437
448
logKey ??= s => s . ToString ( ) ;
438
449
logValue ??= s => s . ToString ( ) ;
439
450
440
451
// Try to build a dictionary and log all duplicates found (if any):
441
- var dupKeys = new Dictionary < TKey , List < string > > ( ) ;
452
+ Dictionary < TKey , List < string > > dupKeys = null ;
442
453
var capacity = source is ICollection < TSource > collection ? collection . Count : 0 ;
443
- var d = new Dictionary < TKey , TElement > ( capacity ) ;
454
+ output . Clear ( ) ;
455
+ output . EnsureCapacity ( capacity ) ;
444
456
foreach ( var item in source )
445
457
{
446
458
var key = keySelector ( item ) ;
@@ -451,14 +463,15 @@ public static Dictionary<TKey, TElement> ToDictionaryWithConflictLog<TSource, TK
451
463
continue ;
452
464
453
465
// Check for a key conflict:
454
- if ( ! d . TryAdd ( key , element ) )
466
+ if ( ! output . TryAdd ( key , element ) )
455
467
{
468
+ dupKeys ??= new Dictionary < TKey , List < string > > ( ) ;
456
469
if ( ! dupKeys . TryGetValue ( key , out var dupKeyMessages ) )
457
470
{
458
471
// Log the initial conflicting value already inserted:
459
472
dupKeyMessages = new List < string >
460
473
{
461
- logValue ( d [ key ] )
474
+ logValue ( output [ key ] )
462
475
} ;
463
476
dupKeys . Add ( key , dupKeyMessages ) ;
464
477
}
@@ -469,15 +482,14 @@ public static Dictionary<TKey, TElement> ToDictionaryWithConflictLog<TSource, TK
469
482
}
470
483
471
484
// If any duplicates were found, throw a descriptive error
472
- if ( dupKeys . Count > 0 )
485
+ if ( dupKeys != null )
473
486
{
474
- var badKeysFormatted = string . Join ( ", " , dupKeys . Select ( p => $ "{ logKey ( p . Key ) } : [{ string . Join ( "," , p . Value ) } ]") ) ;
475
- var msg = $ "{ debugName } , duplicate values found for the following keys: { badKeysFormatted } ";
476
- throw new ArgumentException ( msg ) ;
487
+ var badKeysFormatted = new StringBuilder (
488
+ $ "{ debugName } , duplicate values found for the following keys: ") ;
489
+ foreach ( var p in dupKeys )
490
+ badKeysFormatted . Append ( $ "{ logKey ( p . Key ) } : [{ string . Join ( "," , p . Value ) } ]") ;
491
+ throw new ArgumentException ( badKeysFormatted . ToString ( ) ) ;
477
492
}
478
-
479
- // Return the dictionary we built:
480
- return d ;
481
493
}
482
494
483
495
public static Color ColorLerp ( float t , Color c1 , Color c2 )
0 commit comments