Last active
December 18, 2015 00:38
-
-
Save JohanLarsson/5697899 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class ListExt | |
| { | |
| public static IEnumerable<T[]> GetSubsets<T>(this List<T> set, int subsetLength) | |
| { | |
| int[] indices = Enumerable.Range(0, subsetLength).ToArray(); | |
| while (true) | |
| { | |
| yield return indices.Select(i => set[i]).ToArray(); | |
| indices = Increment(indices, set.Count - 1); | |
| if (indices == null) | |
| break; | |
| } | |
| } | |
| /// <summary> | |
| /// Updates the indices to sample subset for next iteration | |
| /// </summary> | |
| /// <param name="indices"></param> | |
| /// <param name="max"></param> | |
| /// <returns></returns> | |
| private static int[] Increment(int[] indices, int max) | |
| { | |
| for (int i = indices.Length - 1; i >= 0; i--) | |
| { | |
| if (CanIncrement(indices, max, i)) | |
| { | |
| int ci = indices[i] + 1; | |
| indices[i] = ci; | |
| ResetAfter(indices, i, ci); | |
| return indices; | |
| } | |
| } | |
| return null; | |
| } | |
| private static void ResetAfter(int[] indices, int startIndex, int startValue) | |
| { | |
| for (int i = startIndex + 1; i < indices.Length; i++) | |
| { | |
| startValue++; | |
| indices[i] = startValue; | |
| } | |
| } | |
| private static bool CanIncrement(int[] indices, int max, int i) | |
| { | |
| return indices[i] < (max - (indices.Length - 1 - i)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figured it out with sets in Haskell:
It's pretty slow, though. I think we can get it faster if we use only sets and don't go from lists to sets.