2016-02-24 14:26:28 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef GSL_SPAN_H
|
|
|
|
#define GSL_SPAN_H
|
|
|
|
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <gsl/gsl_assert> // for Expects
|
|
|
|
#include <gsl/gsl_byte> // for byte
|
|
|
|
#include <gsl/gsl_util> // for narrow_cast, narrow
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <algorithm> // for lexicographical_compare
|
|
|
|
#include <array> // for array
|
|
|
|
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
|
|
|
|
#include <iterator> // for reverse_iterator, distance, random_access_...
|
2020-02-03 16:45:56 -05:00
|
|
|
#include <memory> // for std::addressof
|
2016-02-24 14:26:28 -05:00
|
|
|
#include <stdexcept>
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <type_traits> // for enable_if_t, declval, is_convertible, inte...
|
2016-02-24 14:26:28 -05:00
|
|
|
#include <utility>
|
|
|
|
|
2019-01-15 13:27:34 -05:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
2017-04-20 10:51:37 -04:00
|
|
|
#pragma warning(push)
|
|
|
|
|
|
|
|
// turn off some warnings that are noisy about our Expects statements
|
|
|
|
#pragma warning(disable : 4127) // conditional expression is constant
|
2020-02-03 13:56:31 -05:00
|
|
|
#pragma warning( \
|
|
|
|
disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
|
2017-07-13 16:53:56 -04:00
|
|
|
#pragma warning(disable : 4702) // unreachable code
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
|
|
|
|
#pragma warning(disable : 26495) // uninitalized member when constructor calls constructor
|
|
|
|
#pragma warning(disable : 26446) // parser bug does not allow attributes on some templates
|
2017-04-20 10:51:37 -04:00
|
|
|
|
|
|
|
#if _MSC_VER < 1910
|
|
|
|
#pragma push_macro("constexpr")
|
|
|
|
#define constexpr /*constexpr*/
|
2018-03-03 22:12:45 -05:00
|
|
|
#define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2018-08-17 14:36:06 -04:00
|
|
|
#endif // _MSC_VER < 1910
|
|
|
|
#endif // _MSC_VER
|
2018-03-03 22:12:45 -05:00
|
|
|
|
|
|
|
// See if we have enough C++17 power to use a static constexpr data member
|
|
|
|
// without needing an out-of-line definition
|
|
|
|
#if !(defined(__cplusplus) && (__cplusplus >= 201703L))
|
|
|
|
#define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
|
|
|
|
#endif // !(defined(__cplusplus) && (__cplusplus >= 201703L))
|
|
|
|
|
2018-06-08 14:41:06 -04:00
|
|
|
// GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t)
|
2018-08-13 00:44:17 -04:00
|
|
|
// While there is a conversion from signed to unsigned, it happens at
|
2020-01-17 00:32:37 -05:00
|
|
|
// compiletime, so the compiler wouldn't have to warn indiscriminately, but
|
2018-08-13 00:44:17 -04:00
|
|
|
// could check if the source value actually doesn't fit into the target type
|
2018-06-08 14:41:06 -04:00
|
|
|
// and only warn in those cases.
|
2019-01-15 13:27:34 -05:00
|
|
|
#if defined(__GNUC__) && __GNUC__ > 6
|
2018-06-08 14:41:06 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
|
|
|
#endif
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
namespace gsl
|
|
|
|
{
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [views.constants], constants
|
2020-02-03 21:13:14 -05:00
|
|
|
constexpr std::size_t dynamic_extent = static_cast<std::size_t>(-1);
|
2016-06-12 21:28:19 -04:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent = dynamic_extent>
|
2016-07-26 21:34:27 -04:00
|
|
|
class span;
|
|
|
|
|
2016-06-12 21:28:19 -04:00
|
|
|
// implementation details
|
2016-03-01 15:11:41 -05:00
|
|
|
namespace details
|
|
|
|
{
|
2016-07-20 16:17:47 -04:00
|
|
|
template <class T>
|
|
|
|
struct is_span_oracle : std::false_type
|
|
|
|
{
|
|
|
|
};
|
2016-03-01 15:11:41 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2016-07-20 16:17:47 -04:00
|
|
|
struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
|
|
|
|
{
|
|
|
|
};
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
template <class T>
|
2016-07-26 21:34:27 -04:00
|
|
|
struct is_span : public is_span_oracle<std::remove_cv_t<T>>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct is_std_array_oracle : std::false_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2016-07-26 21:34:27 -04:00
|
|
|
struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>>
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
};
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <std::size_t From, std::size_t To>
|
2016-07-20 16:17:47 -04:00
|
|
|
struct is_allowed_extent_conversion
|
2016-07-26 22:19:47 -04:00
|
|
|
: public std::integral_constant<bool, From == To || From == gsl::dynamic_extent ||
|
|
|
|
To == gsl::dynamic_extent>
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
};
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
template <class From, class To>
|
|
|
|
struct is_allowed_element_type_conversion
|
2016-09-15 01:01:02 -04:00
|
|
|
: public std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
};
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 21:13:14 -05:00
|
|
|
template <class Type>
|
2016-08-02 00:41:20 -04:00
|
|
|
class span_iterator
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
2020-02-03 21:13:14 -05:00
|
|
|
using value_type = std::remove_cv_t<Type>;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = Type*;
|
|
|
|
using reference = Type&;
|
2016-08-08 16:33:02 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
#ifdef _MSC_VER
|
2020-02-03 21:13:14 -05:00
|
|
|
using _Unchecked_type = pointer;
|
2020-02-03 19:26:36 -05:00
|
|
|
#endif
|
2020-02-03 21:13:14 -05:00
|
|
|
constexpr operator span_iterator<const Type>() const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
return {begin_, end_, current_};
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-08-01 21:49:48 -04:00
|
|
|
|
2020-02-03 21:13:14 -05:00
|
|
|
constexpr reference operator*() const noexcept
|
|
|
|
{
|
|
|
|
Expects(begin_ && current_ && end_);
|
|
|
|
Expects(current_ < end_);
|
|
|
|
return *current_;
|
|
|
|
}
|
2020-02-03 19:26:36 -05:00
|
|
|
|
|
|
|
constexpr pointer operator->() const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
Expects(begin_ && current_ && end_);
|
|
|
|
Expects(current_ < end_);
|
|
|
|
return current_;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2020-01-08 15:34:11 -05:00
|
|
|
constexpr span_iterator& operator++() noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
Expects(begin_ && current_ && end_);
|
|
|
|
Expects(current_ < end_);
|
|
|
|
++current_;
|
2016-07-20 16:17:47 -04:00
|
|
|
return *this;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-01-08 15:34:11 -05:00
|
|
|
constexpr span_iterator operator++(int) noexcept
|
2016-08-01 21:49:48 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
auto ret{*this};
|
|
|
|
++*this;
|
2016-08-01 21:49:48 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-01-08 15:34:11 -05:00
|
|
|
constexpr span_iterator& operator--() noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
Expects(begin_ && current_ && end_);
|
|
|
|
Expects(current_ > begin_);
|
|
|
|
--current_;
|
2016-07-20 16:17:47 -04:00
|
|
|
return *this;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-01-08 15:34:11 -05:00
|
|
|
constexpr span_iterator operator--(int) noexcept
|
2016-08-01 21:49:48 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
auto ret{*this};
|
|
|
|
--*this;
|
2016-08-01 21:49:48 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
constexpr span_iterator& operator+=(const difference_type n) noexcept
|
|
|
|
{
|
|
|
|
Expects(begin_ && current_ && end_);
|
|
|
|
if (n > 0) Expects(end_ - current_ >= n);
|
2020-02-03 21:12:32 -05:00
|
|
|
if (n < 0) Expects(current_ - begin_ >= -n);
|
2020-02-03 19:26:36 -05:00
|
|
|
current_ += n;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr span_iterator operator+(const difference_type n) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
auto ret{*this};
|
2016-08-01 21:49:48 -04:00
|
|
|
return ret += n;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
friend constexpr span_iterator operator+(const difference_type n,
|
2020-02-03 21:13:14 -05:00
|
|
|
const span_iterator& rhs) noexcept
|
2018-05-09 17:01:22 -04:00
|
|
|
{
|
|
|
|
return rhs + n;
|
|
|
|
}
|
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
constexpr span_iterator& operator-=(const difference_type n) noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
Expects(begin_ && end_ && current_);
|
|
|
|
if (n > 0) Expects(end_ - current_ >= n);
|
|
|
|
if (n < 0) Expects(end_ - current_ >= -n);
|
|
|
|
current_ -= n;
|
2016-08-01 21:49:48 -04:00
|
|
|
return *this;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
constexpr span_iterator operator-(const difference_type n) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
auto ret{*this};
|
2016-08-01 21:49:48 -04:00
|
|
|
return ret -= n;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
template <
|
2020-02-03 21:13:14 -05:00
|
|
|
class Type2,
|
|
|
|
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
|
|
|
|
constexpr difference_type operator-(const span_iterator<Type2>& rhs) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
|
2020-02-03 19:26:36 -05:00
|
|
|
return current_ - rhs.current_;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
constexpr reference operator[](const difference_type n) const noexcept
|
|
|
|
{
|
|
|
|
return *(*this + n);
|
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
template <
|
2020-02-03 21:13:14 -05:00
|
|
|
class Type2,
|
|
|
|
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
|
|
|
|
constexpr bool operator==(const span_iterator<Type2>& rhs) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
return begin_ == rhs.begin_ && end_ == rhs.end_ && current_ == rhs.current_;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
template <
|
2020-02-03 21:13:14 -05:00
|
|
|
class Type2,
|
|
|
|
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
|
|
|
|
constexpr bool operator!=(const span_iterator<Type2>& rhs) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
return !(*this == rhs);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
template <
|
2020-02-03 21:13:14 -05:00
|
|
|
class Type2,
|
|
|
|
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
|
|
|
|
constexpr bool operator<(const span_iterator<Type2>& rhs) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
|
2020-02-03 19:26:36 -05:00
|
|
|
return current_ < rhs.current_;
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
template <
|
2020-02-03 21:13:14 -05:00
|
|
|
class Type2,
|
|
|
|
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
|
|
|
|
constexpr bool operator>(const span_iterator<Type2>& rhs) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
return !(*this < rhs);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
template <
|
2020-02-03 21:13:14 -05:00
|
|
|
class Type2,
|
|
|
|
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
|
|
|
|
constexpr bool operator<=(const span_iterator<Type2>& rhs) const noexcept
|
2016-08-08 15:06:47 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
return !(*this > rhs);
|
2016-08-08 15:06:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
template <
|
2020-02-03 21:13:14 -05:00
|
|
|
class Type2,
|
|
|
|
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
|
|
|
|
constexpr bool operator>=(const span_iterator<Type2>& rhs) const noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
return *!(this < rhs);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2018-05-22 21:07:49 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// MSVC++ iterator debugging support; allows STL algorithms in 15.8+
|
|
|
|
// to unwrap span_iterator to a pointer type after a range check in STL
|
|
|
|
// algorithm calls
|
2018-08-13 00:44:17 -04:00
|
|
|
friend constexpr void _Verify_range(span_iterator lhs, span_iterator rhs) noexcept
|
|
|
|
{ // test that [lhs, rhs) forms a valid range inside an STL algorithm
|
2020-02-03 21:13:14 -05:00
|
|
|
Expects(lhs.begin_ == rhs.begin_ // range spans have to match
|
|
|
|
&& lhs.end_ == rhs.end_ &&
|
|
|
|
lhs.current_ <= rhs.current_); // range must not be transposed
|
2018-05-22 21:07:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void _Verify_offset(const difference_type n) const noexcept
|
2020-02-03 20:51:25 -05:00
|
|
|
{ // test that *this + n is within the span of this iterator STL
|
2018-05-22 21:07:49 -04:00
|
|
|
// algorithm call
|
2020-02-03 19:26:36 -05:00
|
|
|
Expects((current_ + n) >= begin_ && (current_ + n) <= end_);
|
2018-05-22 21:07:49 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
|
2018-05-22 21:07:49 -04:00
|
|
|
constexpr pointer _Unwrapped() const noexcept
|
2018-08-13 00:44:17 -04:00
|
|
|
{ // after seeking *this to a high water mark, or using one of the
|
2018-05-22 21:07:49 -04:00
|
|
|
// _Verify_xxx functions above, unwrap this span_iterator to a raw
|
|
|
|
// pointer
|
2020-02-03 19:26:36 -05:00
|
|
|
return current_;
|
2018-05-22 21:07:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the STL that span_iterator should not be unwrapped if it can't
|
|
|
|
// validate in advance, even in release / optimized builds:
|
|
|
|
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
|
|
|
|
static constexpr const bool _Unwrap_when_unverified = false;
|
|
|
|
#else
|
|
|
|
static constexpr bool _Unwrap_when_unverified = false;
|
|
|
|
#endif
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(con .3) // NO-FORMAT: attribute // TODO: false positive
|
2018-05-22 21:07:49 -04:00
|
|
|
constexpr void _Seek_to(const pointer p) noexcept
|
2018-08-13 00:44:17 -04:00
|
|
|
{ // adjust the position of *this to previously verified location p
|
2018-05-22 21:07:49 -04:00
|
|
|
// after _Unwrapped
|
2020-02-03 19:26:36 -05:00
|
|
|
current_ = p;
|
2018-05-22 21:07:49 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
pointer begin_ = nullptr;
|
|
|
|
pointer end_ = nullptr;
|
|
|
|
pointer current_ = nullptr;
|
2016-07-20 16:17:47 -04:00
|
|
|
};
|
2016-06-26 10:00:56 -04:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <std::size_t Ext>
|
2016-07-26 21:34:27 -04:00
|
|
|
class extent_type
|
|
|
|
{
|
|
|
|
public:
|
2020-02-03 13:56:31 -05:00
|
|
|
using index_type = std::size_t;
|
2016-07-26 21:34:27 -04:00
|
|
|
|
2020-02-03 21:12:57 -05:00
|
|
|
static_assert(Ext != dynamic_extent, "A fixed-size span must be >= 0 in size.");
|
2016-07-26 21:34:27 -04:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr extent_type() noexcept {}
|
2016-07-26 21:34:27 -04:00
|
|
|
|
|
|
|
template <index_type Other>
|
2017-01-28 03:08:48 -05:00
|
|
|
constexpr extent_type(extent_type<Other> ext)
|
2016-07-26 21:34:27 -04:00
|
|
|
{
|
|
|
|
static_assert(Other == Ext || Other == dynamic_extent,
|
|
|
|
"Mismatch between fixed-size extent and size of initializing data.");
|
|
|
|
Expects(ext.size() == Ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr extent_type(index_type size) { Expects(size == Ext); }
|
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr index_type size() const noexcept { return Ext; }
|
2016-07-26 21:34:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class extent_type<dynamic_extent>
|
|
|
|
{
|
|
|
|
public:
|
2020-02-03 13:56:31 -05:00
|
|
|
using index_type = std::size_t;
|
2016-07-26 21:34:27 -04:00
|
|
|
|
|
|
|
template <index_type Other>
|
|
|
|
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
|
2018-08-17 14:36:06 -04:00
|
|
|
{}
|
2016-07-26 21:34:27 -04:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
explicit constexpr extent_type(index_type size) : size_(size)
|
|
|
|
{
|
|
|
|
Expects(size != dynamic_extent);
|
|
|
|
}
|
2016-07-26 21:34:27 -04:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr index_type size() const noexcept { return size_; }
|
2016-07-26 21:34:27 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
index_type size_;
|
|
|
|
};
|
2018-03-03 22:12:45 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
|
2018-03-03 22:12:45 -05:00
|
|
|
struct calculate_subspan_type
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
using type = span<ElementType, Count != dynamic_extent
|
|
|
|
? Count
|
|
|
|
: (Extent != dynamic_extent ? Extent - Offset : Extent)>;
|
2018-03-03 22:12:45 -05:00
|
|
|
};
|
2016-03-01 15:11:41 -05:00
|
|
|
} // namespace details
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span], class template span
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2016-07-20 16:17:47 -04:00
|
|
|
class span
|
|
|
|
{
|
2016-02-24 19:11:33 -05:00
|
|
|
public:
|
2016-07-20 16:17:47 -04:00
|
|
|
// constants and types
|
2016-02-24 19:11:33 -05:00
|
|
|
using element_type = ElementType;
|
2017-04-20 10:51:37 -04:00
|
|
|
using value_type = std::remove_cv_t<ElementType>;
|
2020-02-03 13:56:31 -05:00
|
|
|
using index_type = std::size_t;
|
2016-02-24 19:11:33 -05:00
|
|
|
using pointer = element_type*;
|
|
|
|
using reference = element_type&;
|
2020-02-03 13:56:31 -05:00
|
|
|
using difference_type = std::ptrdiff_t;
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
using iterator = details::span_iterator<ElementType>;
|
|
|
|
using const_iterator = details::span_iterator<const ElementType>;
|
2016-02-24 19:11:33 -05:00
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
2016-06-26 10:00:56 -04:00
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2017-04-13 16:55:20 -04:00
|
|
|
using size_type = index_type;
|
|
|
|
|
2018-03-03 22:12:45 -05:00
|
|
|
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
|
2018-08-13 00:44:17 -04:00
|
|
|
static constexpr const index_type extent{Extent};
|
2018-03-03 22:12:45 -05:00
|
|
|
#else
|
2018-08-13 00:44:17 -04:00
|
|
|
static constexpr index_type extent{Extent};
|
2018-03-03 22:12:45 -05:00
|
|
|
#endif
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.cons], span constructors, copy, assignment, and destructor
|
2017-01-28 03:08:48 -05:00
|
|
|
template <bool Dependent = false,
|
2017-04-20 10:51:37 -04:00
|
|
|
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE,
|
|
|
|
// since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0.
|
2020-02-03 13:56:31 -05:00
|
|
|
class = std::enable_if_t<(Dependent || Extent == 0 || Extent == dynamic_extent)>>
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
|
2018-08-17 14:36:06 -04:00
|
|
|
{}
|
2016-02-25 14:42:26 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
constexpr span(pointer ptr, index_type count) noexcept : storage_(ptr, count) {}
|
2016-02-25 14:42:26 -05:00
|
|
|
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr span(pointer firstElem, pointer lastElem) noexcept
|
2020-02-03 21:12:57 -05:00
|
|
|
: storage_(firstElem, static_cast<std::size_t>(std::distance(firstElem, lastElem)))
|
2018-08-17 14:36:06 -04:00
|
|
|
{}
|
2016-07-20 16:17:47 -04:00
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <std::size_t N>
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr span(element_type (&arr)[N]) noexcept
|
2018-08-19 20:10:53 -04:00
|
|
|
: storage_(KnownNotNull{std::addressof(arr[0])}, details::extent_type<N>())
|
2018-08-17 14:36:06 -04:00
|
|
|
{}
|
2016-07-26 22:19:47 -04:00
|
|
|
|
2018-11-05 18:39:41 -05:00
|
|
|
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
|
|
|
|
constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
|
|
|
|
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
|
2020-02-03 13:56:31 -05:00
|
|
|
{}
|
2016-06-14 23:14:17 -04:00
|
|
|
|
2018-11-05 18:39:41 -05:00
|
|
|
constexpr span(std::array<std::remove_const_t<element_type>, 0>&) noexcept
|
|
|
|
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
|
2020-02-03 13:56:31 -05:00
|
|
|
{}
|
2018-11-05 18:39:41 -05:00
|
|
|
|
|
|
|
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
|
2018-11-05 18:39:41 -05:00
|
|
|
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
|
2020-02-03 13:56:31 -05:00
|
|
|
{}
|
2018-11-05 18:39:41 -05:00
|
|
|
|
|
|
|
constexpr span(const std::array<std::remove_const_t<element_type>, 0>&) noexcept
|
|
|
|
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
|
2020-02-03 13:56:31 -05:00
|
|
|
{}
|
2016-02-29 16:16:48 -05:00
|
|
|
|
2016-03-01 15:11:41 -05:00
|
|
|
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
|
|
|
|
// on Container to be a contiguous sequence container.
|
|
|
|
template <class Container,
|
2016-07-20 16:17:47 -04:00
|
|
|
class = std::enable_if_t<
|
2016-07-26 22:19:47 -04:00
|
|
|
!details::is_span<Container>::value && !details::is_std_array<Container>::value &&
|
2016-07-26 21:34:27 -04:00
|
|
|
std::is_convertible<typename Container::pointer, pointer>::value &&
|
|
|
|
std::is_convertible<typename Container::pointer,
|
2016-07-20 16:17:47 -04:00
|
|
|
decltype(std::declval<Container>().data())>::value>>
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr span(Container& cont) noexcept : span(cont.data(), narrow<index_type>(cont.size()))
|
2018-08-17 14:36:06 -04:00
|
|
|
{}
|
2016-03-01 15:11:41 -05:00
|
|
|
|
|
|
|
template <class Container,
|
2016-07-20 16:17:47 -04:00
|
|
|
class = std::enable_if_t<
|
|
|
|
std::is_const<element_type>::value && !details::is_span<Container>::value &&
|
2016-07-26 21:34:27 -04:00
|
|
|
std::is_convertible<typename Container::pointer, pointer>::value &&
|
|
|
|
std::is_convertible<typename Container::pointer,
|
2016-07-20 16:17:47 -04:00
|
|
|
decltype(std::declval<Container>().data())>::value>>
|
2020-02-03 13:56:31 -05:00
|
|
|
constexpr span(const Container& cont) noexcept
|
|
|
|
: span(cont.data(), narrow<index_type>(cont.size()))
|
2018-08-17 14:36:06 -04:00
|
|
|
{}
|
2016-03-17 20:20:33 -04:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr span(const span& other) noexcept = default;
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
template <
|
2020-02-03 13:56:31 -05:00
|
|
|
class OtherElementType, std::size_t OtherExtent,
|
2016-06-12 21:28:19 -04:00
|
|
|
class = std::enable_if_t<
|
2016-07-20 16:17:47 -04:00
|
|
|
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
|
|
|
|
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
|
2016-09-15 01:01:02 -04:00
|
|
|
: storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
|
2018-08-17 14:36:06 -04:00
|
|
|
{}
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
~span() noexcept = default;
|
|
|
|
constexpr span& operator=(const span& other) noexcept = default;
|
2018-05-09 17:01:22 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.sub], span subviews
|
2020-02-03 13:56:31 -05:00
|
|
|
template <std::size_t Count>
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr span<element_type, Count> first() const noexcept
|
2016-03-18 19:49:29 -04:00
|
|
|
{
|
2020-02-03 13:56:31 -05:00
|
|
|
Expects(Count <= size());
|
2016-07-20 16:17:47 -04:00
|
|
|
return {data(), Count};
|
2016-03-18 19:49:29 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <std::size_t Count>
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr span<element_type, Count> last() const noexcept
|
2016-03-18 19:49:29 -04:00
|
|
|
{
|
2020-02-03 13:56:31 -05:00
|
|
|
Expects(size() >= Count);
|
2016-07-20 16:17:47 -04:00
|
|
|
return {data() + (size() - Count), Count};
|
2016-03-18 19:49:29 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <std::size_t Offset, std::size_t Count = dynamic_extent>
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr auto subspan() const noexcept ->
|
2018-08-13 00:44:17 -04:00
|
|
|
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
|
2016-03-18 19:49:29 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
|
2016-03-18 19:49:29 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr span<element_type, dynamic_extent> first(index_type count) const noexcept
|
2016-03-18 19:49:29 -04:00
|
|
|
{
|
2020-02-03 13:56:31 -05:00
|
|
|
Expects(count <= size());
|
2016-07-20 16:17:47 -04:00
|
|
|
return {data(), count};
|
2016-03-18 19:49:29 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr span<element_type, dynamic_extent> last(index_type count) const noexcept
|
2016-03-18 19:49:29 -04:00
|
|
|
{
|
2020-02-03 13:56:31 -05:00
|
|
|
Expects(count <= size());
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
|
2016-03-18 19:49:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr span<element_type, dynamic_extent> subspan(index_type offset,
|
2020-02-03 13:56:31 -05:00
|
|
|
index_type count = dynamic_extent) const
|
|
|
|
noexcept
|
2016-03-18 19:49:29 -04:00
|
|
|
{
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
return make_subspan(offset, count, subspan_selector<Extent>{});
|
2016-03-18 19:49:29 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.obs], span observers
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr index_type size() const noexcept { return storage_.size(); }
|
|
|
|
constexpr index_type size_bytes() const noexcept
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
return size() * narrow_cast<index_type>(sizeof(element_type));
|
|
|
|
}
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr bool empty() const noexcept { return size() == 0; }
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.elem], span element access
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
|
2020-01-08 14:51:15 -05:00
|
|
|
constexpr reference operator[](index_type idx) const noexcept
|
2016-02-25 14:42:26 -05:00
|
|
|
{
|
2020-02-03 21:12:57 -05:00
|
|
|
Expects(idx < size());
|
2016-07-20 12:24:49 -04:00
|
|
|
return data()[idx];
|
2016-02-25 14:42:26 -05:00
|
|
|
}
|
2016-10-26 17:11:24 -04:00
|
|
|
|
2020-01-09 15:13:12 -05:00
|
|
|
constexpr reference front() const noexcept
|
2019-11-19 19:03:55 -05:00
|
|
|
{
|
2020-02-03 16:53:38 -05:00
|
|
|
Expects(size() > 0);
|
2019-11-19 19:03:55 -05:00
|
|
|
return data()[0];
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:13:12 -05:00
|
|
|
constexpr reference back() const noexcept
|
2019-11-19 19:03:55 -05:00
|
|
|
{
|
2020-02-03 16:53:38 -05:00
|
|
|
Expects(size() > 0);
|
2019-11-19 19:03:55 -05:00
|
|
|
return data()[size() - 1];
|
|
|
|
}
|
2020-01-10 13:20:37 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
// 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
|
|
|
|
{
|
|
|
|
return this->operator[](idx);
|
|
|
|
}
|
|
|
|
[[deprecated("Use operator[]")]] constexpr reference operator()(index_type idx) const noexcept
|
|
|
|
{
|
|
|
|
return this->operator[](idx);
|
|
|
|
}
|
2020-01-10 13:20:37 -05:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr pointer data() const noexcept { return storage_.data(); }
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.iter], span iterator support
|
2020-02-03 19:26:36 -05:00
|
|
|
constexpr iterator begin() const noexcept { return {data(), data() + size(), data()}; }
|
|
|
|
constexpr iterator end() const noexcept { return {data(), data() + size(), data() + size()}; }
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2020-02-03 19:26:36 -05:00
|
|
|
constexpr const_iterator cbegin() const noexcept { return {data(), data() + size(), data()}; }
|
2020-02-03 21:13:14 -05:00
|
|
|
constexpr const_iterator cend() const noexcept
|
|
|
|
{
|
|
|
|
return {data(), data() + size(), data() + size()};
|
|
|
|
}
|
2016-07-20 16:17:47 -04:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
|
|
|
|
constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr const_reverse_iterator crbegin() const noexcept
|
|
|
|
{
|
|
|
|
return const_reverse_iterator{cend()};
|
|
|
|
}
|
|
|
|
constexpr const_reverse_iterator crend() const noexcept
|
|
|
|
{
|
|
|
|
return const_reverse_iterator{cbegin()};
|
|
|
|
}
|
2016-06-12 21:28:19 -04:00
|
|
|
|
2018-05-09 17:01:22 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Tell MSVC how to unwrap spans in range-based-for
|
|
|
|
constexpr pointer _Unchecked_begin() const noexcept { return data(); }
|
2018-08-17 14:36:06 -04:00
|
|
|
constexpr pointer _Unchecked_end() const noexcept
|
2018-08-13 00:44:17 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
|
2018-08-13 00:44:17 -04:00
|
|
|
return data() + size();
|
|
|
|
}
|
2018-05-09 17:01:22 -04:00
|
|
|
#endif // _MSC_VER
|
|
|
|
|
2016-07-18 14:38:01 -04:00
|
|
|
private:
|
2018-03-15 15:14:29 -04:00
|
|
|
// Needed to remove unnecessary null check in subspans
|
2018-05-09 17:01:22 -04:00
|
|
|
struct KnownNotNull
|
2018-03-15 15:14:29 -04:00
|
|
|
{
|
|
|
|
pointer p;
|
|
|
|
};
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// this implementation detail class lets us take advantage of the
|
2016-02-28 03:50:53 -05:00
|
|
|
// empty base class optimization to pay for only storage of a single
|
|
|
|
// pointer in the case of fixed-size spans
|
|
|
|
template <class ExtentType>
|
|
|
|
class storage_type : public ExtentType
|
|
|
|
{
|
|
|
|
public:
|
2018-05-09 17:01:22 -04:00
|
|
|
// KnownNotNull parameter is needed to remove unnecessary null check
|
2018-03-15 15:14:29 -04:00
|
|
|
// in subspans and constructors from arrays
|
|
|
|
template <class OtherExtentType>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr storage_type(KnownNotNull data, OtherExtentType ext)
|
|
|
|
: ExtentType(ext), data_(data.p)
|
2018-03-15 15:14:29 -04:00
|
|
|
{
|
2020-02-03 13:56:31 -05:00
|
|
|
Expects(ExtentType::size() != dynamic_extent);
|
2018-03-15 15:14:29 -04:00
|
|
|
}
|
|
|
|
|
2016-02-28 03:50:53 -05:00
|
|
|
template <class OtherExtentType>
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 13:56:31 -05:00
|
|
|
Expects(ExtentType::size() != dynamic_extent);
|
2018-08-13 00:44:17 -04:00
|
|
|
Expects(data || ExtentType::size() == 0);
|
2016-07-20 16:17:47 -04:00
|
|
|
}
|
2016-02-28 03:50:53 -05:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr pointer data() const noexcept { return data_; }
|
2016-02-28 03:50:53 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
pointer data_;
|
|
|
|
};
|
|
|
|
|
2016-07-26 21:34:27 -04:00
|
|
|
storage_type<details::extent_type<Extent>> storage_;
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
|
2018-03-15 15:14:29 -04:00
|
|
|
// 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) {}
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <std::size_t CallerExtent>
|
2018-08-13 00:44:17 -04:00
|
|
|
class subspan_selector
|
|
|
|
{
|
|
|
|
};
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <std::size_t CallerExtent>
|
2018-08-13 00:44:17 -04:00
|
|
|
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
|
2018-03-03 22:12:23 -05:00
|
|
|
subspan_selector<CallerExtent>) const
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<element_type, dynamic_extent> tmp(*this);
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
return tmp.subspan(offset, count);
|
|
|
|
}
|
|
|
|
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute
|
2018-08-13 00:44:17 -04:00
|
|
|
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
|
2018-03-03 22:12:23 -05:00
|
|
|
subspan_selector<dynamic_extent>) const
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
{
|
2020-02-03 19:26:36 -05:00
|
|
|
Expects(size() >= offset);
|
2018-08-13 00:44:17 -04:00
|
|
|
|
|
|
|
if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
Expects(size() - offset >= count);
|
2018-08-13 00:44:17 -04:00
|
|
|
return {KnownNotNull{data() + offset}, count};
|
Minimize checking in subspan (#569)
* rewrite span subspan checks to help optimizations
* Removed checking pointer for null for subspans. We would never
check pointer for null in between ptr and ptr+size in the
original span, so there seems to be no reason to do so for
subspans, provided that the subspan's boundaries are ensured
to be within the range of the original span's boundaries.
This change allows to simplify generated code, for example, to
remove 5 out of 9 branches in code generated from the following
by MSVC, and 4 out 8 branches in clang and gcc-generated code:
span<int> mysubspan(int* p, std::ptrdiff_t size, std::ptrdiff_t i)
{
if (p != nullptr)
{
span<int> s = { p, size };
return s.subspan(i);
}
return { nullptr };
}
Similar effects are achieved for dynamic subspans of static spans,
where in the following code we remove 2 out of 4 branchs in MSVC
and GCC-generated code:
int test_dynamic_subspan_of_static_span(std::ptrdiff_t i)
{
int x[] = { 0,1,2,3,4,5 };
span<int, 6> s = { x };
auto subspan = s.subspan(i);
return subspan.size();
}
2017-11-03 19:13:39 -04:00
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
};
|
|
|
|
|
2018-03-03 22:12:45 -05:00
|
|
|
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2018-03-03 22:12:45 -05:00
|
|
|
constexpr const typename span<ElementType, Extent>::index_type span<ElementType, Extent>::extent;
|
|
|
|
#endif
|
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.comparison], span comparison operators
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return std::equal(l.begin(), l.end(), r.begin(), r.end());
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return !(l == r);
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return !(l > r);
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return r < l;
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
return !(l < r);
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2016-05-29 20:06:29 -04:00
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
// if we only supported compilers with good constexpr support then
|
|
|
|
// this pair of classes could collapse down to a constexpr function
|
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
// we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
|
2016-07-20 16:17:47 -04:00
|
|
|
// constexpr
|
2016-05-29 20:06:29 -04:00
|
|
|
// and so will fail compilation of the template
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
};
|
2016-05-29 20:06:29 -04:00
|
|
|
|
|
|
|
template <class ElementType>
|
2016-07-20 16:17:47 -04:00
|
|
|
struct calculate_byte_size<ElementType, dynamic_extent>
|
2020-02-03 13:56:31 -05:00
|
|
|
: std::integral_constant<std::size_t, dynamic_extent>
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
|
|
|
};
|
2018-08-13 00:44:17 -04:00
|
|
|
} // namespace details
|
2016-05-29 20:06:29 -04:00
|
|
|
|
2016-07-20 16:17:47 -04:00
|
|
|
// [span.objectrep], views of object representation
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2016-07-20 16:17:47 -04:00
|
|
|
span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
|
2018-03-15 15:14:29 -04:00
|
|
|
as_bytes(span<ElementType, Extent> s) noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(type .1) // NO-FORMAT: attribute
|
2016-07-20 16:17:47 -04:00
|
|
|
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
|
|
|
|
}
|
2016-02-24 19:11:33 -05:00
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent,
|
2016-07-20 16:17:47 -04:00
|
|
|
class = std::enable_if_t<!std::is_const<ElementType>::value>>
|
|
|
|
span<byte, details::calculate_byte_size<ElementType, Extent>::value>
|
2018-03-15 15:14:29 -04:00
|
|
|
as_writeable_bytes(span<ElementType, Extent> s) noexcept
|
2016-07-20 16:17:47 -04:00
|
|
|
{
|
2020-02-03 21:13:14 -05:00
|
|
|
GSL_SUPPRESS(type .1) // NO-FORMAT: attribute
|
2016-07-20 16:17:47 -04:00
|
|
|
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-11-16 13:17:04 -05:00
|
|
|
//
|
|
|
|
// make_span() - Utility functions for creating spans
|
|
|
|
//
|
|
|
|
template <class ElementType>
|
2018-08-13 00:44:17 -04:00
|
|
|
constexpr span<ElementType> make_span(ElementType* ptr,
|
|
|
|
typename span<ElementType>::index_type count)
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
return span<ElementType>(ptr, count);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class ElementType>
|
2018-02-23 16:35:36 -05:00
|
|
|
constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
return span<ElementType>(firstElem, lastElem);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-02-13 15:11:45 -05:00
|
|
|
template <class ElementType, std::size_t N>
|
2018-03-15 15:14:29 -04:00
|
|
|
constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
2017-04-25 15:01:49 -04:00
|
|
|
return span<ElementType, N>(arr);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Container>
|
2018-02-23 16:35:36 -05:00
|
|
|
constexpr span<typename Container::value_type> make_span(Container& cont)
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
return span<typename Container::value_type>(cont);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Container>
|
2018-02-23 16:35:36 -05:00
|
|
|
constexpr span<const typename Container::value_type> make_span(const Container& cont)
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
return span<const typename Container::value_type>(cont);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Ptr>
|
2020-02-03 13:56:31 -05:00
|
|
|
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
return span<typename Ptr::element_type>(cont, count);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
|
|
|
template <class Ptr>
|
2018-02-23 16:35:36 -05:00
|
|
|
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
return span<typename Ptr::element_type>(cont);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2016-08-23 04:30:06 -04:00
|
|
|
// Specialization of gsl::at for span
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
2018-02-21 16:33:07 -05:00
|
|
|
constexpr ElementType& at(span<ElementType, Extent> s, index i)
|
2016-08-23 04:30:06 -04:00
|
|
|
{
|
|
|
|
// No bounds checking here because it is done in span::operator[] called below
|
2018-02-21 16:33:07 -05:00
|
|
|
return s[i];
|
2016-08-23 04:30:06 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 07:13:19 -05:00
|
|
|
// [span.obs] Free observer functions
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::index_type
|
|
|
|
ssize(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// [span.iter] Free functions for begin/end functions
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::iterator
|
|
|
|
begin(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.begin();
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent = dynamic_extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::iterator
|
|
|
|
end(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.end();
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::const_iterator
|
|
|
|
cbegin(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.cbegin();
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::const_iterator
|
|
|
|
cend(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.cend();
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::reverse_iterator
|
|
|
|
rbegin(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.rbegin();
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::reverse_iterator
|
|
|
|
rend(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.rend();
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::const_reverse_iterator
|
|
|
|
crbegin(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.crbegin();
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:56:31 -05:00
|
|
|
template <class ElementType, std::size_t Extent>
|
|
|
|
constexpr typename span<ElementType, Extent>::const_reverse_iterator
|
|
|
|
crend(const span<ElementType, Extent>& span) noexcept
|
2019-11-14 07:13:19 -05:00
|
|
|
{
|
|
|
|
return span.crend();
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
} // namespace gsl
|
|
|
|
|
2019-01-15 13:27:34 -05:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
2017-04-20 10:51:37 -04:00
|
|
|
#if _MSC_VER < 1910
|
|
|
|
#undef constexpr
|
|
|
|
#pragma pop_macro("constexpr")
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#endif // _MSC_VER < 1910
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#pragma warning(pop)
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif // _MSC_VER
|
|
|
|
|
2019-01-15 13:27:34 -05:00
|
|
|
#if defined(__GNUC__) && __GNUC__ > 6
|
2018-06-08 14:41:06 -04:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif // __GNUC__ > 6
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif // GSL_SPAN_H
|