Coverage Report

Created: 2025-05-09 10:08

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