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

realm / realm-core / jorgen.edelbo_389

12 Aug 2024 02:13PM UTC coverage: 91.085% (-0.02%) from 91.107%
jorgen.edelbo_389

Pull #7826

Evergreen

jedelbo
Bump file format version
Pull Request #7826: Merge Next major

103458 of 182206 branches covered (56.78%)

3138 of 3500 new or added lines in 53 files covered. (89.66%)

175 existing lines in 17 files now uncovered.

219944 of 241471 relevant lines covered (91.09%)

6840929.52 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
{
146,071,335✔
27
    REALM_ASSERT_DEBUG(width > 0 || m_size == 0);
146,071,335!
28
    m_ubound = uint64_t(-1) >> (64 - width);
146,071,335✔
29
    m_width = width;
146,071,335✔
30
}
146,071,335✔
31

32
inline uint8_t ArrayUnsigned::bit_width(uint64_t value)
33
{
85,305✔
34
    if (value < 0x100) {
85,305✔
35
        return 8;
54,030✔
36
    }
54,030✔
37
    if (value < 0x10000) {
31,275✔
38
        return 16;
14,004✔
39
    }
14,004✔
40
    if (value < 0x100000000) {
17,271✔
41
        return 32;
14,745✔
42
    }
14,745✔
43
    return 64;
2,526✔
44
}
17,271✔
45

46
inline void ArrayUnsigned::_set(size_t ndx, uint8_t width, uint64_t value)
47
{
16,001,262✔
48
    if (width == 8) {
16,001,262✔
49
        reinterpret_cast<uint8_t*>(m_data)[ndx] = uint8_t(value);
5,962,359✔
50
    }
5,962,359✔
51
    else if (width == 16) {
10,038,903✔
52
        reinterpret_cast<uint16_t*>(m_data)[ndx] = uint16_t(value);
4,230,924✔
53
    }
4,230,924✔
54
    else if (width == 32) {
5,807,979✔
55
        reinterpret_cast<uint32_t*>(m_data)[ndx] = uint32_t(value);
5,761,641✔
56
    }
5,761,641✔
57
    else {
46,338✔
58
        reinterpret_cast<uint64_t*>(m_data)[ndx] = uint64_t(value);
46,338✔
59
    }
46,338✔
60
}
16,001,262✔
61

62
inline uint64_t ArrayUnsigned::_get(size_t ndx, uint8_t width) const
63
{
499,935,918✔
64
    if (width == 8) {
499,935,918✔
65
        return reinterpret_cast<uint8_t*>(m_data)[ndx];
22,551,960✔
66
    }
22,551,960✔
67
    if (width == 16) {
477,383,958✔
68
        return reinterpret_cast<uint16_t*>(m_data)[ndx];
64,728,351✔
69
    }
64,728,351✔
70
    if (width == 32) {
412,655,607✔
71
        return reinterpret_cast<uint32_t*>(m_data)[ndx];
389,845,815✔
72
    }
389,845,815✔
73
    return get_direct(m_data, width, ndx);
22,809,792✔
74
    REALM_UNREACHABLE();
UNCOV
75
}
×
76

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

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

93
size_t ArrayUnsigned::lower_bound(uint64_t value) const noexcept
94
{
63,535,566✔
95
    if (m_width == 8) {
63,535,566✔
96
        uint8_t* arr = reinterpret_cast<uint8_t*>(m_data);
15,304,215✔
97
        uint8_t* pos = std::lower_bound(arr, arr + m_size, value);
15,304,215✔
98
        return pos - arr;
15,304,215✔
99
    }
15,304,215✔
100
    else if (m_width == 16) {
48,231,351✔
101
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
39,091,950✔
102
        uint16_t* pos = std::lower_bound(arr, arr + m_size, value);
39,091,950✔
103
        return pos - arr;
39,091,950✔
104
    }
39,091,950✔
105
    else if (m_width == 32) {
9,139,401✔
106
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
8,980,821✔
107
        uint32_t* pos = std::lower_bound(arr, arr + m_size, value);
8,980,821✔
108
        return pos - arr;
8,980,821✔
109
    }
8,980,821✔
110
    else if (m_width < 8) {
158,580✔
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);
158,580✔
127
    uint64_t* pos = std::lower_bound(arr, arr + m_size, value);
158,580✔
128
    return pos - arr;
158,580✔
129
}
63,535,566✔
130

