• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bitset.h"
17 #include "securec.h"
18 
19 namespace panda::es2panda::util {
BitSet(size_t size)20 BitSet::BitSet(size_t size) : size_(size)
21 {
22     size_t dataSize = DataSize();
23     data_ = new uint8_t[dataSize];
24     memset_s(data_, dataSize, 0, dataSize);
25 }
26 
~BitSet()27 BitSet::~BitSet()
28 {
29     delete[] data_;
30 }
31 
DataSize() const32 size_t BitSet::DataSize() const noexcept
33 {
34     return (size_ >> shiftOffset) + 1;
35 }
36 
Clear(bool value)37 void BitSet::Clear(bool value) noexcept
38 {
39     memset_s(data_, DataSize(), value ? ((sizeof(uint8_t) << 8U) - 1) : 0, DataSize());
40 }
41 
Set(size_t pos)42 void BitSet::Set(size_t pos) noexcept
43 {
44     Set(pos, true);
45 }
46 
Set(size_t pos,bool value)47 void BitSet::Set(size_t pos, bool value) noexcept
48 {
49     ASSERT(pos < size_);
50     size_t idx = pos >> shiftOffset;
51     size_t slot = pos & shiftMask;
52 
53     if (value) {
54         data_[idx] |= 1U << slot;
55     } else {
56         data_[idx] &= ~(1U << slot);
57     }
58 }
59 
Test(size_t pos) const60 bool BitSet::Test(size_t pos) const noexcept
61 {
62     ASSERT(pos < size_);
63     size_t idx = pos >> shiftOffset;
64     size_t slot = pos & shiftMask;
65 
66     return (data_[idx] & (1U << slot)) != 0;
67 }
68 
69 }  // namespace panda::es2panda::util
70