1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_processor/containers/bit_vector.h"
18 
19 #include "src/trace_processor/containers/bit_vector_iterators.h"
20 
21 namespace perfetto {
22 namespace trace_processor {
23 
24 BitVector::BitVector() = default;
25 
BitVector(std::initializer_list<bool> init)26 BitVector::BitVector(std::initializer_list<bool> init) {
27   for (bool x : init) {
28     if (x) {
29       AppendTrue();
30     } else {
31       AppendFalse();
32     }
33   }
34 }
35 
BitVector(uint32_t count,bool value)36 BitVector::BitVector(uint32_t count, bool value) {
37   Resize(count, value);
38 }
39 
BitVector(std::vector<Block> blocks,std::vector<uint32_t> counts,uint32_t size)40 BitVector::BitVector(std::vector<Block> blocks,
41                      std::vector<uint32_t> counts,
42                      uint32_t size)
43     : size_(size), counts_(std::move(counts)), blocks_(std::move(blocks)) {}
44 
Copy() const45 BitVector BitVector::Copy() const {
46   return BitVector(blocks_, counts_, size_);
47 }
48 
IterateAllBits() const49 BitVector::AllBitsIterator BitVector::IterateAllBits() const {
50   return AllBitsIterator(this);
51 }
52 
IterateSetBits() const53 BitVector::SetBitsIterator BitVector::IterateSetBits() const {
54   return SetBitsIterator(this);
55 }
56 
UpdateSetBits(const BitVector & o)57 void BitVector::UpdateSetBits(const BitVector& o) {
58   PERFETTO_DCHECK(o.size() <= GetNumBitsSet());
59 
60   // For each set bit in this bitvector, we lookup whether the bit in |other|
61   // at that index (if in bounds) is set. If not, we clear the bit.
62   for (auto it = IterateSetBits(); it; it.Next()) {
63     if (it.ordinal() >= o.size() || !o.IsSet(it.ordinal()))
64       it.Clear();
65   }
66 
67   // After the loop, we should have precisely the same number of bits
68   // set as |other|.
69   PERFETTO_DCHECK(o.GetNumBitsSet() == GetNumBitsSet());
70 }
71 
72 }  // namespace trace_processor
73 }  // namespace perfetto
74