Rename slots in the object index for Qt compatibility

Qt defines slots as a macro, so a Qt header included before
json_view.hpp broke the declarations of the local variable (see #5865).
The test variable is renamed as well.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-18 10:34:23 +02:00
1 parent c17f985fe0
commit bac7daaed7
3 files changed
+15 -15

No files matched your search

@@ -77,7 +77,7 @@ inline void build_object_index(document_data& d, node* obj)
} }
const std::size_t start = d.index_slots.size(); const std::size_t start = d.index_slots.size();
d.index_slots.resize(start + cap, 0); d.index_slots.resize(start + cap, 0);
std::uint32_t* const slots = d.index_slots.data() + start; std::uint32_t* const table = d.index_slots.data() + start;
const std::size_t mask = cap - 1; const std::size_t mask = cap - 1;
bool degenerate = false; bool degenerate = false;
for (const node* k = document_data::first_child(obj), *end = document_data::child_end(obj); k != end; k = document_data::after(k + 1)) for (const node* k = document_data::first_child(obj), *end = document_data::child_end(obj); k != end; k = document_data::after(k + 1))
@@ -87,9 +87,9 @@ inline void build_object_index(document_data& d, node* obj)
std::size_t i = static_cast<std::size_t>(hash) & mask; std::size_t i = static_cast<std::size_t>(hash) & mask;
bool duplicate = false; bool duplicate = false;
std::size_t distance = 0; std::size_t distance = 0;
while (slots[i] != 0) while (table[i] != 0)
{ {
const node* const other = obj + slots[i]; const node* const other = obj + table[i];
if (other->len == k->len && (k->len == 0 || std::memcmp(d.str(*other), key, k->len) == 0)) if (other->len == k->len && (k->len == 0 || std::memcmp(d.str(*other), key, k->len) == 0))
{ {
duplicate = true; // keep the first duplicate = true; // keep the first
@@ -108,7 +108,7 @@ inline void build_object_index(document_data& d, node* obj)
} }
if (!duplicate) if (!duplicate)
{ {
slots[i] = static_cast<std::uint32_t>(k - obj); table[i] = static_cast<std::uint32_t>(k - obj);
} }
} }
if (degenerate) if (degenerate)
@@ -134,12 +134,12 @@ inline void build_object_indexes(document_data& d)
inline const node* find_indexed(const document_data& d, const node* obj, const char* key, std::size_t n) noexcept inline const node* find_indexed(const document_data& d, const node* obj, const char* key, std::size_t n) noexcept
{ {
const document_data::object_index& ix = d.indexes[obj->extra - 1u]; const document_data::object_index& ix = d.indexes[obj->extra - 1u];
const std::uint32_t* const slots = d.index_slots.data() + ix.start; const std::uint32_t* const table = d.index_slots.data() + ix.start;
const std::uint64_t hash = key_hash(key, n); // (a cast of the call would be useless where std::uint64_t is std::size_t) const std::uint64_t hash = key_hash(key, n); // (a cast of the call would be useless where std::uint64_t is std::size_t)
std::size_t i = static_cast<std::size_t>(hash) & ix.mask; std::size_t i = static_cast<std::size_t>(hash) & ix.mask;
for (std::size_t distance = 0; distance <= index_max_displacement; ++distance) for (std::size_t distance = 0; distance <= index_max_displacement; ++distance)
{ {
const std::uint32_t s = slots[i]; const std::uint32_t s = table[i];
if (s == 0) if (s == 0)
{ {
return nullptr; return nullptr;
+6 -6
View File
@@ -3182,7 +3182,7 @@ inline void build_object_index(document_data& d, node* obj)
} }
const std::size_t start = d.index_slots.size(); const std::size_t start = d.index_slots.size();
d.index_slots.resize(start + cap, 0); d.index_slots.resize(start + cap, 0);
std::uint32_t* const slots = d.index_slots.data() + start; std::uint32_t* const table = d.index_slots.data() + start;
const std::size_t mask = cap - 1; const std::size_t mask = cap - 1;
bool degenerate = false; bool degenerate = false;
for (const node* k = document_data::first_child(obj), *end = document_data::child_end(obj); k != end; k = document_data::after(k + 1)) for (const node* k = document_data::first_child(obj), *end = document_data::child_end(obj); k != end; k = document_data::after(k + 1))
@@ -3192,9 +3192,9 @@ inline void build_object_index(document_data& d, node* obj)
std::size_t i = static_cast<std::size_t>(hash) & mask; std::size_t i = static_cast<std::size_t>(hash) & mask;
bool duplicate = false; bool duplicate = false;
std::size_t distance = 0; std::size_t distance = 0;
while (slots[i] != 0) while (table[i] != 0)
{ {
const node* const other = obj + slots[i]; const node* const other = obj + table[i];
if (other->len == k->len && (k->len == 0 || std::memcmp(d.str(*other), key, k->len) == 0)) if (other->len == k->len && (k->len == 0 || std::memcmp(d.str(*other), key, k->len) == 0))
{ {
duplicate = true; // keep the first duplicate = true; // keep the first
@@ -3213,7 +3213,7 @@ inline void build_object_index(document_data& d, node* obj)
} }
if (!duplicate) if (!duplicate)
{ {
slots[i] = static_cast<std::uint32_t>(k - obj); table[i] = static_cast<std::uint32_t>(k - obj);
} }
} }
if (degenerate) if (degenerate)
@@ -3239,12 +3239,12 @@ inline void build_object_indexes(document_data& d)
inline const node* find_indexed(const document_data& d, const node* obj, const char* key, std::size_t n) noexcept inline const node* find_indexed(const document_data& d, const node* obj, const char* key, std::size_t n) noexcept
{ {
const document_data::object_index& ix = d.indexes[obj->extra - 1u]; const document_data::object_index& ix = d.indexes[obj->extra - 1u];
const std::uint32_t* const slots = d.index_slots.data() + ix.start; const std::uint32_t* const table = d.index_slots.data() + ix.start;
const std::uint64_t hash = key_hash(key, n); // (a cast of the call would be useless where std::uint64_t is std::size_t) const std::uint64_t hash = key_hash(key, n); // (a cast of the call would be useless where std::uint64_t is std::size_t)
std::size_t i = static_cast<std::size_t>(hash) & ix.mask; std::size_t i = static_cast<std::size_t>(hash) & ix.mask;
for (std::size_t distance = 0; distance <= index_max_displacement; ++distance) for (std::size_t distance = 0; distance <= index_max_displacement; ++distance)
{ {
const std::uint32_t s = slots[i]; const std::uint32_t s = table[i];
if (s == 0) if (s == 0)
{ {
return nullptr; return nullptr;
+3 -3
View File
@@ -412,7 +412,7 @@ TEST_CASE("json_view large objects")
// make the table build quadratic: such an object gets no table and is // make the table build quadratic: such an object gets no table and is
// searched linearly // searched linearly
constexpr std::size_t members = 300; constexpr std::size_t members = 300;
constexpr std::size_t slots = 1024; // the table size for 300 members: the next power of two >= 600 constexpr std::size_t table_size = 1024; // the table size for 300 members: the next power of two >= 600
std::vector<std::string> colliding; std::vector<std::string> colliding;
std::vector<std::string> spread; std::vector<std::string> spread;
for (std::uint64_t counter = 0; colliding.size() < members || spread.size() < members; ++counter) for (std::uint64_t counter = 0; colliding.size() < members || spread.size() < members; ++counter)
@@ -423,7 +423,7 @@ TEST_CASE("json_view large objects")
{ {
key[i] = static_cast<char>('a' + (x % 26)); key[i] = static_cast<char>('a' + (x % 26));
} }
const bool lands_in_slot_zero = (nlohmann::detail::view::key_hash(key.data(), key.size()) & (slots - 1)) == 0; const bool lands_in_slot_zero = (nlohmann::detail::view::key_hash(key.data(), key.size()) & (table_size - 1)) == 0;
if (lands_in_slot_zero && colliding.size() < members) if (lands_in_slot_zero && colliding.size() < members)
{ {
colliding.push_back(key); colliding.push_back(key);
@@ -470,7 +470,7 @@ TEST_CASE("json_view large objects")
// same length, so the tables are the only difference) // same length, so the tables are the only difference)
with_collisions.shrink_to_fit(); with_collisions.shrink_to_fit();
without_collisions.shrink_to_fit(); without_collisions.shrink_to_fit();
CHECK(without_collisions.memory_usage() >= with_collisions.memory_usage() + (slots * sizeof(std::uint32_t))); CHECK(without_collisions.memory_usage() >= with_collisions.memory_usage() + (table_size * sizeof(std::uint32_t)));
} }
SECTION("shrink_to_fit releases the tables' spare capacity") SECTION("shrink_to_fit releases the tables' spare capacity")