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

realm / realm-core / nicola.cabiddu_1772

31 May 2024 12:58PM UTC coverage: 90.685%. First build
nicola.cabiddu_1772

Pull #7668

Evergreen

jedelbo
Update tests
Pull Request #7668: RCORE-2094 Compressing Integer Arrays

102170 of 180138 branches covered (56.72%)

1749 of 1914 new or added lines in 36 files covered. (91.38%)

217024 of 239317 relevant lines covered (90.68%)

6624441.54 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
{
144,357,684✔
27
    REALM_ASSERT_DEBUG(width > 0 || m_size == 0);
144,357,684!
28
    m_ubound = uint64_t(-1) >> (64 - width);
144,357,684✔
29
    m_width = width;
144,357,684✔
30
}
144,357,684✔
31

32
inline uint8_t ArrayUnsigned::bit_width(uint64_t value)
33
{
81,720✔
34
    if (value < 0x100) {
81,720✔
35
        return 8;
51,345✔
36
    }
51,345✔
37
    if (value < 0x10000) {
30,375✔
38
        return 16;
13,362✔
39
    }
13,362✔
40
    if (value < 0x100000000) {
17,013✔
41
        return 32;
14,721✔
42
    }
14,721✔
43
    return 64;
2,292✔
44
}
17,013✔
45

46
inline void ArrayUnsigned::_set(size_t ndx, uint8_t width, uint64_t value)
47
{
15,524,151✔
48
    if (width == 8) {
15,524,151✔
49
        reinterpret_cast<uint8_t*>(m_data)[ndx] = uint8_t(value);
5,654,172✔
50
    }
5,654,172✔
51
    else if (width == 16) {
9,869,979✔
52
        reinterpret_cast<uint16_t*>(m_data)[ndx] = uint16_t(value);
4,090,044✔
53
    }
4,090,044✔
54
    else if (width == 32) {
5,779,935✔
55
        reinterpret_cast<uint32_t*>(m_data)[ndx] = uint32_t(value);
5,760,777✔
56
    }
5,760,777✔
57
    else {
19,158✔
58
        reinterpret_cast<uint64_t*>(m_data)[ndx] = uint64_t(value);
19,158✔
59
    }
19,158✔
60
}
15,524,151✔
61

62
inline uint64_t ArrayUnsigned::_get(size_t ndx, uint8_t width) const
63
{
493,329,216✔
64
    if (width == 8) {
493,329,216✔
65
        return reinterpret_cast<uint8_t*>(m_data)[ndx];
19,936,482✔
66
    }
19,936,482✔
67
    if (width == 16) {
473,392,734✔
68
        return reinterpret_cast<uint16_t*>(m_data)[ndx];
62,347,101✔
69
    }
62,347,101✔
70
    if (width == 32) {
411,045,633✔
71
        return reinterpret_cast<uint32_t*>(m_data)[ndx];
389,192,922✔
72
    }
389,192,922✔
73
    return get_direct(m_data, width, ndx);
21,852,711✔
74
    REALM_UNREACHABLE();
75
}
×
76

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

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

