From 1cac6d48282459d8855c1cec1eeb4bc0e3db5f89 Mon Sep 17 00:00:00 2001 From: Eugene Kosov Date: Thu, 14 May 2020 15:24:47 +0300 Subject: [PATCH] span cleanup --- include/span.h | 62 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/include/span.h b/include/span.h index 0ed0158088c..0e8516933c6 100644 --- a/include/span.h +++ b/include/span.h @@ -24,11 +24,33 @@ this program; if not, write to the Free Software Foundation, Inc., namespace st_ { +namespace detail +{ + +template struct remove_cv +{ + typedef T type; +}; +template struct remove_cv +{ + typedef T type; +}; +template struct remove_cv +{ + typedef T type; +}; +template struct remove_cv +{ + typedef T type; +}; + +} // namespace detail + template class span { public: typedef ElementType element_type; - typedef ElementType value_type; + typedef typename detail::remove_cv::type value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef element_type *pointer; @@ -38,7 +60,6 @@ public: typedef pointer iterator; typedef const_pointer const_iterator; typedef std::reverse_iterator reverse_iterator; - typedef std::reverse_iterator const_reverse_iterator; span() : data_(NULL), size_(0) {} @@ -64,73 +85,72 @@ public: span &operator=(const span &other) { - data_= other.data_; - size_= other.size_; + data_= other.data(); + size_= other.size(); return *this; } template span first() const { assert(!empty()); - return span(data_, 1); + return span(data(), 1); } template span last() const { assert(!empty()); - return span(data_ + size() - 1, 1); + return span(data() + size() - 1, 1); } span first(size_type count) const { assert(!empty()); - return span(data_, 1); + return span(data(), 1); } span last(size_type count) const { assert(!empty()); - return span(data_ + size() - 1, 1); + return span(data() + size() - 1, 1); } span subspan(size_type offset, size_type count) const { assert(!empty()); assert(size() >= offset + count); - return span(data_ + offset, count); + return span(data() + offset, count); } size_type size() const { return size_; } - size_type size_bytes() const { return size_ * sizeof(ElementType); } - bool empty() const __attribute__((warn_unused_result)) { return size_ == 0; } + size_type size_bytes() const { return size() * sizeof(ElementType); } + bool empty() const __attribute__((warn_unused_result)) + { + return size() == 0; + } reference operator[](size_type idx) const { assert(size() > idx); - return data_[idx]; + return data()[idx]; } reference front() const { assert(!empty()); - return data_[0]; + return data()[0]; } reference back() const { assert(!empty()); - return data_[size() - 1]; - } - pointer data() const - { - assert(!empty()); - return data_; + return data()[size() - 1]; } + pointer data() const { return data_; } iterator begin() const { return data_; } iterator end() const { return data_ + size_; } reverse_iterator rbegin() const { - return std::reverse_iterator(std::advance(end(), -1)); + return std::reverse_iterator(end()); } reverse_iterator rend() const { - return std::reverse_iterator(std::advance(begin(), -1)); + return std::reverse_iterator(begin()); } private: