2019-02-12 15:01:25 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Difficulty.Utils
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// An indexed stack with limited depth. Indexing starts at the top of the stack.
|
2019-02-12 15:01:25 +08:00
|
|
|
|
/// </summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
public class LimitedCapacityStack<T> : IEnumerable<T>
|
2019-02-12 15:01:25 +08:00
|
|
|
|
{
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The number of elements in the stack.
|
|
|
|
|
/// </summary>
|
2019-02-12 15:01:25 +08:00
|
|
|
|
public int Count { get; private set; }
|
|
|
|
|
|
|
|
|
|
private readonly T[] array;
|
|
|
|
|
private readonly int capacity;
|
|
|
|
|
private int marker; // Marks the position of the most recently added item.
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// Constructs a new <see cref="LimitedCapacityStack{T}"/>.
|
2019-02-12 15:01:25 +08:00
|
|
|
|
/// </summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// <param name="capacity">The number of items the stack can hold.</param>
|
|
|
|
|
public LimitedCapacityStack(int capacity)
|
2019-02-12 15:01:25 +08:00
|
|
|
|
{
|
|
|
|
|
if (capacity < 0)
|
2019-11-28 22:21:21 +08:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(capacity));
|
2019-02-12 15:01:25 +08:00
|
|
|
|
|
|
|
|
|
this.capacity = capacity;
|
|
|
|
|
array = new T[capacity];
|
|
|
|
|
marker = capacity; // Set marker to the end of the array, outside of the indexed range by one.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// Retrieves the item at an index in the stack.
|
2019-02-12 15:01:25 +08:00
|
|
|
|
/// </summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// <param name="i">The index of the item to retrieve. The top of the stack is returned at index 0.</param>
|
2019-02-12 15:01:25 +08:00
|
|
|
|
public T this[int i]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (i < 0 || i > Count - 1)
|
2019-11-28 21:52:05 +08:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(i));
|
2019-02-12 15:01:25 +08:00
|
|
|
|
|
|
|
|
|
i += marker;
|
|
|
|
|
if (i > capacity - 1)
|
|
|
|
|
i -= capacity;
|
|
|
|
|
|
|
|
|
|
return array[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// Pushes an item to this <see cref="LimitedCapacityStack{T}"/>.
|
2019-02-12 15:01:25 +08:00
|
|
|
|
/// </summary>
|
2019-02-19 12:51:19 +08:00
|
|
|
|
/// <param name="item">The item to push.</param>
|
|
|
|
|
public void Push(T item)
|
2019-02-12 15:01:25 +08:00
|
|
|
|
{
|
2019-02-19 12:51:19 +08:00
|
|
|
|
// Overwrite the oldest item instead of shifting every item by one with every addition.
|
2019-02-12 15:01:25 +08:00
|
|
|
|
if (marker == 0)
|
|
|
|
|
marker = capacity - 1;
|
|
|
|
|
else
|
|
|
|
|
--marker;
|
|
|
|
|
|
|
|
|
|
array[marker] = item;
|
|
|
|
|
|
|
|
|
|
if (Count < capacity)
|
|
|
|
|
++Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns an enumerator which enumerates items in the history starting from the most recently added one.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
for (int i = marker; i < capacity; ++i)
|
|
|
|
|
yield return array[i];
|
|
|
|
|
|
|
|
|
|
if (Count == capacity)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2019-02-12 15:01:25 +08:00
|
|
|
|
for (int i = 0; i < marker; ++i)
|
|
|
|
|
yield return array[i];
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
2019-02-12 15:01:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
}
|