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

realm / realm-core / jorgen.edelbo_391

13 Aug 2024 07:57AM UTC coverage: 91.091% (-0.02%) from 91.107%
jorgen.edelbo_391

Pull #7826

Evergreen

web-flow
Merge pull request #7979 from realm/test-upgrade-files

Create test file in file-format 24
Pull Request #7826: Merge Next major

103486 of 182216 branches covered (56.79%)

3157 of 3519 new or added lines in 54 files covered. (89.71%)

191 existing lines in 22 files now uncovered.

219971 of 241486 relevant lines covered (91.09%)

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

32
inline uint8_t ArrayUnsigned::bit_width(uint64_t value)
33
{
85,509✔
34
    if (value < 0x100) {
85,509✔
35
        return 8;
54,144✔
36
    }
54,144✔
37
    if (value < 0x10000) {
31,365✔
38
        return 16;
14,052✔
39
    }
14,052✔
40
    if (value < 0x100000000) {
17,313✔
41
        return 32;
14,751✔
42
    }
14,751✔
43
    return 64;
2,562✔
44
}
17,313✔
45

46
inline void ArrayUnsigned::_set(size_t ndx, uint8_t width, uint64_t value)
47
{
16,017,717✔
48
    if (width == 8) {
16,017,717✔
49
        reinterpret_cast<uint8_t*>(m_data)[ndx] = uint8_t(value);
5,960,880✔
50
    }
5,960,880✔
51
    else if (width == 16) {
10,056,837✔
52
        reinterpret_cast<uint16_t*>(m_data)[ndx] = uint16_t(value);
4,248,279✔
53
    }
4,248,279✔
54
    else if (width == 32) {
5,808,558✔
55
        reinterpret_cast<uint32_t*>(m_data)[ndx] = uint32_t(value);
5,760,393✔
56
    }
5,760,393✔
57
    else {
48,165✔
58
        reinterpret_cast<uint64_t*>(m_data)[ndx] = uint64_t(value);
48,165✔
59
    }
48,165✔
60
}
16,017,717✔
61

62
inline uint64_t ArrayUnsigned::_get(size_t ndx, uint8_t width) const
63
{
495,933,669✔
64
    if (width == 8) {
495,933,669✔
65
        return reinterpret_cast<uint8_t*>(m_data)[ndx];
22,525,713✔
66
    }
22,525,713✔
67
    if (width == 16) {
473,407,956✔
68
        return reinterpret_cast<uint16_t*>(m_data)[ndx];
65,124,438✔
69
    }
65,124,438✔
70
    if (width == 32) {
408,283,518✔
71
        return reinterpret_cast<uint32_t*>(m_data)[ndx];
386,906,580✔
72
    }
386,906,580✔
73
    return get_direct(m_data, width, ndx);
21,376,938✔
74
    REALM_UNREACHABLE();
UNCOV
75
}
×
76

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

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

93
size_t ArrayUnsigned::lower_bound(uint64_t value) const noexcept
94
{
63,579,513✔
95
    if (m_width == 8) {
63,579,513✔
96
        uint8_t* arr = reinterpret_cast<uint8_t*>(m_data);
15,337,674✔
97
        uint8_t* pos = std::lower_bound(arr, arr + m_size, value);
15,337,674✔
98
        return pos - arr;
15,337,674✔
99
    }
15,337,674✔
100
    else if (m_width == 16) {
48,241,839✔
101
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
39,217,203✔
102
        uint16_t* pos = std::lower_bound(arr, arr + m_size, value);
39,217,203✔
103
        return pos - arr;
39,217,203✔
104
    }
39,217,203✔
105
    else if (m_width == 32) {
9,024,636✔
106
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
8,858,217✔
107
        uint32_t* pos = std::lower_bound(arr, arr + m_size, value);
8,858,217✔
108
        return pos - arr;
8,858,217✔
109
    }
8,858,217✔
110
    else if (m_width < 8) {
166,419✔
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);
166,419✔
127
    uint64_t* pos = std::lower_bound(arr, arr + m_size, value);
166,419✔
128
    return pos - arr;
166,419✔
129
}
63,579,513✔
130

131
size_t ArrayUnsigned::upper_bound(uint64_t value) const noexcept
132
{
338,675,505✔
133
    if (m_width == 8) {
338,675,505✔
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) {
338,673,957✔
139
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
22,249,887✔
140
        uint16_t* pos = std::upper_bound(arr, arr + m_size, value);
22,249,887✔
141
        return pos - arr;
22,249,887✔
142
    }
22,249,887✔
143
    else if (m_width == 32) {
316,424,070✔
144
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
294,308,727✔
145
        uint32_t* pos = std::upper_bound(arr, arr + m_size, value);
294,308,727✔
146
        return pos - arr;
294,308,727✔
147
    }
294,308,727✔
148
    else if (m_width < 8) {
22,115,343✔
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);
22,115,343✔
165
    uint64_t* pos = std::upper_bound(arr, arr + m_size, value);
22,115,343✔
166
    return pos - arr;
22,115,343✔
167
}
338,675,505✔
168

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

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

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

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

185
    // Move values above insertion (may expand)
186
    if (do_expand) {
3,049,398✔
187
        size_t i = old_size;
18,894✔
188
        while (i > ndx) {
18,906✔
189
            --i;
12✔
190
            auto tmp = _get(i, old_width);
12✔
191
            _set(i + 1, new_width, tmp);
12✔
192
        }
12✔
193
    }
18,894✔
194
    else if (ndx != m_size) {
3,030,513✔
195
        size_t w = (new_width >> 3);
3,029,880✔
196

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

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

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

207
    // Expand values before insertion
208
    if (do_expand) {
3,049,398✔
209
        size_t i = ndx;
18,894✔
210
        while (i != 0) {
979,020✔
211
            --i;
960,126✔
212
            _set(i, new_width, _get(i, old_width));
960,126✔
213
        }
960,126✔
214
    }
18,894✔
215
}
3,049,398✔
216

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

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

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

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

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

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

236
uint64_t ArrayUnsigned::get(size_t index) const
237
{
491,523,579✔
238
    return _get(index, m_width);
491,523,579✔
239
}
491,523,579✔
240

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

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

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