Files
Umbraco-CMS/src/Umbraco.Core/Collections/StackQueue.cs

40 lines
970 B
C#
Raw Normal View History

using System.Collections.Generic;
2021-10-29 11:03:59 +02:00
namespace Umbraco.Core.Collections
{
/// <summary>
2021-10-29 10:14:52 +02:00
/// Collection that can be both a queue and a stack.
/// </summary>
/// <typeparam name="T"></typeparam>
public class StackQueue<T>
{
2021-10-29 10:14:52 +02:00
private readonly LinkedList<T> _linkedList = new();
2021-10-29 10:14:52 +02:00
public int Count => _linkedList.Count;
2021-10-29 10:14:52 +02:00
public void Clear() => _linkedList.Clear();
2021-10-29 10:14:52 +02:00
public void Push(T obj) => _linkedList.AddFirst(obj);
public void Enqueue(T obj) => _linkedList.AddFirst(obj);
public T Pop()
{
2021-10-29 10:14:52 +02:00
T obj = _linkedList.First.Value;
_linkedList.RemoveFirst();
return obj;
}
public T Dequeue()
{
2021-10-29 10:14:52 +02:00
T obj = _linkedList.Last.Value;
_linkedList.RemoveLast();
return obj;
}
2021-10-29 10:14:52 +02:00
public T PeekStack() => _linkedList.First.Value;
2021-10-29 10:14:52 +02:00
public T PeekQueue() => _linkedList.Last.Value;
}
}