Skip to content

Commit

Permalink
Add readonly Capacity property to PriorityQueue<TElement, TPriority> (#…
Browse files Browse the repository at this point in the history
…110727)

* Add readonly Capacity property to PriorityQueue<TElement, TPriority>

* Add test for EnsureCapacity and TrimExcess

* remove param

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

Co-authored-by: Stephen Toub <[email protected]>

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

Co-authored-by: Stephen Toub <[email protected]>

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

Co-authored-by: Stephen Toub <[email protected]>

---------

Co-authored-by: Stephen Toub <[email protected]>
  • Loading branch information
xPaw and stephentoub authored Jan 22, 2025
1 parent 4758103 commit 4dabc7b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libraries/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ public PriorityQueue(int initialCapacity) { }
public PriorityQueue(int initialCapacity, System.Collections.Generic.IComparer<TPriority>? comparer) { }
public System.Collections.Generic.IComparer<TPriority> Comparer { get { throw null; } }
public int Count { get { throw null; } }
public int Capacity { get { throw null; } }
public System.Collections.Generic.PriorityQueue<TElement, TPriority>.UnorderedItemsCollection UnorderedItems { get { throw null; } }
public void Clear() { }
public TElement Dequeue() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public PriorityQueue(IEnumerable<(TElement Element, TPriority Priority)> items,
/// </summary>
public int Count => _size;

/// <summary>
/// Gets the total numbers of elements the queue's backing storage can hold without resizing.
/// </summary>
public int Capacity => _nodes.Length;

/// <summary>
/// Gets the priority comparer used by the <see cref="PriorityQueue{TElement, TPriority}"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ protected PriorityQueue<TElement, TPriority> CreatePriorityQueue(
return queue;
}

[Theory]
[InlineData(1)]
[InlineData(100)]
public void CreateWithCapacity_EqualsCapacityProperty(int capacity)
{
var queue = new PriorityQueue<TElement, TPriority>(capacity);
Assert.Equal(capacity, queue.Capacity);
}

[Fact]
public void PriorityQueue_EnsureCapacityThenTrimExcess_CapacityUpdates()
{
var queue = new PriorityQueue<TElement, TPriority>(2);
Assert.Equal(2, queue.Capacity);

queue.EnsureCapacity(12);
Assert.InRange(queue.Capacity, 12, int.MaxValue);

queue.TrimExcess();
Assert.Equal(0, queue.Capacity);
}

#endregion

#region Constructors
Expand Down

0 comments on commit 4dabc7b

Please sign in to comment.