93
size_t ArrayUnsigned::lower_bound(uint64_t value) const noexcept
94
{
61,851,282✔
95
    auto width = get_width_from_header(get_header());
61,851,282✔
96

97
    if (width == 8) {
61,851,282✔
98
        uint8_t* arr = reinterpret_cast<uint8_t*>(m_data);
14,285,286✔
99
        uint8_t* pos = std::lower_bound(arr, arr + m_size, value);
14,285,286✔
100
        return pos - arr;
14,285,286✔
101
    }
14,285,286✔
102
    else if (width == 16) {
47,565,996✔
103
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
38,728,314✔
104
        uint16_t* pos = std::lower_bound(arr, arr + m_size, value);
38,728,314✔
105
        return pos - arr;
38,728,314✔
106
    }
38,728,314✔
107
    else if (width == 32) {
8,837,682✔
108
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
8,811,270✔
109
        uint32_t* pos = std::lower_bound(arr, arr + m_size, value);
8,811,270✔
110
        return pos - arr;
8,811,270✔
111
    }
8,811,270✔
112
    else if (width < 8) {
26,412✔
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,412✔
129
    uint64_t* pos = std::lower_bound(arr, arr + m_size, value);
26,412✔
130
    return pos - arr;
26,412✔
131
}
61,851,282✔
132

133
size_t ArrayUnsigned::upper_bound(uint64_t value) const noexcept
134
{
342,116,415✔
135
    auto width = get_width_from_header(get_header());
342,116,415✔
136

137
    if (width == 8) {
342,116,415✔
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) {
342,114,867✔
143
        uint16_t* arr = reinterpret_cast<uint16_t*>(m_data);
21,816,744✔
144
        uint16_t* pos = std::upper_bound(arr, arr + m_size, value);
21,816,744✔
145
        return pos - arr;
21,816,744✔
146
    }
21,816,744✔
147
    else if (width == 32) {
320,298,123✔
148
        uint32_t* arr = reinterpret_cast<uint32_t*>(m_data);
292,182,285✔
149
        uint32_t* pos = std::upper_bound(arr, arr + m_size, value);
292,182,285✔
150
        return pos - arr;
292,182,285✔
151
    }
292,182,285✔
152
    else if (width < 8) {
28,115,838✔
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);
28,115,838✔
169
    uint64_t* pos = std::upper_bound(arr, arr + m_size, value);
28,115,838✔
170
    return pos - arr;
28,115,838✔
171
}
342,116,415✔
172

173
void ArrayUnsigned::insert(size_t ndx, uint64_t value)
174
{
2,904,804✔
175
    REALM_ASSERT_DEBUG(m_width >= 8);
2,904,804✔
176

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

182
    REALM_ASSERT_DEBUG(!do_expand || new_width > m_width);
2,904,804✔
183
    REALM_ASSERT_DEBUG(ndx <= m_size);
2,904,804✔
184

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

189
    // Move values above insertion (may expand)
190
    if (do_expand) {
2,904,804✔
191
        size_t i = old_size;
18,012✔
192
        while (i > ndx) {
18,024✔
193
            --i;
12✔
194
            auto tmp = _get(i, old_width);
12✔
195
            _set(i + 1, new_width, tmp);
12✔
196
        }
12✔
197
    }
18,012✔
198
    else if (ndx != m_size) {
2,886,804✔
199
        size_t w = (new_width >> 3);
2,886,459✔
200

201
        char* src_begin = m_data + ndx * w;
2,886,459✔
202
        char* src_end = m_data + old_size * w;
2,886,459✔
203
        char* dst = src_end + w;
2,886,459✔
204

205
        std::copy_backward(src_begin, src_end, dst);
2,886,459✔
206
    }
2,886,459✔
207

208
    // Insert the new value
209
    _set(ndx, new_width, value);
2,904,804✔
210

211
    // Expand values before insertion
212
    if (do_expand) {
2,904,804✔
213
        size_t i = ndx;
18,012✔
214
        while (i != 0) {
919,023✔
215
            --i;
901,011✔
216
            _set(i, new_width, _get(i, old_width));
901,011✔
217
        }
901,011✔
218
    }
18,012✔
219
}
2,904,804✔
220

221
void ArrayUnsigned::erase(size_t ndx)
222
{
4,995,108✔
223
    REALM_ASSERT_DEBUG(m_width >= 8);
4,995,108✔
224

225
    copy_on_write(); // Throws
4,995,108✔
226

227
    size_t w = m_width >> 3;
4,995,108✔
228

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

233
    std::copy_n(src, num_bytes, dst);
4,995,108✔
234

235
    // Update size (also in header)
236
    --m_size;
4,995,108✔
237
    set_header_size(m_size);
4,995,108✔
238
}
4,995,108✔
239

240
uint64_t ArrayUnsigned::get(size_t index) const
241
{
488,397,909✔
242
    return _get(index, m_width);
488,397,909✔
243
}
488,397,909✔
244

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

250
    if (value > m_ubound) {
8,640,033✔
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,640,033✔
265
}
8,640,033✔
266

267
void ArrayUnsigned::truncate(size_t ndx)
268
{
11,736✔
269
    m_size = ndx;
11,736✔
270
    copy_on_write();
11,736✔
271
    set_header_size(m_size);
11,736✔
272
    if (ndx == 0) {
11,736✔
273
        set_width(8);
6,888✔
274
        set_width_in_header(8, get_header());
6,888✔
275
    }
6,888✔
276
}
11,736✔
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