/root/doris/be/src/util/bitmap.h
Line | Count | Source |
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 "common/cast_set.h" |
32 | | #include "util/bit_util.h" |
33 | | |
34 | | namespace doris { |
35 | | #include "common/compile_check_begin.h" |
36 | | |
37 | | // Return the number of bytes necessary to store the given number of bits. |
38 | 25.0M | inline size_t BitmapSize(size_t num_bits) { |
39 | 25.0M | return (num_bits + 7) / 8; |
40 | 25.0M | } |
41 | | |
42 | | // Set the given bit. |
43 | | inline void BitmapSet(uint8_t* bitmap, size_t idx) { |
44 | | bitmap[idx >> 3] |= 1 << (idx & 7); |
45 | | } |
46 | | |
47 | | // Switch the given bit to the specified value. |
48 | 451k | inline void BitmapChange(uint8_t* bitmap, size_t idx, bool value) { |
49 | 451k | bitmap[idx >> 3] = |
50 | 451k | cast_set<uint8_t>((bitmap[idx >> 3] & ~(1 << (idx & 7))) | ((!!value) << (idx & 7))); |
51 | 451k | } |
52 | | |
53 | | // Clear the given bit. |
54 | | inline void BitmapClear(uint8_t* bitmap, size_t idx) { |
55 | | bitmap[idx >> 3] &= ~(1 << (idx & 7)); |
56 | | } |
57 | | |
58 | | // Test/get the given bit. |
59 | 902k | inline bool BitmapTest(const uint8_t* bitmap, size_t idx) { |
60 | 902k | return bitmap[idx >> 3] & (1 << (idx & 7)); |
61 | 902k | } |
62 | | |
63 | | // Merge the two bitmaps using bitwise or. Both bitmaps should have at least |
64 | | // n_bits valid bits. |
65 | 0 | inline void BitmapMergeOr(uint8_t* dst, const uint8_t* src, size_t n_bits) { |
66 | 0 | size_t n_bytes = BitmapSize(n_bits); |
67 | 0 | for (size_t i = 0; i < n_bytes; i++) { |
68 | 0 | *dst++ |= *src++; |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | | // Set bits from offset to (offset + num_bits) to the specified value |
73 | | void BitmapChangeBits(uint8_t* bitmap, size_t offset, size_t num_bits, bool value); |
74 | | |
75 | | // Find the first bit of the specified value, starting from the specified offset. |
76 | | bool BitmapFindFirst(const uint8_t* bitmap, size_t offset, size_t bitmap_size, bool value, |
77 | | size_t* idx); |
78 | | |
79 | | // Find the first set bit in the bitmap, at the specified offset. |
80 | | inline bool BitmapFindFirstSet(const uint8_t* bitmap, size_t offset, size_t bitmap_size, |
81 | | size_t* idx) { |
82 | | return BitmapFindFirst(bitmap, offset, bitmap_size, true, idx); |
83 | | } |
84 | | |
85 | | // Find the first zero bit in the bitmap, at the specified offset. |
86 | | inline bool BitmapFindFirstZero(const uint8_t* bitmap, size_t offset, size_t bitmap_size, |
87 | | size_t* idx) { |
88 | | return BitmapFindFirst(bitmap, offset, bitmap_size, false, idx); |
89 | | } |
90 | | |
91 | | // Returns true if the bitmap contains only ones. |
92 | 0 | inline bool BitMapIsAllSet(const uint8_t* bitmap, size_t offset, size_t bitmap_size) { |
93 | 0 | DCHECK_LT(offset, bitmap_size); |
94 | 0 | size_t idx; |
95 | 0 | return !BitmapFindFirstZero(bitmap, offset, bitmap_size, &idx); |
96 | 0 | } |
97 | | |
98 | | // Returns true if the bitmap contains only zeros. |
99 | 0 | inline bool BitmapIsAllZero(const uint8_t* bitmap, size_t offset, size_t bitmap_size) { |
100 | 0 | DCHECK_LT(offset, bitmap_size); |
101 | 0 | size_t idx; |
102 | 0 | return !BitmapFindFirstSet(bitmap, offset, bitmap_size, &idx); |
103 | 0 | } |
104 | | |
105 | | // Returns true if the two bitmaps are equal. |
106 | | // |
107 | | // It is assumed that both bitmaps have 'bitmap_size' number of bits. |
108 | 0 | inline bool BitmapEquals(const uint8_t* bm1, const uint8_t* bm2, size_t bitmap_size) { |
109 | 0 | size_t num_full_bytes = bitmap_size >> 3; |
110 | 0 | if (memcmp(bm1, bm2, num_full_bytes)) { |
111 | 0 | return false; |
112 | 0 | } |
113 | 0 |
|
114 | 0 | // Check any remaining bits in one extra operation. |
115 | 0 | size_t num_remaining_bits = bitmap_size - (num_full_bytes << 3); |
116 | 0 | if (num_remaining_bits == 0) { |
117 | 0 | return true; |
118 | 0 | } |
119 | 0 | DCHECK_LT(num_remaining_bits, 8); |
120 | 0 | auto mask = (1 << num_remaining_bits) - 1; |
121 | 0 | return (bm1[num_full_bytes] & mask) == (bm2[num_full_bytes] & mask); |
122 | 0 | } |
123 | | |
124 | | // Iterator which yields ranges of set and unset bits. |
125 | | // Example usage: |
126 | | // bool value; |
127 | | // size_t size; |
128 | | // BitmapIterator iter(bitmap, n_bits); |
129 | | // while ((size = iter.Next(&value))) { |
130 | | // printf("bitmap block len=%lu value=%d\n", size, value); |
131 | | // } |
132 | | class BitmapIterator { |
133 | | public: |
134 | | BitmapIterator(const uint8_t* map, size_t num_bits) |
135 | 451k | : offset_(0), num_bits_(num_bits), map_(map) {} |
136 | | |
137 | 0 | void Reset(const uint8_t* map, size_t num_bits) { |
138 | 0 | offset_ = 0; |
139 | 0 | num_bits_ = num_bits; |
140 | 0 | map_ = map; |
141 | 0 | } |
142 | | |
143 | 0 | void Reset() { |
144 | 0 | offset_ = 0; |
145 | 0 | num_bits_ = 0; |
146 | 0 | map_ = nullptr; |
147 | 0 | } |
148 | | |
149 | | bool done() const { return (num_bits_ - offset_) == 0; } |
150 | | |
151 | | void SeekTo(size_t bit) { |
152 | | DCHECK_LE(bit, num_bits_); |
153 | | offset_ = bit; |
154 | | } |
155 | | |
156 | | size_t Next(bool* value, size_t max_run) { |
157 | | return NextWithLimit(value, std::min(num_bits_, max_run + offset_)); |
158 | | } |
159 | | |
160 | 902k | size_t Next(bool* value) { return NextWithLimit(value, num_bits_); } |
161 | | |
162 | | private: |
163 | 902k | size_t NextWithLimit(bool* value, size_t limit) { |
164 | 902k | size_t len = limit - offset_; |
165 | 902k | if (len == 0) [[unlikely]] { |
166 | 451k | return (0); |
167 | 451k | } |
168 | | |
169 | 451k | *value = BitmapTest(map_, offset_); |
170 | | |
171 | 451k | size_t index; |
172 | 451k | if (BitmapFindFirst(map_, offset_, limit, !(*value), &index)) { |
173 | 5 | len = index - offset_; |
174 | 451k | } else { |
175 | 451k | index = limit; |
176 | 451k | } |
177 | | |
178 | 451k | offset_ = index; |
179 | 451k | return len; |
180 | 902k | } |
181 | | |
182 | | private: |
183 | | size_t offset_; |
184 | | size_t num_bits_; |
185 | | const uint8_t* map_ = nullptr; |
186 | | }; |
187 | | |
188 | | /// Bitmap vector utility class. |
189 | | /// TODO: investigate perf. |
190 | | /// - Precomputed bitmap |
191 | | /// - Explicit Set/Unset() apis |
192 | | /// - Bigger words |
193 | | /// - size bitmap to Mersenne prime. |
194 | | class Bitmap { |
195 | | public: |
196 | 204k | Bitmap(int64_t num_bits) { |
197 | 204k | DCHECK_GE(num_bits, 0); |
198 | 204k | buffer_.resize(BitUtil::round_up_numi_64(cast_set<uint32_t>(num_bits))); |
199 | 204k | num_bits_ = num_bits; |
200 | 204k | } |
201 | | |
202 | | /// Resize bitmap and set all bits to zero. |
203 | 200k | void Reset(int64_t num_bits) { |
204 | 200k | DCHECK_GE(num_bits, 0); |
205 | 200k | buffer_.resize(BitUtil::round_up_numi_64(cast_set<uint32_t>(num_bits))); |
206 | 200k | num_bits_ = num_bits; |
207 | 200k | SetAllBits(false); |
208 | 200k | } |
209 | | |
210 | | /// Compute memory usage of a bitmap, not including the Bitmap object itself. |
211 | 0 | static int64_t MemUsage(int64_t num_bits) { |
212 | 0 | DCHECK_GE(num_bits, 0); |
213 | 0 | return BitUtil::round_up_numi_64(cast_set<uint32_t>(num_bits)) * sizeof(int64_t); |
214 | 0 | } |
215 | | |
216 | | /// Compute memory usage of this bitmap, not including the Bitmap object itself. |
217 | 0 | int64_t MemUsage() const { return MemUsage(num_bits_); } |
218 | | |
219 | | /// Sets the bit at 'bit_index' to v. |
220 | 122k | void Set(int64_t bit_index, bool v) { |
221 | 122k | int64_t word_index = bit_index >> NUM_OFFSET_BITS; |
222 | 122k | bit_index &= BIT_INDEX_MASK; |
223 | 122k | DCHECK_LT(word_index, buffer_.size()); |
224 | 122k | if (v) { |
225 | 122k | buffer_[word_index] |= (1LL << bit_index); |
226 | 18.4E | } else { |
227 | 18.4E | buffer_[word_index] &= ~(1LL << bit_index); |
228 | 18.4E | } |
229 | 122k | } |
230 | | |
231 | | /// Returns true if the bit at 'bit_index' is set. |
232 | 121k | bool Get(int64_t bit_index) const { |
233 | 121k | int64_t word_index = bit_index >> NUM_OFFSET_BITS; |
234 | 121k | bit_index &= BIT_INDEX_MASK; |
235 | 121k | DCHECK_LT(word_index, buffer_.size()); |
236 | 121k | return (buffer_[word_index] & (1LL << bit_index)) != 0; |
237 | 121k | } |
238 | | |
239 | 200k | void SetAllBits(bool b) { memset(&buffer_[0], 255 * b, buffer_.size() * sizeof(uint64_t)); } |
240 | | |
241 | 0 | int64_t num_bits() const { return num_bits_; } |
242 | | |
243 | | /// If 'print_bits' prints 0/1 per bit, otherwise it prints the int64_t value. |
244 | | std::string DebugString(bool print_bits) const; |
245 | | |
246 | | private: |
247 | | std::vector<uint64_t> buffer_; |
248 | | int64_t num_bits_; |
249 | | |
250 | | /// Used for bit shifting and masking for the word and offset calculation. |
251 | | static const int64_t NUM_OFFSET_BITS = 6; |
252 | | static const int64_t BIT_INDEX_MASK = 63; |
253 | | }; |
254 | | #include "common/compile_check_end.h" |
255 | | } // namespace doris |