Initialize(capacity);
}
- if (comparer != null && comparer != EqualityComparer<TKey>.Default) // first check for null to avoid forcing default comparer instantiation unnecessarily
+ if (comparer is not null && comparer != EqualityComparer<TKey>.Default) // first check for null to avoid forcing default comparer instantiation unnecessarily
{
_comparer = comparer;
}
// Special-case EqualityComparer<string>.Default, StringComparer.Ordinal, and StringComparer.OrdinalIgnoreCase.
// We use a non-randomized comparer for improved perf, falling back to a randomized comparer if the
// hash buckets become unbalanced.
-
if (typeof(TKey) == typeof(string))
{
- if (_comparer is null)
- {
- _comparer = (IEqualityComparer<TKey>)NonRandomizedStringEqualityComparer.WrappedAroundDefaultComparer;
- }
- else if (ReferenceEquals(_comparer, StringComparer.Ordinal))
- {
- _comparer = (IEqualityComparer<TKey>)NonRandomizedStringEqualityComparer.WrappedAroundStringComparerOrdinal;
- }
- else if (ReferenceEquals(_comparer, StringComparer.OrdinalIgnoreCase))
+ IEqualityComparer<string>? stringComparer = NonRandomizedStringEqualityComparer.GetStringComparer(_comparer);
+ if (stringComparer is not null)
{
- _comparer = (IEqualityComparer<TKey>)NonRandomizedStringEqualityComparer.WrappedAroundStringComparerOrdinalIgnoreCase;
+ _comparer = (IEqualityComparer<TKey>?)stringComparer;
}
}
}
public HashSet(IEqualityComparer<T>? comparer)
{
- if (comparer != null && comparer != EqualityComparer<T>.Default) // first check for null to avoid forcing default comparer instantiation unnecessarily
+ if (comparer is not null && comparer != EqualityComparer<T>.Default) // first check for null to avoid forcing default comparer instantiation unnecessarily
{
_comparer = comparer;
}
// Special-case EqualityComparer<string>.Default, StringComparer.Ordinal, and StringComparer.OrdinalIgnoreCase.
// We use a non-randomized comparer for improved perf, falling back to a randomized comparer if the
// hash buckets become unbalanced.
-
if (typeof(T) == typeof(string))
{
- if (_comparer is null)
- {
- _comparer = (IEqualityComparer<T>)NonRandomizedStringEqualityComparer.WrappedAroundDefaultComparer;
- }
- else if (ReferenceEquals(_comparer, StringComparer.Ordinal))
- {
- _comparer = (IEqualityComparer<T>)NonRandomizedStringEqualityComparer.WrappedAroundStringComparerOrdinal;
- }
- else if (ReferenceEquals(_comparer, StringComparer.OrdinalIgnoreCase))
+ IEqualityComparer<string>? stringComparer = NonRandomizedStringEqualityComparer.GetStringComparer(_comparer);
+ if (stringComparer is not null)
{
- _comparer = (IEqualityComparer<T>)NonRandomizedStringEqualityComparer.WrappedAroundStringComparerOrdinalIgnoreCase;
+ _comparer = (IEqualityComparer<T>?)stringComparer;
}
}
}
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
-using System.Globalization;
using System.Runtime.Serialization;
-using Internal.Runtime.CompilerServices;
namespace System.Collections.Generic
{
// that was passed in to the ctor. The caller chooses one of these singletons so that the
// GetUnderlyingEqualityComparer method can return the correct value.
- internal static readonly NonRandomizedStringEqualityComparer WrappedAroundDefaultComparer = new OrdinalComparer(EqualityComparer<string?>.Default);
- internal static readonly NonRandomizedStringEqualityComparer WrappedAroundStringComparerOrdinal = new OrdinalComparer(StringComparer.Ordinal);
- internal static readonly NonRandomizedStringEqualityComparer WrappedAroundStringComparerOrdinalIgnoreCase = new OrdinalIgnoreCaseComparer(StringComparer.OrdinalIgnoreCase);
+ private static readonly NonRandomizedStringEqualityComparer WrappedAroundDefaultComparer = new OrdinalComparer(EqualityComparer<string?>.Default);
+ private static readonly NonRandomizedStringEqualityComparer WrappedAroundStringComparerOrdinal = new OrdinalComparer(StringComparer.Ordinal);
+ private static readonly NonRandomizedStringEqualityComparer WrappedAroundStringComparerOrdinalIgnoreCase = new OrdinalIgnoreCaseComparer(StringComparer.OrdinalIgnoreCase);
private readonly IEqualityComparer<string?> _underlyingComparer;
return RandomizedStringEqualityComparer.Create(_underlyingComparer, ignoreCase: true);
}
}
+
+ public static IEqualityComparer<string>? GetStringComparer(object? comparer)
+ {
+ // Special-case EqualityComparer<string>.Default, StringComparer.Ordinal, and StringComparer.OrdinalIgnoreCase.
+ // We use a non-randomized comparer for improved perf, falling back to a randomized comparer if the
+ // hash buckets become unbalanced.
+ if (comparer is null)
+ {
+ return WrappedAroundDefaultComparer;
+ }
+ else if (ReferenceEquals(comparer, StringComparer.Ordinal))
+ {
+ return WrappedAroundStringComparerOrdinal;
+ }
+ else if (ReferenceEquals(comparer, StringComparer.OrdinalIgnoreCase))
+ {
+ return WrappedAroundStringComparerOrdinalIgnoreCase;
+ }
+
+ return null;
+ }
}
}