Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita4b53f5

Browse files
committed
Feedback
1 parent8458629 commita4b53f5

File tree

2 files changed

+46
-186
lines changed

2 files changed

+46
-186
lines changed

‎src/Shared/Dictionary/SmallCapacityDictionary.cs‎

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespaceMicrosoft.AspNetCore.Internal.Dictionary
1111
{
1212
/// <summary>
13-
/// An <see cref="IDictionary{String, Object}"/> typefor route values.
13+
/// An <see cref="IDictionary{String, Object}"/> typeto hold a small amount of items (4 or less in the common case).
1414
/// </summary>
1515
internalclassSmallCapacityDictionary<TKey,TValue>:IDictionary<TKey,TValue>,IReadOnlyDictionary<TKey,TValue>whereTKey:notnull
1616
{
@@ -27,6 +27,7 @@ internal class SmallCapacityDictionary<TKey, TValue> : IDictionary<TKey, TValue>
2727
/// The new instance will take ownership of the array, and may mutate it.
2828
/// </summary>
2929
/// <param name="items">The items array.</param>
30+
/// <param name="comparer"></param>
3031
/// <returns>A new <see cref="SmallCapacityDictionary{TKey, TValue}"/>.</returns>
3132
publicstaticSmallCapacityDictionary<TKey,TValue>FromArray(KeyValuePair<TKey,TValue>[]items,IEqualityComparer<TKey>?comparer=null)
3233
{
@@ -39,11 +40,14 @@ public static SmallCapacityDictionary<TKey, TValue> FromArray(KeyValuePair<TKey,
3940

4041
if(items.Length>DefaultArrayThreshold)
4142
{
42-
//Don't use dictionary for large arrays.
43-
vardict=newDictionary<TKey,TValue>(comparer);
43+
//Use dictionary for large arrays.
44+
vardict=newDictionary<TKey,TValue>(items.Length,comparer);
4445
foreach(variteminitems)
4546
{
46-
dict[item.Key]=item.Value;
47+
if(item.Key!=null)
48+
{
49+
dict[item.Key]=item.Value;
50+
}
4751
}
4852

4953
returnnewSmallCapacityDictionary<TKey,TValue>(comparer)
@@ -153,23 +157,16 @@ public SmallCapacityDictionary(IEqualityComparer<TKey> comparer, int capacity)
153157
/// property names are keys, and property values are the values, and copied into the dictionary.
154158
/// Only public instance non-index properties are considered.
155159
/// </remarks>
156-
publicSmallCapacityDictionary(IEnumerable<KeyValuePair<TKey,TValue>>values)
160+
publicSmallCapacityDictionary(IEnumerable<KeyValuePair<TKey,TValue>>values,IEqualityComparer<TKey>comparer=null,intcapacity=0)
157161
{
158-
_comparer=EqualityComparer<TKey>.Default;
162+
_comparer=comparer??EqualityComparer<TKey>.Default;
159163

160-
if(valuesisIEnumerable<KeyValuePair<TKey,TValue>>keyValueEnumerable)
161-
{
162-
_arrayStorage=Array.Empty<KeyValuePair<TKey,TValue>>();
164+
_arrayStorage=newKeyValuePair<TKey,TValue>[capacity];
163165

164-
foreach(varkvpinkeyValueEnumerable)
165-
{
166-
Add(kvp.Key,kvp.Value);
167-
}
168-
169-
return;
166+
foreach(varkvpinvalues)
167+
{
168+
Add(kvp.Key,kvp.Value);
170169
}
171-
172-
_arrayStorage=Array.Empty<KeyValuePair<TKey,TValue>>();
173170
}
174171

175172
/// <inheritdoc />
@@ -184,11 +181,6 @@ public TValue this[TKey key]
184181

185182
TryGetValue(key,outvarvalue);
186183

187-
if(value==null)
188-
{
189-
ThrowKeyNotFoundException(nameof(key));
190-
}
191-
192184
returnvalue;
193185
}
194186

@@ -604,12 +596,6 @@ private static void ThrowArgumentNullExceptionForKey()
604596
thrownewArgumentNullException("key");
605597
}
606598

607-
[DoesNotReturn]
608-
privatestaticvoidThrowKeyNotFoundException(stringkeyName)
609-
{
610-
thrownewKeyNotFoundException($"The given key '{keyName}' was not present in the dictionary.");
611-
}
612-
613599
[MethodImpl(MethodImplOptions.AggressiveInlining)]
614600
privatevoidEnsureCapacity(intcapacity)
615601
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp