1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 07:42:57 +08:00

Add and use limited capacity queue

This commit is contained in:
Bartłomiej Dach 2020-08-18 19:13:18 +02:00
parent 9fb494d5d3
commit 6c759f31f1
6 changed files with 226 additions and 23 deletions

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using osu.Game.Rulesets.Difficulty.Utils;
using osu.Game.Rulesets.Taiko.Objects; using osu.Game.Rulesets.Taiko.Objects;
namespace osu.Game.Rulesets.Taiko.Difficulty.Preprocessing namespace osu.Game.Rulesets.Taiko.Difficulty.Preprocessing
@ -27,16 +28,15 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Preprocessing
private void findRolls(int patternLength) private void findRolls(int patternLength)
{ {
List<TaikoDifficultyHitObject> history = new List<TaikoDifficultyHitObject>(); var history = new LimitedCapacityQueue<TaikoDifficultyHitObject>(2 * patternLength);
int repetitionStart = 0; int repetitionStart = 0;
for (int i = 0; i < hitObjects.Count; i++) for (int i = 0; i < hitObjects.Count; i++)
{ {
history.Add(hitObjects[i]); history.Enqueue(hitObjects[i]);
if (history.Count < 2 * patternLength) continue; if (!history.Full)
continue;
if (history.Count > 2 * patternLength) history.RemoveAt(0);
bool isRepeat = true; bool isRepeat = true;

View File

@ -2,9 +2,9 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using osu.Game.Rulesets.Difficulty.Preprocessing; using osu.Game.Rulesets.Difficulty.Preprocessing;
using osu.Game.Rulesets.Difficulty.Skills; using osu.Game.Rulesets.Difficulty.Skills;
using osu.Game.Rulesets.Difficulty.Utils;
using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing; using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing;
using osu.Game.Rulesets.Taiko.Objects; using osu.Game.Rulesets.Taiko.Objects;
@ -27,7 +27,7 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Skills
/// <summary> /// <summary>
/// List of the last <see cref="mono_history_max_length"/> most recent mono patterns, with the most recent at the end of the list. /// List of the last <see cref="mono_history_max_length"/> most recent mono patterns, with the most recent at the end of the list.
/// </summary> /// </summary>
private readonly List<int> monoHistory = new List<int>(); private readonly LimitedCapacityQueue<int> monoHistory = new LimitedCapacityQueue<int>(mono_history_max_length);
protected override double StrainValueOf(DifficultyHitObject current) protected override double StrainValueOf(DifficultyHitObject current)
{ {
@ -83,10 +83,7 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Skills
const int l = 2; const int l = 2;
double penalty = 1.0; double penalty = 1.0;
monoHistory.Add(currentMonoLength); monoHistory.Enqueue(currentMonoLength);
if (monoHistory.Count > mono_history_max_length)
monoHistory.RemoveAt(0);
for (int start = monoHistory.Count - l - 1; start >= 0; start--) for (int start = monoHistory.Count - l - 1; start >= 0; start--)
{ {

View File

@ -2,9 +2,9 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using osu.Game.Rulesets.Difficulty.Preprocessing; using osu.Game.Rulesets.Difficulty.Preprocessing;
using osu.Game.Rulesets.Difficulty.Skills; using osu.Game.Rulesets.Difficulty.Skills;
using osu.Game.Rulesets.Difficulty.Utils;
using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing; using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing;
using osu.Game.Rulesets.Taiko.Objects; using osu.Game.Rulesets.Taiko.Objects;
@ -17,7 +17,7 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Skills
private const double strain_decay = 0.96; private const double strain_decay = 0.96;
private double currentStrain; private double currentStrain;
private readonly List<TaikoDifficultyHitObject> rhythmHistory = new List<TaikoDifficultyHitObject>(); private readonly LimitedCapacityQueue<TaikoDifficultyHitObject> rhythmHistory = new LimitedCapacityQueue<TaikoDifficultyHitObject>(rhythm_history_max_length);
private const int rhythm_history_max_length = 8; private const int rhythm_history_max_length = 8;
private int notesSinceRhythmChange; private int notesSinceRhythmChange;
@ -32,10 +32,7 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Skills
{ {
double penalty = 1; double penalty = 1;
rhythmHistory.Add(hitobject); rhythmHistory.Enqueue(hitobject);
if (rhythmHistory.Count > rhythm_history_max_length)
rhythmHistory.RemoveAt(0);
for (int l = 2; l <= rhythm_history_max_length / 2; l++) for (int l = 2; l <= rhythm_history_max_length / 2; l++)
{ {

View File

@ -1,10 +1,10 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Game.Rulesets.Difficulty.Preprocessing; using osu.Game.Rulesets.Difficulty.Preprocessing;
using osu.Game.Rulesets.Difficulty.Skills; using osu.Game.Rulesets.Difficulty.Skills;
using osu.Game.Rulesets.Difficulty.Utils;
using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing; using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing;
using osu.Game.Rulesets.Taiko.Objects; using osu.Game.Rulesets.Taiko.Objects;
@ -18,7 +18,7 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Skills
protected override double StrainDecayBase => 0.4; protected override double StrainDecayBase => 0.4;
private const int max_history_length = 2; private const int max_history_length = 2;
private readonly List<double> notePairDurationHistory = new List<double>(); private readonly LimitedCapacityQueue<double> notePairDurationHistory = new LimitedCapacityQueue<double>(max_history_length);
private double offhandObjectDuration = double.MaxValue; private double offhandObjectDuration = double.MaxValue;
@ -56,10 +56,7 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Skills
if (hitObject.ObjectIndex == 1) if (hitObject.ObjectIndex == 1)
return 1; return 1;
notePairDurationHistory.Add(hitObject.DeltaTime + offhandObjectDuration); notePairDurationHistory.Enqueue(hitObject.DeltaTime + offhandObjectDuration);
if (notePairDurationHistory.Count > max_history_length)
notePairDurationHistory.RemoveAt(0);
double shortestRecentNote = notePairDurationHistory.Min(); double shortestRecentNote = notePairDurationHistory.Min();
objectStrain += speedBonus(shortestRecentNote); objectStrain += speedBonus(shortestRecentNote);

View File

@ -0,0 +1,98 @@
// 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 NUnit.Framework;
using osu.Game.Rulesets.Difficulty.Utils;
namespace osu.Game.Tests.NonVisual
{
[TestFixture]
public class LimitedCapacityQueueTest
{
private const int capacity = 3;
private LimitedCapacityQueue<int> queue;
[SetUp]
public void SetUp()
{
queue = new LimitedCapacityQueue<int>(capacity);
}
[Test]
public void TestEmptyQueue()
{
Assert.AreEqual(0, queue.Count);
Assert.Throws<ArgumentOutOfRangeException>(() => _ = queue[0]);
Assert.Throws<InvalidOperationException>(() => _ = queue.Dequeue());
int count = 0;
foreach (var _ in queue)
count++;
Assert.AreEqual(0, count);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
public void TestBelowCapacity(int count)
{
for (int i = 0; i < count; ++i)
queue.Enqueue(i);
Assert.AreEqual(count, queue.Count);
for (int i = 0; i < count; ++i)
Assert.AreEqual(i, queue[i]);
int j = 0;
foreach (var item in queue)
Assert.AreEqual(j++, item);
for (int i = queue.Count; i < queue.Count + capacity; i++)
Assert.Throws<ArgumentOutOfRangeException>(() => _ = queue[i]);
}
[TestCase(4)]
[TestCase(5)]
[TestCase(6)]
public void TestEnqueueAtFullCapacity(int count)
{
for (int i = 0; i < count; ++i)
queue.Enqueue(i);
Assert.AreEqual(capacity, queue.Count);
for (int i = 0; i < queue.Count; ++i)
Assert.AreEqual(count - capacity + i, queue[i]);
int j = count - capacity;
foreach (var item in queue)
Assert.AreEqual(j++, item);
for (int i = queue.Count; i < queue.Count + capacity; i++)
Assert.Throws<ArgumentOutOfRangeException>(() => _ = queue[i]);
}
[TestCase(4)]
[TestCase(5)]
[TestCase(6)]
public void TestDequeueAtFullCapacity(int count)
{
for (int i = 0; i < count; ++i)
queue.Enqueue(i);
for (int i = 0; i < capacity; ++i)
{
Assert.AreEqual(count - capacity + i, queue.Dequeue());
Assert.AreEqual(2 - i, queue.Count);
}
Assert.Throws<InvalidOperationException>(() => queue.Dequeue());
}
}
}

View File

@ -0,0 +1,114 @@
// 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 with limited capacity.
/// Respects first-in-first-out insertion order.
/// </summary>
public class LimitedCapacityQueue<T> : IEnumerable<T>
{
/// <summary>
/// The number of elements in the queue.
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Whether the queue is full (adding any new items will cause removing existing ones).
/// </summary>
public bool Full => Count == capacity;
private readonly T[] array;
private readonly int capacity;
// Markers tracking the queue's first and last element.
private int start, end;
/// <summary>
/// Constructs a new <see cref="LimitedCapacityQueue{T}"/>
/// </summary>
/// <param name="capacity">The number of items the queue can hold.</param>
public LimitedCapacityQueue(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException(nameof(capacity));
this.capacity = capacity;
array = new T[capacity];
start = 0;
end = -1;
}
/// <summary>
/// Removes an item from the front of the <see cref="LimitedCapacityQueue{T}"/>.
/// </summary>
/// <returns>The item removed from the front of the queue.</returns>
public T Dequeue()
{
if (Count == 0)
throw new InvalidOperationException("Queue is empty.");
var result = array[start];
start = (start + 1) % capacity;
Count--;
return result;
}
/// <summary>
/// Adds an item to the back of the <see cref="LimitedCapacityQueue{T}"/>.
/// If the queue is holding <see cref="Count"/> elements at the point of addition,
/// the item at the front of the queue will be removed.
/// </summary>
/// <param name="item">The item to be added to the back of the queue.</param>
public void Enqueue(T item)
{
end = (end + 1) % capacity;
if (Count == capacity)
start = (start + 1) % capacity;
else
Count++;
array[end] = item;
}
/// <summary>
/// Retrieves the item at the given index in the queue.
/// </summary>
/// <param name="index">
/// The index of the item to retrieve.
/// The item with index 0 is at the front of the queue
/// (it was added the earliest).
/// </param>
public T this[int index]
{
get
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index));
return array[(start + index) % capacity];
}
}
/// <summary>
/// Enumerates the queue from its start to its end.
/// </summary>
public IEnumerator<T> GetEnumerator()
{
if (Count == 0)
yield break;
for (int i = 0; i < Count; i++)
yield return array[(start + i) % capacity];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}