From 71985e0b6006084b92f70184c15b89c83adf1bd9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 17 Oct 2026 14:21:48 +0200 Subject: [PATCH] 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 --- include/nlohmann/detail/view/object_index.hpp | 12 ++++++------ single_include/nlohmann/json_view.hpp | 12 ++++++------ tests/src/unit-json_view_dump.cpp | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/nlohmann/detail/view/object_index.hpp b/include/nlohmann/detail/view/object_index.hpp index c77fa6348..c498d8585 100644 --- a/include/nlohmann/detail/view/object_index.hpp +++ b/include/nlohmann/detail/view/object_index.hpp @@ -77,7 +77,7 @@ inline void build_object_index(document_data& d, node* obj) } const std::size_t start = d.index_slots.size(); 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; 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)) @@ -87,9 +87,9 @@ inline void build_object_index(document_data& d, node* obj) std::size_t i = static_cast(hash) & mask; bool duplicate = false; 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)) { duplicate = true; // keep the first @@ -108,7 +108,7 @@ inline void build_object_index(document_data& d, node* obj) } if (!duplicate) { - slots[i] = static_cast(k - obj); + table[i] = static_cast(k - obj); } } 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 { 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) std::size_t i = static_cast(hash) & ix.mask; 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) { return nullptr; diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 71d50dad1..db08bcb11 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -3179,7 +3179,7 @@ inline void build_object_index(document_data& d, node* obj) } const std::size_t start = d.index_slots.size(); 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; 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)) @@ -3189,9 +3189,9 @@ inline void build_object_index(document_data& d, node* obj) std::size_t i = static_cast(hash) & mask; bool duplicate = false; 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)) { duplicate = true; // keep the first @@ -3210,7 +3210,7 @@ inline void build_object_index(document_data& d, node* obj) } if (!duplicate) { - slots[i] = static_cast(k - obj); + table[i] = static_cast(k - obj); } } if (degenerate) @@ -3236,12 +3236,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 { 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) std::size_t i = static_cast(hash) & ix.mask; 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) { return nullptr; diff --git a/tests/src/unit-json_view_dump.cpp b/tests/src/unit-json_view_dump.cpp index 1a9f59f51..cfe621188 100644 --- a/tests/src/unit-json_view_dump.cpp +++ b/tests/src/unit-json_view_dump.cpp @@ -412,7 +412,7 @@ TEST_CASE("json_view large objects") // make the table build quadratic: such an object gets no table and is // searched linearly 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 colliding; std::vector spread; 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('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) { colliding.push_back(key); @@ -470,7 +470,7 @@ TEST_CASE("json_view large objects") // same length, so the tables are the only difference) with_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")