@@ -19,7 +19,7 @@ internal class SmallCapacityDictionary<TKey, TValue> : IDictionary<TKey, TValue>
19
19
20
20
internal KeyValuePair < TKey , TValue > [ ] _arrayStorage ;
21
21
private int _count ;
22
- private Dictionary < TKey , TValue > ? _backup ;
22
+ internal Dictionary < TKey , TValue > ? _backup ;
23
23
private IEqualityComparer < TKey > _comparer ;
24
24
25
25
/// <summary>
@@ -28,23 +28,25 @@ internal class SmallCapacityDictionary<TKey, TValue> : IDictionary<TKey, TValue>
28
28
/// </summary>
29
29
/// <param name="items">The items array.</param>
30
30
/// <returns>A new <see cref="SmallCapacityDictionary{TKey, TValue}"/>.</returns>
31
- public static SmallCapacityDictionary < TKey , TValue > FromArray ( KeyValuePair < TKey , TValue > [ ] items )
31
+ public static SmallCapacityDictionary < TKey , TValue > FromArray ( KeyValuePair < TKey , TValue > [ ] items , IEqualityComparer < TKey > ? comparer = null )
32
32
{
33
33
if ( items == null )
34
34
{
35
35
throw new ArgumentNullException ( nameof ( items ) ) ;
36
36
}
37
37
38
+ comparer = comparer ?? EqualityComparer < TKey > . Default ;
39
+
38
40
if ( items . Length > DefaultArrayThreshold )
39
41
{
40
42
// Don't use dictionary for large arrays.
41
- var dict = new Dictionary < TKey , TValue > ( ) ;
43
+ var dict = new Dictionary < TKey , TValue > ( comparer ) ;
42
44
foreach ( var item in items )
43
45
{
44
46
dict [ item . Key ] = item . Value ;
45
47
}
46
48
47
- return new SmallCapacityDictionary < TKey , TValue > ( )
49
+ return new SmallCapacityDictionary < TKey , TValue > ( comparer )
48
50
{
49
51
_backup = dict
50
52
} ;
@@ -328,7 +330,7 @@ public void Add(TKey key, TValue value)
328
330
329
331
if ( ContainsKeyArray ( key ) )
330
332
{
331
- throw new ArgumentException ( $ "An element with the key '{ nameof ( key ) } ' already exists in the { nameof ( SmallCapacityDictionary < TKey , TValue > ) } .") ;
333
+ throw new ArgumentException ( $ "An element with the key '{ key } ' already exists in the { nameof ( SmallCapacityDictionary < TKey , TValue > ) } .", nameof ( key ) ) ;
332
334
}
333
335
334
336
_arrayStorage [ _count ] = new KeyValuePair < TKey , TValue > ( key , value ) ;
@@ -618,7 +620,7 @@ private void EnsureCapacitySlow(int capacity)
618
620
{
619
621
if ( _arrayStorage . Length < capacity )
620
622
{
621
- if ( capacity < DefaultArrayThreshold )
623
+ if ( capacity > DefaultArrayThreshold )
622
624
{
623
625
_backup = new Dictionary < TKey , TValue > ( capacity ) ;
624
626
foreach ( var item in _arrayStorage )
@@ -747,14 +749,16 @@ public void Dispose()
747
749
public bool MoveNext ( )
748
750
{
749
751
var dictionary = _dictionary ;
752
+ if ( dictionary . _arrayStorage . Length >= _index )
753
+ {
754
+ return false ;
755
+ }
750
756
751
- // The uncommon case is that the propertyStorage is in use
752
757
Current = dictionary . _arrayStorage [ _index ] ;
753
758
_index ++ ;
754
759
return true ;
755
760
}
756
761
757
-
758
762
/// <inheritdoc />
759
763
public void Reset ( )
760
764
{
0 commit comments