From 86be2366c73a19b77cc7a6674cc0e3d118efcdf9 Mon Sep 17 00:00:00 2001 From: Stephan Dollberg Date: Fri, 17 Aug 2018 19:47:03 +0100 Subject: [PATCH] Fix #717 - Add empty() to strided_span (#718) --- include/gsl/multi_span | 2 ++ tests/strided_span_tests.cpp | 3 +++ 2 files changed, 5 insertions(+) diff --git a/include/gsl/multi_span b/include/gsl/multi_span index a7c96ce..c8f4aae 100644 --- a/include/gsl/multi_span +++ b/include/gsl/multi_span @@ -1931,6 +1931,8 @@ public: constexpr pointer data() const noexcept { return data_; } + constexpr bool empty() const noexcept { return this->size() == 0; } + constexpr explicit operator bool() const noexcept { return data_ != nullptr; } constexpr iterator begin() const { return iterator{this, true}; } diff --git a/tests/strided_span_tests.cpp b/tests/strided_span_tests.cpp index 42eff7a..2310f5b 100644 --- a/tests/strided_span_tests.cpp +++ b/tests/strided_span_tests.cpp @@ -67,12 +67,14 @@ TEST_CASE("span_section") const multi_span av = as_multi_span(multi_span{data}, dim<5>(), dim<10>()); const strided_span av_section_1 = av.section({1, 2}, {3, 4}); + CHECK(!av_section_1.empty()); CHECK((av_section_1[{0, 0}] == 12)); CHECK((av_section_1[{0, 1}] == 13)); CHECK((av_section_1[{1, 0}] == 22)); CHECK((av_section_1[{2, 3}] == 35)); const strided_span av_section_2 = av_section_1.section({1, 2}, {2, 2}); + CHECK(!av_section_2.empty()); CHECK((av_section_2[{0, 0}] == 24)); CHECK((av_section_2[{0, 1}] == 25)); CHECK((av_section_2[{1, 0}] == 34)); @@ -563,6 +565,7 @@ TEST_CASE("empty_strided_spans") strided_span empty_sav{empty_av, {0, 1}}; CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0}); + CHECK(empty_sav.empty()); CHECK_THROWS_AS(empty_sav[0], fail_fast); CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast); CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast);