forked from volure/keystoreBrute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PermuteUtils.cs
53 lines (48 loc) · 1.75 KB
/
PermuteUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
namespace keystoreBrute
{
using System;
using System.Collections.Generic;
// Original code from Ian Griffiths: http://www.interact-sw.co.uk/iangblog/2004/09/16/permuterate
public class PermuteUtils
{
// Returns an enumeration of enumerators, one for each permutation
// of the input.
public static IEnumerable<IEnumerable<T>> Permute<T> (IEnumerable<T> list, int count)
{
if (count == 0) {
yield return new T[0];
} else {
int startingElementIndex = 0;
foreach (T startingElement in list) {
IEnumerable<T> remainingItems = AllExcept (list, startingElementIndex);
foreach (IEnumerable<T> permutationOfRemainder in Permute (remainingItems, count - 1)) {
yield return Concat<T> (new T[] { startingElement }, permutationOfRemainder);
}
startingElementIndex += 1;
}
}
}
// Enumerates over contents of both lists.
public static IEnumerable<T> Concat<T> (IEnumerable<T> a, IEnumerable<T> b)
{
foreach (T item in a) {
yield return item;
}
foreach (T item in b) {
yield return item;
}
}
// Enumerates over all items in the input, skipping over the item
// with the specified offset.
public static IEnumerable<T> AllExcept<T> (IEnumerable<T> input, int indexToSkip)
{
int index = 0;
foreach (T item in input) {
if (index != indexToSkip)
yield return item;
index += 1;
}
}
}
}