public bool MoveNext() { if (reseted) { this.current = this.parent; this.reseted = false; return this.current != null; } if (this.current == null) throw new InvalidOperationException("Enumeration already finished"); this.current = this.current.GetSuccessor(); return this.current != null; }
public void Reset() { this.reseted = true; this.current = null; }
/// <summary> /// Creates the next permutation in lexicographic order. /// </summary> /// <returns> /// The next <see cref="Permutation"/> instance if there remain any; /// otherwize a null reference. /// </returns> public Permutation GetSuccessor() { Permutation result = new Permutation(this.order); int left, right; for (int k = 0; k < result.order; ++k) // Step #0 - copy current data into result { result.data[k] = this.data[k]; } left = result.order - 2; // Step #1 - Find left value while ((result.data[left] > result.data[left + 1]) && (left >= 1)) { --left; } if ((left == 0) && (this.data[left] > this.data[left + 1])) return null; right = result.order - 1; // Step #2 - find right; first value > left while (result.data[left] > result.data[right]) { --right; } int temp = result.data[left]; // Step #3 - swap [left] and [right] result.data[left] = result.data[right]; result.data[right] = temp; int i = left + 1; // Step #4 - order the tail int j = result.order - 1; while (i < j) { temp = result.data[i]; result.data[i++] = result.data[j]; result.data[j--] = temp; } return result; }
public PermutationEnumerator(Permutation parent) { if (parent == null) throw new ArgumentNullException("parent"); this.parent = parent; }
public void K2N5() { Permutation p = new Permutation(5); ShowPermutation(p); }
public void Identity2() { Permutation p = new Permutation(2); ShowPermutation(p); }
private static void ShowPermutation(Permutation p) { int i = 0; foreach (Permutation s in p.GetSuccessors()) Console.WriteLine("{0}: {1}", i++, s); }