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

afxres / binary-cxx / 42839007876

10 Nov 2026 02:53PM UTC coverage: 98.649% (-0.8%) from 99.409%
42839007876

push

github

afxres
New hash policy for named object layout

22 of 30 new or added lines in 1 file covered. (73.33%)

1022 of 1036 relevant lines covered (98.65%)

234.72 hits per line

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

89.33
/code/binary/include/binary/components/NamedObjectLayout.hpp
1
#ifndef BINARY_COMPONENTS_NAMEDOBJECTLAYOUT_HPP
2
#define BINARY_COMPONENTS_NAMEDOBJECTLAYOUT_HPP
3

4
#include <array>
5
#include <bit>
6
#include <cstddef>
7
#include <cstdint>
8
#include <cstring>
9
#include <format>
10
#include <span>
11
#include <stdexcept>
12
#include <string_view>
13
#include <tuple>
14
#include <type_traits>
15
#include <utility>
16

17
#include "binary/ConverterExtensions.hpp"
18

19
namespace binary::components {
20
namespace detail {
21
struct Slot {
22
    uint64_t Head;
23
    uint64_t Tail;
24
};
25

26
inline constexpr auto PackedItemCountLimit = size_t{11};
27
inline constexpr auto PackedItemBytesLimit = 2 * sizeof(uint64_t) - 1;
28

29
template <typename Item>
30
constexpr auto Load(std::string_view name, size_t offset) {
92 ✔
31
    if (std::is_constant_evaluated() || std::endian::native != std::endian::little) {
92 ✔
NEW
32
        uint64_t value = 0;
×
NEW
33
        for (size_t index = 0; index < sizeof(Item); index++) {
×
NEW
34
            value |= static_cast<uint64_t>(static_cast<unsigned char>(name[offset + index])) << (0x08U * index);
×
35
        }
NEW
36
        return static_cast<Item>(value);
×
37
    }
38
    Item value{};
92 ✔
39
    std::memcpy(&value, name.data() + offset, sizeof(Item));
92 ✔
40
    return value;
92 ✔
41
}
42

43
constexpr auto Pack(std::string_view name) {
45 ✔
44
    using BIN1 = uint8_t;
45
    using BIN2 = uint16_t;
46
    using BIN4 = uint32_t;
47
    using BIN8 = uint64_t;
48
    const size_t length = name.size();
45 ✔
49
    uint64_t tail = (length & 0x08U) == 0 ? 0UL : Load<BIN8>(name, length & 0x07U);
45 ✔
50
    uint64_t head = (length & 0x04U) == 0 ? 0UL : Load<BIN4>(name, length & 0x03U);
45 ✔
51
    if ((length & 0x02U) != 0) {
45 ✔
52
        head = (head << 0x10U) | Load<BIN2>(name, length & 0x01U);
21 ✔
53
    }
54
    if ((length & 0x01U) != 0) {
45 ✔
55
        head = (head << 0x08U) | Load<BIN1>(name, length & 0x00U);
17 ✔
56
    }
57
    return Slot{(head << 0x08U) | static_cast<uint8_t>(length), tail};
45 ✔
58
}
59

60
template <size_t Count, size_t... Index>
61
constexpr auto MakeSlots(const std::array<std::string_view, Count>& names, std::index_sequence<Index...>) {
62
    return std::array<Slot, sizeof...(Index)>{Pack(names[Index])...};
63
}
64

65
constexpr auto Hash(std::string_view name) {
5 ✔
66
    uint32_t value = static_cast<uint32_t>(name.size());
5 ✔
67
    size_t index = 0;
5 ✔
68
    for (; index + 4 <= name.size(); index += 4) {
22 ✔
69
        value = value * 33U + Load<uint32_t>(name, index);
17 ✔
70
    }
71
    for (; index < name.size(); index++) {
11 ✔
72
        value = value * 33U + static_cast<unsigned char>(name[index]);
6 ✔
73
    }
74
    return value;
5 ✔
75
}
76

77
constexpr auto Test(std::string_view left, std::string_view right) {
3 ✔
78
    if (left.size() != right.size()) {
3 ✔
NEW
79
        return false;
×
80
    }
81
    size_t length = left.size();
3 ✔
82
    size_t offset = 0;
3 ✔
83
    for (; length >= 8; length -= 8, offset += 8) {
9 ✔
84
        if (Load<uint64_t>(left, offset) != Load<uint64_t>(right, offset)) {
6 ✔
NEW
85
            return false;
×
86
        }
87
    }
88
    if ((length & 0x04U) != 0 && Load<uint32_t>(left, offset + (length & 0x03U)) != Load<uint32_t>(right, offset + (length & 0x03U))) {
3 ✔
NEW
89
        return false;
×
90
    }
91
    if ((length & 0x02U) != 0 && Load<uint16_t>(left, offset + (length & 0x01U)) != Load<uint16_t>(right, offset + (length & 0x01U))) {
3 ✔
NEW
92
        return false;
×
93
    }
94
    return (length & 0x01U) == 0 || left[offset] == right[offset];
3 ✔
95
}
96

97
template <size_t Count>
98
struct ChainTable {
99
    std::array<uint32_t, std::bit_ceil(Count * 2 < 16 ? 16 : Count * 2)> Heads;
100
    std::array<uint32_t, Count> Items;
101
    std::array<uint32_t, Count> Nexts;
102
};
103

104
template <size_t Count>
105
constexpr ChainTable<Count> MakeChain(const std::array<std::string_view, Count>& names) {
106
    ChainTable<Count> table{};
107
    for (auto& head : table.Heads) {
108
        head = UINT32_MAX;
109
    }
110
    for (size_t index = 0; index < Count; index++) {
111
        const uint32_t hash = Hash(names[index]);
112
        const uint32_t bucket = hash & static_cast<uint32_t>(table.Heads.size() - 1);
113
        table.Items[index] = hash;
114
        table.Nexts[index] = table.Heads[bucket];
115
        table.Heads[bucket] = static_cast<uint32_t>(index);
116
    }
117
    return table;
118
}
119

120
template <size_t Count>
121
constexpr auto HasLongName(const std::array<std::string_view, Count>& names) {
122
    for (const auto name : names) {
123
        if (name.size() > PackedItemBytesLimit) {
124
            return true;
125
        }
126
    }
127
    return false;
128
}
129

130
template <size_t Count>
131
constexpr auto GetDuplicateIndex(const std::array<std::string_view, Count>& names) {
2 ✔
132
    for (size_t left = 0; left < Count; left++) {
5 ✔
133
        for (size_t right = left + 1; right < Count; right++) {
7 ✔
134
            if (names[left] == names[right]) {
4 ✔
135
                return left;
1 ✔
136
            }
137
        }
138
    }
139
    return SIZE_MAX;
1 ✔
140
}
141

142
template <typename Schema, size_t... Index>
143
constexpr auto MakeNames(std::index_sequence<Index...>) {
144
    return std::array<std::string_view, sizeof...(Index)>{std::get<Index>(Schema::Members).Name...};
145
}
146

147
template <typename Schema, size_t... Index>
148
constexpr auto MakeOptionalFlags(std::index_sequence<Index...>) {
149
    return std::array<bool, sizeof...(Index)>{std::get<Index>(Schema::Members).IsOptional...};
150
}
151
}
152

153
template <typename Schema>
154
struct NamedObjectLayout {
155
    using Members = std::remove_const_t<decltype(Schema::Members)>;
156
    static constexpr auto Count = std::tuple_size_v<Members>;
157
    static constexpr auto Names = detail::MakeNames<Schema>(std::make_index_sequence<Count>());
158
    static constexpr auto Slots = detail::MakeSlots(Names, std::make_index_sequence<Count>());
159
    static constexpr auto Chain = detail::MakeChain(Names);
160
    static_assert(detail::GetDuplicateIndex(Names) == SIZE_MAX, "named object member names must be unique");
161

162
    static constexpr auto Query(std::string_view name) noexcept {
51 ✔
163
        if constexpr (Count <= detail::PackedItemCountLimit && !detail::HasLongName(Names)) {
164
            if (name.size() > detail::PackedItemBytesLimit) {
46 ✔
165
                return SIZE_MAX;
1 ✔
166
            }
167
            const detail::Slot probe = detail::Pack(name);
45 ✔
168
            for (size_t index = 0; index < Count; index++) {
77 ✔
169
                if (Slots[index].Head == probe.Head && Slots[index].Tail == probe.Tail) {
72 ✔
170
                    return index;
40 ✔
171
                }
172
            }
173
            return SIZE_MAX;
5 ✔
174
        } else {
175
            const uint32_t hash = detail::Hash(name);
5 ✔
176
            const uint32_t mask = static_cast<uint32_t>(Chain.Heads.size() - 1);
5 ✔
177
            for (uint32_t cursor = Chain.Heads[hash & mask]; cursor != UINT32_MAX; cursor = Chain.Nexts[cursor]) {
7 ✔
178
                if (Chain.Items[cursor] == hash && detail::Test(name, Names[cursor])) {
5 ✔
179
                    return static_cast<size_t>(cursor);
3 ✔
180
                }
181
            }
182
            return SIZE_MAX;
2 ✔
183
        }
184
    }
185

186
    static void Parse(std::span<const std::byte> span, std::array<std::span<const std::byte>, Count>& slices) {
24 ✔
187
        static constexpr auto optionals = detail::MakeOptionalFlags<Schema>(std::make_index_sequence<Count>());
188
        auto copy = span;
24 ✔
189
        while (!copy.empty()) {
62 ✔
190
            auto head = ::binary::DecodeWithLengthPrefix(copy);
41 ✔
191
            auto tail = ::binary::DecodeWithLengthPrefix(copy);
41 ✔
192
            const std::string_view name(reinterpret_cast<const char*>(head.data()), head.size());
41 ✔
193
            const size_t index = Query(name);
41 ✔
194
            if (index == SIZE_MAX) {
41 ✔
195
                continue;
4 ✔
196
            }
197
            if (slices[index].data() != nullptr) {
37 ✔
198
                throw std::invalid_argument(std::format("named key '{}' already exists", name));
3 ✔
199
            }
200
            slices[index] = tail;
34 ✔
201
        }
202

203
        for (size_t index = 0; index < Count; index++) {
61 ✔
204
            if (slices[index].data() == nullptr && !optionals[index]) {
44 ✔
205
                throw std::invalid_argument(std::format("named key '{}' does not exist", Names[index]));
4 ✔
206
            }
207
        }
208
    }
17 ✔
209
};
210
}
211

212
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc