view: add more assertions in slicing code

This commit is contained in:
Danny Robson 2019-01-20 17:58:05 +11:00
parent f7c477b163
commit f1cf6933a9

View File

@ -83,11 +83,10 @@ namespace cruft {
typename CountT, typename CountT,
typename = std::enable_if_t<std::is_integral_v<CountT>,void> typename = std::enable_if_t<std::is_integral_v<CountT>,void>
> >
view ( constexpr view (
const BeginT &_begin, const BeginT &_begin,
CountT _size CountT _size
): ) : view (_begin, _begin + _size)
view (_begin, _begin + _size)
{ ; } { ; }
@ -283,6 +282,9 @@ namespace cruft {
> >
split (BeginT pos) const split (BeginT pos) const
{ {
CHECK_GE (pos, m_begin);
CHECK_LE (pos, m_end );
return { return {
{ m_begin, pos }, { m_begin, pos },
{ pos, m_end } { pos, m_end }
@ -298,6 +300,8 @@ namespace cruft {
[[nodiscard]] constexpr auto [[nodiscard]] constexpr auto
split (IndexT idx) const split (IndexT idx) const
{ {
CHECK_LE (idx, size ());
auto last = m_begin; auto last = m_begin;
std::advance (last, idx); std::advance (last, idx);
return split (last); return split (last);
@ -310,9 +314,13 @@ namespace cruft {
// "abc".slice(0, -1) == "abc" // "abc".slice(0, -1) == "abc"
// "abc".slice(0, -2) == "ab" // "abc".slice(0, -2) == "ab"
template <typename IndexA, typename IndexB> template <typename IndexA, typename IndexB>
[[nodiscard]]constexpr auto [[nodiscard]] constexpr
auto
slice (IndexA a, IndexB b) const slice (IndexA a, IndexB b) const
{ {
CHECK_LIMIT (cruft::abs (a), IndexA {0}, cruft::cast::lossless<IndexA> (size ()));
CHECK_LIMIT (cruft::abs (b), IndexB {0}, cruft::cast::lossless<IndexB> (size ()));
auto first = m_begin; auto first = m_begin;
auto last = m_begin; auto last = m_begin;
@ -328,8 +336,8 @@ namespace cruft {
typename IndexT, typename IndexT,
typename = std::enable_if_t<std::is_integral_v<IndexT>> typename = std::enable_if_t<std::is_integral_v<IndexT>>
> >
constexpr auto [[nodiscard]] constexpr auto
head (IndexT idx) head (IndexT idx) const
{ {
return std::get<0> (split (idx)); return std::get<0> (split (idx));
} }
@ -340,19 +348,7 @@ namespace cruft {
typename IndexT, typename IndexT,
typename = std::enable_if_t<std::is_integral_v<IndexT>> typename = std::enable_if_t<std::is_integral_v<IndexT>>
> >
constexpr auto [[nodiscard]] constexpr auto
tail (IndexT idx)
{
return std::get<1> (split (idx));
}
//---------------------------------------------------------------------
template <
typename IndexT,
typename = std::enable_if_t<std::is_integral_v<IndexT>>
>
constexpr auto
tail (IndexT idx) const tail (IndexT idx) const
{ {
return std::get<1> (split (idx)); return std::get<1> (split (idx));