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

realm / realm-core / jorgen.edelbo_338

03 Jul 2024 03:00PM UTC coverage: 90.856% (-0.008%) from 90.864%
jorgen.edelbo_338

Pull #7803

Evergreen

nicola-cab
Merge branch 'next-major' into feature/string-compression
Pull Request #7803: Feature/string compression

103028 of 180606 branches covered (57.05%)

1144 of 1267 new or added lines in 33 files covered. (90.29%)

155 existing lines in 24 files now uncovered.

218583 of 240583 relevant lines covered (90.86%)

7959624.7 hits per line

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

85.5
/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
{
227,056,404✔
27
    REALM_ASSERT_DEBUG(width > 0 || m_size == 0);
227,056,404!
28
    m_ubound = uint64_t(-1) >> (64 - width);
227,056,404✔
29
    m_width = width;
227,056,404✔
30
}
227,056,404✔
31

32
inline uint8_t ArrayUnsigned::bit_width(uint64_t value)
33
{
435,984✔
34
    if (value < 0x100) {
435,984✔
35
        return 8;
52,728✔
36
    }
52,728✔
37
    if (value < 0x10000) {
383,256✔
38
        return 16;
288,708✔
39
    }
288,708✔
40
    if (value < 0x100000000) {
94,548✔
41
        return 32;
92,178✔
42
    }
92,178✔
43
    return 64;
2,370✔
44
}
94,548✔
45

46
inline void ArrayUnsigned::_set(size_t ndx, uint8_t width, uint64_t value)
47
{
97,341,141✔
48
    if (width == 8) {
97,341,141✔
49
        reinterpret_cast<uint8_t*>(m_data)[ndx] = uint8_t(value);
5,936,019✔
50
    }
5,936,019✔
51
    else if (width == 16) {
91,405,122✔
52
        reinterpret_cast<uint16_t*>(m_data)[ndx] = uint16_t(value);
74,476,668✔
53
    }
74,476,668✔
54
    else if (width == 32) {
16,941,111✔
55
        reinterpret_cast<uint32_t*>(m_data)[ndx] = uint32_t(value);
16,941,111✔
56
    }
16,941,111✔
57
    else {
4,294,967,294✔
58
        reinterpret_cast<uint64_t*>(m_data)[ndx] = uint64_t(value);
4,294,967,294✔
59
    }
4,294,967,294✔
60
}
97,341,141✔
61

62
inline uint64_t ArrayUnsigned::_get(size_t ndx, uint8_t width) const
63
{
558,446,046✔
64
    if (width == 8) {
558,446,046✔
65
        return reinterpret_cast<uint8_t*>(m_data)[ndx];
22,465,902✔
66
    }
22,465,902✔
67
    if (width == 16) {
535,980,144✔
68
        return reinterpret_cast<uint16_t*>(m_data)[ndx];
74,208,342✔
69
    }
74,208,342✔
70
    if (width == 32) {
461,771,802✔
71
        return reinterpret_cast<uint32_t*>(m_data)[ndx];
440,902,740✔
72
    }
440,902,740✔
73
    return get_direct(m_data, width, ndx);
20,869,062✔
74
    REALM_UNREACHABLE();
75
}
×
76

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

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

