Skip to content

Commit 996e65c

Browse files
Merge pull request #42 from JohanMabille/refactoring
pycontainer refactored
2 parents db4bbad + 6e73b0a commit 996e65c

File tree

6 files changed

+27
-244
lines changed

6 files changed

+27
-244
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ install:
2424
- conda info -a
2525
- conda install pytest -c conda-forge
2626
- cd test
27-
- conda install xtensor==0.7.1 pytest numpy pybind11==2.0.1 -c conda-forge
27+
- conda install xtensor==0.7.2 pytest numpy pybind11==2.0.1 -c conda-forge
2828
- xcopy /S %APPVEYOR_BUILD_FOLDER%\include %MINICONDA%\include
2929

3030
build_script:

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ install:
5959
# Useful for debugging any issues with conda
6060
- conda info -a
6161
- cd test
62-
- conda install xtensor==0.7.1 pytest numpy pybind11==2.0.1 -c conda-forge
62+
- conda install xtensor==0.7.2 pytest numpy pybind11==2.0.1 -c conda-forge
6363
- cp -r $TRAVIS_BUILD_DIR/include/* $HOME/miniconda/include/
6464

6565
script:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ conda install -c conda-forge xtensor-python
3030

3131
| `xtensor-python` | `xtensor` | `pybind11` |
3232
|-------------------|------------|-------------|
33-
| master | 0.7.1 | ^2.0.0 |
33+
| master | 0.7.2 | ^2.0.0 |
3434
| 0.6.1 | 0.7.1 | ^2.0.0 |
3535
| 0.6.0 | 0.7.1 | ^2.0.0 |
3636
| 0.5.0 | 0.5.0 | ^2.0.0 |

include/xtensor-python/pyarray.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace xt
8383

8484
template <class T>
8585
struct xiterable_inner_types<pyarray<T>>
86-
: pycontainer_iterable_types<pyarray<T>>
86+
: xcontainer_iterable_types<pyarray<T>>
8787
{
8888
};
8989

@@ -96,6 +96,7 @@ namespace xt
9696
using backstrides_type = pyarray_backstrides<pyarray<T>>;
9797
using inner_shape_type = pybuffer_adaptor<std::size_t>;
9898
using inner_strides_type = pystrides_adaptor<sizeof(T)>;
99+
using inner_backstrides_type = backstrides_type;
99100
using temporary_type = pyarray<T>;
100101
};
101102

@@ -122,6 +123,7 @@ namespace xt
122123
using backstrides_type = typename base_type::backstrides_type;
123124
using inner_shape_type = typename base_type::inner_shape_type;
124125
using inner_strides_type = typename base_type::inner_strides_type;
126+
using inner_backstrides_type = typename base_type::inner_backstrides_type;
125127

126128
pyarray() = default;
127129
pyarray(const value_type& t);
@@ -154,28 +156,28 @@ namespace xt
154156

155157
inner_shape_type m_shape;
156158
inner_strides_type m_strides;
157-
mutable backstrides_type m_backstrides;
159+
mutable inner_backstrides_type m_backstrides;
158160
container_type m_data;
159161

160162
void init_array(const shape_type& shape, const strides_type& strides);
161163
void init_from_python();
162164

163165
const inner_shape_type& shape_impl() const;
164166
const inner_strides_type& strides_impl() const;
165-
const backstrides_type& backstrides_impl() const;
167+
const inner_backstrides_type& backstrides_impl() const;
166168

167169
container_type& data_impl();
168170
const container_type& data_impl() const;
169171

170-
friend class pycontainer<pyarray<T>>;
172+
friend class xcontainer<pyarray<T>>;
171173
};
172174

173175
/**************************************
174176
* pyarray_backstrides implementation *
175177
**************************************/
176178

177179
template <class A>
178-
inline pyarray_backstrides<A>::pyarray_backstrides(const A& a)
180+
inline pyarray_backstrides<A>::pyarray_backstrides(const array_type& a)
179181
: p_a(&a)
180182
{
181183
}
@@ -349,10 +351,10 @@ namespace xt
349351
}
350352

351353
template <class T>
352-
inline auto pyarray<T>::backstrides_impl() const -> const backstrides_type&
354+
inline auto pyarray<T>::backstrides_impl() const -> const inner_backstrides_type&
353355
{
354-
// The pyarray objet may be copied, invalidating m_backstrides. Building
355-
// it each time it isasked avoids tricky bugs.
356+
// The pyarray object may be copied, invalidating m_backstrides. Building
357+
// it each time it is needed avoids tricky bugs.
356358
m_backstrides = backstrides_type(*this);
357359
return m_backstrides;
358360
}

