From decb6bbab7c5cb791dae34e0e9cbb25879b2fbbc Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 22 Jan 2025 20:28:51 +0000 Subject: [PATCH] added new stuff! :D --- Libraries/Core/Utils/RandomUtil.cs | 51 ++++++++++++++++-------- SptCommon/Extensions/ListExtensions.cs | 11 +++++ UnitTests/Tests/Utils/HashUtilTests.cs | 4 +- UnitTests/Tests/Utils/RandomUtilTests.cs | 4 +- 4 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 SptCommon/Extensions/ListExtensions.cs diff --git a/Libraries/Core/Utils/RandomUtil.cs b/Libraries/Core/Utils/RandomUtil.cs index 8751405d..704b84bb 100644 --- a/Libraries/Core/Utils/RandomUtil.cs +++ b/Libraries/Core/Utils/RandomUtil.cs @@ -1,25 +1,15 @@ using System.Security.Cryptography; using SptCommon.Annotations; using Core.Models.Utils; +using Core.Utils.Cloners; +using SptCommon.Extensions; namespace Core.Utils; // TODO: Finish porting this class [Injectable(InjectionType.Singleton)] -public class RandomUtil +public class RandomUtil(ISptLogger _logger, ICloner _cloner) { - protected ISptLogger? _logger = null; - - public RandomUtil - ( - ISptLogger logger - ) - { - _logger = logger; - } - - public RandomUtil() { } - public readonly Random Random = new(); /// @@ -284,9 +274,36 @@ public class RandomUtil /// Whether to draw with replacement. Defaults to true. /// The type of elements in the list. /// A List containing the drawn elements. - public IEnumerable DrawRandomFromList(IEnumerable originalList, int count = 1, bool replacement = true) + public List DrawRandomFromList(List originalList, int count = 1, bool replacement = true) { - throw new NotImplementedException("ICloneable needs implemented on types before this can be written"); + var list = originalList; + var drawCount = count; + + if (!replacement) + { + list = _cloner.Clone(originalList); + // Adjust drawCount to avoid drawing more elements than available + if (drawCount > list.Count) + { + drawCount = list.Count; + } + } + + var results = new List(); + for (var i = 0; i < drawCount; i++) + { + var randomIndex = RandInt(list.Count); + if (replacement) + { + results.Add(list[randomIndex]); + } + else + { + results.Add(list.Splice(randomIndex, 1)[0]); + } + } + + return results; } /// @@ -300,7 +317,9 @@ public class RandomUtil /// A list of randomly drawn keys from the dictionary. public List DrawRandomFromDict(Dictionary dict, int count = 1, bool replacement = true) where TKey : notnull { - throw new NotImplementedException("ICloneable needs implemented on types before this can be written"); + var keys = dict.Keys.ToList(); + var randomKeys = DrawRandomFromList(keys, count, replacement); + return randomKeys; } /// diff --git a/SptCommon/Extensions/ListExtensions.cs b/SptCommon/Extensions/ListExtensions.cs new file mode 100644 index 00000000..dea6bdd1 --- /dev/null +++ b/SptCommon/Extensions/ListExtensions.cs @@ -0,0 +1,11 @@ +namespace SptCommon.Extensions; + +public static class ListExtensions +{ + public static List Splice(this List source, int index, int count) + { + var items = source.GetRange(index, count); + source.RemoveRange(index,count); + return items; + } +} diff --git a/UnitTests/Tests/Utils/HashUtilTests.cs b/UnitTests/Tests/Utils/HashUtilTests.cs index 02b08d80..e685cbbb 100644 --- a/UnitTests/Tests/Utils/HashUtilTests.cs +++ b/UnitTests/Tests/Utils/HashUtilTests.cs @@ -1,11 +1,13 @@ using Core.Utils; +using Core.Utils.Cloners; +using UnitTests.Mock; namespace UnitTests.Tests.Utils; [TestClass] public class HashUtilTests { - protected HashUtil _hashUtil = new(new RandomUtil()); + protected HashUtil _hashUtil = new(new RandomUtil(new MockLogger(), new JsonCloner(new JsonUtil()))); [TestMethod] public void GenerateTest() diff --git a/UnitTests/Tests/Utils/RandomUtilTests.cs b/UnitTests/Tests/Utils/RandomUtilTests.cs index 22ca5f19..170d18a9 100644 --- a/UnitTests/Tests/Utils/RandomUtilTests.cs +++ b/UnitTests/Tests/Utils/RandomUtilTests.cs @@ -1,11 +1,13 @@ using Core.Utils; +using Core.Utils.Cloners; +using UnitTests.Mock; namespace UnitTests.Tests.Utils; [TestClass] public sealed class RandomUtilTests { - private RandomUtil _randomUtil = new(); + private RandomUtil _randomUtil = new(new MockLogger(), new JsonCloner(new JsonUtil())); [TestMethod] public void GetIntTest()