93
size_t ArrayUnsigned::lower_bound(uint64_t value) const noexcept
94
{
63,263,814✔
95
    auto width = get_width_from_header(get_header());
63,263,814✔
96

97
    if (width == 8) {
63,263,814✔
98
        uint8_t* arr = reinterpret_cast<uint8_t*>(m_data);
15,199,344✔
99
        uint8_t* pos = std::lower_bound(arr, arr + m_size, value);
15,199,344✔
100
        return pos - arr;
15,199,344✔
101
    }
15,199,344✔
102
    else if (width == 16) {
48,064,470✔
103
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
39,141,804✔
104
        uint16_t* pos = std::lower_bound(arr, arr + m_size, value);
39,141,804✔
105
        return pos - arr;
39,141,804✔
106
    }
39,141,804✔
107
    else if (width == 32) {
8,922,666✔
108
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
8,896,491✔
109
        uint32_t* pos = std::lower_bound(arr, arr + m_size, value);
8,896,491✔
110
        return pos - arr;
8,896,491✔
111
    }
8,896,491✔
112
    else if (width < 8) {
26,175✔
NEW
113
        switch (width) {
×
114
            case 0:
×
115
                return realm::lower_bound<0>(m_data, m_size, value);
×
116
            case 1:
×
117
                return realm::lower_bound<1>(m_data, m_size, value);
×
118
            case 2:
×
119
                return realm::lower_bound<2>(m_data, m_size, value);
×
120
            case 4:
×
121
                return realm::lower_bound<4>(m_data, m_size, value);
×
122
            default:
×
123
                REALM_UNREACHABLE();
124
                break;
×
125
        }
×
126
        return npos;
×
127
    }
×
128
    uint64_t* arr = reinterpret_cast<uint64_t*>(m_data);
26,175✔
129
    uint64_t* pos = std::lower_bound(arr, arr + m_size, value);
26,175✔
130
    return pos - arr;
26,175✔
131
}
63,263,814✔
132

133
size_t ArrayUnsigned::upper_bound(uint64_t value) const noexcept
134
{
350,503,986✔
135
    auto width = get_width_from_header(get_header());
350,503,986✔
136

137
    if (width == 8) {
350,503,986✔
138
        uint8_t* arr = reinterpret_cast<uint8_t*>(m_data);
1,548✔
139
        uint8_t* pos = std::upper_bound(arr, arr + m_size, value);
1,548✔
140
        return pos - arr;
1,548✔
141
    }
1,548✔
142
    else if (width == 16) {
350,502,438✔
143
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
21,831,804✔
144
        uint16_t* pos = std::upper_bound(arr, arr + m_size, value);
21,831,804✔
145
        return pos - arr;
21,831,804✔
146
    }
21,831,804✔
147
    else if (width == 32) {
328,670,634✔
148
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
303,315,435✔
149
        uint32_t* pos = std::upper_bound(arr, arr + m_size, value);
303,315,435✔
150
        return pos - arr;
303,315,435✔
151
    }
303,315,435✔
152
    else if (width < 8) {
25,355,199✔
NEW
153
        switch (width) {
×
154
            case 0:
×
155
                return realm::upper_bound<0>(m_data, m_size, value);
×
156
            case 1:
×
157
                return realm::upper_bound<1>(m_data, m_size, value);
×
158
            case 2:
×
159
                return realm::upper_bound<2>(m_data, m_size, value);
×
160
            case 4:
×
161
                return realm::upper_bound<4>(m_data, m_size, value);
×
162
            default:
×
163
                REALM_UNREACHABLE();
164
                break;
×
165
        }
×
166
        return npos;
×
167
    }
×
168
    uint64_t* arr = reinterpret_cast<uint64_t*>(m_data);
25,355,199✔
169
    uint64_t* pos = std::upper_bound(arr, arr + m_size, value);
25,355,199✔
170
    return pos - arr;
25,355,199✔
171
}
350,503,986✔
172

