Coverage Report

Created: 2026-04-11 00:05

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