WingMan

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 13  →  ?path2? @ 14
/trunk/WingMan/Utilities/Extensions.cs
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace WingMan.Utilities
{
public static class Extensions
{
/// <summary>
/// Sequentially removes all the elements from the first sequence that are in the second sequence.
/// </summary>
/// <typeparam name="T">the type o the collection</typeparam>
/// <param name="o">the first sequence to remove from</param>
/// <param name="p">the second sequence to remove</param>
/// <returns>the first sequence excluding the second sequence</returns>
public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T>
{
using (var ea = a.GetEnumerator())
{
using (var eb = b.GetEnumerator())
{
while (ea.MoveNext())
{
if (eb.MoveNext() && ea.Current.Equals(eb.Current))
continue;
yield return ea.Current;
}
}
}
}
 
/// <summary>
/// Determines whether a sequence is contained within another sequence.
/// </summary>
/// <returns>true if and only if the first set is contained in the second set</returns>
/// <param name="a">The set to check</param>
/// <param name="b">The set to check against</param>
/// <typeparam name="T">the set type</typeparam>
public static bool SubsetEquals<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T>
{
return !a.OrderBy(s => s).SequenceExcept(b.OrderBy(s => s)).Any();
}
}
}