173
void ArrayUnsigned::insert(size_t ndx, uint64_t value)
174
{
84,396,813✔
175
    REALM_ASSERT_DEBUG(m_width >= 8);
84,396,813✔
176

177
    bool do_expand = value > (uint64_t)m_ubound;
84,396,813✔
178
    const uint8_t old_width = m_width;
84,396,813✔
179
    const uint8_t new_width = do_expand ? bit_width(value) : m_width;
84,396,813✔
180
    const auto old_size = m_size;
84,396,813✔
181

182
    REALM_ASSERT_DEBUG(!do_expand || new_width > m_width);
84,396,813✔
183
    REALM_ASSERT_DEBUG(ndx <= m_size);
84,396,813✔
184

185
    // Check if we need to copy before modifying
186
    copy_on_write();              // Throws
84,396,813✔
187
    alloc(m_size + 1, new_width); // Throws
84,396,813✔
188

189
    // Move values above insertion (may expand)
190
    if (do_expand) {
84,396,813✔
191
        size_t i = old_size;
96,090✔
192
        while (i > ndx) {
96,102✔
193
            --i;
12✔
194
            auto tmp = _get(i, old_width);
12✔
195
            _set(i + 1, new_width, tmp);
12✔
196
        }
12✔
197
    }
96,090✔
198
    else if (ndx != m_size) {
84,300,723✔
199
        size_t w = (new_width >> 3);
84,276,855✔
200

201
        char* src_begin = m_data + ndx * w;
84,276,855✔
202
        char* src_end = m_data + old_size * w;
84,276,855✔
203
        char* dst = src_end + w;
84,276,855✔
204

205
        std::copy_backward(src_begin, src_end, dst);
84,276,855✔
206
    }
84,276,855✔
207

208
    // Insert the new value
209
    _set(ndx, new_width, value);
84,396,813✔
210

211
    // Expand values before insertion
212
    if (do_expand) {
84,396,813✔
213
        size_t i = ndx;
96,090✔
214
        while (i != 0) {
1,049,439✔
215
            --i;
953,349✔
216
            _set(i, new_width, _get(i, old_width));
953,349✔
217
        }
953,349✔
218
    }
96,090✔
219
}
84,396,813✔
220

221
void ArrayUnsigned::erase(size_t ndx)
222
{
5,277,555✔
223
    REALM_ASSERT_DEBUG(m_width >= 8);
5,277,555✔
224

225
    copy_on_write(); // Throws
5,277,555✔
226

227
    size_t w = m_width >> 3;
5,277,555✔
228

229
    char* dst = m_data + ndx * w;
5,277,555✔
230
    const char* src = dst + w;
5,277,555✔
231
    size_t num_bytes = (m_size - ndx - 1) * w;
5,277,555✔
232

233
    std::copy_n(src, num_bytes, dst);
5,277,555✔
234

235
    // Update size (also in header)
236
    --m_size;
5,277,555✔
237
    set_header_size(m_size);
5,277,555✔
238
}
5,277,555✔
239

240
uint64_t ArrayUnsigned::get(size_t index) const
241
{
554,399,130✔
242
    return _get(index, m_width);
554,399,130✔
243
}
554,399,130✔
244

245
void ArrayUnsigned::set(size_t ndx, uint64_t value)
246
{
8,923,383✔
247
    REALM_ASSERT_DEBUG(m_width >= 8);
8,923,383✔
248
    copy_on_write(); // Throws
8,923,383✔
249

250
    if (value > m_ubound) {
8,923,383✔
251
        const uint8_t old_width = m_width;
12,036✔
252
        const uint8_t new_width = bit_width(value);
12,036✔
253

254
        alloc(m_size, new_width); // Throws
12,036✔
255

256
        size_t i = m_size;
12,036✔
257
        while (i) {
3,090,930✔
258
            i--;
3,078,894✔
259
            auto v = _get(i, old_width);
3,078,894✔
260
            _set(i, new_width, v);
3,078,894✔
261
        }
3,078,894✔
262
    }
12,036✔
263

264
    _set(ndx, m_width, value);
8,923,383✔
265
}
8,923,383✔
266

267
void ArrayUnsigned::truncate(size_t ndx)
268
{
12,777✔
269
    m_size = ndx;
12,777✔
270
    copy_on_write();
12,777✔
271
    set_header_size(m_size);
12,777✔
272
    if (ndx == 0) {
12,777✔
273
        set_width(8);
7,914✔
274
        set_width_in_header(8, get_header());
7,914✔
275
    }
7,914✔
276
}
12,777✔
277

278
} // 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