From a30f01ca4cc264a5bef1f239714f880fd0236b50 Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Tue, 14 Jan 2025 01:12:08 -0500 Subject: [PATCH] Return arrays instead of lists. --- RhythmGameUtilities.Tests/UtilitiesTest.cs | 19 ++++++------------- RhythmGameUtilities/Scripts/Utilities.cs | 11 ++++++----- UnityPackage/Editor/Tests/UtilitiesTest.cs | 19 ++++++------------- UnityPackage/Scripts/Utilities.cs | 11 ++++++----- 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/RhythmGameUtilities.Tests/UtilitiesTest.cs b/RhythmGameUtilities.Tests/UtilitiesTest.cs index 15c4361..e92be1b 100644 --- a/RhythmGameUtilities.Tests/UtilitiesTest.cs +++ b/RhythmGameUtilities.Tests/UtilitiesTest.cs @@ -70,25 +70,18 @@ public void TestCalculateBeatBars() var beatBars = Utilities.CalculateBeatBars(bpmChanges, resolution, timeSignature, true); - Assert.That(beatBars.Count, Is.EqualTo(440)); + Assert.That(beatBars.Length, Is.EqualTo(440)); } [Test] public void TestFindPositionNearGivenTick() { - var notes = new List + var notes = new Note[] { - new() { Position = 768 }, - new() { Position = 960 }, - new() { Position = 1152 }, - new() { Position = 1536 }, - new() { Position = 1728 }, - new() { Position = 1920 }, - new() { Position = 2304 }, - new() { Position = 2496 }, - new() { Position = 2688 }, - new() { Position = 3072 }, - new() { Position = 3264 }, + new() { Position = 768 }, new() { Position = 960 }, new() { Position = 1152 }, + new() { Position = 1536 }, new() { Position = 1728 }, new() { Position = 1920 }, + new() { Position = 2304 }, new() { Position = 2496 }, new() { Position = 2688 }, + new() { Position = 3072 }, new() { Position = 3264 }, }; Assert.That(Utilities.FindPositionNearGivenTick(notes, 100), Is.Null); diff --git a/RhythmGameUtilities/Scripts/Utilities.cs b/RhythmGameUtilities/Scripts/Utilities.cs index 26168f9..2dde294 100644 --- a/RhythmGameUtilities/Scripts/Utilities.cs +++ b/RhythmGameUtilities/Scripts/Utilities.cs @@ -84,10 +84,9 @@ public static int RoundUpToTheNearestMultiplier(int value, int multiplier) return UtilitiesInternal.RoundUpToTheNearestMultiplier(value, multiplier); } - public static List CalculateBeatBars(Dictionary bpmChanges, int resolution = 192, int ts = 4, + public static BeatBar[] CalculateBeatBars(Dictionary bpmChanges, int resolution = 192, int ts = 4, bool includeHalfNotes = true) { - var beatBars = new List(); var ptrArray = UtilitiesInternal.CalculateBeatBarsInternal(bpmChanges.Keys.ToArray(), bpmChanges.Values.ToArray(), bpmChanges.Count, resolution, ts, includeHalfNotes, @@ -95,12 +94,14 @@ public static List CalculateBeatBars(Dictionary bpmChanges, i var beatBarSize = Marshal.SizeOf(typeof(BeatBar)); + var beatBars = new BeatBar[size]; + for (var i = 0; i < size; i += 1) { var beatBarSizePtr = new IntPtr(ptrArray.ToInt64() + beatBarSize * i); var beatBar = Marshal.PtrToStructure(beatBarSizePtr); - beatBars.Add(beatBar); + beatBars[i] = beatBar; } Marshal.FreeHGlobal(ptrArray); @@ -108,10 +109,10 @@ public static List CalculateBeatBars(Dictionary bpmChanges, i return beatBars; } - public static Note? FindPositionNearGivenTick(List notes, int tick, int delta = 50) + public static Note? FindPositionNearGivenTick(Note[] notes, int tick, int delta = 50) { var left = 0; - var right = notes.Count - 1; + var right = notes.Length - 1; while (left <= right) { diff --git a/UnityPackage/Editor/Tests/UtilitiesTest.cs b/UnityPackage/Editor/Tests/UtilitiesTest.cs index 15c4361..e92be1b 100644 --- a/UnityPackage/Editor/Tests/UtilitiesTest.cs +++ b/UnityPackage/Editor/Tests/UtilitiesTest.cs @@ -70,25 +70,18 @@ public void TestCalculateBeatBars() var beatBars = Utilities.CalculateBeatBars(bpmChanges, resolution, timeSignature, true); - Assert.That(beatBars.Count, Is.EqualTo(440)); + Assert.That(beatBars.Length, Is.EqualTo(440)); } [Test] public void TestFindPositionNearGivenTick() { - var notes = new List + var notes = new Note[] { - new() { Position = 768 }, - new() { Position = 960 }, - new() { Position = 1152 }, - new() { Position = 1536 }, - new() { Position = 1728 }, - new() { Position = 1920 }, - new() { Position = 2304 }, - new() { Position = 2496 }, - new() { Position = 2688 }, - new() { Position = 3072 }, - new() { Position = 3264 }, + new() { Position = 768 }, new() { Position = 960 }, new() { Position = 1152 }, + new() { Position = 1536 }, new() { Position = 1728 }, new() { Position = 1920 }, + new() { Position = 2304 }, new() { Position = 2496 }, new() { Position = 2688 }, + new() { Position = 3072 }, new() { Position = 3264 }, }; Assert.That(Utilities.FindPositionNearGivenTick(notes, 100), Is.Null); diff --git a/UnityPackage/Scripts/Utilities.cs b/UnityPackage/Scripts/Utilities.cs index 26168f9..2dde294 100644 --- a/UnityPackage/Scripts/Utilities.cs +++ b/UnityPackage/Scripts/Utilities.cs @@ -84,10 +84,9 @@ public static int RoundUpToTheNearestMultiplier(int value, int multiplier) return UtilitiesInternal.RoundUpToTheNearestMultiplier(value, multiplier); } - public static List CalculateBeatBars(Dictionary bpmChanges, int resolution = 192, int ts = 4, + public static BeatBar[] CalculateBeatBars(Dictionary bpmChanges, int resolution = 192, int ts = 4, bool includeHalfNotes = true) { - var beatBars = new List(); var ptrArray = UtilitiesInternal.CalculateBeatBarsInternal(bpmChanges.Keys.ToArray(), bpmChanges.Values.ToArray(), bpmChanges.Count, resolution, ts, includeHalfNotes, @@ -95,12 +94,14 @@ public static List CalculateBeatBars(Dictionary bpmChanges, i var beatBarSize = Marshal.SizeOf(typeof(BeatBar)); + var beatBars = new BeatBar[size]; + for (var i = 0; i < size; i += 1) { var beatBarSizePtr = new IntPtr(ptrArray.ToInt64() + beatBarSize * i); var beatBar = Marshal.PtrToStructure(beatBarSizePtr); - beatBars.Add(beatBar); + beatBars[i] = beatBar; } Marshal.FreeHGlobal(ptrArray); @@ -108,10 +109,10 @@ public static List CalculateBeatBars(Dictionary bpmChanges, i return beatBars; } - public static Note? FindPositionNearGivenTick(List notes, int tick, int delta = 50) + public static Note? FindPositionNearGivenTick(Note[] notes, int tick, int delta = 50) { var left = 0; - var right = notes.Count - 1; + var right = notes.Length - 1; while (left <= right) {