iigs-sprite-compiler/SpriteCompiler/Helpers/EnumerableExtensions.cs
2016-12-16 22:52:14 -06:00

86 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpriteCompiler.Helpers
{
public static class EnumerableExtensions
{
public static IEnumerable<Tuple<T, T>> Pairs<T>(this IEnumerable<T> source)
{
return source.Zip(source.Skip(1), (x, y) => Tuple.Create(x, y));
}
public static IEnumerable<IGrouping<T, T>> GroupAdjacent<T>(this IEnumerable<T> source, Func<T, T, bool> comparer)
{
return GroupAdjacent(source, x => x, comparer);
}
public static IEnumerable<IGrouping<TKey, TSource>> GroupAdjacent<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return GroupAdjacent(source, keySelector, (a, b) => a.Equals(b));
}
public static IEnumerable<IGrouping<TKey, TSource>> GroupAdjacent<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, bool> comparer)
{
TKey last = default(TKey);
bool haveLast = false;
List<TSource> list = new List<TSource>();
foreach (TSource s in source)
{
TKey k = keySelector(s);
if (haveLast)
{
if (!comparer(last, k))
{
yield return new GroupOfAdjacent<TSource, TKey>(list, last);
list = new List<TSource>();
list.Add(s);
last = k;
}
else
{
list.Add(s);
last = k;
}
}
else
{
list.Add(s);
last = k;
haveLast = true;
}
}
if (haveLast)
{
yield return new GroupOfAdjacent<TSource, TKey>(list, last);
}
}
}
public class GroupOfAdjacent<TSource, TKey> : IEnumerable<TSource>, IGrouping<TKey, TSource>
{
public TKey Key { get; set; }
private List<TSource> GroupList { get; set; }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((System.Collections.Generic.IEnumerable<TSource>)this).GetEnumerator();
}
System.Collections.Generic.IEnumerator<TSource> System.Collections.Generic.IEnumerable<TSource>.GetEnumerator()
{
foreach (var s in GroupList)
yield return s;
}
public GroupOfAdjacent(List<TSource> source, TKey key)
{
GroupList = source;
Key = key;
}
}
}