Skip to content

Commit

Permalink
TryPop and TryPeek methods created
Browse files Browse the repository at this point in the history
  • Loading branch information
Ts-Pytham committed Dec 27, 2022
1 parent 7ef3969 commit b5c5aa8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
23 changes: 23 additions & 0 deletions GenericCollectionsExtension/Stack/IStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,39 @@ public interface IStack<T> : ICollection<T>
/// </summary>
int Capacity { get; }

/// <summary>
/// Gets a value indicating whether the priority stack is empty.
/// </summary>
bool IsEmpty { get; }

/// <summary>
/// Returns the element at the top of the stack without removing it.
/// </summary>
/// <returns>The element at the top of the stack.</returns>
T Peek();

/// <summary>
/// Returns a value that indicates whether there is an object at the top of the
/// <see cref="IStack{T}"/>, and if one is present, copies it to the result parameter.
/// The object is not removed from the <see cref="IStack{T}"/>.
/// </summary>
/// <param name="result">If present, the object at the top of the <see cref="IStack{T}"/>; otherwise, the default value of <see cref="{T}"/>.</param>
/// <returns><see langword="true"/> if there is an object at the top of the <see cref="IStack{T}"/>; <see langword="false"/> if the <see cref="IStack{T}"/> is empty.</returns>
bool TryPeek(out T result);

/// Removes and returns the element at the top of the stack.
/// </summary>
/// <returns>The element at the top of the stack.</returns>
T Pop();

/// <summary>
/// Returns a value that indicates whether there is an object at the top of the
/// <see cref="IStack{T}"/>, and if one is present, copies it to the result parameter,
/// and removes it from the <see cref="IStack{T}"/>.
/// </summary>
/// <param name="result">If present, the object at the top of the <see cref="IStack{T}"/>; otherwise, the default value of <see cref="{T}"/>.</param>
/// <returns><see langword="true"/> if there is an object at the top of the <see cref="IStack{T}"/>; <see langword="false"/> if the <see cref="IStack{T}"/> is empty.</returns>
bool TryPop(out T result);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public interface IPriorityStack<T> : IStack<T>
/// <param name="item">The element to add to the stack.</param>
/// <param name="priority">The priority of the element.</param>
void Push(T item, int priority);

}
}
30 changes: 29 additions & 1 deletion GenericCollectionsExtension/Stack/PriorityStack/PriorityStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class PriorityStack<T> : IPriorityStack<T>, ICollection<T>, IEnumerable<T

public bool IsReadOnly => false;

/// <inheritdoc cref="IStack{T}.IsEmpty/>
public bool IsEmpty { get => Count == 0; }

/// <summary>
/// Constructs a new instance of the <see cref="PriorityStack{T}"/> class.
/// </summary>
Expand Down Expand Up @@ -91,17 +94,42 @@ public T Peek()
return _stack[Count - 1].Value;
}

/// <inheritdoc cref="IStack{T}.TryPeek(out T)"/>
public bool TryPeek(out T result)
{
if (!IsEmpty)
{
result = _stack[Count - 1].Value;
return true;
}

result = default;
return false;
}

/// <inheritdoc cref="IStack{T}.Pop"/>
public T Pop()
{
if (Count == 0)
if (IsEmpty)
throw new IndexOutOfRangeException();

var value = _stack[Count - 1].Value;
_stack.RemoveAt(Count - 1);
return value;
}

/// <inheritdoc cref="IStack{T}.TryPop(out T)"/>
public bool TryPop(out T result)
{
if (!IsEmpty)
{
result = Pop();
return true;
}
result = default;
return false;
}

/// <inheritdoc cref="IPriorityStack{T}.Push(T, int)"/>
public void Push(T item, int priority)
{
Expand Down

0 comments on commit b5c5aa8

Please sign in to comment.