2017-06-06 05:45:22 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.OsuDifficulty.Utils
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-06-06 21:39:37 +08:00
|
|
|
|
/// An indexed stack with Push() only, which disposes items at the bottom after the capacity is full.
|
2017-06-06 05:45:22 +08:00
|
|
|
|
/// Indexing starts at the top of the stack.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class History<T> : IEnumerable<T>
|
|
|
|
|
{
|
2017-06-06 07:08:34 +08:00
|
|
|
|
public int Count { get; private set; }
|
2017-06-06 05:45:22 +08:00
|
|
|
|
|
2017-06-06 07:08:34 +08:00
|
|
|
|
private readonly T[] array;
|
2017-06-06 21:39:37 +08:00
|
|
|
|
private readonly int capacity;
|
2017-06-06 05:45:22 +08:00
|
|
|
|
private int marker; // Marks the position of the most recently added item.
|
|
|
|
|
|
2017-06-06 21:39:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the History class that is empty and has the specified capacity.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="capacity">The number of items the History can hold.</param>
|
|
|
|
|
public History(int capacity)
|
2017-06-06 05:45:22 +08:00
|
|
|
|
{
|
2017-06-06 21:39:37 +08:00
|
|
|
|
if (capacity < 0)
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
|
|
|
|
|
this.capacity = capacity;
|
|
|
|
|
array = new T[capacity];
|
|
|
|
|
marker = capacity; // Set marker to the end of the array, outside of the indexed range by one.
|
2017-06-06 05:45:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 21:39:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The most recently added item is returned at index 0.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public T this[int i]
|
2017-06-06 05:45:22 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-06-08 02:29:03 +08:00
|
|
|
|
if (i < 0 || i > Count - 1)
|
2017-06-06 05:45:22 +08:00
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
|
|
|
|
|
|
i += marker;
|
2017-06-06 21:39:37 +08:00
|
|
|
|
if (i > capacity - 1)
|
|
|
|
|
i -= capacity;
|
2017-06-06 05:45:22 +08:00
|
|
|
|
|
|
|
|
|
return array[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-06-07 00:59:46 +08:00
|
|
|
|
/// Adds the item as the most recent one in the history.
|
|
|
|
|
/// The oldest item is disposed if the history is full.
|
2017-06-06 05:45:22 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void Push(T item) // Overwrite the oldest item instead of shifting every item by one with every addition.
|
|
|
|
|
{
|
|
|
|
|
if (marker == 0)
|
2017-06-06 21:39:37 +08:00
|
|
|
|
marker = capacity - 1;
|
2017-06-06 05:45:22 +08:00
|
|
|
|
else
|
|
|
|
|
--marker;
|
|
|
|
|
|
|
|
|
|
array[marker] = item;
|
|
|
|
|
|
2017-06-06 21:39:37 +08:00
|
|
|
|
if (Count < capacity)
|
2017-06-06 05:45:22 +08:00
|
|
|
|
++Count;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 21:39:37 +08:00
|
|
|
|
/// <summary>
|
2017-06-07 00:59:46 +08:00
|
|
|
|
/// Returns an enumerator which enumerates items in the history starting from the most recently added one.
|
2017-06-06 21:39:37 +08:00
|
|
|
|
/// </summary>
|
2017-06-06 05:45:22 +08:00
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
|
|
|
{
|
2017-06-06 21:39:37 +08:00
|
|
|
|
for (int i = marker; i < capacity; ++i)
|
2017-06-06 05:45:22 +08:00
|
|
|
|
yield return array[i];
|
|
|
|
|
|
2017-06-06 21:39:37 +08:00
|
|
|
|
if (Count == capacity)
|
2017-06-06 05:45:22 +08:00
|
|
|
|
for (int i = 0; i < marker; ++i)
|
|
|
|
|
yield return array[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
}
|