include/xtensor-python/pycontainer.hpp

Lines changed: 7 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ namespace xt
2323
{
2424

2525
template <class D>
26-
class pycontainer : public pybind11::object, public xiterable<D>
26+
class pycontainer : public pybind11::object,
27+
public xcontainer<D>
2728
{
2829

2930
public:
3031

3132
using derived_type = D;
3233

34+
using base_type = xcontainer<D>;
3335
using inner_types = xcontainer_inner_types<D>;
3436
using container_type = typename inner_types::container_type;
3537
using value_type = typename container_type::value_type;
@@ -57,58 +59,13 @@ namespace xt
5759
using broadcast_iterator = typename iterable_base::broadcast_iterator;
5860
using const_broadcast_iterator = typename iterable_base::broadcast_iterator;
5961

60-
size_type size() const;
61-
size_type dimension() const;
62-
63-
const inner_shape_type& shape() const;
64-
const inner_strides_type& strides() const;
65-
const backstrides_type& backstrides() const;
66-
6762
void reshape(const shape_type& shape);
6863
void reshape(const shape_type& shape, layout l);
6964
void reshape(const shape_type& shape, const strides_type& strides);
7065

71-
template <class... Args>
72-
reference operator()(Args... args);
73-
74-
template <class... Args>
75-
const_reference operator()(Args... args) const;
76-
77-
reference operator[](const xindex& index);
78-
const_reference operator[](const xindex& index) const;
79-
80-
template <class It>
81-
reference element(It first, It last);
82-
83-
template <class It>
84-
const_reference element(It first, It last) const;
85-
86-
container_type& data();
87-
const container_type& data() const;
88-
89-
template <class S>
90-
bool broadcast_shape(S& shape) const;
91-
92-
template <class S>
93-
bool is_trivial_broadcast(const S& strides) const;
94-
95-
iterator begin();
96-
iterator end();
97-
98-
const_iterator begin() const;
99-
const_iterator end() const;
100-
const_iterator cbegin() const;
101-
const_iterator cend() const;
102-
103-
template <class S>
104-
stepper stepper_begin(const S& shape);
105-
template <class S>
106-
stepper stepper_end(const S& shape);
107-
108-
template <class S>
109-
const_stepper stepper_begin(const S& shape) const;
110-
template <class S>
111-
const_stepper stepper_end(const S& shape) const;
66+
using base_type::operator();
67+
using base_type::begin;
68+
using base_type::end;
11269

11370
protected:
11471

@@ -132,17 +89,6 @@ namespace xt
13289
PyArrayObject* python_array();
13390
};
13491

135-
template <class D>
136-
struct pycontainer_iterable_types
137-
: xcontainer_iterable_types<D>
138-
{
139-
using stepper = xstepper<D>;
140-
using const_stepper = xstepper<const D>;
141-
using inner_shape_type = typename xcontainer_inner_types<D>::inner_shape_type;
142-
using broadcast_iterator = xiterator<stepper, inner_shape_type*>;
143-
using const_broadcast_iterator = xiterator<const_stepper, inner_shape_type*>;
144-
};
145-
14692
namespace detail
14793
{
14894
// TODO : switch on pybind11::is_fmt_numeric when it is available
@@ -253,40 +199,10 @@ namespace xt
253199
return reinterpret_cast<PyArrayObject*>(this->m_ptr);
254200
}
255201

256-
template <class D>
257-
inline auto pycontainer<D>::size() const -> size_type
258-
{
259-
return data().size();
260-
}
261-
262-
template <class D>
263-
inline auto pycontainer<D>::dimension() const -> size_type
264-
{
265-
return shape().size();
266-
}
267-
268-
template <class D>
269-
inline auto pycontainer<D>::shape() const -> const inner_shape_type&
270-
{
271-
return static_cast<const derived_type*>(this)->shape_impl();
272-
}
273-
274-
template <class D>
275-
inline auto pycontainer<D>::strides() const -> const inner_strides_type&
276-
{
277-
return static_cast<const derived_type*>(this)->strides_impl();
278-
}
279-
280-
template <class D>
281-
inline auto pycontainer<D>::backstrides() const -> const backstrides_type&
282-
{
283-
return static_cast<const derived_type*>(this)->backstrides_impl();
284-
}
285-
286202
template <class D>
287203
inline void pycontainer<D>::reshape(const shape_type& shape)
288204
{
289-
if(shape.size() != dimension() || !std::equal(shape.begin(), shape.end(), this->shape().begin()))
205+
if(shape.size() != this->dimension() || !std::equal(shape.begin(), shape.end(), this->shape().begin()))
290206
{
291207
reshape(shape, layout::row_major);
292208
}
@@ -307,143 +223,6 @@ namespace xt
307223
*static_cast<derived_type*>(this) = std::move(tmp);
308224
}
309225

310-
template <class D>
311-
template <class... Args>
312-
inline auto pycontainer<D>::operator()(Args... args) -> reference
313-
{
314-
size_type index = data_offset<size_type>(strides(), static_cast<size_type>(args)...);
315-
return data()[index];
316-
}
317-
318-
template <class D>
319-
template <class... Args>
320-
inline auto pycontainer<D>::operator()(Args... args) const -> const_reference
321-
{
322-
size_type index = data_offset<size_type>(strides(), static_cast<size_type>(args)...);
323-
return data()[index];
324-
}
325-
326-
template <class D>
327-
inline auto pycontainer<D>::operator[](const xindex& index) -> reference
328-
{
329-
return element(index.cbegin(), index.cend());
330-
}
331-
332-
template <class D>
333-
inline auto pycontainer<D>::operator[](const xindex& index) const -> const_reference
334-
{
335-
return element(index.cbegin(), index.cend());
336-
}
337-
338-
template <class D>
339-
template <class It>
340-
inline auto pycontainer<D>::element(It first, It last) -> reference
341-
{
342-
return data()[element_offset<size_type>(strides(), first, last)];
343-
}
344-
345-
template <class D>
346-
template <class It>
347-
inline auto pycontainer<D>::element(It first, It last) const -> const_reference
348-
{
349-
return data()[element_offset<size_type>(strides(), first, last)];
350-
}
351-
352-
template <class D>
353-
inline auto pycontainer<D>::data() -> container_type&
354-
{
355-
return static_cast<derived_type*>(this)->data_impl();
356-
}
357-
358-
template <class D>
359-
inline auto pycontainer<D>::data() const -> const container_type&
360-
{
361-
return static_cast<const derived_type*>(this)->data_impl();
362-
}
363-
364-
template <class D>
365-
template <class S>
366-
inline bool pycontainer<D>::broadcast_shape(S& shape) const
367-
{
368-
return xt::broadcast_shape(this->shape(), shape);
369-
}
370-
371-
template <class D>
372-
template <class S>
373-
inline bool pycontainer<D>::is_trivial_broadcast(const S& strides) const
374-
{
375-
const inner_strides_type& str = this->strides();
376-
return str.size() == strides.size() &&
377-
std::equal(str.cbegin(), str.cend(), strides.begin());
378-
}
379-
380-
template <class D>
381-
inline auto pycontainer<D>::begin() -> iterator
382-
{
383-
return data().begin();
384-
}
385-
386-
template <class D>
387-
inline auto pycontainer<D>::end() -> iterator
388-
{
389-
return data().end();
390-
}
391-
392-
template <class D>
393-
inline auto pycontainer<D>::begin() const -> const_iterator
394-
{
395-
return cbegin();
396-
}
397-
398-
template <class D>
399-
inline auto pycontainer<D>::end() const -> const_iterator
400-
{
401-
return cend();
402-
}
403-
404-
template <class D>
405-
inline auto pycontainer<D>::cbegin() const -> const_iterator
406-
{
407-
return data().cbegin();
408-
}
409-
410-
template <class D>
411-
inline auto pycontainer<D>::cend() const -> const_iterator
412-
{
413-
return data().cend();
414-
}
415-
416-
template <class D>
417-
template <class S>
418-
inline auto pycontainer<D>::stepper_begin(const S& shape) -> stepper
419-
{
420-
size_type offset = shape.size() - dimension();
421-
return stepper(static_cast<derived_type*>(this), data().begin(), offset);
422-
}
423-
424-
template <class D>
425-
template <class S>
426-
inline auto pycontainer<D>::stepper_end(const S& shape) -> stepper
427-
{
428-
size_type offset = shape.size() - dimension();
429-
return stepper(static_cast<derived_type*>(this), data().end(), offset);
430-
}
431-
432-
template <class D>
433-
template <class S>
434-
inline auto pycontainer<D>::stepper_begin(const S& shape) const -> const_stepper
435-
{
436-
size_type offset = shape.size() - dimension();
437-
return const_stepper(static_cast<const derived_type*>(this), data().begin(), offset);
438-
}
439-
440-
template <class D>
441-
template <class S>
442-
inline auto pycontainer<D>::stepper_end(const S& shape) const -> const_stepper
443-
{
444-
size_type offset = shape.size() - dimension();
445-
return const_stepper(static_cast<const derived_type*>(this), data().end(), offset);
446-
}
447226
}
448227

449228
#endif

0 commit comments

Comments
 (0)