Skip to content

Commit aed617b

Browse files
committed
Moved pyarray_backstrides to a dedicated file
1 parent dd079a7 commit aed617b

File tree

3 files changed

+380
-289
lines changed

3 files changed

+380
-289
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ message(STATUS "Found numpy: ${NUMPY_INCLUDE_DIRS}")
4646

4747
set(XTENSOR_PYTHON_HEADERS
4848
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyarray.hpp
49+
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyarray_backstrides.hpp
4950
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pycontainer.hpp
5051
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pystrides_adaptor.hpp
5152
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pytensor.hpp

include/xtensor-python/pyarray.hpp

Lines changed: 1 addition & 289 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "xtensor/xiterator.hpp"
1919
#include "xtensor/xsemantic.hpp"
2020

21+
#include "pyarray_backstrides.hpp"
2122
#include "pycontainer.hpp"
2223
#include "pystrides_adaptor.hpp"
2324
#include "xtensor_type_caster_base.hpp"
@@ -100,200 +101,6 @@ namespace pybind11
100101

101102
namespace xt
102103
{
103-
104-
/**************************
105-
* pybackstrides_iterator *
106-
**************************/
107-
108-
template <class B>
109-
class pybackstrides_iterator
110-
{
111-
public:
112-
113-
using self_type = pybackstrides_iterator<B>;
114-
115-
using value_type = typename B::value_type;
116-
using pointer = const value_type*;
117-
using reference = value_type;
118-
using difference_type = std::ptrdiff_t;
119-
using iterator_category = std::random_access_iterator_tag;
120-
121-
inline pybackstrides_iterator(const B* b, std::size_t offset)
122-
: p_b(b), m_offset(offset)
123-
{
124-
}
125-
126-
inline reference operator*() const
127-
{
128-
return p_b->operator[](m_offset);
129-
}
130-
131-
inline pointer operator->() const
132-
{
133-
// Returning the address of a temporary
134-
value_type res = p_b->operator[](m_offset);
135-
return &res;
136-
}
137-
138-
inline reference operator[](difference_type n) const
139-
{
140-
return p_b->operator[](m_offset + n);
141-
}
142-
143-
inline self_type& operator++()
144-
{
145-
++m_offset;
146-
return *this;
147-
}
148-
149-
inline self_type& operator--()
150-
{
151-
--m_offset;
152-
return *this;
153-
}
154-
155-
inline self_type operator++(int)
156-
{
157-
self_type tmp(*this);
158-
++m_offset;
159-
return tmp;
160-
}
161-
162-
inline self_type operator--(int)
163-
{
164-
self_type tmp(*this);
165-
--m_offset;
166-
return tmp;
167-
}
168-
169-
inline self_type& operator+=(difference_type n)
170-
{
171-
m_offset += n;
172-
return *this;
173-
}
174-
175-
inline self_type& operator-=(difference_type n)
176-
{
177-
m_offset -= n;
178-
return *this;
179-
}
180-
181-
inline self_type operator+(difference_type n) const
182-
{
183-
return self_type(p_b, m_offset + n);
184-
}
185-
186-
inline self_type operator-(difference_type n) const
187-
{
188-
return self_type(p_b, m_offset - n);
189-
}
190-
191-
inline self_type operator-(const self_type& rhs) const
192-
{
193-
self_type tmp(*this);
194-
tmp -= (m_offset - rhs.m_offset);
195-
return tmp;
196-
}
197-
198-
inline std::size_t offset() const
199-
{
200-
return m_offset;
201-
}
202-
203-
private:
204-
205-
const B* p_b;
206-
std::size_t m_offset;
207-
};
208-
209-
template <class B>
210-
inline bool operator==(const pybackstrides_iterator<B>& lhs,
211-
const pybackstrides_iterator<B>& rhs)
212-
{
213-
return lhs.offset() == rhs.offset();
214-
}
215-
216-
template <class B>
217-
inline bool operator!=(const pybackstrides_iterator<B>& lhs,
218-
const pybackstrides_iterator<B>& rhs)
219-
{
220-
return !(lhs == rhs);
221-
}
222-
223-
template <class B>
224-
inline bool operator<(const pybackstrides_iterator<B>& lhs,
225-
const pybackstrides_iterator<B>& rhs)
226-
{
227-
return lhs.offset() < rhs.offset();
228-
}
229-
230-
template <class B>
231-
inline bool operator<=(const pybackstrides_iterator<B>& lhs,
232-
const pybackstrides_iterator<B>& rhs)
233-
{
234-
return (lhs < rhs) || (lhs == rhs);
235-
}
236-
237-
template <class B>
238-
inline bool operator>(const pybackstrides_iterator<B>& lhs,
239-
const pybackstrides_iterator<B>& rhs)
240-
{
241-
return !(lhs <= rhs);
242-
}
243-
244-
template <class B>
245-
inline bool operator>=(const pybackstrides_iterator<B>& lhs,
246-
const pybackstrides_iterator<B>& rhs)
247-
{
248-
return !(lhs < rhs);
249-
}
250-
251-
template <class A>
252-
class pyarray_backstrides
253-
{
254-
public:
255-
256-
using self_type = pyarray_backstrides<A>;
257-
using array_type = A;
258-
using value_type = typename array_type::size_type;
259-
using const_reference = value_type;
260-
using reference = const_reference;
261-
using const_pointer = const value_type*;
262-
using pointer = const_pointer;
263-
using size_type = typename array_type::size_type;
264-
using difference_type = typename array_type::difference_type;
265-
266-
using const_iterator = pybackstrides_iterator<self_type>;
267-
using iterator = const_iterator;
268-
using reverse_iterator = std::reverse_iterator<iterator>;
269-
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
270-
271-
pyarray_backstrides() = default;
272-
pyarray_backstrides(const array_type& a);
273-
274-
bool empty() const;
275-
size_type size() const;
276-
277-
value_type operator[](size_type i) const;
278-
279-
const_reference front() const;
280-
const_reference back() const;
281-
282-
const_iterator begin() const;
283-
const_iterator end() const;
284-
const_iterator cbegin() const;
285-
const_iterator cend() const;
286-
287-
const_reverse_iterator rbegin() const;
288-
const_reverse_iterator rend() const;
289-
const_reverse_iterator crbegin() const;
290-
const_reverse_iterator crend() const;
291-
292-
private:
293-
294-
const array_type* p_a;
295-
};
296-
297104
template <class T, layout_type L>
298105
struct xiterable_inner_types<pyarray<T, L>>
299106
: xcontainer_iterable_types<pyarray<T, L>>
@@ -425,101 +232,6 @@ namespace xt
425232
friend class pycontainer<pyarray<T, L>>;
426233
};
427234

428-
/**************************************
429-
* pyarray_backstrides implementation *
430-
**************************************/
431-
432-
template <class A>
433-
inline pyarray_backstrides<A>::pyarray_backstrides(const array_type& a)
434-
: p_a(&a)
435-
{
436-
}
437-
438-
template <class A>
439-
inline bool pyarray_backstrides<A>::empty() const
440-
{
441-
return p_a->dimension() == 0;
442-
}
443-
444-
template <class A>
445-
inline auto pyarray_backstrides<A>::size() const -> size_type
446-
{
447-
return p_a->dimension();
448-
}
449-
450-
template <class A>
451-
inline auto pyarray_backstrides<A>::operator[](size_type i) const -> value_type
452-
{
453-
value_type sh = p_a->shape()[i];
454-
value_type res = sh == 1 ? 0 : (sh - 1) * p_a->strides()[i];
455-
return res;
456-
}
457-
458-
template <class A>
459-
inline auto pyarray_backstrides<A>::front() const -> const_reference
460-
{
461-
value_type sh = p_a->shape()[0];
462-
value_type res = sh == 1 ? 0 : (sh - 1) * p_a->strides()[0];
463-
return res;
464-
}
465-
466-
template <class A>
467-
inline auto pyarray_backstrides<A>::back() const -> const_reference
468-
{
469-
auto index = p_a->size() - 1;
470-
value_type sh = p_a->shape()[index];
471-
value_type res = sh == 1 ? 0 : (sh - 1) * p_a->strides()[index];
472-
return res;
473-
}
474-
475-
template <class A>
476-
inline auto pyarray_backstrides<A>::begin() const -> const_iterator
477-
{
478-
return cbegin();
479-
}
480-
481-
template <class A>
482-
inline auto pyarray_backstrides<A>::end() const -> const_iterator
483-
{
484-
return cend();
485-
}
486-
487-
template <class A>
488-
inline auto pyarray_backstrides<A>::cbegin() const -> const_iterator
489-
{
490-
return const_iterator(this, 0);
491-
}
492-
493-
template <class A>
494-
inline auto pyarray_backstrides<A>::cend() const -> const_iterator
495-
{
496-
return const_iterator(this, size());
497-
}
498-
499-
template <class A>
500-
inline auto pyarray_backstrides<A>::rbegin() const -> const_reverse_iterator
501-
{
502-
return crbegin();
503-
}
504-
505-
template <class A>
506-
inline auto pyarray_backstrides<A>::rend() const -> const_reverse_iterator
507-
{
508-
return crend();
509-
}
510-
511-
template <class A>
512-
inline auto pyarray_backstrides<A>::crbegin() const -> const_reverse_iterator
513-
{
514-
return const_reverse_iterator(end());
515-
}
516-
517-
template <class A>
518-
inline auto pyarray_backstrides<A>::crend() const -> const_reverse_iterator
519-
{
520-
return const_reverse_iterator(begin());
521-
}
522-
523235
/**************************
524236
* pyarray implementation *
525237
**************************/

0 commit comments

Comments
 (0)