From 0c6ce424abe6a75c90d8c3c038736cd12af75b18 Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Tue, 4 Feb 2020 12:01:27 -0800 Subject: [PATCH] refactoring index_type to size_type, changing expects --- include/gsl/span | 91 ++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 37 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index 09a10a9..83a2e02 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -296,7 +296,7 @@ namespace details constexpr void _Verify_offset(const difference_type n) const noexcept { // test that *this + n is within the span of this iterator STL // algorithm call - Expects((current_ + n) >= begin_ && (current_ + n) <= end_); + Expects( n <= (current_ - begin_) && n <= (end_ - current_)); } GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute @@ -331,13 +331,13 @@ namespace details class extent_type { public: - using index_type = std::size_t; + using size_type = std::size_t; static_assert(Ext != dynamic_extent, "A fixed-size span must be >= 0 in size."); constexpr extent_type() noexcept {} - template + template constexpr extent_type(extent_type ext) { static_assert(Other == Ext || Other == dynamic_extent, @@ -345,30 +345,30 @@ namespace details Expects(ext.size() == Ext); } - constexpr extent_type(index_type size) { Expects(size == Ext); } + constexpr extent_type(size_type size) { Expects(size == Ext); } - constexpr index_type size() const noexcept { return Ext; } + constexpr size_type size() const noexcept { return Ext; } }; template <> class extent_type { public: - using index_type = std::size_t; + using size_type = std::size_t; - template + template explicit constexpr extent_type(extent_type ext) : size_(ext.size()) {} - explicit constexpr extent_type(index_type size) : size_(size) + explicit constexpr extent_type(size_type size) : size_(size) { Expects(size != dynamic_extent); } - constexpr index_type size() const noexcept { return size_; } + constexpr size_type size() const noexcept { return size_; } private: - index_type size_; + size_type size_; }; template @@ -388,7 +388,7 @@ public: // constants and types using element_type = ElementType; using value_type = std::remove_cv_t; - using index_type = std::size_t; + using size_type = std::size_t; using pointer = element_type*; using reference = element_type&; using difference_type = std::ptrdiff_t; @@ -398,12 +398,12 @@ public: using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; - using size_type = index_type; + using index_type [[deprecated("use size_type instead of index_type")]] = size_type; #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND) - static constexpr const index_type extent{Extent}; + static constexpr const size_type extent{Extent}; #else - static constexpr index_type extent{Extent}; + static constexpr size_type extent{Extent}; #endif // [span.cons], span constructors, copy, assignment, and destructor @@ -414,7 +414,7 @@ public: constexpr span() noexcept : storage_(nullptr, details::extent_type<0>()) {} - constexpr span(pointer ptr, index_type count) noexcept : storage_(ptr, count) {} + constexpr span(pointer ptr, size_type count) noexcept : storage_(ptr, count) {} constexpr span(pointer firstElem, pointer lastElem) noexcept : storage_(firstElem, static_cast(std::distance(firstElem, lastElem))) @@ -451,7 +451,7 @@ public: std::is_convertible::value && std::is_convertible().data())>::value>> - constexpr span(Container& cont) noexcept : span(cont.data(), narrow(cont.size())) + constexpr span(Container& cont) noexcept : span(cont.data(), narrow(cont.size())) {} template ().data())>::value>> constexpr span(const Container& cont) noexcept - : span(cont.data(), narrow(cont.size())) + : span(cont.data(), narrow(cont.size())) {} constexpr span(const span& other) noexcept = default; @@ -504,36 +504,36 @@ public: return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count}; } - constexpr span first(index_type count) const noexcept + constexpr span first(size_type count) const noexcept { Expects(count <= size()); return {data(), count}; } - constexpr span last(index_type count) const noexcept + constexpr span last(size_type count) const noexcept { Expects(count <= size()); return make_subspan(size() - count, dynamic_extent, subspan_selector{}); } - constexpr span subspan(index_type offset, - index_type count = dynamic_extent) const + constexpr span subspan(size_type offset, + size_type count = dynamic_extent) const noexcept { return make_subspan(offset, count, subspan_selector{}); } // [span.obs], span observers - constexpr index_type size() const noexcept { return storage_.size(); } - constexpr index_type size_bytes() const noexcept + constexpr size_type size() const noexcept { return storage_.size(); } + constexpr size_type size_bytes() const noexcept { - return size() * narrow_cast(sizeof(element_type)); + return size() * narrow_cast(sizeof(element_type)); } constexpr bool empty() const noexcept { return size() == 0; } // [span.elem], span element access GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute - constexpr reference operator[](index_type idx) const noexcept + constexpr reference operator[](size_type idx) const noexcept { Expects(idx < size()); return data()[idx]; @@ -552,11 +552,11 @@ public: } // at and operator() are deprecated to align to the public member functions of std::span - [[deprecated("Use operator[]")]] constexpr reference at(index_type idx) const noexcept + [[deprecated("Use operator[]")]] constexpr reference at(size_type idx) const noexcept { return this->operator[](idx); } - [[deprecated("Use operator[]")]] constexpr reference operator()(index_type idx) const noexcept + [[deprecated("Use operator[]")]] constexpr reference operator()(size_type idx) const noexcept { return this->operator[](idx); } @@ -564,13 +564,30 @@ public: constexpr pointer data() const noexcept { return storage_.data(); } // [span.iter], span iterator support - constexpr iterator begin() const noexcept { return {data(), data() + size(), data()}; } - constexpr iterator end() const noexcept { return {data(), data() + size(), data() + size()}; } + constexpr iterator begin() const noexcept + { + const auto data = storage_.data(); + return {data, data + size(), data}; + } + + constexpr iterator end() const noexcept + { + const auto data = storage_.data(); + const auto size = storage_.size(); + return {data, data + size, data + size}; + } + + constexpr const_iterator cbegin() const noexcept + { + const auto data = storage_.data(); + return {data, data + size(), data}; + } - constexpr const_iterator cbegin() const noexcept { return {data(), data() + size(), data()}; } constexpr const_iterator cend() const noexcept { - return {data(), data() + size(), data() + size()}; + const auto data = storage_.data(); + const auto size = storage_.size(); + return {data, data + size, data + size}; } constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } @@ -635,7 +652,7 @@ private: // The rest is needed to remove unnecessary null check // in subspans and constructors from arrays - constexpr span(KnownNotNull ptr, index_type count) : storage_(ptr, count) {} + constexpr span(KnownNotNull ptr, size_type count) : storage_(ptr, count) {} template class subspan_selector @@ -643,7 +660,7 @@ private: }; template - span make_subspan(index_type offset, index_type count, + span make_subspan(size_type offset, size_type count, subspan_selector) const { const span tmp(*this); @@ -651,7 +668,7 @@ private: } GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute - span make_subspan(index_type offset, index_type count, + span make_subspan(size_type offset, size_type count, subspan_selector) const { Expects(size() >= offset); @@ -665,7 +682,7 @@ private: #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND) template -constexpr const typename span::index_type span::extent; +constexpr const typename span::size_type span::extent; #endif // [span.comparison], span comparison operators @@ -748,7 +765,7 @@ as_writeable_bytes(span s) noexcept // template constexpr span make_span(ElementType* ptr, - typename span::index_type count) + typename span::size_type count) { return span(ptr, count); } @@ -799,7 +816,7 @@ constexpr ElementType& at(span s, index i) // [span.obs] Free observer functions template -constexpr typename span::index_type +constexpr typename span::size_type ssize(const span& span) noexcept { return span.size();