From f219e868064c5bb8887cbf2728c5840babdaee25 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 2 Aug 2024 09:51:30 +0100 Subject: [PATCH] Added etl::count_of, etl::has_duplicates, etl::has_duplicates_of --- .../etl/generators/type_traits_generator.h | 60 +++++++++++++++++-- include/etl/type_traits.h | 60 +++++++++++++++++-- test/test_type_traits.cpp | 46 ++++++++++++++ 3 files changed, 158 insertions(+), 8 deletions(-) diff --git a/include/etl/generators/type_traits_generator.h b/include/etl/generators/type_traits_generator.h index c1d73d103..f086b8414 100644 --- a/include/etl/generators/type_traits_generator.h +++ b/include/etl/generators/type_traits_generator.h @@ -2296,16 +2296,68 @@ typedef integral_constant true_type; using signed_type_t = typename signed_type::type; #endif -//********************************************* -// type_identity + //********************************************* + // type_identity -template -struct type_identity { typedef T type; }; + template + struct type_identity { typedef T type; }; #if ETL_USING_CPP11 template using type_identity_t = typename type_identity::type; #endif + +#if ETL_USING_CPP11 + //********************************************* + // has_duplicates + template + struct has_duplicates; + + template + struct has_duplicates : etl::conditional_t::value, + etl::true_type, + has_duplicates> {}; + + template <> + struct has_duplicates<> : etl::false_type {}; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool has_duplicates_v = etl::has_duplicates::value; +#endif + +#if ETL_USING_CPP11 + //********************************************* + // count_of + template + struct count_of; + + template + struct count_of : etl::integral_constant::value + + count_of::value> {}; + + template + struct count_of : etl::integral_constant {}; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr size_t count_of_v = etl::count_of::value; +#endif + +#if ETL_USING_CPP11 + //********************************************* + // has_duplicates_of + template + struct has_duplicates_of : etl::bool_constant<(etl::count_of::value > 1U)> {}; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool has_duplicates_of_v = etl::has_duplicates_of::value; +#endif } // Helper macros diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index 1a40f0264..795789945 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -2289,16 +2289,68 @@ typedef integral_constant true_type; using signed_type_t = typename signed_type::type; #endif -//********************************************* -// type_identity + //********************************************* + // type_identity -template -struct type_identity { typedef T type; }; + template + struct type_identity { typedef T type; }; #if ETL_USING_CPP11 template using type_identity_t = typename type_identity::type; #endif + +#if ETL_USING_CPP11 + //********************************************* + // has_duplicates + template + struct has_duplicates; + + template + struct has_duplicates : etl::conditional_t::value, + etl::true_type, + has_duplicates> {}; + + template <> + struct has_duplicates<> : etl::false_type {}; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool has_duplicates_v = etl::has_duplicates::value; +#endif + +#if ETL_USING_CPP11 + //********************************************* + // count_of + template + struct count_of; + + template + struct count_of : etl::integral_constant::value + + count_of::value> {}; + + template + struct count_of : etl::integral_constant {}; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr size_t count_of_v = etl::count_of::value; +#endif + +#if ETL_USING_CPP11 + //********************************************* + // has_duplicates_of + template + struct has_duplicates_of : etl::bool_constant<(etl::count_of::value > 1U)> {}; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool has_duplicates_of_v = etl::has_duplicates_of::value; +#endif } // Helper macros diff --git a/test/test_type_traits.cpp b/test/test_type_traits.cpp index 7b1979cac..4316a697f 100644 --- a/test/test_type_traits.cpp +++ b/test/test_type_traits.cpp @@ -1333,4 +1333,50 @@ namespace { CHECK_CLOSE(type_identity_test_add(1.5f, 2), 3.5f, 0.01f); } + + //************************************************************************* + TEST(test_has_duplicates) + { +#if ETL_USING_CPP17 + CHECK_FALSE((etl::has_duplicates_v)); + CHECK_FALSE((etl::has_duplicates_v)); + CHECK_TRUE((etl::has_duplicates_v)); +#else + CHECK_FALSE((etl::has_duplicates::value)); + CHECK_FALSE((etl::has_duplicates::value)); + CHECK_TRUE((etl::has_duplicates::value)); +#endif + } + + //************************************************************************* + TEST(test_has_duplicates_of) + { +#if ETL_USING_CPP17 + CHECK_FALSE((etl::has_duplicates_of_v)); + CHECK_TRUE((etl::has_duplicates_of_v)); // char is duplicated. + CHECK_FALSE((etl::has_duplicates_of_v)); // int is not duplicated. +#else + CHECK_FALSE((etl::has_duplicates_of::value)); + CHECK_TRUE((etl::has_duplicates_of::value)); // char is duplicated. + CHECK_FALSE((etl::has_duplicates_of::value)); // int is not duplicated. +#endif + } + + //************************************************************************* + TEST(test_count_of) + { +#if ETL_USING_CPP17 + CHECK_EQUAL(0, (etl::count_of_v)); + CHECK_EQUAL(0, (etl::count_of_v)); + CHECK_EQUAL(1, (etl::count_of_v)); + CHECK_EQUAL(1, (etl::count_of_v)); + CHECK_EQUAL(2, (etl::count_of_v)); +#else + CHECK_EQUAL(0, (etl::count_of::value)); + CHECK_EQUAL(0, (etl::count_of::value)); + CHECK_EQUAL(1, (etl::count_of::value)); + CHECK_EQUAL(1, (etl::count_of::value)); + CHECK_EQUAL(2, (etl::count_of::value)); +#endif + } }