public void TestBasic() { BitSet b = new BitSet(0); for(int i = 0; i < 31; ++i) { Assert.AreEqual(false, b[i]); } b[3] = true; for (int i = 0; i < 31; ++i) { Assert.AreEqual(i == 3, b[i]); } b[30] = true; b[0] = true; for (int i = 0; i < 31; ++i) { Assert.AreEqual(i == 3 || i == 30 || i == 0, b[i]); } b[30] = false; for (int i = 0; i < 32; ++i) { Assert.AreEqual(i == 3 || i == 0, b[i]); } b[31] = true; for (int i = 0; i < 32; ++i) { Assert.AreEqual(i==31 || i == 3 || i == 0, b[i]); } }
public BigInteger calc(int curState, BigInteger pMult) { if (curState == endPosition) { return 0; } if (expectedValue[curState] != -1) { return expectedValue[curState]; } Logger.LogTrace("calc {} pMult {}", string.Join("", curState.ToBinaryString(nTotal).Replace('1', 'X').Replace('0', '.').Reverse()), pMult); BigInteger totalReturn = 0; //Choose each possibility for (int i = 0; i < nTotal; ++i) { int pos = i; int price = nTotal; BitSet csBitSet = new BitSet(curState); while (true) { if (!csBitSet[pos]) { Logger.ChangeIndent(4); BigInteger toAdd = calc(curState | 1 << pos, pMult / nTotal); totalReturn += price * pMult + toAdd; Logger.LogTrace("Calculating spot {} next free {} Add [ price {} * mult {} + rest {} = {} ]. Total {}", i, pos, price, pMult, toAdd, price * pMult + toAdd, totalReturn); Logger.ChangeIndent(-4); break; } ++pos; if (pos == nTotal) pos = 0; --price; Preconditions.checkState(price >= 1); } } //Here rotate the binary nTotal times as X..XX is the same as ..XXX or .XXX. int equivalentState = curState; //Logger.LogInfo("1 eq state {}", equivalentState.ToBinaryString(nTotal)); for (int i = 0; i < nTotal; ++i) { bool firstBit = equivalentState.GetBit(0); equivalentState >>= 1; if (firstBit) equivalentState = equivalentState.SetBit(nTotal - 1); else equivalentState = equivalentState.ClearBit(nTotal - 1); // Logger.LogInfo("eq state {}", equivalentState.ToBinaryString(nTotal)); expectedValue[equivalentState] = totalReturn; } Preconditions.checkState(equivalentState == curState); return expectedValue[curState] = totalReturn; }