#ifndef BOOST_COMPUTE_ITERATOR_STRIDED_ITERATOR_HPP
#define BOOST_COMPUTE_ITERATOR_STRIDED_ITERATOR_HPP
#include <cstddef>
#include <iterator>
#include <boost/config.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/compute/functional.hpp>
#include <boost/compute/detail/meta_kernel.hpp>
#include <boost/compute/detail/is_buffer_iterator.hpp>
#include <boost/compute/detail/read_write_single_value.hpp>
#include <boost/compute/iterator/detail/get_base_iterator_buffer.hpp>
#include <boost/compute/type_traits/is_device_iterator.hpp>
#include <boost/compute/type_traits/result_of.hpp>
namespace boost {
namespace compute {
template<class Iterator>
class strided_iterator;
namespace detail {
template<class Iterator>
class strided_iterator_base
{
public:
typedef ::boost::iterator_adaptor<
::boost::compute::strided_iterator<Iterator>,
Iterator,
typename std::iterator_traits<Iterator>::value_type,
typename std::iterator_traits<Iterator>::iterator_category
> type;
};
template<class IndexExpr, class Stride>
struct stride_expr
{
stride_expr(const IndexExpr &expr, const Stride &stride)
: m_index_expr(expr),
m_stride(stride)
{
}
const IndexExpr m_index_expr;
const Stride m_stride;
};
template<class IndexExpr, class Stride>
inline stride_expr<IndexExpr, Stride> make_stride_expr(const IndexExpr &expr,
const Stride &stride)
{
return stride_expr<IndexExpr, Stride>(expr, stride);
}
template<class IndexExpr, class Stride>
inline meta_kernel& operator<<(meta_kernel &kernel,
const stride_expr<IndexExpr, Stride> &expr)
{
return kernel << "(" << static_cast<ulong_>(expr.m_stride)
<< " * (" << expr.m_index_expr << "))";
}
template<class Iterator, class Stride, class IndexExpr>
struct strided_iterator_index_expr
{
typedef typename std::iterator_traits<Iterator>::value_type result_type;
strided_iterator_index_expr(const Iterator &input_iter,
const Stride &stride,
const IndexExpr &index_expr)
: m_input_iter(input_iter),
m_stride(stride),
m_index_expr(index_expr)
{
}
const Iterator m_input_iter;
const Stride m_stride;
const IndexExpr m_index_expr;
};
template<class Iterator, class Stride, class IndexExpr>
inline meta_kernel& operator<<(meta_kernel &kernel,
const strided_iterator_index_expr<Iterator,
Stride,
IndexExpr> &expr)
{
return kernel << expr.m_input_iter[make_stride_expr(expr.m_index_expr, expr.m_stride)];
}
}
template<class Iterator>
class strided_iterator :
public detail::strided_iterator_base<Iterator>::type
{
public:
typedef typename
detail::strided_iterator_base<Iterator>::type super_type;
typedef typename super_type::value_type value_type;
typedef typename super_type::reference reference;
typedef typename super_type::base_type base_type;
typedef typename super_type::difference_type difference_type;
strided_iterator(Iterator iterator, difference_type stride)
: super_type(iterator),
m_stride(static_cast<difference_type>(stride))
{
BOOST_ASSERT_MSG(stride > 0, "Stride must be greater than zero");
}
strided_iterator(const strided_iterator<Iterator> &other)
: super_type(other.base()),
m_stride(other.m_stride)
{
}
strided_iterator<Iterator>&
operator=(const strided_iterator<Iterator> &other)
{
if(this != &other){
super_type::operator=(other);
m_stride = other.m_stride;
}
return *this;
}
~strided_iterator()
{
}
size_t get_index() const
{
return super_type::base().get_index();
}
const buffer& get_buffer() const
{
return detail::get_base_iterator_buffer(*this);
}
template<class IndexExpression>
detail::strided_iterator_index_expr<Iterator, difference_type, IndexExpression>
operator[](const IndexExpression &expr) const
{
typedef
typename detail::strided_iterator_index_expr<Iterator,
difference_type,
IndexExpression>
StridedIndexExprType;
return StridedIndexExprType(super_type::base(),m_stride, expr);
}
private:
friend class ::boost::iterator_core_access;
reference dereference() const
{
return reference();
}
bool equal(const strided_iterator<Iterator> &other) const
{
return (other.m_stride == m_stride)
&& (other.base_reference() == this->base_reference());
}
void increment()
{
std::advance(super_type::base_reference(), m_stride);
}
void decrement()
{
std::advance(super_type::base_reference(),-m_stride);
}
void advance(typename super_type::difference_type n)
{
std::advance(super_type::base_reference(), n * m_stride);
}
difference_type distance_to(const strided_iterator<Iterator> &other) const
{
return std::distance(this->base_reference(), other.base_reference()) / m_stride;
}
private:
difference_type m_stride;
};
template<class Iterator>
inline strided_iterator<Iterator>
make_strided_iterator(Iterator iterator,
typename std::iterator_traits<Iterator>::difference_type stride)
{
return strided_iterator<Iterator>(iterator, stride);
}
template<class Iterator>
strided_iterator<Iterator>
make_strided_iterator_end(Iterator first,
Iterator last,
typename std::iterator_traits<Iterator>::difference_type stride)
{
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
difference_type range = std::distance(first, last);
difference_type d = (range - 1) / stride;
d *= stride;
d -= range;
Iterator end_for_strided_iterator = last;
std::advance(end_for_strided_iterator, d + stride);
return strided_iterator<Iterator>(end_for_strided_iterator, stride);
}
template<class Iterator>
struct is_device_iterator<strided_iterator<Iterator> > : boost::true_type {};
} }
#endif