• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / jorgen.edelbo_322

20 Jun 2024 09:43AM UTC coverage: 84.879% (-6.1%) from 90.966%
jorgen.edelbo_322

Pull #7826

Evergreen

jedelbo
remove set_direct methods from integer compressors
Pull Request #7826: Merge Next major

66056 of 81292 branches covered (81.26%)

3131 of 3738 new or added lines in 54 files covered. (83.76%)

566 existing lines in 29 files now uncovered.

84791 of 99896 relevant lines covered (84.88%)

15351931.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.35
/src/realm/array_unsigned.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * 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, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <realm/array_unsigned.hpp>
20
#include <realm/array_direct.hpp>
21
#include <algorithm>
22

23
namespace realm {
24

25
void ArrayUnsigned::set_width(uint8_t width)
26
{
144,156,786✔
27
    REALM_ASSERT_DEBUG(width > 0 || m_size == 0);
144,156,786!
28
    m_ubound = uint64_t(-1) >> (64 - width);
144,156,786✔
29
    m_width = width;
144,156,786✔
30
}
144,156,786✔
31

32
inline uint8_t ArrayUnsigned::bit_width(uint64_t value)
33
{
81,735✔
34
    if (value < 0x100) {
81,735✔
35
        return 8;
51,405✔
36
    }
51,405✔
37
    if (value < 0x10000) {
30,330✔
38
        return 16;
13,221✔
39
    }
13,221✔
40
    if (value < 0x100000000) {
17,109✔
41
        return 32;
14,757✔
42
    }
14,757✔
43
    return 64;
2,352✔
44
}
17,109✔
45

46
inline void ArrayUnsigned::_set(size_t ndx, uint8_t width, uint64_t value)
47
{
15,516,690✔
48
    if (width == 8) {
15,516,690✔
49
        reinterpret_cast<uint8_t*>(m_data)[ndx] = uint8_t(value);
5,654,568✔
50
    }
5,654,568✔
51
    else if (width == 16) {
9,862,122✔
52
        reinterpret_cast<uint16_t*>(m_data)[ndx] = uint16_t(value);
4,084,755✔
53
    }
4,084,755✔
54
    else if (width == 32) {
5,777,367✔
55
        reinterpret_cast<uint32_t*>(m_data)[ndx] = uint32_t(value);
5,756,130✔
56
    }
5,756,130✔
57
    else {
21,237✔
58
        reinterpret_cast<uint64_t*>(m_data)[ndx] = uint64_t(value);
21,237✔
59
    }
21,237✔
60
}
15,516,690✔
61

62
inline uint64_t ArrayUnsigned::_get(size_t ndx, uint8_t width) const
63
{
489,866,550✔
64
    if (width == 8) {
489,866,550✔
65
        return reinterpret_cast<uint8_t*>(m_data)[ndx];
19,905,741✔
66
    }
19,905,741✔
67
    if (width == 16) {
469,960,809✔
68
        return reinterpret_cast<uint16_t*>(m_data)[ndx];
62,198,748✔
69
    }
62,198,748✔
70
    if (width == 32) {
407,762,061✔
71
        return reinterpret_cast<uint32_t*>(m_data)[ndx];
385,441,056✔
72
    }
385,441,056✔
73
    return get_direct(m_data, width, ndx);
22,321,005✔
74
    REALM_UNREACHABLE();
UNCOV
75
}
×
76

77
void ArrayUnsigned::create(size_t initial_size, uint64_t ubound_value)
78
{
51,729✔
79
    MemRef mem = create_node(initial_size, get_alloc(), false, Node::type_Normal, wtype_Bits,
51,729✔
80
                             bit_width(ubound_value)); // Throws
51,729✔
81
    init_from_mem(mem);
51,729✔
82
}
51,729✔
83

84
void ArrayUnsigned::update_from_parent() noexcept
85
{
106,734✔
86
    REALM_ASSERT_DEBUG(is_attached());
106,734✔
87
    ArrayParent* parent = get_parent();
106,734✔
88
    REALM_ASSERT_DEBUG(parent);
106,734✔
89
    ref_type new_ref = get_ref_from_parent();
106,734✔
90
    init_from_ref(new_ref);
106,734✔
91
}
106,734✔
92

93
size_t ArrayUnsigned::lower_bound(uint64_t value) const noexcept
94
{
61,847,130✔
95
    if (m_width == 8) {
61,847,130✔
96
        uint8_t* arr = reinterpret_cast<uint8_t*>(m_data);
14,257,734✔
97
        uint8_t* pos = std::lower_bound(arr, arr + m_size, value);
14,257,734✔
98
        return pos - arr;
14,257,734✔
99
    }
14,257,734✔
100
    else if (m_width == 16) {
47,589,396✔
101
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
38,785,452✔
102
        uint16_t* pos = std::lower_bound(arr, arr + m_size, value);
38,785,452✔
103
        return pos - arr;
38,785,452✔
104
    }
38,785,452✔
105
    else if (m_width == 32) {
8,822,862✔
106
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
8,795,727✔
107
        uint32_t* pos = std::lower_bound(arr, arr + m_size, value);
8,795,727✔
108
        return pos - arr;
8,795,727✔
109
    }
8,795,727✔
110
    else if (m_width < 8) {
2,147,510,782✔
111
        switch (m_width) {
×
112
            case 0:
×
113
                return realm::lower_bound<0>(m_data, m_size, value);
×
114
            case 1:
×
115
                return realm::lower_bound<1>(m_data, m_size, value);
×
116
            case 2:
×
117
                return realm::lower_bound<2>(m_data, m_size, value);
×
118
            case 4:
×
119
                return realm::lower_bound<4>(m_data, m_size, value);
×
120
            default:
×
121
                REALM_UNREACHABLE();
122
                break;
×
123
        }
×
124
        return npos;
×
125
    }
×
126
    uint64_t* arr = reinterpret_cast<uint64_t*>(m_data);
2,147,510,782✔
127
    uint64_t* pos = std::lower_bound(arr, arr + m_size, value);
2,147,510,782✔
128
    return pos - arr;
2,147,510,782✔
129
}
61,847,130✔
130

131
size_t ArrayUnsigned::upper_bound(uint64_t value) const noexcept
132
{
339,846,933✔
133
    if (m_width == 8) {
339,846,933✔
134
        uint8_t* arr = reinterpret_cast<uint8_t*>(m_data);
1,548✔
135
        uint8_t* pos = std::upper_bound(arr, arr + m_size, value);
1,548✔
136
        return pos - arr;
1,548✔
137
    }
1,548✔
138
    else if (m_width == 16) {
339,845,385✔
139
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
21,619,761✔
140
        uint16_t* pos = std::upper_bound(arr, arr + m_size, value);
21,619,761✔
141
        return pos - arr;
21,619,761✔
142
    }
21,619,761✔
143
    else if (m_width == 32) {
318,225,624✔
144
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
293,929,518✔
145
        uint32_t* pos = std::upper_bound(arr, arr + m_size, value);
293,929,518✔
146
        return pos - arr;
293,929,518✔
147
    }
293,929,518✔
148
    else if (m_width < 8) {
24,296,106✔
149
        switch (m_width) {
×
150
            case 0:
×
151
                return realm::upper_bound<0>(m_data, m_size, value);
×
152
            case 1:
×
153
                return realm::upper_bound<1>(m_data, m_size, value);
×
154
            case 2:
×
155
                return realm::upper_bound<2>(m_data, m_size, value);
×
156
            case 4:
×
157
                return realm::upper_bound<4>(m_data, m_size, value);
×
158
            default:
×
159
                REALM_UNREACHABLE();
160
                break;
×
161
        }
×
162
        return npos;
×
163
    }
×
164
    uint64_t* arr = reinterpret_cast<uint64_t*>(m_data);
24,296,106✔
165
    uint64_t* pos = std::upper_bound(arr, arr + m_size, value);
24,296,106✔
166
    return pos - arr;
24,296,106✔
167
}
339,846,933✔
168

169
void ArrayUnsigned::insert(size_t ndx, uint64_t value)
170
{
2,901,954✔
171
    REALM_ASSERT_DEBUG(m_width >= 8);
2,901,954✔
172

173
    bool do_expand = value > (uint64_t)m_ubound;
2,901,954✔
174
    const uint8_t old_width = m_width;
2,901,954✔
175
    const uint8_t new_width = do_expand ? bit_width(value) : m_width;
2,901,954✔
176
    const auto old_size = m_size;
2,901,954✔
177

178
    REALM_ASSERT_DEBUG(!do_expand || new_width > m_width);
2,901,954✔
179
    REALM_ASSERT_DEBUG(ndx <= m_size);
2,901,954✔
180

181
    // Check if we need to copy before modifying
182
    copy_on_write();              // Throws
2,901,954✔
183
    alloc(m_size + 1, new_width); // Throws
2,901,954✔
184

185
    // Move values above insertion (may expand)
186
    if (do_expand) {
2,901,954✔
187
        size_t i = old_size;
17,970✔
188
        while (i > ndx) {
17,982✔
189
            --i;
12✔
190
            auto tmp = _get(i, old_width);
12✔
191
            _set(i + 1, new_width, tmp);
12✔
192
        }
12✔
193
    }
17,970✔
194
    else if (ndx != m_size) {
2,884,005✔
195
        size_t w = (new_width >> 3);
2,883,942✔
196

197
        char* src_begin = m_data + ndx * w;
2,883,942✔
198
        char* src_end = m_data + old_size * w;
2,883,942✔
199
        char* dst = src_end + w;
2,883,942✔
200

201
        std::copy_backward(src_begin, src_end, dst);
2,883,942✔
202
    }
2,883,942✔
203

204
    // Insert the new value
205
    _set(ndx, new_width, value);
2,901,954✔
206

207
    // Expand values before insertion
208
    if (do_expand) {
2,901,954✔
209
        size_t i = ndx;
17,970✔
210
        while (i != 0) {
913,134✔
211
            --i;
895,164✔
212
            _set(i, new_width, _get(i, old_width));
895,164✔
213
        }
895,164✔
214
    }
17,970✔
215
}
2,901,954✔
216

217
void ArrayUnsigned::erase(size_t ndx)
218
{
4,995,090✔
219
    REALM_ASSERT_DEBUG(m_width >= 8);
4,995,090✔
220

221
    copy_on_write(); // Throws
4,995,090✔
222

223
    size_t w = m_width >> 3;
4,995,090✔
224

225
    char* dst = m_data + ndx * w;
4,995,090✔
226
    const char* src = dst + w;
4,995,090✔
227
    size_t num_bytes = (m_size - ndx - 1) * w;
4,995,090✔
228

229
    std::copy_n(src, num_bytes, dst);
4,995,090✔
230

231
    // Update size (also in header)
232
    --m_size;
4,995,090✔
233
    set_header_size(m_size);
4,995,090✔
234
}
4,995,090✔
235

236
uint64_t ArrayUnsigned::get(size_t index) const
237
{
485,930,634✔
238
    return _get(index, m_width);
485,930,634✔
239
}
485,930,634✔
240

241
void ArrayUnsigned::set(size_t ndx, uint64_t value)
242
{
8,641,146✔
243
    REALM_ASSERT_DEBUG(m_width >= 8);
8,641,146✔
244
    copy_on_write(); // Throws
8,641,146✔
245

246
    if (value > m_ubound) {
8,641,146✔
247
        const uint8_t old_width = m_width;
12,036✔
248
        const uint8_t new_width = bit_width(value);
12,036✔
249

250
        alloc(m_size, new_width); // Throws
12,036✔
251

252
        size_t i = m_size;
12,036✔
253
        while (i) {
3,090,930✔
254
            i--;
3,078,894✔
255
            auto v = _get(i, old_width);
3,078,894✔
256
            _set(i, new_width, v);
3,078,894✔
257
        }
3,078,894✔
258
    }
12,036✔
259

260
    _set(ndx, m_width, value);
8,641,146✔
261
}
8,641,146✔
262

263
void ArrayUnsigned::truncate(size_t ndx)
264
{
11,490✔
265
    m_size = ndx;
11,490✔
266
    copy_on_write();
11,490✔
267
    set_header_size(m_size);
11,490✔
268
    if (ndx == 0) {
11,490✔
269
        set_width(8);
6,738✔
270
        set_width_in_header(8, get_header());
6,738✔
271
    }
6,738✔
272
}
11,490✔
273

274
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc