add gsl::index typedef (#620)

* rename index in multi_span to span_index

gsl::index is name reserved for different type

* add gsl::index typedef

cppcoreguidelines referece: ES.107: Don't use unsigned for subscripts, prefer gsl::index

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-subscripts

* rename span_index to multi_span_index
This commit is contained in:
paweldac authored and Neil MacIntosh committed 2018-02-21 13:33:07 -08:00
1 parent 73db6ef98f
commit b3870ca020
6 files changed
+137 -127

No files matched your search

+12 -9
View File
@@ -43,6 +43,9 @@ namespace gsl
// GSL.util: utilities
//
// index type for all container indexes/subscripts/sizes
using index = std::ptrdiff_t;
// final_action allows you to ensure something gets run at the end of a scope
template <class F>
class final_action
@@ -117,25 +120,25 @@ T narrow(U u)
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
//
template <class T, std::size_t N>
constexpr T& at(T (&arr)[N], const std::ptrdiff_t index)
constexpr T& at(T (&arr)[N], const index i)
{
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(N));
return arr[static_cast<std::size_t>(index)];
Expects(i >= 0 && i < narrow_cast<index>(N));
return arr[static_cast<std::size_t>(i)];
}
template <class Cont>
constexpr auto at(Cont& cont, const std::ptrdiff_t index) -> decltype(cont[cont.size()])
constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
{
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(cont.size()));
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
using size_type = decltype(cont.size());
return cont[static_cast<size_type>(index)];
return cont[static_cast<size_type>(i)];
}
template <class T>
constexpr T at(const std::initializer_list<T> cont, const std::ptrdiff_t index)
constexpr T at(const std::initializer_list<T> cont, const index i)
{
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(cont.size()));
return *(cont.begin() + index);
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
return *(cont.begin() + i);
}
} // namespace gsl
+35 -35
View File
@@ -85,12 +85,12 @@ namespace details
}
template <std::size_t Rank>
class index final
class multi_span_index final
{
static_assert(Rank > 0, "Rank must be greater than 0!");
template <std::size_t OtherRank>
friend class index;
friend class multi_span_index;
public:
static const std::size_t rank = Rank;
@@ -99,22 +99,22 @@ public:
using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
constexpr index() GSL_NOEXCEPT {}
constexpr multi_span_index() GSL_NOEXCEPT {}
constexpr index(const value_type (&values)[Rank]) GSL_NOEXCEPT
constexpr multi_span_index(const value_type (&values)[Rank]) GSL_NOEXCEPT
{
std::copy(values, values + Rank, elems);
}
template <typename... Ts, typename = std::enable_if_t<(sizeof...(Ts) == Rank) &&
details::are_integral<Ts...>::value>>
constexpr index(Ts... ds) GSL_NOEXCEPT : elems{narrow_cast<value_type>(ds)...}
constexpr multi_span_index(Ts... ds) GSL_NOEXCEPT : elems{narrow_cast<value_type>(ds)...}
{
}
constexpr index(const index& other) GSL_NOEXCEPT = default;
constexpr multi_span_index(const multi_span_index& other) GSL_NOEXCEPT = default;
constexpr index& operator=(const index& rhs) GSL_NOEXCEPT = default;
constexpr multi_span_index& operator=(const multi_span_index& rhs) GSL_NOEXCEPT = default;
// Preconditions: component_idx < rank
constexpr reference operator[](std::size_t component_idx)
@@ -130,75 +130,75 @@ public:
return elems[component_idx];
}
constexpr bool operator==(const index& rhs) const GSL_NOEXCEPT
constexpr bool operator==(const multi_span_index& rhs) const GSL_NOEXCEPT
{
return std::equal(elems, elems + rank, rhs.elems);
}
constexpr bool operator!=(const index& rhs) const GSL_NOEXCEPT { return !(*this == rhs); }
constexpr bool operator!=(const multi_span_index& rhs) const GSL_NOEXCEPT { return !(*this == rhs); }
constexpr index operator+() const GSL_NOEXCEPT { return *this; }
constexpr multi_span_index operator+() const GSL_NOEXCEPT { return *this; }
constexpr index operator-() const GSL_NOEXCEPT
constexpr multi_span_index operator-() const GSL_NOEXCEPT
{
index ret = *this;
multi_span_index ret = *this;
std::transform(ret, ret + rank, ret, std::negate<value_type>{});
return ret;
}
constexpr index operator+(const index& rhs) const GSL_NOEXCEPT
constexpr multi_span_index operator+(const multi_span_index& rhs) const GSL_NOEXCEPT
{
index ret = *this;
multi_span_index ret = *this;
ret += rhs;
return ret;
}
constexpr index operator-(const index& rhs) const GSL_NOEXCEPT
constexpr multi_span_index operator-(const multi_span_index& rhs) const GSL_NOEXCEPT
{
index ret = *this;
multi_span_index ret = *this;
ret -= rhs;
return ret;
}
constexpr index& operator+=(const index& rhs) GSL_NOEXCEPT
constexpr multi_span_index& operator+=(const multi_span_index& rhs) GSL_NOEXCEPT
{
std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
return *this;
}
constexpr index& operator-=(const index& rhs) GSL_NOEXCEPT
constexpr multi_span_index& operator-=(const multi_span_index& rhs) GSL_NOEXCEPT
{
std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
return *this;
}
constexpr index operator*(value_type v) const GSL_NOEXCEPT
constexpr multi_span_index operator*(value_type v) const GSL_NOEXCEPT
{
index ret = *this;
multi_span_index ret = *this;
ret *= v;
return ret;
}
constexpr index operator/(value_type v) const GSL_NOEXCEPT
constexpr multi_span_index operator/(value_type v) const GSL_NOEXCEPT
{
index ret = *this;
multi_span_index ret = *this;
ret /= v;
return ret;
}
friend constexpr index operator*(value_type v, const index& rhs) GSL_NOEXCEPT
friend constexpr multi_span_index operator*(value_type v, const multi_span_index& rhs) GSL_NOEXCEPT
{
return rhs * v;
}
constexpr index& operator*=(value_type v) GSL_NOEXCEPT
constexpr multi_span_index& operator*=(value_type v) GSL_NOEXCEPT
{
std::transform(elems, elems + rank, elems,
[v](value_type x) { return std::multiplies<value_type>{}(x, v); });
return *this;
}
constexpr index& operator/=(value_type v) GSL_NOEXCEPT
constexpr multi_span_index& operator/=(value_type v) GSL_NOEXCEPT
{
std::transform(elems, elems + rank, elems,
[v](value_type x) { return std::divides<value_type>{}(x, v); });
@@ -497,8 +497,8 @@ namespace details
}
template <std::size_t Rank, bool Enabled = (Rank > 1),
typename Ret = std::enable_if_t<Enabled, index<Rank - 1>>>
constexpr Ret shift_left(const index<Rank>& other) GSL_NOEXCEPT
typename Ret = std::enable_if_t<Enabled, multi_span_index<Rank - 1>>>
constexpr Ret shift_left(const multi_span_index<Rank>& other) GSL_NOEXCEPT
{
Ret ret{};
for (std::size_t i = 0; i < Rank - 1; ++i) {
@@ -535,7 +535,7 @@ public:
static const std::ptrdiff_t static_size = MyRanges::TotalSize;
using size_type = std::ptrdiff_t;
using index_type = index<rank>;
using index_type = multi_span_index<rank>;
using const_index_type = std::add_const_t<index_type>;
using iterator = bounds_iterator<const_index_type>;
using const_iterator = bounds_iterator<const_index_type>;
@@ -631,9 +631,9 @@ public:
return m_ranges.contains(idx) != -1;
}
constexpr size_type operator[](std::size_t index) const GSL_NOEXCEPT
constexpr size_type operator[](std::size_t idx) const GSL_NOEXCEPT
{
return m_ranges.elementNum(index);
return m_ranges.elementNum(idx);
}
template <std::size_t Dim = 0>
@@ -698,7 +698,7 @@ public:
using const_reference = std::add_const_t<reference>;
using size_type = value_type;
using difference_type = value_type;
using index_type = index<rank>;
using index_type = multi_span_index<rank>;
using const_index_type = std::add_const_t<index_type>;
using iterator = bounds_iterator<const_index_type>;
using const_iterator = bounds_iterator<const_index_type>;
@@ -1478,15 +1478,15 @@ public:
constexpr pointer data() const GSL_NOEXCEPT { return data_; }
template <typename FirstIndex>
constexpr reference operator()(FirstIndex index)
constexpr reference operator()(FirstIndex idx)
{
return this->operator[](narrow_cast<std::ptrdiff_t>(index));
return this->operator[](narrow_cast<std::ptrdiff_t>(idx));
}
template <typename FirstIndex, typename... OtherIndices>
constexpr reference operator()(FirstIndex index, OtherIndices... indices)
constexpr reference operator()(FirstIndex firstIndex, OtherIndices... indices)
{
index_type idx = {narrow_cast<std::ptrdiff_t>(index),
index_type idx = {narrow_cast<std::ptrdiff_t>(firstIndex),
narrow_cast<std::ptrdiff_t>(indices)...};
return this->operator[](idx);
}
+5 -5
View File
@@ -125,10 +125,10 @@ namespace details
span_iterator() = default;
constexpr span_iterator(const Span* span, typename Span::index_type index) GSL_NOEXCEPT
: span_(span), index_(index)
constexpr span_iterator(const Span* span, typename Span::index_type idx) GSL_NOEXCEPT
: span_(span), index_(idx)
{
Expects(span == nullptr || (0 <= index_ && index <= span_->size()));
Expects(span == nullptr || (0 <= index_ && index_ <= span_->size()));
}
friend span_iterator<Span, true>;
@@ -662,10 +662,10 @@ span<typename Ptr::element_type> make_span(Ptr& cont)
// Specialization of gsl::at for span
template <class ElementType, std::ptrdiff_t Extent>
constexpr ElementType& at(span<ElementType, Extent> s, std::ptrdiff_t index)
constexpr ElementType& at(span<ElementType, Extent> s, index i)
{
// No bounds checking here because it is done in span::operator[] called below
return s[index];
return s[i];
}
} // namespace gsl