wabbajack/Wabbajack.Common/Extensions/EnumerableExt.cs

65 lines
2.0 KiB
C#
Raw Normal View History

2019-11-03 06:01:19 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace Wabbajack
{
public static class EnumerableExt
{
#region Shuffle
/// https://stackoverflow.com/questions/5807128/an-extension-method-on-ienumerable-needed-for-shuffling
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
return source.ShuffleIterator(rng);
}
private static IEnumerable<T> ShuffleIterator<T>(
this IEnumerable<T> source, Random rng)
{
var buffer = source.ToList();
for (int i = 0; i < buffer.Count; i++)
{
int j = rng.Next(i, buffer.Count);
yield return buffer[j];
buffer[j] = buffer[i];
}
}
#endregion
public static IEnumerable<T> Cons<T>(this IEnumerable<T> coll, T next)
{
yield return next;
foreach (var itm in coll) yield return itm;
}
2020-04-09 21:05:07 +00:00
/// <summary>
/// Converts and filters a nullable enumerable to a non-nullable enumerable
/// </summary>
public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> e)
where T : class
{
// Filter out nulls
return e.Where(e => e != null)
// Cast to non nullable type
.Select(e => e!);
}
/// <summary>
/// Selects items that are castable to the desired type
/// </summary>
/// <typeparam name="T">Type of the original enumerable to cast from</typeparam>
/// <typeparam name="R">Type to attempt casting to</typeparam>
/// <param name="e">Enumerable to process</param>
/// <returns>Enumerable with only objects that were castable</returns>
public static IEnumerable<R> WhereCastable<T, R>(this IEnumerable<T> e)
where T : class
where R : T
{
return e.Where(e => e is R)
.Select(e => (R)e);
}
2019-11-03 06:01:19 +00:00
}
}