blob: 34cdb65174743bfbe9614704d5c51e2b809ef873 [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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
Dan Sinclaire7786682017-03-29 15:18:41 -040023#include "fxbarcode/common/BC_CommonBitArray.h"
Tom Sepez8b6186f2017-03-28 12:06:45 -070024
25#include <utility>
26
Dan Sinclaire7786682017-03-29 15:18:41 -040027#include "fxbarcode/utils.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040028
29CBC_CommonBitArray::CBC_CommonBitArray(CBC_CommonBitArray* array) {
30 m_size = array->GetSize();
Tom Sepez8b6186f2017-03-28 12:06:45 -070031 m_bits = array->GetBits();
Dan Sinclair1770c022016-03-14 14:14:16 -040032}
Tom Sepez8b6186f2017-03-28 12:06:45 -070033
Dan Sinclair1770c022016-03-14 14:14:16 -040034CBC_CommonBitArray::CBC_CommonBitArray() {
Tom Sepez8b6186f2017-03-28 12:06:45 -070035 m_bits.resize(1);
Dan Sinclair1770c022016-03-14 14:14:16 -040036 m_size = 0;
37}
Tom Sepez8b6186f2017-03-28 12:06:45 -070038
Dan Sinclair1770c022016-03-14 14:14:16 -040039CBC_CommonBitArray::CBC_CommonBitArray(int32_t size) {
Tom Sepez8b6186f2017-03-28 12:06:45 -070040 m_bits.resize((size + 31) >> 5);
Dan Sinclair1770c022016-03-14 14:14:16 -040041 m_size = size;
42}
Tom Sepez8b6186f2017-03-28 12:06:45 -070043
44CBC_CommonBitArray::~CBC_CommonBitArray() {}
45
46size_t CBC_CommonBitArray::GetSize() {
Dan Sinclair1770c022016-03-14 14:14:16 -040047 return m_size;
48}
Tom Sepez8b6186f2017-03-28 12:06:45 -070049
50std::vector<int32_t>& CBC_CommonBitArray::GetBits() {
Dan Sinclair1770c022016-03-14 14:14:16 -040051 return m_bits;
52}
Tom Sepez8b6186f2017-03-28 12:06:45 -070053
54size_t CBC_CommonBitArray::GetSizeInBytes() {
Dan Sinclair1770c022016-03-14 14:14:16 -040055 return (m_size + 7) >> 3;
56}
Tom Sepez8b6186f2017-03-28 12:06:45 -070057
58bool CBC_CommonBitArray::Get(size_t i) {
Dan Sinclair1770c022016-03-14 14:14:16 -040059 return (m_bits[i >> 5] & (1 << (i & 0x1f))) != 0;
60}
Tom Sepez8b6186f2017-03-28 12:06:45 -070061
62void CBC_CommonBitArray::Set(size_t i) {
Dan Sinclair1770c022016-03-14 14:14:16 -040063 m_bits[i >> 5] |= 1 << (i & 0x1F);
64}
Tom Sepez8b6186f2017-03-28 12:06:45 -070065
66void CBC_CommonBitArray::Flip(size_t i) {
Dan Sinclair1770c022016-03-14 14:14:16 -040067 m_bits[i >> 5] ^= 1 << (i & 0x1F);
68}
Tom Sepez8b6186f2017-03-28 12:06:45 -070069
70void CBC_CommonBitArray::SetBulk(size_t i, int32_t newBits) {
Dan Sinclair1770c022016-03-14 14:14:16 -040071 m_bits[i >> 5] = newBits;
72}
Tom Sepez8b6186f2017-03-28 12:06:45 -070073
Dan Sinclair1770c022016-03-14 14:14:16 -040074void CBC_CommonBitArray::Clear() {
Tom Sepez8b6186f2017-03-28 12:06:45 -070075 for (auto& value : m_bits)
76 value = 0;
Dan Sinclair1770c022016-03-14 14:14:16 -040077}
Tom Sepez8b6186f2017-03-28 12:06:45 -070078
79bool CBC_CommonBitArray::IsRange(size_t start,
80 size_t end,
tsepezd19e9122016-11-02 15:43:18 -070081 bool value,
82 int32_t& e) {
Dan Sinclair1770c022016-03-14 14:14:16 -040083 if (end < start) {
84 e = BCExceptionEndLessThanStart;
tsepezd19e9122016-11-02 15:43:18 -070085 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -040086 }
87 if (end == start) {
tsepezd19e9122016-11-02 15:43:18 -070088 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -040089 }
90 end--;
91 int32_t firstInt = start >> 5;
92 int32_t lastInt = end >> 5;
93 int32_t i;
94 for (i = firstInt; i <= lastInt; i++) {
95 int32_t firstBit = i > firstInt ? 0 : start & 0x1F;
96 int32_t lastBit = i < lastInt ? 31 : end & 0x1F;
97 int32_t mask;
98 if (firstBit == 0 && lastBit == 31) {
99 mask = -1;
100 } else {
101 mask = 0;
102 for (int32_t j = firstBit; j <= lastBit; j++) {
103 mask |= 1 << j;
104 }
105 }
106 if ((m_bits[i] & mask) != (value ? mask : 0)) {
tsepezd19e9122016-11-02 15:43:18 -0700107 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400108 }
109 }
tsepezd19e9122016-11-02 15:43:18 -0700110 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400111}
Tom Sepez8b6186f2017-03-28 12:06:45 -0700112
Dan Sinclair1770c022016-03-14 14:14:16 -0400113int32_t* CBC_CommonBitArray::GetBitArray() {
Tom Sepez8b6186f2017-03-28 12:06:45 -0700114 return m_bits.data();
Dan Sinclair1770c022016-03-14 14:14:16 -0400115}
Tom Sepez8b6186f2017-03-28 12:06:45 -0700116
Dan Sinclair1770c022016-03-14 14:14:16 -0400117void CBC_CommonBitArray::Reverse() {
Tom Sepez8b6186f2017-03-28 12:06:45 -0700118 std::vector<int32_t> newBits(m_bits.size());
119 for (size_t i = 0; i < m_size; i++) {
120 if (Get(m_size - i - 1))
Dan Sinclair1770c022016-03-14 14:14:16 -0400121 newBits[i >> 5] |= 1 << (i & 0x1F);
Dan Sinclair1770c022016-03-14 14:14:16 -0400122 }
Tom Sepez8b6186f2017-03-28 12:06:45 -0700123 m_bits = std::move(newBits);
Dan Sinclair1770c022016-03-14 14:14:16 -0400124}