private void succ_next(int next) { int howMany = _result[next - 1].getHowMany(); GrayWord currentWord = _result[next - 1]; GrayWord nextWord = _result[next]; copyPreviousWord(next); if (howMany % 2 == 0) //liczba jedynek parzysta { nextWord.negate(0); } else //liczba jedynek nieparzysta { int z; if (currentWord.isEmpty()) { throw new Exception("trying to negate first 1 in empty sequence"); } else { z = currentWord.findFirst1(); } if (z + 1 >= n) { throw new Exception("z+1 > n"); } nextWord.negate(z + 1); } }
static void TestWord() { GrayWord gw = new GrayWord(); gw.zero(4); Debug.Assert(gw.isEmpty()); gw.negate(1); Debug.Assert(gw.ToString().Equals("0, 1, 0, 0")); Debug.Assert(gw.findFirst1() == 1); Debug.Assert(!gw.isEmpty()); Debug.Assert(gw.getHowMany() == 1); gw.negate(2); Debug.Assert(gw.getHowMany() == 2); }
public void generateCode(int n) { GrayWord newWord; numberOfWords = powerOf2(n); _result = new List <GrayWord>(); for (int i = 0; i < numberOfWords; i++) { //if (_result[i] == null) if (_result.Count() <= i) { newWord = new GrayWord(); newWord.zero(n); _result.Add(newWord); } if (i > 0) { succ_next(i); } } //for (int i = 1; i < numberOfWords; i++) // succ_next(i); }