zxingwp7.common.BitArray.isRange C# (CSharp) Méthode

isRange() public méthode

Efficient method to check if a range of bits is set, or not set.
public isRange ( int start, int end, bool value_Renamed ) : bool
start int start of range, inclusive. ///
end int end of range, exclusive ///
value_Renamed bool
Résultat bool
        public bool isRange(int start, int end, bool value_Renamed)
        {
            if (end < start)
            {
                throw new ArgumentException();
            }
            if (end == start)
            {
                return true; // empty range matches
            }
            end--; // will be easier to treat this as the last actually set bit -- inclusive    
            int firstInt = start >> 5;
            int lastInt = end >> 5;
            for (int i = firstInt; i <= lastInt; i++)
            {
                int firstBit = i > firstInt ? 0 : start & 0x1F;
                int lastBit = i < lastInt ? 31 : end & 0x1F;
                int mask;
                if (firstBit == 0 && lastBit == 31)
                {
                    mask = - 1;
                }
                else
                {
                    mask = 0;
                    for (int j = firstBit; j <= lastBit; j++)
                    {
                        mask |= 1 << j;
                    }
                }

                // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
                // equals the mask, or we're looking for 0s and the masked portion is not all 0s
                if ((bits[i] & mask) != (value_Renamed ? mask : 0))
                {
                    return false;
                }
            }
            return true;
        }

Usage Example

        private static int[] findAsteriskPattern(BitArray row)
        {
            int width = row.Size;
            int rowOffset = 0;
            while (rowOffset < width)
            {
                if (row.get_Renamed(rowOffset))
                {
                    break;
                }
                rowOffset++;
            }

            int counterPosition = 0;
            var counters = new int[9];
            int patternStart = rowOffset;
            bool isWhite = false;
            int patternLength = counters.Length;

            for (int i = rowOffset; i < width; i++)
            {
                bool pixel = row.get_Renamed(i);
                if (pixel ^ isWhite)
                {
                    counters[counterPosition]++;
                }
                else
                {
                    if (counterPosition == patternLength - 1)
                    {
                        if (toNarrowWidePattern(counters) == ASTERISK_ENCODING)
                        {
                            // Look for whitespace before start pattern, >= 50% of width of start pattern
                            if (row.isRange(Math.Max(0, patternStart - (i - patternStart)/2), patternStart, false))
                            {
                                return new[] {patternStart, i};
                            }
                        }
                        patternStart += counters[0] + counters[1];
                        for (int y = 2; y < patternLength; y++)
                        {
                            counters[y - 2] = counters[y];
                        }
                        counters[patternLength - 2] = 0;
                        counters[patternLength - 1] = 0;
                        counterPosition--;
                    }
                    else
                    {
                        counterPosition++;
                    }
                    counters[counterPosition] = 1;
                    isWhite = !isWhite;
                }
            }
            throw ReaderException.Instance;
        }