131
size_t ArrayUnsigned::upper_bound(uint64_t value) const noexcept
132
{
341,146,404✔
133
    if (m_width == 8) {
341,146,404✔
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) {
341,144,856✔
139
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
21,966,147✔
140
        uint16_t* pos = std::upper_bound(arr, arr + m_size, value);
21,966,147✔
141
        return pos - arr;
21,966,147✔
142
    }
21,966,147✔
143
    else if (m_width == 32) {
319,178,709✔
144
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
294,749,580✔
145
        uint32_t* pos = std::upper_bound(arr, arr + m_size, value);
294,749,580✔
146
        return pos - arr;
294,749,580✔
147
    }
294,749,580✔
148
    else if (m_width < 8) {
24,429,129✔
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,429,129✔
165
    uint64_t* pos = std::upper_bound(arr, arr + m_size, value);
24,429,129✔
166
    return pos - arr;
24,429,129✔
167
}
341,146,404✔
168

169
void ArrayUnsigned::insert(size_t ndx, uint64_t value)
170
{
3,031,149✔
171
    REALM_ASSERT_DEBUG(m_width >= 8);
3,031,149✔
172

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

178
    REALM_ASSERT_DEBUG(!do_expand || new_width > m_width);
3,031,149✔
179
    REALM_ASSERT_DEBUG(ndx <= m_size);
3,031,149✔
180

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

185
    // Move values above insertion (may expand)
186
    if (do_expand) {
3,031,149✔
187
        size_t i = old_size;
18,792✔
188
        while (i > ndx) {
18,804✔
189
            --i;
12✔
190
            auto tmp = _get(i, old_width);
12✔
191
            _set(i + 1, new_width, tmp);
12✔
192
        }
12✔
193
    }
18,792✔
194
    else if (ndx != m_size) {
3,012,357✔
195
        size_t w = (new_width >> 3);
3,011,844✔
196

197
        char* src_begin = m_data + ndx * w;
3,011,844✔
198
        char* src_end = m_data + old_size * w;
3,011,844✔
199
        char* dst = src_end + w;
3,011,844✔
200

201
        std::copy_backward(src_begin, src_end, dst);
3,011,844✔
202
    }
3,011,844✔
203

204
    // Insert the new value
205
    _set(ndx, new_width, value);
3,031,149✔
206

207
    // Expand values before insertion
208
    if (do_expand) {
3,031,149✔
209
        size_t i = ndx;
18,792✔
210
        while (i != 0) {
977,406✔
211
            --i;
958,614✔
212
            _set(i, new_width, _get(i, old_width));
958,614✔
213
        }
958,614✔
214
    }
18,792✔
215
}
3,031,149✔
216

217
void ArrayUnsigned::erase(size_t ndx)
218
{
5,317,239✔
219
    REALM_ASSERT_DEBUG(m_width >= 8);
5,317,239✔
220

221
    copy_on_write(); // Throws
5,317,239✔
222

223
    size_t w = m_width >> 3;
5,317,239✔
224

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

229
    std::copy_n(src, num_bytes, dst);
5,317,239✔
230

231
    // Update size (also in header)
232
    --m_size;
5,317,239✔
233
    set_header_size(m_size);
5,317,239✔
234
}
5,317,239✔
235

236
uint64_t ArrayUnsigned::get(size_t index) const
237
{
495,430,611✔
238
    return _get(index, m_width);
495,430,611✔
239
}
495,430,611✔
240

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

246
    if (value > m_ubound) {
8,933,304✔
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,933,304✔
261
}
8,933,304✔
262

263
void ArrayUnsigned::truncate(size_t ndx)
264
{
12,513✔
265
    m_size = ndx;
12,513✔
266
    copy_on_write();
12,513✔
267
    set_header_size(m_size);
12,513✔
268
    if (ndx == 0) {
12,513✔
269
        set_width(8);
7,785✔
270
        set_width_in_header(8, get_header());
7,785✔
271
    }
7,785✔
272
}
12,513✔
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