Home | History | Annotate | Download | only in common
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 // Original code is licensed as follows:
      7 /*
      8  * Copyright 2007 ZXing authors
      9  *
     10  * Licensed under the Apache License, Version 2.0 (the "License");
     11  * you may not use this file except in compliance with the License.
     12  * You may obtain a copy of the License at
     13  *
     14  *      http://www.apache.org/licenses/LICENSE-2.0
     15  *
     16  * Unless required by applicable law or agreed to in writing, software
     17  * distributed under the License is distributed on an "AS IS" BASIS,
     18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     19  * See the License for the specific language governing permissions and
     20  * limitations under the License.
     21  */
     22 
     23 #include "xfa/fxbarcode/common/BC_CommonBitArray.h"
     24 #include "xfa/fxbarcode/utils.h"
     25 
     26 CBC_CommonBitArray::CBC_CommonBitArray(CBC_CommonBitArray* array) {
     27   m_size = array->GetSize();
     28   m_bits.Copy(array->GetBits());
     29 }
     30 CBC_CommonBitArray::CBC_CommonBitArray() {
     31   m_bits.SetSize(1);
     32   m_size = 0;
     33 }
     34 CBC_CommonBitArray::CBC_CommonBitArray(int32_t size) {
     35   m_bits.SetSize((size + 31) >> 5);
     36   m_size = size;
     37 }
     38 CBC_CommonBitArray::~CBC_CommonBitArray() {
     39   m_size = 0;
     40 }
     41 int32_t CBC_CommonBitArray::GetSize() {
     42   return m_size;
     43 }
     44 CFX_ArrayTemplate<int32_t>& CBC_CommonBitArray::GetBits() {
     45   return m_bits;
     46 }
     47 int32_t CBC_CommonBitArray::GetSizeInBytes() {
     48   return (m_size + 7) >> 3;
     49 }
     50 bool CBC_CommonBitArray::Get(int32_t i) {
     51   return (m_bits[i >> 5] & (1 << (i & 0x1f))) != 0;
     52 }
     53 void CBC_CommonBitArray::Set(int32_t i) {
     54   m_bits[i >> 5] |= 1 << (i & 0x1F);
     55 }
     56 void CBC_CommonBitArray::Flip(int32_t i) {
     57   m_bits[i >> 5] ^= 1 << (i & 0x1F);
     58 }
     59 void CBC_CommonBitArray::SetBulk(int32_t i, int32_t newBits) {
     60   m_bits[i >> 5] = newBits;
     61 }
     62 void CBC_CommonBitArray::Clear() {
     63   FXSYS_memset(&m_bits[0], 0x00, m_bits.GetSize() * sizeof(int32_t));
     64 }
     65 bool CBC_CommonBitArray::IsRange(int32_t start,
     66                                  int32_t end,
     67                                  bool value,
     68                                  int32_t& e) {
     69   if (end < start) {
     70     e = BCExceptionEndLessThanStart;
     71     return false;
     72   }
     73   if (end == start) {
     74     return true;
     75   }
     76   end--;
     77   int32_t firstInt = start >> 5;
     78   int32_t lastInt = end >> 5;
     79   int32_t i;
     80   for (i = firstInt; i <= lastInt; i++) {
     81     int32_t firstBit = i > firstInt ? 0 : start & 0x1F;
     82     int32_t lastBit = i < lastInt ? 31 : end & 0x1F;
     83     int32_t mask;
     84     if (firstBit == 0 && lastBit == 31) {
     85       mask = -1;
     86     } else {
     87       mask = 0;
     88       for (int32_t j = firstBit; j <= lastBit; j++) {
     89         mask |= 1 << j;
     90       }
     91     }
     92     if ((m_bits[i] & mask) != (value ? mask : 0)) {
     93       return false;
     94     }
     95   }
     96   return true;
     97 }
     98 int32_t* CBC_CommonBitArray::GetBitArray() {
     99   return &m_bits[0];
    100 }
    101 void CBC_CommonBitArray::Reverse() {
    102   int32_t* newBits = FX_Alloc(int32_t, m_bits.GetSize());
    103   FXSYS_memset(newBits, 0x00, m_bits.GetSize() * sizeof(int32_t));
    104   int32_t size = m_size;
    105   int32_t i;
    106   for (i = 0; i < size; i++) {
    107     if (Get(size - i - 1)) {
    108       newBits[i >> 5] |= 1 << (i & 0x1F);
    109     }
    110   }
    111   FXSYS_memcpy(&m_bits[0], newBits, m_bits.GetSize() * sizeof(int32_t));
    112   FX_Free(newBits);
    113 }
    114