From c126e1fd0cae9b16389cdfd793b82f7136319b70 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 3 Mar 2022 12:00:47 +0100 Subject: [PATCH] Replace == with key_compare in ordered_map --- include/nlohmann/detail/macro_scope.hpp | 6 ++++++ include/nlohmann/detail/macro_unscope.hpp | 1 + include/nlohmann/ordered_map.hpp | 19 ++++++++++------- single_include/nlohmann/json.hpp | 26 ++++++++++++++++------- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index f636b908a..cc9ac5fc7 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -106,6 +106,12 @@ #endif #endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address) + #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] +#else + #define JSON_NO_UNIQUE_ADDRESS +#endif + // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index 377d3f11d..ec57b02cc 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -14,6 +14,7 @@ #undef NLOHMANN_BASIC_JSON_TPL #undef JSON_EXPLICIT #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL +#undef JSON_NO_UNIQUE_ADDRESS #ifndef JSON_TEST_KEEP_MACROS #undef JSON_CATCH diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 9e600e60d..6779fdf9a 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -47,7 +47,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return {it, false}; } @@ -70,7 +70,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it->second; } @@ -83,7 +83,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it->second; } @@ -96,7 +96,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { // Since we cannot move const Keys, re-construct them in place for (auto next = it; ++next != this->end(); ++it) @@ -168,7 +168,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return 1; } @@ -180,7 +180,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it; } @@ -192,7 +192,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it; } @@ -209,7 +209,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == value.first) + if (m_compare(it->first, value.first)) { return {it, false}; } @@ -230,6 +230,9 @@ template , insert(*it); } } + +private: + JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare(); }; } // namespace nlohmann diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f410dbfc6..e806a8f00 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2411,6 +2411,12 @@ using is_detected_convertible = #endif #endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address) + #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] +#else + #define JSON_NO_UNIQUE_ADDRESS +#endif + // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push @@ -17359,7 +17365,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return {it, false}; } @@ -17382,7 +17388,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it->second; } @@ -17395,7 +17401,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it->second; } @@ -17408,7 +17414,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { // Since we cannot move const Keys, re-construct them in place for (auto next = it; ++next != this->end(); ++it) @@ -17480,7 +17486,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return 1; } @@ -17492,7 +17498,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it; } @@ -17504,7 +17510,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == key) + if (m_compare(it->first, key)) { return it; } @@ -17521,7 +17527,7 @@ template , { for (auto it = this->begin(); it != this->end(); ++it) { - if (it->first == value.first) + if (m_compare(it->first, value.first)) { return {it, false}; } @@ -17542,6 +17548,9 @@ template , insert(*it); } } + +private: + JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare(); }; } // namespace nlohmann @@ -22293,6 +22302,7 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef NLOHMANN_BASIC_JSON_TPL #undef JSON_EXPLICIT #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL +#undef JSON_NO_UNIQUE_ADDRESS #ifndef JSON_TEST_KEEP_MACROS #undef JSON_CATCH