From 6be4e80770295fe0723f62c54c8114ee93c2bf4c Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 06:00:52 +0100 Subject: [PATCH] Avoid O(N^2) template instantiations in no_duplicate_field_names --- .../rfl/internal/no_duplicate_field_names.hpp | 65 +++++++++---------- tests/json/test_literal.cpp | 34 ++++++++++ 2 files changed, 65 insertions(+), 34 deletions(-) diff --git a/include/rfl/internal/no_duplicate_field_names.hpp b/include/rfl/internal/no_duplicate_field_names.hpp index 4e3f90dc2..86bf5f671 100644 --- a/include/rfl/internal/no_duplicate_field_names.hpp +++ b/include/rfl/internal/no_duplicate_field_names.hpp @@ -1,51 +1,48 @@ #ifndef RFL_INTERNAL_NO_DUPLICATE_FIELD_NAMES_HPP_ #define RFL_INTERNAL_NO_DUPLICATE_FIELD_NAMES_HPP_ +#include +#include +#include +#include +#include + #include "../Tuple.hpp" namespace rfl { namespace internal { -namespace no_duplicate_field_names_helpers { - -template -constexpr inline void compare_two_fields() { - if constexpr (_j < _i) { - using FieldType1 = std::remove_cvref_t>; - using FieldType2 = std::remove_cvref_t>; - - constexpr auto field_name_i = FieldType1::name_; - constexpr auto field_name_j = FieldType2::name_; - - constexpr bool no_duplicate = (field_name_i != field_name_j); - - static_assert(no_duplicate, - "Duplicate field names are not allowed in either named " - "tuples or Literals."); - } -} - -template -constexpr inline void iterate_over_j(std::integer_sequence) { - (compare_two_fields(), ...); -} - -template -constexpr inline void iterate_over_i(std::integer_sequence) { - (iterate_over_j(std::make_integer_sequence()), ...); -} -} // namespace no_duplicate_field_names_helpers - template constexpr inline bool no_duplicate_field_names() { - constexpr auto num_fields = rfl::tuple_size_v; + constexpr std::size_t num_fields = rfl::tuple_size_v; if constexpr (num_fields <= 1) { return true; } else { - no_duplicate_field_names_helpers::iterate_over_i( - std::make_integer_sequence()); - return true; + constexpr bool result = + [](std::index_sequence<_is...>) { + std::array arr{ + std::remove_cvref_t>::name_ + .string_view()...}; + for (std::size_t gap = arr.size() / 2; gap > 0; gap /= 2) { + for (std::size_t i = gap; i < arr.size(); ++i) { + const auto key = arr[i]; + std::size_t j = i; + while (j >= gap && key < arr[j - gap]) { + arr[j] = arr[j - gap]; + j -= gap; + } + arr[j] = key; + } + } + for (std::size_t i = 1; i < arr.size(); ++i) { + if (arr[i] == arr[i - 1]) { + return false; + } + } + return true; + }(std::make_index_sequence()); + return result; } } diff --git a/tests/json/test_literal.cpp b/tests/json/test_literal.cpp index d4c28b6de..877a7b8d7 100644 --- a/tests/json/test_literal.cpp +++ b/tests/json/test_literal.cpp @@ -38,4 +38,38 @@ TEST(json, test_literal_reports_unsupported_string) { "string 'Ned'. The following strings are supported: 'Homer', " "'Marge', 'Bart', 'Lisa', 'Maggie'."); } + +TEST(json, test_literal_has_duplicates) { + static_assert(!FirstName::has_duplicates()); + static_assert(!LastName::has_duplicates()); + static_assert(rfl::Literal<"a", "a">::has_duplicates()); + static_assert(rfl::Literal<"a", "b", "c", "a">::has_duplicates()); + static_assert(rfl::Literal<"z", "m", "a", "m", "b">::has_duplicates()); + + using UniqueLarge = rfl::Literal< + "f15", "f03", "f28", "f00", "f19", "f07", "f24", "f11", "f31", "f02", + "f16", "f21", "f08", "f26", "f13", "f05", "f29", "f01", "f18", "f10", + "f25", "f06", "f22", "f14", "f30", "f04", "f17", "f20", "f09", "f27", + "f12", "f23">; + using DuplicateLarge = rfl::Literal< + "f15", "f03", "f28", "f00", "f19", "f07", "f24", "f11", "f31", "f02", + "f16", "f21", "f08", "f26", "f13", "f05", "f29", "f01", "f18", "f10", + "f25", "f06", "f22", "f14", "f30", "f04", "f17", "f20", "f09", "f27", + "f12", "f19">; + static_assert(!UniqueLarge::has_duplicates()); + static_assert(DuplicateLarge::has_duplicates()); + + static_assert(rfl::internal::no_duplicate_field_names>()); + static_assert(rfl::internal::no_duplicate_field_names< + rfl::Tuple>>()); + static_assert( + rfl::internal::no_duplicate_field_names< + rfl::Tuple, rfl::Field<"a", int>, + rfl::Field<"c", int>>>()); + static_assert( + !rfl::internal::no_duplicate_field_names< + rfl::Tuple, rfl::Field<"a", int>, + rfl::Field<"b", double>>>()); + SUCCEED(); +} } // namespace test_literal