mirror of
https://github.com/osukey/osukey.git
synced 2025-07-01 16:29:58 +09:00
Implement dynamic previous hitobject retention for Skill class
There is no reason we should be limiting skills to knowing only the previous 2 objects. This originally existed as an angle implementation detail of the original pp+ codebase which made its way here, but didn't get used in the same way.
This commit is contained in:
110
osu.Game/Rulesets/Difficulty/Utils/ReverseQueue.cs
Normal file
110
osu.Game/Rulesets/Difficulty/Utils/ReverseQueue.cs
Normal file
@ -0,0 +1,110 @@
|
||||
// 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>
|
||||
/// An indexed queue where items are indexed beginning from the end instead of the start.
|
||||
/// </summary>
|
||||
public class ReverseQueue<T> : IEnumerable<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// The number of elements in the <see cref="ReverseQueue{T}"/>.
|
||||
/// </summary>
|
||||
public int Count { get; private set; }
|
||||
|
||||
private T[] items;
|
||||
private int capacity;
|
||||
private int start;
|
||||
|
||||
public ReverseQueue(int initialCapacity)
|
||||
{
|
||||
if (initialCapacity <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(initialCapacity));
|
||||
|
||||
items = new T[initialCapacity];
|
||||
capacity = initialCapacity;
|
||||
start = 0;
|
||||
Count = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the item at an index in the <see cref="ReverseQueue{T}"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the item to retrieve. The most recently enqueued item is at index 0.</param>
|
||||
public T this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index < 0 || index > Count - 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
|
||||
int reverseIndex = Count - 1 - index;
|
||||
return items[(start + reverseIndex) % capacity];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enqueues an item to this <see cref="ReverseQueue{T}"/>.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to enqueue.</param>
|
||||
public void Enqueue(T item)
|
||||
{
|
||||
if (Count == capacity)
|
||||
{
|
||||
// Double the buffer size
|
||||
var buffer = new T[capacity * 2];
|
||||
|
||||
// Copy items to new queue
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
buffer[i] = items[(start + i) % capacity];
|
||||
}
|
||||
|
||||
// Replace array with new buffer
|
||||
items = buffer;
|
||||
capacity *= 2;
|
||||
start = 0;
|
||||
}
|
||||
|
||||
items[(start + Count) % capacity] = item;
|
||||
Count++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dequeues an item from the <see cref="ReverseQueue{T}"/> and returns it.
|
||||
/// </summary>
|
||||
/// <returns>The item dequeued from the <see cref="ReverseQueue{T}"/>.</returns>
|
||||
public T Dequeue()
|
||||
{
|
||||
var item = items[start];
|
||||
start = (start + 1) % capacity;
|
||||
Count--;
|
||||
return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the <see cref="ReverseQueue{T}"/> of all items.
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
start = 0;
|
||||
Count = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator which enumerates items in the <see cref="ReverseQueue{T}"/> starting from the most recently enqueued one.
|
||||
/// </summary>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (int i = Count - 1; i >= 0; i--)
|
||||
yield return items[(start + i) % capacity];
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user