Skip to content

Commit

Permalink
Merge pull request #2564 from BioDataAnalysis/fixing_some_iterator_is…
Browse files Browse the repository at this point in the history
…sues

Fixing some iterator issues
  • Loading branch information
JohanMabille authored Aug 21, 2023
2 parents e75ba74 + 427484b commit 5df89c6
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 15 deletions.
8 changes: 4 additions & 4 deletions include/xtensor/xfunctor_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,8 @@ namespace xt
template <layout_type L>
inline auto xfunctor_applier_base<D>::crbegin() const noexcept
{
return xfunctor_iterator<const functor_type, decltype(m_e.template rbegin<L>())>(
m_e.template rbegin<L>(),
return xfunctor_iterator<const functor_type, decltype(m_e.template crbegin<L>())>(
m_e.template crbegin<L>(),
&m_functor
);
}
Expand All @@ -1178,8 +1178,8 @@ namespace xt
template <layout_type L>
inline auto xfunctor_applier_base<D>::crend() const noexcept
{
return xfunctor_iterator<const functor_type, decltype(m_e.template rend<L>())>(
m_e.template rend<L>(),
return xfunctor_iterator<const functor_type, decltype(m_e.template crend<L>())>(
m_e.template crend<L>(),
&m_functor
);
}
Expand Down
72 changes: 61 additions & 11 deletions include/xtensor/xiterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ namespace xt
void stepper_tools<layout_type::row_major>::increment_stepper(S& stepper, IT& index, const ST& shape)
{
using size_type = typename S::size_type;
size_type i = index.size();
const size_type size = index.size();
size_type i = size;
while (i != 0)
{
--i;
Expand All @@ -625,7 +626,19 @@ namespace xt
}
if (i == 0)
{
std::copy(shape.cbegin(), shape.cend(), index.begin());
if (size != size_type(0))
{
std::transform(
shape.cbegin(),
shape.cend() - 1,
index.begin(),
[](const auto& v)
{
return v - 1;
}
);
index[size - 1] = shape[size - 1];
}
stepper.to_end(layout_type::row_major);
}
}
Expand All @@ -640,8 +653,9 @@ namespace xt
)
{
using size_type = typename S::size_type;
size_type i = index.size();
size_type leading_i = index.size() - 1;
const size_type size = index.size();
const size_type leading_i = size - 1;
size_type i = size;
while (i != 0 && n != 0)
{
--i;
Expand Down Expand Up @@ -673,7 +687,19 @@ namespace xt
}
if (i == 0 && n != 0)
{
std::copy(shape.cbegin(), shape.cend(), index.begin());
if (size != size_type(0))
{
std::transform(
shape.cbegin(),
shape.cend() - 1,
index.begin(),
[](const auto& v)
{
return v - 1;
}
);
index[leading_i] = shape[leading_i];
}
stepper.to_end(layout_type::row_major);
}
}
Expand Down Expand Up @@ -760,7 +786,7 @@ namespace xt
void stepper_tools<layout_type::column_major>::increment_stepper(S& stepper, IT& index, const ST& shape)
{
using size_type = typename S::size_type;
size_type size = index.size();
const size_type size = index.size();
size_type i = 0;
while (i != size)
{
Expand All @@ -782,7 +808,19 @@ namespace xt
}
if (i == size)
{
std::copy(shape.cbegin(), shape.cend(), index.begin());
if (size != size_type(0))
{
std::transform(
shape.cbegin() + 1,
shape.cend(),
index.begin() + 1,
[](const auto& v)
{
return v - 1;
}
);
index[0] = shape[0];
}
stepper.to_end(layout_type::column_major);
}
}
Expand All @@ -797,9 +835,9 @@ namespace xt
)
{
using size_type = typename S::size_type;
size_type size = index.size();
const size_type size = index.size();
const size_type leading_i = 0;
size_type i = 0;
size_type leading_i = 0;
while (i != size && n != 0)
{
size_type inc = (i == leading_i) ? n : 1;
Expand All @@ -808,7 +846,7 @@ namespace xt
index[i] += inc;
stepper.step(i, inc);
n -= inc;
if (i != leading_i || index.size() == 1)
if (i != leading_i || size == 1)
{
i = 0;
continue;
Expand All @@ -832,7 +870,19 @@ namespace xt
}
if (i == size && n != 0)
{
std::copy(shape.cbegin(), shape.cend(), index.begin());
if (size != size_type(0))
{
std::transform(
shape.cbegin() + 1,
shape.cend(),
index.begin() + 1,
[](const auto& v)
{
return v - 1;
}
);
index[leading_i] = shape[leading_i];
}
stepper.to_end(layout_type::column_major);
}
}
Expand Down
59 changes: 59 additions & 0 deletions include/xtensor/xstrided_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ namespace xt
using storage_type = typename base_type::storage_type;
using linear_iterator = typename storage_type::iterator;
using const_linear_iterator = typename storage_type::const_iterator;
using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;

using iterable_base = select_iterable_base_t<L, xexpression_type::static_layout, self_type>;
using inner_shape_type = typename base_type::inner_shape_type;
Expand Down Expand Up @@ -217,9 +219,18 @@ namespace xt

linear_iterator linear_begin();
linear_iterator linear_end();
const_linear_iterator linear_begin() const;
const_linear_iterator linear_end() const;
const_linear_iterator linear_cbegin() const;
const_linear_iterator linear_cend() const;

reverse_linear_iterator linear_rbegin();
reverse_linear_iterator linear_rend();
const_reverse_linear_iterator linear_rbegin() const;
const_reverse_linear_iterator linear_rend() const;
const_reverse_linear_iterator linear_crbegin() const;
const_reverse_linear_iterator linear_crend() const;

template <class ST, class STEP = stepper>
disable_indexed_stepper_t<STEP> stepper_begin(const ST& shape);
template <class ST, class STEP = stepper>
Expand Down Expand Up @@ -485,6 +496,18 @@ namespace xt
return this->storage().begin() + static_cast<std::ptrdiff_t>(data_offset() + size());
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_begin() const -> const_linear_iterator
{
return this->linear_cbegin();
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_end() const -> const_linear_iterator
{
return this->linear_cend();
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_cbegin() const -> const_linear_iterator
{
Expand All @@ -497,6 +520,42 @@ namespace xt
return this->storage().cbegin() + static_cast<std::ptrdiff_t>(data_offset() + size());
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() -> reverse_linear_iterator
{
return reverse_linear_iterator(this->linear_begin());
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rend() -> reverse_linear_iterator
{
return reverse_linear_iterator(this->linear_end());
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() const -> const_reverse_linear_iterator
{
return this->linear_crbegin();
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_rend() const -> const_reverse_linear_iterator
{
return this->linear_crend();
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_crbegin() const -> const_reverse_linear_iterator
{
return const_reverse_linear_iterator(this->linear_cbegin());
}

template <class CT, class S, layout_type L, class FST>
inline auto xstrided_view<CT, S, L, FST>::linear_crend() const -> const_reverse_linear_iterator
{
return const_reverse_linear_iterator(this->linear_cend());
}

/***************
* stepper api *
***************/
Expand Down

0 comments on commit 5df89c6

Please sign in to comment.