Coverage Report

Created: 2024-11-21 11:46

/root/doris/be/src/util/bitmap.cpp
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.cpp
19
// and modified by Doris
20
21
#include "util/bitmap.h"
22
23
#include <sstream>
24
25
#include "gutil/stringprintf.h"
26
27
namespace doris {
28
29
0
std::string Bitmap::DebugString(bool print_bits) const {
30
0
    int64_t words = BitUtil::round_up(num_bits_, 64) / 64;
31
0
    std::stringstream ss;
32
0
    ss << "Size (" << num_bits_ << ") words (" << words << ") ";
33
0
    if (print_bits) {
34
0
        for (int i = 0; i < num_bits(); ++i) {
35
0
            if (Get(i)) {
36
0
                ss << "1";
37
0
            } else {
38
0
                ss << "0";
39
0
            }
40
0
        }
41
0
    } else {
42
0
        for (auto v : buffer_) {
43
0
            ss << v << ".";
44
0
        }
45
0
    }
46
0
    ss << std::endl;
47
0
    return ss.str();
48
0
}
49
50
2
void BitmapChangeBits(uint8_t* bitmap, size_t offset, size_t num_bits, bool value) {
51
2
    DCHECK_GT(num_bits, 0);
52
53
2
    size_t start_byte = (offset >> 3);
54
2
    size_t end_byte = (offset + num_bits - 1) >> 3;
55
2
    int single_byte = (start_byte == end_byte);
56
57
    // Change the last bits of the first byte
58
2
    size_t left = offset & 0x7;
59
2
    size_t right = (single_byte) ? (left + num_bits) : 8;
60
2
    uint8_t mask = ((0xff << left) & (0xff >> (8 - right)));
61
2
    if (value) {
62
1
        bitmap[start_byte++] |= mask;
63
1
    } else {
64
1
        bitmap[start_byte++] &= ~mask;
65
1
    }
66
67
    // Nothing left... I'm done
68
2
    if (single_byte) {
69
1
        return;
70
1
    }
71
72
    // change the middle bits
73
1
    if (end_byte > start_byte) {
74
1
        const uint8_t pattern8[2] = {0x00, 0xff};
75
1
        memset(bitmap + start_byte, pattern8[value], end_byte - start_byte);
76
1
    }
77
78
    // change the first bits of the last byte
79
1
    right = offset + num_bits - (end_byte << 3);
80
1
    mask = (0xff >> (8 - right));
81
1
    if (value) {
82
1
        bitmap[end_byte] |= mask;
83
1
    } else {
84
0
        bitmap[end_byte] &= ~mask;
85
0
    }
86
1
}
87
88
bool BitmapFindFirst(const uint8_t* bitmap, size_t offset, size_t bitmap_size, bool value,
89
451k
                     size_t* idx) {
90
451k
    const uint64_t pattern64[2] = {0xffffffffffffffff, 0x0000000000000000};
91
451k
    const uint8_t pattern8[2] = {0xff, 0x00};
92
451k
    size_t bit;
93
94
451k
    DCHECK_LE(offset, bitmap_size);
95
96
    // Jump to the byte at specified offset
97
451k
    const uint8_t* p = bitmap + (offset >> 3);
98
451k
    size_t num_bits = bitmap_size - offset;
99
100
    // Find a 'value' bit at the end of the first byte
101
451k
    if ((bit = offset & 0x7)) {
102
21
        for (; bit < 8 && num_bits > 0; ++bit) {
103
17
            if (BitmapTest(p, bit) == value) {
104
1
                *idx = ((p - bitmap) << 3) + bit;
105
1
                return true;
106
1
            }
107
108
16
            num_bits--;
109
16
        }
110
111
4
        p++;
112
4
    }
113
114
    // check 64bit at the time for a 'value' bit
115
451k
    const uint64_t* u64 = (const uint64_t*)p;
116
451k
    while (num_bits >= 64 && *u64 == pattern64[value]) {
117
281
        num_bits -= 64;
118
281
        u64++;
119
281
    }
120
121
    // check 8bit at the time for a 'value' bit
122
451k
    p = (const uint8_t*)u64;
123
451k
    while (num_bits >= 8 && *p == pattern8[value]) {
124
32
        num_bits -= 8;
125
32
        p++;
126
32
    }
127
128
    // Find a 'value' bit at the beginning of the last byte
129
902k
    for (bit = 0; num_bits > 0; ++bit) {
130
451k
        if (BitmapTest(p, bit) == value) {
131
7
            *idx = ((p - bitmap) << 3) + bit;
132
7
            return true;
133
7
        }
134
451k
        num_bits--;
135
451k
    }
136
137
451k
    return false;
138
451k
}
139
140
4
std::string BitmapToString(const uint8_t* bitmap, size_t num_bits) {
141
4
    std::string s;
142
4
    size_t index = 0;
143
8
    while (index < num_bits) {
144
4
        StringAppendF(&s, "%4zu: ", index);
145
12
        for (int i = 0; i < 8 && index < num_bits; ++i) {
146
48
            for (int j = 0; j < 8 && index < num_bits; ++j) {
147
40
                StringAppendF(&s, "%d", BitmapTest(bitmap, index));
148
40
                index++;
149
40
            }
150
8
            StringAppendF(&s, " ");
151
8
        }
152
4
        StringAppendF(&s, "\n");
153
4
    }
154
4
    return s;
155
4
}
156
157
} // namespace doris