blob: 10da9bef1b4990937ced6f75fae111c38e102d91 [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
Dan Sinclair1770c022016-03-14 14:14:16 -040079int32_t* CBC_CommonBitArray::GetBitArray() {
Tom Sepez8b6186f2017-03-28 12:06:45 -070080 return m_bits.data();
Dan Sinclair1770c022016-03-14 14:14:16 -040081}
Tom Sepez8b6186f2017-03-28 12:06:45 -070082
Dan Sinclair1770c022016-03-14 14:14:16 -040083void CBC_CommonBitArray::Reverse() {
Tom Sepez8b6186f2017-03-28 12:06:45 -070084 std::vector<int32_t> newBits(m_bits.size());
85 for (size_t i = 0; i < m_size; i++) {
86 if (Get(m_size - i - 1))
Dan Sinclair1770c022016-03-14 14:14:16 -040087 newBits[i >> 5] |= 1 << (i & 0x1F);
Dan Sinclair1770c022016-03-14 14:14:16 -040088 }
Tom Sepez8b6186f2017-03-28 12:06:45 -070089 m_bits = std::move(newBits);
Dan Sinclair1770c022016-03-14 14:14:16 -040090}