public int[] GetNext() { if (firstRun) { firstRun = false; return currentState; } if (currentLevel == n) { currentLevel++; return currentState; } int next = cutoff; while (next < n-1 && currentState[next] != 1) { next++; } //If we've run out of combinations if (currentState[next] == 0) { bool foundValidStackItem = false; //Check the stack if (arrayStack.Count <= 0) { currentLevel++; fillArrayWithLevel(currentLevel); cutoff = 0; return currentState; } else { while (arrayStack.Count > 0 && !foundValidStackItem) { CombinationData cd = arrayStack.Pop(); if (cd.array[cd.cutoff] == 0) { foundValidStackItem = true; cutoff = cd.cutoff; currentState = cd.array; } } if (!foundValidStackItem) { currentLevel++; if (currentLevel == n) { hasMore = false; } fillArrayWithLevel(currentLevel); cutoff = 0; return currentState; } else { return GetNext(); } } } else //We're on a 1 { int[] array = new int[n]; currentState[next] = 0; currentState[next - 1] = 1; Array.Copy(currentState, array, n); CombinationData cd = new CombinationData(); cd.array = array; cd.cutoff = cutoff; arrayStack.Push(cd); cutoff = next; return currentState; } }
public int[] GetNext() { if (firstRun) { firstRun = false; return(currentState); } if (currentLevel == n) { currentLevel++; return(currentState); } int next = cutoff; while (next < n - 1 && currentState[next] != 1) { next++; } //If we've run out of combinations if (currentState[next] == 0) { bool foundValidStackItem = false; //Check the stack if (arrayStack.Count <= 0) { currentLevel++; fillArrayWithLevel(currentLevel); cutoff = 0; return(currentState); } else { while (arrayStack.Count > 0 && !foundValidStackItem) { CombinationData cd = arrayStack.Pop(); if (cd.array[cd.cutoff] == 0) { foundValidStackItem = true; cutoff = cd.cutoff; currentState = cd.array; } } if (!foundValidStackItem) { currentLevel++; if (currentLevel == n) { hasMore = false; } fillArrayWithLevel(currentLevel); cutoff = 0; return(currentState); } else { return(GetNext()); } } } else //We're on a 1 { int[] array = new int[n]; currentState[next] = 0; currentState[next - 1] = 1; Array.Copy(currentState, array, n); CombinationData cd = new CombinationData(); cd.array = array; cd.cutoff = cutoff; arrayStack.Push(cd); cutoff = next; return(currentState); } }