This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
141 additions
and
141 deletions.
There are no files selected for viewing
282 changes: 141 additions & 141 deletions
282
src/Microsoft.AspNetCore.Razor.Language/src/ComparerUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,141 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Language; | ||
|
||
internal static class ComparerUtilities | ||
{ | ||
public static bool Equals<T>(IReadOnlyList<T>? first, IReadOnlyList<T>? second, IEqualityComparer<T>? comparer) | ||
{ | ||
if (first == second) | ||
{ | ||
return true; | ||
} | ||
|
||
if (first is null) | ||
{ | ||
return second is null; | ||
} | ||
else if (second is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (first.Count != second.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
comparer ??= EqualityComparer<T>.Default; | ||
|
||
for (var i = 0; i < first.Count; i++) | ||
{ | ||
if (!comparer.Equals(first[i], second[i])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static void AddToHash<T>(ref HashCodeCombiner hash, IReadOnlyList<T> list, IEqualityComparer<T>? comparer) | ||
{ | ||
comparer ??= EqualityComparer<T>.Default; | ||
|
||
for (var i = 0; i < list.Count; i++) | ||
{ | ||
hash.Add(list[i], comparer); | ||
} | ||
} | ||
|
||
public static bool Equals<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>? first, IReadOnlyDictionary<TKey, TValue>? second, IEqualityComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer) | ||
where TKey : notnull | ||
{ | ||
if (first == second) | ||
{ | ||
return true; | ||
} | ||
|
||
if (first is null) | ||
{ | ||
return second is null; | ||
} | ||
else if (second is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (first.Count != second.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
keyComparer ??= EqualityComparer<TKey>.Default; | ||
valueComparer ??= EqualityComparer<TValue>.Default; | ||
|
||
// 🐇 Avoid enumerator allocations for Dictionary<TKey, TValue> | ||
if (first is Dictionary<TKey, TValue> firstDictionary | ||
&& second is Dictionary<TKey, TValue> secondDictionary) | ||
{ | ||
using var firstEnumerator = firstDictionary.GetEnumerator(); | ||
using var secondEnumerator = secondDictionary.GetEnumerator(); | ||
while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) | ||
{ | ||
if (!keyComparer.Equals(firstEnumerator.Current.Key, secondEnumerator.Current.Key) | ||
|| !valueComparer.Equals(firstEnumerator.Current.Value, secondEnumerator.Current.Value)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
Debug.Assert(!firstEnumerator.MoveNext() && !secondEnumerator.MoveNext(), "We already know the collections have same count."); | ||
return true; | ||
} | ||
else | ||
{ | ||
using var firstEnumerator = first.GetEnumerator(); | ||
using var secondEnumerator = second.GetEnumerator(); | ||
while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) | ||
{ | ||
if (!keyComparer.Equals(firstEnumerator.Current.Key, secondEnumerator.Current.Key) | ||
|| !valueComparer.Equals(firstEnumerator.Current.Value, secondEnumerator.Current.Value)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
Debug.Assert(!firstEnumerator.MoveNext() && !secondEnumerator.MoveNext(), "We already know the collections have same count."); | ||
return true; | ||
} | ||
} | ||
|
||
public static void AddToHash<TKey, TValue>(ref HashCodeCombiner hash, IReadOnlyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer) | ||
where TKey : notnull | ||
{ | ||
keyComparer ??= EqualityComparer<TKey>.Default; | ||
valueComparer ??= EqualityComparer<TValue>.Default; | ||
|
||
// 🐇 Avoid enumerator allocations for Dictionary<TKey, TValue> | ||
if (dictionary is Dictionary<TKey, TValue> typedDictionary) | ||
{ | ||
foreach (var kvp in typedDictionary) | ||
{ | ||
hash.Add(kvp.Key, keyComparer); | ||
hash.Add(kvp.Value, valueComparer); | ||
} | ||
} | ||
else | ||
{ | ||
foreach (var kvp in dictionary) | ||
{ | ||
hash.Add(kvp.Key, keyComparer); | ||
hash.Add(kvp.Value, valueComparer); | ||
} | ||
} | ||
} | ||
} | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Language; | ||
|
||
internal static class ComparerUtilities | ||
{ | ||
public static bool Equals<T>(IReadOnlyList<T>? first, IReadOnlyList<T>? second, IEqualityComparer<T>? comparer) | ||
{ | ||
if (first == second) | ||
{ | ||
return true; | ||
} | ||
|
||
if (first is null) | ||
{ | ||
return second is null; | ||
} | ||
else if (second is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (first.Count != second.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
comparer ??= EqualityComparer<T>.Default; | ||
|
||
for (var i = 0; i < first.Count; i++) | ||
{ | ||
if (!comparer.Equals(first[i], second[i])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static void AddToHash<T>(ref HashCodeCombiner hash, IReadOnlyList<T> list, IEqualityComparer<T>? comparer) | ||
{ | ||
comparer ??= EqualityComparer<T>.Default; | ||
|
||
for (var i = 0; i < list.Count; i++) | ||
{ | ||
hash.Add(list[i], comparer); | ||
} | ||
} | ||
|
||
public static bool Equals<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>? first, IReadOnlyDictionary<TKey, TValue>? second, IEqualityComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer) | ||
where TKey : notnull | ||
{ | ||
if (first == second) | ||
{ | ||
return true; | ||
} | ||
|
||
if (first is null) | ||
{ | ||
return second is null; | ||
} | ||
else if (second is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (first.Count != second.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
keyComparer ??= EqualityComparer<TKey>.Default; | ||
valueComparer ??= EqualityComparer<TValue>.Default; | ||
|
||
// 🐇 Avoid enumerator allocations for Dictionary<TKey, TValue> | ||
if (first is Dictionary<TKey, TValue> firstDictionary | ||
&& second is Dictionary<TKey, TValue> secondDictionary) | ||
{ | ||
using var firstEnumerator = firstDictionary.GetEnumerator(); | ||
using var secondEnumerator = secondDictionary.GetEnumerator(); | ||
while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) | ||
{ | ||
if (!keyComparer.Equals(firstEnumerator.Current.Key, secondEnumerator.Current.Key) | ||
|| !valueComparer.Equals(firstEnumerator.Current.Value, secondEnumerator.Current.Value)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
Debug.Assert(!firstEnumerator.MoveNext() && !secondEnumerator.MoveNext(), "We already know the collections have same count."); | ||
return true; | ||
} | ||
else | ||
{ | ||
using var firstEnumerator = first.GetEnumerator(); | ||
using var secondEnumerator = second.GetEnumerator(); | ||
while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) | ||
{ | ||
if (!keyComparer.Equals(firstEnumerator.Current.Key, secondEnumerator.Current.Key) | ||
|| !valueComparer.Equals(firstEnumerator.Current.Value, secondEnumerator.Current.Value)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
Debug.Assert(!firstEnumerator.MoveNext() && !secondEnumerator.MoveNext(), "We already know the collections have same count."); | ||
return true; | ||
} | ||
} | ||
|
||
public static void AddToHash<TKey, TValue>(ref HashCodeCombiner hash, IReadOnlyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? keyComparer, IEqualityComparer<TValue>? valueComparer) | ||
where TKey : notnull | ||
{ | ||
keyComparer ??= EqualityComparer<TKey>.Default; | ||
valueComparer ??= EqualityComparer<TValue>.Default; | ||
|
||
// 🐇 Avoid enumerator allocations for Dictionary<TKey, TValue> | ||
if (dictionary is Dictionary<TKey, TValue> typedDictionary) | ||
{ | ||
foreach (var kvp in typedDictionary) | ||
{ | ||
hash.Add(kvp.Key, keyComparer); | ||
hash.Add(kvp.Value, valueComparer); | ||
} | ||
} | ||
else | ||
{ | ||
foreach (var kvp in dictionary) | ||
{ | ||
hash.Add(kvp.Key, keyComparer); | ||
hash.Add(kvp.Value, valueComparer); | ||
} | ||
} | ||
} | ||
} |