index

// Get Least Upper Bound Given Total Bits and Similarity

// Note: The similarity must be less than or equal to the number of total bits.

// Note: The min value must be less than or equal to the test case.

typedef unsigned int uint;

vector<bool> GrayCode :: GetLeastUpperBound(uint nTotalBits, uint nSim, vector<bool> v);
vector<bool> GrayCode :: DecrementUpperBound(bool& bOK, uint nPos, uint nTotalBits, uint nSimBoundary, vector<bool> vUpperBound, vector<bool> vLowerBound);

vector<bool> GrayCode :: GetLeastUpperBound(uint nTotalBits, uint nSim, vector<bool> v)
{          //          starting right to left, one bit at a time 0->1

            vector<bool> vUpperBound = v;

            for(int nPos = v.size() - 1; nPos >= 0; nPos--)
            {
                        vUpperBound = v;
                        if(!v[nPos])
                        {
                                    vUpperBound[nPos] = true;

                                    bool bOK = false;

                                    vector<bool> vUpperBoundDecrement = DecrementUpperBound(bOK, nPos, nTotalBoundaryBits, nSimBoundary, vUpperBound, v);

                                    if(bOK)
                                    {
                                                return vUpperBoundDecrement;
                                    }

                                    // check this if the above failed

                                    if(BoolVectorSimilarityWithZeroVector(vUpperBound) == nSimBoundary)
                                    {
                                                return vUpperBound;
                                    }
                        }
            }
            return vUpperBound;
}

 vector<bool> GrayCode :: DecrementUpperBound(bool& bOK, uint nPos, uint nTotalBits, uint nSimBoundary, vector<bool> vUpperBound, vector<bool> vLowerBound)
{          //          starting right to left, one bit at a time 1->0, reset than try again, if equal to the Sim, reset upper bound to the lower value
            bOK = false;

            vector<bool> vDecrementUpperBound = vUpperBound;
            for(int n = nTotalBits - 1; n > (int)nPos; n--)
            {
                        vDecrementUpperBound = vUpperBound;

                        if(vUpperBound[n])
                        {
                                    vDecrementUpperBound[n] = false;

                                    if((BoolVectorSimilarityWithZeroVector(vDecrementUpperBound) == nSimBoundary) && (vLowerBound < vDecrementUpperBound))
                                   {
                                                bOK = true;
                                                vUpperBound = vDecrementUpperBound;      //          reset upper bound to the lower value with the correct Sim
                                    }
                        }
            }
            return vDecrementUpperBound;
}