Skip to content

Instantly share code, notes, and snippets.

@dtaylor-530
Last active May 9, 2018 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtaylor-530/442b4ca4f8a92b513b29c47affda38a9 to your computer and use it in GitHub Desktop.
Save dtaylor-530/442b4ca4f8a92b513b29c47affda38a9 to your computer and use it in GitHub Desktop.
Kaos Combinatorics Extensions
/// <summary>
/// rearranges the elements into a sequence of element sets where no set has the same elements as another
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="c"></param>
/// <param name="size"></param>
/// <returns></returns>
public static IEnumerable<IEnumerable<T>> CombineDistinct<T>(this IEnumerable<T> c, int size)
{
return c.ReArrange(new Kaos.Combinatorics.Combination(c.Count(), size).EnumerateDistinct());
}
/// <summary>
/// produces sequence of sets of elements where no set has the same elements as another
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static IEnumerable<IEnumerable<int>> EnumerateDistinct(this Kaos.Combinatorics.Combination c)
{
HashSet<int> hs = new HashSet<int>();
foreach (var x in c.GetRows())
{
if (x.All(_ => hs.Add(_)))
yield return x;
}
}
/// <summary>
/// Rearranges the elements into a sequence according to their index and the index of sequence-sets in the given parameter
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ts"></param>
/// <param name="enms"></param>
/// <returns></returns>
public static IEnumerable<IEnumerable<T>> ReArrange<T>(this IEnumerable<T> ts, IEnumerable<IEnumerable<int>> enms)
{
return enms.Select(_ => _.Select(__ => Enumerable.ElementAt(ts, __)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment