From 4f96a8424d4aafc0e59d01747bf52e83d9b45cce Mon Sep 17 00:00:00 2001 From: rolandreichweinbmw Date: Wed, 4 Dec 2024 11:17:32 +0100 Subject: [PATCH] Added const iterators to span (#986) --- include/etl/span.h | 76 +++++++++++++++++++++++++++++-- test/test_span_dynamic_extent.cpp | 4 ++ test/test_span_fixed_extent.cpp | 4 ++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/include/etl/span.h b/include/etl/span.h index 7aa11c960..acf76f1d5 100644 --- a/include/etl/span.h +++ b/include/etl/span.h @@ -71,8 +71,10 @@ namespace etl typedef T* pointer; typedef const T* const_pointer; - typedef T* iterator; - typedef ETL_OR_STD::reverse_iterator reverse_iterator; + typedef T* iterator; + typedef const T* const_iterator; + typedef ETL_OR_STD::reverse_iterator reverse_iterator; + typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; typedef etl::circular_iterator circular_iterator; typedef etl::circular_iterator > reverse_circular_iterator; @@ -185,6 +187,14 @@ namespace etl return pbegin; } + //************************************************************************* + /// Returns a const iterator to the beginning of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT + { + return pbegin; + } + //************************************************************************* /// Returns an iterator to the beginning of the span. //************************************************************************* @@ -201,6 +211,14 @@ namespace etl return circular_iterator(begin(), end()); } + //************************************************************************* + /// Returns a const iterator to the end of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT + { + return (pbegin + Extent); + } + //************************************************************************* /// Returns an iterator to the end of the span. //************************************************************************* @@ -209,6 +227,14 @@ namespace etl return (pbegin + Extent); } + //************************************************************************* + // Returns a const reverse iterator to the reverse beginning of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT + { + return const_reverse_iterator((pbegin + Extent)); + } + //************************************************************************* // Returns an reverse iterator to the reverse beginning of the span. //************************************************************************* @@ -225,6 +251,14 @@ namespace etl return reverse_circular_iterator(rbegin(), rend()); } + //************************************************************************* + /// Returns a const reverse iterator to the end of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT + { + return const_reverse_iterator(pbegin); + } + //************************************************************************* /// Returns a reverse iterator to the end of the span. //************************************************************************* @@ -413,8 +447,10 @@ namespace etl typedef T* pointer; typedef const T* const_pointer; - typedef T* iterator; - typedef ETL_OR_STD::reverse_iterator reverse_iterator; + typedef T* iterator; + typedef const T* const_iterator; + typedef ETL_OR_STD::reverse_iterator reverse_iterator; + typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; typedef etl::circular_iterator circular_iterator; typedef etl::circular_iterator > reverse_circular_iterator; @@ -544,6 +580,14 @@ namespace etl return pbegin; } + //************************************************************************* + /// Returns a const iterator to the beginning of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT + { + return pbegin; + } + //************************************************************************* /// Returns an iterator to the beginning of the span. //************************************************************************* @@ -560,6 +604,14 @@ namespace etl return circular_iterator(begin(), end()); } + //************************************************************************* + /// Returns a const iterator to the end of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT + { + return pend; + } + //************************************************************************* /// Returns an iterator to the end of the span. //************************************************************************* @@ -576,6 +628,14 @@ namespace etl return reverse_iterator(pend); } + //************************************************************************* + // Returns a const reverse iterator to the reverse beginning of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT + { + return const_reverse_iterator(pend); + } + //************************************************************************* /// Returns a reverse circular iterator to the end of the span. //************************************************************************* @@ -584,6 +644,14 @@ namespace etl return reverse_circular_iterator(rbegin(), rend()); } + //************************************************************************* + /// Returns a const reverse iterator to the end of the span. + //************************************************************************* + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT + { + return const_reverse_iterator(pbegin); + } + //************************************************************************* /// Returns a reverse iterator to the end of the span. //************************************************************************* diff --git a/test/test_span_dynamic_extent.cpp b/test/test_span_dynamic_extent.cpp index 300ee58c5..7a1546700 100644 --- a/test/test_span_dynamic_extent.cpp +++ b/test/test_span_dynamic_extent.cpp @@ -409,15 +409,19 @@ namespace View view(etldata.begin(), etldata.end()); CView cview(etldata.begin(), etldata.end()); + CHECK_EQUAL(etldata.cbegin(), view.cbegin()); CHECK_EQUAL(etldata.begin(), view.begin()); CHECK_EQUAL(etldata.begin(), cview.begin()); + CHECK_EQUAL(etldata.cend(), view.crbegin().base()); CHECK_EQUAL(etldata.end(), view.rbegin().base()); CHECK_EQUAL(etldata.end(), cview.rbegin().base()); + CHECK_EQUAL(etldata.cend(), view.cend()); CHECK_EQUAL(etldata.end(), view.end()); CHECK_EQUAL(etldata.end(), cview.end()); + CHECK_EQUAL(etldata.cbegin(), view.crend().base()); CHECK_EQUAL(etldata.begin(), view.rend().base()); CHECK_EQUAL(etldata.begin(), cview.rend().base()); } diff --git a/test/test_span_fixed_extent.cpp b/test/test_span_fixed_extent.cpp index 2c760783a..33e8a6f56 100644 --- a/test/test_span_fixed_extent.cpp +++ b/test/test_span_fixed_extent.cpp @@ -397,15 +397,19 @@ namespace View view(etldata.begin(), etldata.end()); CView cview(etldata.begin(), etldata.end()); + CHECK_EQUAL(etldata.cbegin(), view.cbegin()); CHECK_EQUAL(etldata.begin(), view.begin()); CHECK_EQUAL(etldata.begin(), cview.begin()); + CHECK_EQUAL(etldata.cend(), view.crbegin().base()); CHECK_EQUAL(etldata.end(), view.rbegin().base()); CHECK_EQUAL(etldata.end(), cview.rbegin().base()); + CHECK_EQUAL(etldata.cend(), view.cend()); CHECK_EQUAL(etldata.end(), view.end()); CHECK_EQUAL(etldata.end(), cview.end()); + CHECK_EQUAL(etldata.cbegin(), view.crend().base()); CHECK_EQUAL(etldata.begin(), view.rend().base()); CHECK_EQUAL(etldata.begin(), cview.rend().base()); }