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

46 lines
1.2 KiB
C#
Raw Normal View History

namespace Umbraco.Cms.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>
2021-12-17 16:33:23 +01:00
public class StackQueue<T>
{
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();
public void Push(T? obj) => _linkedList.AddFirst(obj);
2021-10-29 10:14:52 +02:00
public void Enqueue(T? obj) => _linkedList.AddFirst(obj);
public T Pop()
{
T? obj = default(T);
if (_linkedList.First is not null)
{
obj = _linkedList.First.Value;
}
_linkedList.RemoveFirst();
return obj!;
}
public T Dequeue()
{
T? obj = default(T);
if (_linkedList.Last is not null)
{
obj = _linkedList.Last.Value;
}
_linkedList.RemoveLast();
return obj!;
}
2022-01-13 09:27:37 +01:00
public T? PeekStack() => _linkedList.First is not null ? _linkedList.First.Value : default;
2022-01-13 09:27:37 +01:00
public T? PeekQueue() => _linkedList.Last is not null ? _linkedList.Last.Value : default;
}
}