/root/doris/be/src/util/bitmap.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/bitmap.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <glog/logging.h> |
24 | | #include <stdint.h> |
25 | | #include <string.h> |
26 | | |
27 | | #include <algorithm> |
28 | | #include <string> |
29 | | #include <vector> |
30 | | |
31 | | #include "gutil/port.h" |
32 | | #include "util/bit_util.h" |
33 | | |
34 | | namespace doris { |
35 | | |
36 | | // Return the number of bytes necessary to store the given number of bits. |
37 | 25.3k | inline size_t BitmapSize(size_t num_bits) { |
38 | 25.3k | return (num_bits + 7) / 8; |
39 | 25.3k | } |
40 | | |
41 | | // Set the given bit. |
42 | 2.80k | inline void BitmapSet(uint8_t* bitmap, size_t idx) { |
43 | 2.80k | bitmap[idx >> 3] |= 1 << (idx & 7); |
44 | 2.80k | } |
45 | | |
46 | | // Switch the given bit to the specified value. |
47 | 451k | inline void BitmapChange(uint8_t* bitmap, size_t idx, bool value) { |
48 | 451k | bitmap[idx >> 3] = (bitmap[idx >> 3] & ~(1 << (idx & 7))) | ((!!value) << (idx & 7)); |
49 | 451k | } |
50 | | |
51 | | // Clear the given bit. |
52 | 2 | inline void BitmapClear(uint8_t* bitmap, size_t idx) { |
53 | 2 | bitmap[idx >> 3] &= ~(1 << (idx & 7)); |
54 | 2 | } |
55 | | |
56 | | // Test/get the given bit. |
57 | 902k | inline bool BitmapTest(const uint8_t* bitmap, size_t idx) { |
58 | 902k | return bitmap[idx >> 3] & (1 << (idx & 7)); |
59 | 902k | } |
60 | | |
61 | | // Merge the two bitmaps using bitwise or. Both bitmaps should have at least |
62 | | // n_bits valid bits. |
63 | 0 | inline void BitmapMergeOr(uint8_t* dst, const uint8_t* src, size_t n_bits) { |
64 | 0 | size_t n_bytes = BitmapSize(n_bits); |
65 | 0 | for (size_t i = 0; i < n_bytes; i++) { |
66 | 0 | *dst++ |= *src++; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | // Set bits from offset to (offset + num_bits) to the specified value |
71 | | void BitmapChangeBits(uint8_t* bitmap, size_t offset, size_t num_bits, bool value); |
72 | | |
73 | | // Find the first bit of the specified value, starting from the specified offset. |
74 | | bool BitmapFindFirst(const uint8_t* bitmap, size_t offset, size_t bitmap_size, bool value, |
75 | | size_t* idx); |
76 | | |
77 | | // Find the first set bit in the bitmap, at the specified offset. |
78 | | inline bool BitmapFindFirstSet(const uint8_t* bitmap, size_t offset, size_t bitmap_size, |
79 | 2 | size_t* idx) { |
80 | 2 | return BitmapFindFirst(bitmap, offset, bitmap_size, true, idx); |
81 | 2 | } |
82 | | |
83 | | // Find the first zero bit in the bitmap, at the specified offset. |
84 | | inline bool BitmapFindFirstZero(const uint8_t* bitmap, size_t offset, size_t bitmap_size, |
85 | 2 | size_t* idx) { |
86 | 2 | return BitmapFindFirst(bitmap, offset, bitmap_size, false, idx); |
87 | 2 | } |
88 | | |
89 | | // Returns true if the bitmap contains only ones. |
90 | 0 | inline bool BitMapIsAllSet(const uint8_t* bitmap, size_t offset, size_t bitmap_size) { |
91 | 0 | DCHECK_LT(offset, bitmap_size); |
92 | 0 | size_t idx; |
93 | 0 | return !BitmapFindFirstZero(bitmap, offset, bitmap_size, &idx); |
94 | 0 | } |
95 | | |
96 | | // Returns true if the bitmap contains only zeros. |
97 | 0 | inline bool BitmapIsAllZero(const uint8_t* bitmap, size_t offset, size_t bitmap_size) { |
98 | 0 | DCHECK_LT(offset, bitmap_size); |
99 | 0 | size_t idx; |
100 | 0 | return !BitmapFindFirstSet(bitmap, offset, bitmap_size, &idx); |
101 | 0 | } |
102 | | |
103 | | // Returns true if the two bitmaps are equal. |
104 | | // |
105 | | // It is assumed that both bitmaps have 'bitmap_size' number of bits. |
106 | 0 | inline bool BitmapEquals(const uint8_t* bm1, const uint8_t* bm2, size_t bitmap_size) { |
107 | 0 | size_t num_full_bytes = bitmap_size >> 3; |
108 | 0 | if (memcmp(bm1, bm2, num_full_bytes)) { |
109 | 0 | return false; |
110 | 0 | } |
111 | 0 |
|
112 | 0 | // Check any remaining bits in one extra operation. |
113 | 0 | size_t num_remaining_bits = bitmap_size - (num_full_bytes << 3); |
114 | 0 | if (num_remaining_bits == 0) { |
115 | 0 | return true; |
116 | 0 | } |
117 | 0 | DCHECK_LT(num_remaining_bits, 8); |
118 | 0 | uint8_t mask = (1 << num_remaining_bits) - 1; |
119 | 0 | return (bm1[num_full_bytes] & mask) == (bm2[num_full_bytes] & mask); |
120 | 0 | } |
121 | | |
122 | | // This function will print the bitmap content in a format like the following: |
123 | | // eg: 0001110000100010110011001100001100110011 |
124 | | // output: |
125 | | // 0000: 00011100 00100010 11001100 11000011 |
126 | | // 0016: 00110011 |
127 | | std::string BitmapToString(const uint8_t* bitmap, size_t num_bits); |
128 | | |
129 | | // Iterator which yields ranges of set and unset bits. |
130 | | // Example usage: |
131 | | // bool value; |
132 | | // size_t size; |
133 | | // BitmapIterator iter(bitmap, n_bits); |
134 | | // while ((size = iter.Next(&value))) { |
135 | | // printf("bitmap block len=%lu value=%d\n", size, value); |
136 | | // } |
137 | | class BitmapIterator { |
138 | | public: |
139 | | BitmapIterator(const uint8_t* map, size_t num_bits) |
140 | 451k | : offset_(0), num_bits_(num_bits), map_(map) {} |
141 | | |
142 | 0 | void Reset(const uint8_t* map, size_t num_bits) { |
143 | 0 | offset_ = 0; |
144 | 0 | num_bits_ = num_bits; |
145 | 0 | map_ = map; |
146 | 0 | } |
147 | | |
148 | 0 | void Reset() { |
149 | 0 | offset_ = 0; |
150 | 0 | num_bits_ = 0; |
151 | 0 | map_ = nullptr; |
152 | 0 | } |
153 | | |
154 | 3 | bool done() const { return (num_bits_ - offset_) == 0; } |
155 | | |
156 | 2 | void SeekTo(size_t bit) { |
157 | 2 | DCHECK_LE(bit, num_bits_); |
158 | 2 | offset_ = bit; |
159 | 2 | } |
160 | | |
161 | 1 | size_t Next(bool* value, size_t max_run) { |
162 | 1 | return NextWithLimit(value, std::min(num_bits_, max_run + offset_)); |
163 | 1 | } |
164 | | |
165 | 902k | size_t Next(bool* value) { return NextWithLimit(value, num_bits_); } |
166 | | |
167 | | private: |
168 | 902k | size_t NextWithLimit(bool* value, size_t limit) { |
169 | 902k | size_t len = limit - offset_; |
170 | 902k | if (PREDICT_FALSE(len == 0)) return (0); |
171 | | |
172 | 451k | *value = BitmapTest(map_, offset_); |
173 | | |
174 | 451k | size_t index; |
175 | 451k | if (BitmapFindFirst(map_, offset_, limit, !(*value), &index)) { |
176 | 5 | len = index - offset_; |
177 | 451k | } else { |
178 | 451k | index = limit; |
179 | 451k | } |
180 | | |
181 | 451k | offset_ = index; |
182 | 451k | return len; |
183 | 902k | } |
184 | | |
185 | | private: |
186 | | size_t offset_; |
187 | | size_t num_bits_; |
188 | | const uint8_t* map_ = nullptr; |
189 | | }; |
190 | | |
191 | | /// Bitmap vector utility class. |
192 | | /// TODO: investigate perf. |
193 | | /// - Precomputed bitmap |
194 | | /// - Explicit Set/Unset() apis |
195 | | /// - Bigger words |
196 | | /// - size bitmap to Mersenne prime. |
197 | | class Bitmap { |
198 | | public: |
199 | 0 | Bitmap(int64_t num_bits) { |
200 | 0 | DCHECK_GE(num_bits, 0); |
201 | 0 | buffer_.resize(BitUtil::round_up_numi_64(num_bits)); |
202 | 0 | num_bits_ = num_bits; |
203 | 0 | } |
204 | | |
205 | | /// Resize bitmap and set all bits to zero. |
206 | 0 | void Reset(int64_t num_bits) { |
207 | 0 | DCHECK_GE(num_bits, 0); |
208 | 0 | buffer_.resize(BitUtil::round_up_numi_64(num_bits)); |
209 | 0 | num_bits_ = num_bits; |
210 | 0 | SetAllBits(false); |
211 | 0 | } |
212 | | |
213 | | /// Compute memory usage of a bitmap, not including the Bitmap object itself. |
214 | 0 | static int64_t MemUsage(int64_t num_bits) { |
215 | 0 | DCHECK_GE(num_bits, 0); |
216 | 0 | return BitUtil::round_up_numi_64(num_bits) * sizeof(int64_t); |
217 | 0 | } |
218 | | |
219 | | /// Compute memory usage of this bitmap, not including the Bitmap object itself. |
220 | 0 | int64_t MemUsage() const { return MemUsage(num_bits_); } |
221 | | |
222 | | /// Sets the bit at 'bit_index' to v. |
223 | 0 | void Set(int64_t bit_index, bool v) { |
224 | 0 | int64_t word_index = bit_index >> NUM_OFFSET_BITS; |
225 | 0 | bit_index &= BIT_INDEX_MASK; |
226 | 0 | DCHECK_LT(word_index, buffer_.size()); |
227 | 0 | if (v) { |
228 | 0 | buffer_[word_index] |= (1LL << bit_index); |
229 | 0 | } else { |
230 | 0 | buffer_[word_index] &= ~(1LL << bit_index); |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | | /// Returns true if the bit at 'bit_index' is set. |
235 | 0 | bool Get(int64_t bit_index) const { |
236 | 0 | int64_t word_index = bit_index >> NUM_OFFSET_BITS; |
237 | 0 | bit_index &= BIT_INDEX_MASK; |
238 | 0 | DCHECK_LT(word_index, buffer_.size()); |
239 | 0 | return (buffer_[word_index] & (1LL << bit_index)) != 0; |
240 | 0 | } |
241 | | |
242 | 0 | void SetAllBits(bool b) { memset(&buffer_[0], 255 * b, buffer_.size() * sizeof(uint64_t)); } |
243 | | |
244 | 0 | int64_t num_bits() const { return num_bits_; } |
245 | | |
246 | | /// If 'print_bits' prints 0/1 per bit, otherwise it prints the int64_t value. |
247 | | std::string DebugString(bool print_bits) const; |
248 | | |
249 | | private: |
250 | | std::vector<uint64_t> buffer_; |
251 | | int64_t num_bits_; |
252 | | |
253 | | /// Used for bit shifting and masking for the word and offset calculation. |
254 | | static const int64_t NUM_OFFSET_BITS = 6; |
255 | | static const int64_t BIT_INDEX_MASK = 63; |
256 | | }; |
257 | | |
258 | | } // namespace doris |