From 8491133351a789fba09a22dd5998dd4a62ceb846 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 12 Feb 2023 11:26:03 +0100 Subject: [PATCH] Work in progress --- include/etl/span.h | 112 +++++++++++++++------- test/test_span_dynamic_extent.cpp | 149 +++++++++++++++++++++++++++--- test/test_span_fixed_extent.cpp | 108 +++++++++++++++++++--- test/vs2019/cpp.hint | 13 +++ 4 files changed, 322 insertions(+), 60 deletions(-) diff --git a/include/etl/span.h b/include/etl/span.h index bfd7a4f4..a84520ad 100644 --- a/include/etl/span.h +++ b/include/etl/span.h @@ -33,6 +33,7 @@ SOFTWARE. #include "platform.h" #include "iterator.h" +#include "algorithm.h" #include "circular_iterator.h" #include "nullptr.h" #include "hash.h" @@ -345,23 +346,24 @@ namespace etl return pbegin[i]; } - //************************************************************************* - /// Compare two spans for equality. - //************************************************************************* - template , value_type>::value, bool> = true> - ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span& other) const ETL_NOEXCEPT - { - return ((pbegin == other.pbegin) && (Extent == other.size())); - } + ////************************************************************************* + ///// Compare two spans for equality. + ////************************************************************************* + //template ::type, value_type>::value, bool>::type = true> + //ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span& other) const ETL_NOEXCEPT + //{ + // return ((begin() == other.begin()) && (Extent == other.size())) || + // etl::equal(begin(), end(), other.begin(), other.end()); + //} - //************************************************************************* - /// Compare two spans for non-equality. - //************************************************************************* - template , value_type>::value, bool> = true> - ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span& other) const ETL_NOEXCEPT - { - return !(*this == other); - } + ////************************************************************************* + ///// Compare two spans for non-equality. + ////************************************************************************* + //template ::type, value_type>::value, bool>::type = true> + //ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span& other) const ETL_NOEXCEPT + //{ + // return !(*this == other); + //} //************************************************************************* /// Obtains a span that is a view over the first COUNT elements of this span. @@ -749,23 +751,25 @@ namespace etl return pbegin[i]; } - //************************************************************************* - /// Compare two spans for equality. - //************************************************************************* - template , value_type>::value, bool> = true> - ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span& other) const ETL_NOEXCEPT - { - return ((pbegin == other.pbegin) && (pend == other.pend)); - } + ////************************************************************************* + ///// Compare two spans for equality. + ////************************************************************************* + //template ::type, value_type>::value, bool>::type = true> + //ETL_NODISCARD ETL_CONSTEXPR bool operator==(const etl::span& other) const ETL_NOEXCEPT + //{ + // return (empty() && other.empty()) || + // ((begin() == other.begin()) && (end() == other.end())) || + // etl::equal(begin(), end(), other.begin(), other.end()); + //} - //************************************************************************* - /// Compare two spans for non-equality. - //************************************************************************* - template , value_type>::value, bool> = true> - ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span& other) const ETL_NOEXCEPT - { - return !(*this == other); - } + ////************************************************************************* + ///// Compare two spans for non-equality. + ////************************************************************************* + //template ::type, value_type>::value, bool>::type = true> + //ETL_NODISCARD ETL_CONSTEXPR bool operator!=(const etl::span& other) const ETL_NOEXCEPT + //{ + // return !(*this == other); + //} //************************************************************************* /// Obtains a span that is a view over the first COUNT elements of this span. @@ -845,6 +849,46 @@ namespace etl pointer pend; }; + //************************************************************************* + /// Compare two spans for equality. + //************************************************************************* + template + ETL_NODISCARD + ETL_CONSTEXPR + typename etl::enable_if::type, typename etl::remove_cv::type>::value, bool>::type + operator ==(const etl::span& lhs, const etl::span& rhs) ETL_NOEXCEPT + { + return (lhs.begin() == rhs.begin()) && (lhs.size() == rhs.size()); + } + + //************************************************************************* + /// Compare two spans for inequality. + //************************************************************************* + template + ETL_NODISCARD + ETL_CONSTEXPR + bool operator !=(const etl::span& lhs, const etl::span& rhs) ETL_NOEXCEPT + { + return !(lhs == rhs); + } + + //************************************************************************* + /// Equality function. + /// Performs a comparision of the range values. + /// Returns true if one of the following are true + /// 1. Both spans are empty. + /// 2. They both point to the same range of data. + /// 3. The values in the two ranges are equal. + //************************************************************************* + template + etl::enable_if_t, etl::remove_cv_t>, bool> + equal(const etl::span& lhs, const etl::span& rhs) + { + return (lhs.empty() && rhs.empty()) || + ((lhs.begin() == rhs.begin()) && (lhs.size() == rhs.size())) || + etl::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); + } + //************************************************************************* /// Template deduction guides. //************************************************************************* @@ -869,7 +913,7 @@ namespace etl span(const etl::array&) -> span; -#if ETL_USING_STL +#if ETL_USING_STL && ETL_USING_CPP11 template span(std::array&) ->span; @@ -890,7 +934,7 @@ namespace etl size_t operator()(const etl::span& view) const { return etl::private_hash::generic_hash(reinterpret_cast(&view[0]), - reinterpret_cast(&view[view.size()])); + reinterpret_cast(&view[view.size()])); } }; #endif diff --git a/test/test_span_dynamic_extent.cpp b/test/test_span_dynamic_extent.cpp index 944b991e..d26178d1 100644 --- a/test/test_span_dynamic_extent.cpp +++ b/test/test_span_dynamic_extent.cpp @@ -973,15 +973,77 @@ namespace //************************************************************************* TEST(test_operator_equality) { - etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data2{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; + etl::array data4{ 0, 1, 2, 3, 5, 6, 7, 8, 9 }; - View view1{data1}; - View view2{data1}; - View view3{data2}; + int i; - CHECK_TRUE((view1 == view2)); - CHECK_FALSE((view1 == view3)); + View view1{ data1 }; + View view2{ data1 }; + View view3{ data2 }; + View view4{ data3 }; + View view5{ data4 }; + View view6; + View view7; + View view8(&i, &i); + + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_FALSE(etl::equal(view1, view5)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + CHECK_TRUE(etl::equal(view6, view8)); + CHECK_TRUE(etl::equal(view8, view8)); + + CHECK_TRUE(view1 == view2); + CHECK_FALSE(view1 == view3); + CHECK_FALSE(view1 == view4); + CHECK_FALSE(view1 == view5); + CHECK_TRUE(view6 == view6); + CHECK_TRUE(view6 == view7); + CHECK_FALSE(view6 == view8); + CHECK_TRUE(view8 == view8); + } + + //************************************************************************* + TEST(test_operator_equality_one_is_const) + { + etl::array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data2{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; + etl::array data4{ 0, 1, 2, 3, 5, 6, 7, 8, 9 }; + + int i; + + View view1{ data1 }; + CView view2{ data1 }; + CView view3{ data2 }; + CView view4{ data3 }; + CView view5{ data4 }; + View view6; + CView view7; + CView view8(&i, &i); + + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_FALSE(etl::equal(view1, view5)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + CHECK_TRUE(etl::equal(view6, view8)); + CHECK_TRUE(etl::equal(view8, view8)); + + CHECK_TRUE(view1 == view2); + CHECK_FALSE(view1 == view3); + CHECK_FALSE(view1 == view4); + CHECK_FALSE(view1 == view5); + CHECK_TRUE(view6 == view6); + CHECK_TRUE(view6 == view7); + CHECK_FALSE(view6 == view8); + CHECK_TRUE(view8 == view8); } //************************************************************************* @@ -989,13 +1051,76 @@ namespace { etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; + etl::array data4{ 0, 1, 2, 3, 5, 6, 7, 8, 9 }; - View view1{data1}; - View view2{data1}; - View view3{data2}; + int i; - CHECK_FALSE((view1 != view2)); - CHECK_TRUE((view1 != view3)); + View view1{ data1 }; + View view2{ data1 }; + View view3{ data2 }; + View view4{ data3 }; + View view5{ data4 }; + View view6; + View view7; + View view8(&i, &i); + + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_FALSE(etl::equal(view1, view5)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + CHECK_TRUE(etl::equal(view6, view8)); + CHECK_TRUE(etl::equal(view8, view8)); + + CHECK_FALSE(view1 != view2); + CHECK_TRUE(view1 != view3); + CHECK_TRUE(view1 != view4); + CHECK_TRUE(view1 != view5); + CHECK_FALSE(view6 != view6); + CHECK_FALSE(view6 != view7); + CHECK_TRUE(view6 != view8); + CHECK_FALSE(view8 != view8); + } + + //************************************************************************* + TEST(test_operator_not_equal_one_is_const) + { + etl::array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data2{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; + etl::array data4{ 0, 1, 2, 3, 5, 6, 7, 8, 9 }; + + int i; + + View view1{ data1 }; + CView view2{ data1 }; + CView view3{ data2 }; + CView view4{ data3 }; + CView view5{ data4 }; + View view6; + View view7; + View view8(&i, &i); + + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_FALSE(etl::equal(view1, view5)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + CHECK_TRUE(etl::equal(view6, view8)); + CHECK_TRUE(etl::equal(view8, view8)); + + CHECK_FALSE(view1 != view1); + CHECK_FALSE(view1 != view2); + CHECK_TRUE(view1 != view3); + CHECK_TRUE(view1 != view4); + CHECK_TRUE(view1 != view5); + CHECK_FALSE(view6 != view6); + CHECK_FALSE(view6 != view7); + CHECK_TRUE(view6 != view8); + CHECK_FALSE(view8 != view8); } #include "etl/private/diagnostic_pop.h" diff --git a/test/test_span_fixed_extent.cpp b/test/test_span_fixed_extent.cpp index 82b3a51f..7e6e327a 100644 --- a/test/test_span_fixed_extent.cpp +++ b/test/test_span_fixed_extent.cpp @@ -963,29 +963,109 @@ namespace //************************************************************************* TEST(test_operator_equality) { - etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data2{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; - View view1{data1}; - View view2{data1}; - View view3{data2}; + View view1{ data1 }; + View view2{ data1 }; + View view3{ data2 }; + View view4{ data3 }; + View view6; + View view7; - CHECK_TRUE((view1 == view2)); - CHECK_FALSE((view1 == view3)); + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + + CHECK_TRUE(view1 == view2); + CHECK_FALSE(view1 == view3); + CHECK_FALSE(view1 == view4); + CHECK_TRUE(view6 == view6); + CHECK_TRUE(view6 == view7); + } + + //************************************************************************* + TEST(test_operator_equality_one_is_const) + { + etl::array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data2{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; + + View view1{ data1 }; + CView view2{ data1 }; + CView view3{ data2 }; + CView view4{ data3 }; + View view6; + CView view7; + + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + + CHECK_TRUE(view1 == view2); + CHECK_FALSE(view1 == view3); + CHECK_FALSE(view1 == view4); + CHECK_TRUE(view6 == view6); + CHECK_TRUE(view6 == view7); } //************************************************************************* TEST(test_operator_not_equal) { - etl::array data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - etl::array data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + etl::array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data2{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; - View view1{data1}; - View view2{data1}; - View view3{data2}; + View view1{ data1 }; + View view2{ data1 }; + View view3{ data2 }; + View view4{ data3 }; + View view6; + View view7; - CHECK_FALSE((view1 != view2)); - CHECK_TRUE((view1 != view3)); + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + + CHECK_FALSE(view1 != view2); + CHECK_TRUE(view1 != view3); + CHECK_TRUE(view1 != view4); + CHECK_FALSE(view6 != view6); + CHECK_FALSE(view6 != view7); + } + + //************************************************************************* + TEST(test_operator_not_equal_one_is_const) + { + etl::array data1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data2{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + etl::array data3{ 0, 1, 2, 3, 4, 4, 6, 7, 8, 9 }; + + View view1{ data1 }; + CView view2{ data1 }; + CView view3{ data2 }; + CView view4{ data3 }; + View view6; + View view7; + + CHECK_TRUE(etl::equal(view1, view2)); + CHECK_TRUE(etl::equal(view1, view3)); + CHECK_FALSE(etl::equal(view1, view4)); + CHECK_TRUE(etl::equal(view6, view6)); + CHECK_TRUE(etl::equal(view6, view7)); + + CHECK_FALSE(view1 != view2); + CHECK_TRUE(view1 != view3); + CHECK_TRUE(view1 != view4); + CHECK_FALSE(view6 != view6); + CHECK_FALSE(view6 != view7); } #include "etl/private/diagnostic_pop.h" diff --git a/test/vs2019/cpp.hint b/test/vs2019/cpp.hint index 3c9d8d88..0fd4bafc 100644 --- a/test/vs2019/cpp.hint +++ b/test/vs2019/cpp.hint @@ -38,3 +38,16 @@ // such as names of functions and macros. // For more information see https://go.microsoft.com/fwlink/?linkid=865984 #define ETL_NULLPTR +// Hint files help the Visual Studio IDE interpret Visual C++ identifiers +// such as names of functions and macros. +// For more information see https://go.microsoft.com/fwlink/?linkid=865984 +#define ETL_NODISCARD [[nodiscard]] +#define ETL_NODISCARD +// Hint files help the Visual Studio IDE interpret Visual C++ identifiers +// such as names of functions and macros. +// For more information see https://go.microsoft.com/fwlink/?linkid=865984 +#define ETL_CONSTEXPR +// Hint files help the Visual Studio IDE interpret Visual C++ identifiers +// such as names of functions and macros. +// For more information see https://go.microsoft.com/fwlink/?linkid=865984 +#define ETL_NOEXCEPT