1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-27 07:09:54 +08:00

Initial tidy-up pass on IntervalGroupingUtils

This commit is contained in:
Dean Herbert
2025-02-05 15:41:31 +09:00
Unverified
parent 325483192a
commit 8447679db9
2 changed files with 23 additions and 35 deletions
@@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq;
using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing.Rhythm.Data;
using osu.Game.Rulesets.Taiko.Difficulty.Utils;
@@ -31,13 +32,9 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Preprocessing.Rhythm
private static List<SameRhythmHitObjectGrouping> createSameRhythmGroupedHitObjects(List<TaikoDifficultyHitObject> hitObjects)
{
var rhythmGroups = new List<SameRhythmHitObjectGrouping>();
var groups = IntervalGroupingUtils.GroupByInterval(hitObjects);
foreach (var group in groups)
{
var previous = rhythmGroups.Count > 0 ? rhythmGroups[^1] : null;
rhythmGroups.Add(new SameRhythmHitObjectGrouping(previous, group));
}
foreach (var grouped in IntervalGroupingUtils.GroupByInterval(hitObjects))
rhythmGroups.Add(new SameRhythmHitObjectGrouping(rhythmGroups.LastOrDefault(), grouped));
return rhythmGroups;
}
@@ -45,13 +42,9 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Preprocessing.Rhythm
private static List<SamePatternsGroupedHitObjects> createSamePatternGroupedHitObjects(List<SameRhythmHitObjectGrouping> rhythmGroups)
{
var patternGroups = new List<SamePatternsGroupedHitObjects>();
var groups = IntervalGroupingUtils.GroupByInterval(rhythmGroups);
foreach (var group in groups)
{
var previous = patternGroups.Count > 0 ? patternGroups[^1] : null;
patternGroups.Add(new SamePatternsGroupedHitObjects(previous, group));
}
foreach (var grouped in IntervalGroupingUtils.GroupByInterval(rhythmGroups))
patternGroups.Add(new SamePatternsGroupedHitObjects(patternGroups.LastOrDefault(), grouped));
return patternGroups;
}
@@ -8,56 +8,51 @@ namespace osu.Game.Rulesets.Taiko.Difficulty.Utils
{
public static class IntervalGroupingUtils
{
public static List<List<T>> GroupByInterval<T>(IReadOnlyList<T> data, double marginOfError = 5) where T : IHasInterval
public static List<List<T>> GroupByInterval<T>(IReadOnlyList<T> objects) where T : IHasInterval
{
var groups = new List<List<T>>();
if (data.Count == 0)
return groups;
int i = 0;
while (i < data.Count)
{
var group = createGroup(data, ref i, marginOfError);
groups.Add(group);
}
while (i < objects.Count)
groups.Add(createNextGroup(objects, ref i));
return groups;
}
private static List<T> createGroup<T>(IReadOnlyList<T> data, ref int i, double marginOfError) where T : IHasInterval
private static List<T> createNextGroup<T>(IReadOnlyList<T> objects, ref int i) where T : IHasInterval
{
var children = new List<T> { data[i] };
const double margin_of_error = 5;
var groupedObjects = new List<T> { objects[i] };
i++;
for (; i < data.Count - 1; i++)
for (; i < objects.Count - 1; i++)
{
// An interval change occured, add the current data if the next interval is larger.
if (!Precision.AlmostEquals(data[i].Interval, data[i + 1].Interval, marginOfError))
// An interval change occured, add the current object if the next interval is larger.
if (!Precision.AlmostEquals(objects[i].Interval, objects[i + 1].Interval, margin_of_error))
{
if (data[i + 1].Interval > data[i].Interval + marginOfError)
if (objects[i + 1].Interval > objects[i].Interval + margin_of_error)
{
children.Add(data[i]);
groupedObjects.Add(objects[i]);
i++;
}
return children;
return groupedObjects;
}
// No interval change occurred
children.Add(data[i]);
groupedObjects.Add(objects[i]);
}
// Check if the last two objects in the data form a "flat" rhythm pattern within the specified margin of error.
// Check if the last two objects in the object form a "flat" rhythm pattern within the specified margin of error.
// If true, add the current object to the group and increment the index to process the next object.
if (data.Count > 2 && i < data.Count &&
Precision.AlmostEquals(data[^1].Interval, data[^2].Interval, marginOfError))
if (objects.Count > 2 && i < objects.Count && Precision.AlmostEquals(objects[^1].Interval, objects[^2].Interval, margin_of_error))
{
children.Add(data[i]);
groupedObjects.Add(objects[i]);
i++;
}
return children;
return groupedObjects;
}
}
}