cirnogodot/Scripts/Utils/ShuffleExtensions.cs

45 lines
1.2 KiB
C#
Raw Normal View History

2025-04-15 15:19:36 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
namespace Cirno.Scripts.Utils;
public static class ShuffleExtensions
{
//private static Random rng = new Random();
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, int? desiredLength = null)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var list = source.ToList();
int originalCount = list.Count;
if (originalCount == 0) yield break;
// Repeat elements to meet the desired length
2025-04-15 16:22:30 +02:00
if (desiredLength > originalCount)
2025-04-15 15:19:36 +02:00
{
int countToAdd = desiredLength.Value - originalCount;
for (int i = 0; i < countToAdd; i++)
{
list.Add(list[i % originalCount]);
}
}
// Fisher-Yates shuffle
for (int i = list.Count - 1; i > 0; i--)
{
//int j = rng.Next(i + 1);
int j = GD.RandRange(0, i);
(list[i], list[j]) = (list[j], list[i]);
}
// Return only up to desired length if specified
foreach (var item in list.Take(desiredLength ?? list.Count))
{
yield return item;
}
}
}