10
10
namespace Microsoft . AspNetCore . Internal . Dictionary
11
11
{
12
12
/// <summary>
13
- /// An <see cref="IDictionary{String, Object}"/> type for route values .
13
+ /// An <see cref="IDictionary{String, Object}"/> type to hold a small amount of items (4 or less in the common case) .
14
14
/// </summary>
15
15
internal class SmallCapacityDictionary < TKey , TValue > : IDictionary < TKey , TValue > , IReadOnlyDictionary < TKey , TValue > where TKey : notnull
16
16
{
@@ -27,6 +27,7 @@ internal class SmallCapacityDictionary<TKey, TValue> : IDictionary<TKey, TValue>
27
27
/// The new instance will take ownership of the array, and may mutate it.
28
28
/// </summary>
29
29
/// <param name="items">The items array.</param>
30
+ /// <param name="comparer"></param>
30
31
/// <returns>A new <see cref="SmallCapacityDictionary{TKey, TValue}"/>.</returns>
31
32
public static SmallCapacityDictionary < TKey , TValue > FromArray ( KeyValuePair < TKey , TValue > [ ] items , IEqualityComparer < TKey > ? comparer = null )
32
33
{
@@ -39,11 +40,14 @@ public static SmallCapacityDictionary<TKey, TValue> FromArray(KeyValuePair<TKey,
39
40
40
41
if ( items . Length > DefaultArrayThreshold )
41
42
{
42
- // Don't use dictionary for large arrays.
43
- var dict = new Dictionary < TKey , TValue > ( comparer ) ;
43
+ // Use dictionary for large arrays.
44
+ var dict = new Dictionary < TKey , TValue > ( items . Length , comparer ) ;
44
45
foreach ( var item in items )
45
46
{
46
- dict [ item . Key ] = item . Value ;
47
+ if ( item . Key != null )
48
+ {
49
+ dict [ item . Key ] = item . Value ;
50
+ }
47
51
}
48
52
49
53
return new SmallCapacityDictionary < TKey , TValue > ( comparer )
@@ -153,23 +157,16 @@ public SmallCapacityDictionary(IEqualityComparer<TKey> comparer, int capacity)
153
157
/// property names are keys, and property values are the values, and copied into the dictionary.
154
158
/// Only public instance non-index properties are considered.
155
159
/// </remarks>
156
- public SmallCapacityDictionary ( IEnumerable < KeyValuePair < TKey , TValue > > values )
160
+ public SmallCapacityDictionary ( IEnumerable < KeyValuePair < TKey , TValue > > values , IEqualityComparer < TKey > comparer = null , int capacity = 0 )
157
161
{
158
- _comparer = EqualityComparer < TKey > . Default ;
162
+ _comparer = comparer ?? EqualityComparer < TKey > . Default ;
159
163
160
- if ( values is IEnumerable < KeyValuePair < TKey , TValue > > keyValueEnumerable )
161
- {
162
- _arrayStorage = Array . Empty < KeyValuePair < TKey , TValue > > ( ) ;
164
+ _arrayStorage = new KeyValuePair < TKey , TValue > [ capacity ] ;
163
165
164
- foreach ( var kvp in keyValueEnumerable )
165
- {
166
- Add ( kvp . Key , kvp . Value ) ;
167
- }
168
-
169
- return ;
166
+ foreach ( var kvp in values )
167
+ {
168
+ Add ( kvp . Key , kvp . Value ) ;
170
169
}
171
-
172
- _arrayStorage = Array . Empty < KeyValuePair < TKey , TValue > > ( ) ;
173
170
}
174
171
175
172
/// <inheritdoc />
@@ -184,11 +181,6 @@ public TValue this[TKey key]
184
181
185
182
TryGetValue ( key , out var value ) ;
186
183
187
- if ( value == null )
188
- {
189
- ThrowKeyNotFoundException ( nameof ( key ) ) ;
190
- }
191
-
192
184
return value ;
193
185
}
194
186
@@ -604,12 +596,6 @@ private static void ThrowArgumentNullExceptionForKey()
604
596
throw new ArgumentNullException ( "key" ) ;
605
597
}
606
598
607
- [ DoesNotReturn ]
608
- private static void ThrowKeyNotFoundException ( string keyName )
609
- {
610
- throw new KeyNotFoundException ( $ "The given key '{ keyName } ' was not present in the dictionary.") ;
611
- }
612
-
613
599
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
614
600
private void EnsureCapacity ( int capacity )
615
601
{
0 commit comments