Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 210 additions & 1 deletion include/xtensor-python/pyarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,183 @@ namespace pybind11
namespace xt
{

/**************************
* pybackstrides_iterator *
**************************/

template <class B>
class pybackstrides_iterator
{
public:

using self_type = pybackstrides_iterator<B>;

using value_type = typename B::value_type;
using pointer = const value_type*;
using reference = value_type;
using difference_type = std::ptrdiff_t;
using iterator_category = std::random_access_iterator_tag;

inline pybackstrides_iterator(const B* b, std::size_t offset)
: p_b(b), m_offset(offset)
{
}

inline reference operator*() const
{
return p_b->operator[](m_offset);
}

inline pointer operator->() const
{
// Returning the address of a temporary
value_type res = p_b->operator[](m_offset);
return &res;
}

inline reference operator[](difference_type n) const
{
return p_b->operator[](m_offset + n);
}

inline self_type& operator++()
{
++m_offset;
return *this;
}

inline self_type& operator--()
{
--m_offset;
return *this;
}

inline self_type operator++(int)
{
self_type tmp(*this);
++m_offset;
return tmp;
}

inline self_type operator--(int)
{
self_type tmp(*this);
--m_offset;
return tmp;
}

inline self_type& operator+=(difference_type n)
{
m_offset += n;
return *this;
}

inline self_type& operator-=(difference_type n)
{
m_offset -= n;
return *this;
}

inline self_type operator+(difference_type n) const
{
return self_type(p_b, m_offset + n);
}

inline self_type operator-(difference_type n) const
{
return self_type(p_b, m_offset - n);
}

inline self_type operator-(const self_type& rhs) const
{
self_type tmp(*this);
tmp -= (m_offset - rhs.m_offset);
return tmp;
}

inline std::size_t offset() const
{
return m_offset;
}

private:

const B* p_b;
std::size_t m_offset;
};

template <class B>
inline bool operator==(const pybackstrides_iterator<B>& lhs,
const pybackstrides_iterator<B>& rhs)
{
return lhs.offset() == rhs.offset();
}

template <class B>
inline bool operator!=(const pybackstrides_iterator<B>& lhs,
const pybackstrides_iterator<B>& rhs)
{
return !(lhs == rhs);
}

template <class B>
inline bool operator<(const pybackstrides_iterator<B>& lhs,
const pybackstrides_iterator<B>& rhs)
{
return lhs.offset() < rhs.offset();
}

template <class B>
inline bool operator<=(const pybackstrides_iterator<B>& lhs,
const pybackstrides_iterator<B>& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}

template <class B>
inline bool operator>(const pybackstrides_iterator<B>& lhs,
const pybackstrides_iterator<B>& rhs)
{
return !(lhs <= rhs);
}

template <class B>
inline bool operator>=(const pybackstrides_iterator<B>& lhs,
const pybackstrides_iterator<B>& rhs)
{
return !(lhs < rhs);
}

template <class A>
class pyarray_backstrides
{
public:

using self_type = pyarray_backstrides<A>;
using array_type = A;
using value_type = typename array_type::size_type;
using const_reference = value_type;
using const_pointer = const value_type*;
using size_type = typename array_type::size_type;
using difference_type = typename array_type::difference_type;

using const_iterator = pybackstrides_iterator<self_type>;

pyarray_backstrides() = default;
pyarray_backstrides(const array_type& a);

bool empty() const;
size_type size() const;

value_type operator[](size_type i) const;

size_type size() const;
const_reference front() const;
const_reference back() const;

const_iterator begin() const;
const_iterator end() const;
const_iterator cbegin() const;
const_iterator cend() const;

private:

Expand Down Expand Up @@ -213,6 +375,12 @@ namespace xt
{
}

template <class A>
inline bool pyarray_backstrides<A>::empty() const
{
return p_a->dimension() == 0;
}

template <class A>
inline auto pyarray_backstrides<A>::size() const -> size_type
{
Expand All @@ -227,6 +395,47 @@ namespace xt
return res;
}

template <class A>
inline auto pyarray_backstrides<A>::front() const -> const_reference
{
value_type sh = p_a->shape()[0];
value_type res = sh == 1 ? 0 : (sh - 1) * p_a->strides()[0];
return res;
}

template <class A>
inline auto pyarray_backstrides<A>::back() const -> const_reference
{
auto index = p_a->size() - 1;
value_type sh = p_a->shape()[index];
value_type res = sh == 1 ? 0 : (sh - 1) * p_a->strides()[index];
return res;
}

template <class A>
inline auto pyarray_backstrides<A>::begin() const -> const_iterator
{
return cbegin();
}

template <class A>
inline auto pyarray_backstrides<A>::end() const -> const_iterator
{
return cend();
}

template <class A>
inline auto pyarray_backstrides<A>::cbegin() const -> const_iterator
{
const_iterator(this, 0);
}

template <class A>
inline auto pyarray_backstrides<A>::cend() const -> const_iterator
{
const_iterator(this, size());
}

/**************************
* pyarray implementation *
**************************/
Expand Down
51 changes: 38 additions & 13 deletions include/xtensor-python/pystrides_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace xt
template <std::size_t N>
class pystrides_iterator;

/*********************************
* pystrides_adaptor declaration *
*********************************/

template <std::size_t N>
class pystrides_adaptor
{
Expand Down Expand Up @@ -53,14 +57,13 @@ namespace xt
size_type m_size;
};

/*************************************
* pystrides_iterator implementation *
*************************************/
/**********************************
* pystrides_iterator declaration *
**********************************/

template <std::size_t N>
class pystrides_iterator
{

public:

using self_type = pystrides_iterator<N>;
Expand All @@ -76,16 +79,29 @@ namespace xt
{
}

inline reference operator*() const { return *p_current / N; }
inline pointer operator->() const { return p_current; }
inline reference operator*() const
{
return *p_current / N;
}

inline reference operator[](difference_type n) const { return *(p_current + n) / N; }
inline pointer operator->() const
{
// Returning the address of a temporary
value_type res = *p_current / N;
return &res;
}

inline reference operator[](difference_type n) const
{
return *(p_current + n) / N;
}

inline self_type& operator++()
{
++p_current;
return *this;
}

inline self_type& operator--()
{
--p_current;
Expand All @@ -110,14 +126,23 @@ namespace xt
p_current += n;
return *this;
}

inline self_type& operator-=(difference_type n)
{
p_current -= n;
return *this;
}

inline self_type operator+(difference_type n) const { return self_type(p_current + n); }
inline self_type operator-(difference_type n) const { return self_type(p_current - n); }
inline self_type operator+(difference_type n) const
{
return self_type(p_current + n);
}

inline self_type operator-(difference_type n) const
{
return self_type(p_current - n);
}

inline self_type operator-(const self_type& rhs) const
{
self_type tmp(*this);
Expand Down Expand Up @@ -217,25 +242,25 @@ namespace xt
template <std::size_t N>
inline auto pystrides_adaptor<N>::begin() const -> const_iterator
{
return const_iterator(p_data);
return cbegin();
}

template <std::size_t N>
inline auto pystrides_adaptor<N>::end() const -> const_iterator
{
return const_iterator(p_data + m_size);
return cend();
}

template <std::size_t N>
inline auto pystrides_adaptor<N>::cbegin() const -> const_iterator
{
return begin();
return const_iterator(p_data);
}

template <std::size_t N>
inline auto pystrides_adaptor<N>::cend() const -> const_iterator
{
return end();
return const_iterator(p_data + m_size);
}
}

Expand Down