Skip to content

Commit

Permalink
Misc PriorityQueue fixes (#48539)
Browse files Browse the repository at this point in the history
* PriorityQueue fixes

* add missing enumerator invalidations

* Update src/libraries/System.Collections/src/System/Collections/Generic/PriorityQueue.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* add Debug assertion for heap arity

* presize buffer if EnqueueRange input is ICollection

* move capacity check outside of the core Grow method

* remove ICollection detection from EnumerableHelpers

* address feedback

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
eiriktsarpalis and stephentoub authored Mar 5, 2021
1 parent 9849163 commit dc73562
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ namespace System.Collections.Generic
/// </summary>
internal static partial class EnumerableHelpers
{
/// <summary>Attempt to determine the count of the source enumerable without forcing an enumeration.</summary>
/// <param name="source">The source enumerable.</param>
/// <param name="count">The count determined by the type test.</param>
/// <returns>
/// True if the source enumerable could be determined without enumerating, false otherwise.
/// </returns>
internal static bool TryGetCount<T>(IEnumerable<T> source, out int count)
{
if (source is ICollection<T> ict)
{
count = ict.Count;
return true;
}

count = 0;
return false;
}

/// <summary>Converts an enumerable to an array using the same logic as List{T}.</summary>
/// <param name="source">The enumerable to convert.</param>
/// <param name="length">The number of items stored in the resulting array, 0-indexed.</param>
Expand Down
Loading

0 comments on commit dc73562

Please sign in to comment.