Skip to content

Commit 7fab009

Browse files
committed
addressed issues from review
1 parent 8fb97e6 commit 7fab009

File tree

5 files changed

+32
-96
lines changed

5 files changed

+32
-96
lines changed

include/xtensor/xblockwise_reducer.hpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "xshape.hpp"
88
#include "xblockwise_reducer_functors.hpp"
9-
#include "xgrid_iterator.hpp"
9+
#include "xmultiindex_iterator.hpp"
1010
#include "xreducer.hpp"
1111

1212
namespace xt
@@ -50,12 +50,13 @@ class xblockwise_reducer
5050
using mapping_type = filter_fixed_shape_t<shape_type>;
5151
using input_chunked_view_type = xchunked_view<const std::decay_t<CT> &>;
5252
using input_const_chunked_iterator_type = typename input_chunked_view_type::const_chunk_iterator;
53+
using input_chunk_range_type = std::array<xmultiindex_iterator<input_chunk_index_type>, 2>;
5354

5455
template<class CI>
5556
void assign_to_chunk(CI & result_chunk_iter) const;
5657

5758
template<class CI>
58-
std::array<xsubgrid_iterator<input_chunk_index_type>, 2> compute_input_chunk_range(CI & result_chunk_iter) const;
59+
input_chunk_range_type compute_input_chunk_range(CI & result_chunk_iter) const;
5960

6061
input_const_chunked_iterator_type get_input_chunk_iter(input_chunk_index_type input_chunk_index) const;
6162
void init_shapes();
@@ -184,12 +185,14 @@ void xblockwise_reducer<CT, F, X, O>::assign_to_chunk(CI & result_chunk_iter) co
184185

185186
template<class CT, class F, class X, class O>
186187
template<class CI>
187-
auto xblockwise_reducer<CT, F, X, O>::compute_input_chunk_range(CI & result_chunk_iter) const -> std::array<xsubgrid_iterator<input_chunk_index_type>, 2>
188+
auto xblockwise_reducer<CT, F, X, O>::compute_input_chunk_range(CI & result_chunk_iter) const -> input_chunk_range_type
188189
{
189-
auto input_chunks_begin = construct_container_with_size<input_chunk_index_type>(m_e_chunked_view.dimension());
190-
auto input_chunks_end = construct_container_with_size<input_chunk_index_type>(m_e_chunked_view.dimension());
190+
auto input_chunks_begin = xtl::make_sequence<input_chunk_index_type>(m_e_chunked_view.dimension(), 0);
191+
auto input_chunks_end = xtl::make_sequence<input_chunk_index_type>(m_e_chunked_view.dimension());
192+
193+
XTENSOR_ASSERT(input_chunks_begin.size() == m_e_chunked_view.dimension());
194+
XTENSOR_ASSERT(input_chunks_end.size() == m_e_chunked_view.dimension());
191195

192-
std::fill(input_chunks_begin.begin(), input_chunks_begin.end(), 0);
193196
std::copy(m_e_chunked_view.grid_shape().begin(), m_e_chunked_view.grid_shape().end(), input_chunks_end.begin());
194197

195198
const auto & chunk_index = result_chunk_iter.chunk_index();
@@ -202,9 +205,9 @@ auto xblockwise_reducer<CT, F, X, O>::compute_input_chunk_range(CI & result_chun
202205
input_chunks_end[input_ax_index] = chunk_index[result_ax_index] + 1;
203206
}
204207
}
205-
return std::array<xsubgrid_iterator<input_chunk_index_type>, 2>{
206-
subgrid_iterator_begin<input_chunk_index_type>(input_chunks_begin, input_chunks_end),
207-
subgrid_iterator_end<input_chunk_index_type>(input_chunks_begin, input_chunks_end)
208+
return input_chunk_range_type{
209+
multiindex_iterator_begin<input_chunk_index_type>(input_chunks_begin, input_chunks_end),
210+
multiindex_iterator_end<input_chunk_index_type>(input_chunks_begin, input_chunks_end)
208211
};
209212
}
210213

@@ -318,7 +321,8 @@ namespace blockwise
318321
{\
319322
using input_expression_type = std::decay_t<E>;\
320323
using axes_type = filter_fixed_shape_t<typename input_expression_type::shape_type>;\
321-
axes_type axes = construct_container_with_size<axes_type>(e.dimension());\
324+
axes_type axes = xtl::make_sequence<axes_type>(e.dimension());\
325+
XTENSOR_ASSERT(axes.dimension() == e.dimension());\
322326
std::iota(axes.begin(), axes.end(), 0);\
323327
using functor_type = FUNCTOR <typename input_expression_type::value_type, T>;\
324328
return blockwise_reducer(\
@@ -392,7 +396,8 @@ namespace blockwise
392396
{\
393397
using input_expression_type = std::decay_t<E>;\
394398
using axes_type = filter_fixed_shape_t<typename input_expression_type::shape_type>;\
395-
axes_type axes = construct_container_with_size<axes_type>(e.dimension());\
399+
axes_type axes = xtl::make_sequence<axes_type>(e.dimension());\
400+
XTENSOR_ASSERT(axes.dimension() == e.dimension());\
396401
std::iota(axes.begin(), axes.end(), 0);\
397402
using functor_type = FUNCTOR <typename input_expression_type::value_type>;\
398403
return blockwise_reducer(\
@@ -462,7 +467,8 @@ namespace blockwise
462467
{\
463468
using input_expression_type = std::decay_t<E>;\
464469
using axes_type = filter_fixed_shape_t<typename input_expression_type::shape_type>;\
465-
axes_type axes = construct_container_with_size<axes_type>(e.dimension());\
470+
axes_type axes = xtl::make_sequence<axes_type>(e.dimension());\
471+
XTENSOR_ASSERT(axes.dimension() == e.dimension());\
466472
std::iota(axes.begin(), axes.end(), 0);\
467473
using functor_type = FUNCTOR <typename input_expression_type::value_type>;\
468474
return blockwise_reducer(\

include/xtensor/xgrid_iterator.hpp renamed to include/xtensor/xmultiindex_iterator.hpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* The full license is in the file LICENSE, distributed with this software. *
88
****************************************************************************/
99

10-
#ifndef XTENSOR_XGRID_ITERATOR
11-
#define XTENSOR_XGRID_ITERATOR
10+
#ifndef XTENSOR_XMULTIINDEX_ITERATOR
11+
#define XTENSOR_XMULTIINDEX_ITERATOR
1212

1313
#include "xtl/xsequence.hpp"
1414

@@ -19,10 +19,10 @@ namespace xt
1919
{
2020

2121
template<class S>
22-
class xsubgrid_iterator
22+
class xmultiindex_iterator
2323
{
2424
public:
25-
using self_type = xsubgrid_iterator<S>;
25+
using self_type = xmultiindex_iterator<S>;
2626
using shape_type = S;
2727

2828
using value_type = shape_type;
@@ -31,14 +31,10 @@ namespace xt
3131
using difference_type = std::size_t;
3232
using iterator_category = std::forward_iterator_tag;
3333

34-
xsubgrid_iterator() = default;
35-
xsubgrid_iterator(const xsubgrid_iterator&) = default;
36-
xsubgrid_iterator(xsubgrid_iterator&&) = default;
37-
xsubgrid_iterator& operator=(const xsubgrid_iterator&) = default;
38-
xsubgrid_iterator& operator=(xsubgrid_iterator&&) = default;
34+
xmultiindex_iterator() = default;
3935

4036
template<class B, class E, class C>
41-
xsubgrid_iterator(B && begin, E && end, C && current, const std::size_t linear_index)
37+
xmultiindex_iterator(B && begin, E && end, C && current, const std::size_t linear_index)
4238
: m_begin(std::forward<B>(begin)),
4339
m_end(std::forward<E>(end)),
4440
m_current(std::forward<C>(current)),
@@ -100,12 +96,12 @@ namespace xt
10096

10197

10298
template<class S, class B, class E>
103-
auto subgrid_iterator_begin(B && roi_begin, E && roi_end)
99+
auto multiindex_iterator_begin(B && roi_begin, E && roi_end)
104100
{
105101
S current;
106102
resize_container(current, roi_begin.size());
107103
std::copy(roi_begin.begin(), roi_begin.end(), current.begin());
108-
return xsubgrid_iterator<S>(
104+
return xmultiindex_iterator<S>(
109105
std::forward<B>(roi_begin),
110106
std::forward<E>(roi_end),
111107
std::move(current),
@@ -114,7 +110,7 @@ namespace xt
114110
}
115111

116112
template<class S, class B, class E>
117-
auto subgrid_iterator_end(B && roi_begin, E && roi_end)
113+
auto multiindex_iterator_end(B && roi_begin, E && roi_end)
118114
{
119115
S current;
120116
resize_container(current, roi_begin.size());
@@ -126,7 +122,7 @@ namespace xt
126122
linear_index *= roi_end[i] - roi_begin[i];
127123
}
128124

129-
return xsubgrid_iterator<S>(
125+
return xmultiindex_iterator<S>(
130126
std::forward<B>(roi_begin),
131127
std::forward<E>(roi_end),
132128
std::move(current),

include/xtensor/xutils.hpp

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -331,72 +331,6 @@ namespace xt
331331
}
332332
}
333333

334-
/***********************************
335-
* construct_container_with_size implementation *
336-
***********************************/
337-
namespace detail
338-
{
339-
340-
template<class C>
341-
struct construct_container_with_size_impl
342-
{
343-
static C op(typename C::size_type size)
344-
{
345-
C c;
346-
const bool success = resize_container(c, size);
347-
if(!success)
348-
{
349-
XTENSOR_THROW(std::runtime_error, "cannot resize container to given size");
350-
}
351-
c.resize(size);
352-
return c;
353-
}
354-
};
355-
356-
template <class T, class A>
357-
struct construct_container_with_size_impl<std::vector<T,A>>
358-
{
359-
using container_type = std::vector<T,A>;
360-
static container_type op(typename container_type::size_type size)
361-
{
362-
return container_type(size);
363-
}
364-
};
365-
366-
template <class T, std::size_t N>
367-
struct construct_container_with_size_impl<std::array<T, N>>
368-
{
369-
using container_type = std::array<T, N>;
370-
static container_type op(typename container_type::size_type size)
371-
{
372-
if(size != N)
373-
{
374-
XTENSOR_THROW(std::runtime_error, "cannot resize container to given size");
375-
}
376-
return container_type{};
377-
}
378-
};
379-
380-
template <std::size_t... I>
381-
struct construct_container_with_size_impl<xt::fixed_shape<I...>>
382-
{
383-
using container_type = xt::fixed_shape<I...>;
384-
static container_type op(std::size_t size)
385-
{
386-
if(sizeof...(I) != size)
387-
{
388-
XTENSOR_THROW(std::runtime_error, "cannot resize container to given size");
389-
}
390-
return container_type{};
391-
}
392-
};
393-
}
394-
395-
template <class C, class S>
396-
inline C construct_container_with_size(S size)
397-
{
398-
return detail::construct_container_with_size_impl<C>::op(size);
399-
}
400334
/***********************************
401335
* resize_container implementation *
402336
***********************************/

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ set(COMMON_BASE
143143
test_xexpression_traits.cpp
144144
test_xfunction.cpp
145145
test_xfunc_on_xexpression.cpp
146-
test_xgrid_iterator.cpp
146+
test_xmultiindex_iterator.cpp
147147
test_xiterator.cpp
148148
test_xmath.cpp
149149
test_xoperation.cpp

test/test_xgrid_iterator.cpp renamed to test/test_xmultiindex_iterator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#include "test_common.hpp"
22
#include <vector>
33

4-
#include "xtensor/xgrid_iterator.hpp"
4+
#include "xtensor/xmultiindex_iterator.hpp"
55

66
namespace xt
77
{
88

9-
TEST_SUITE("xgrid_iterator")
9+
TEST_SUITE("xmultiindex_iterator")
1010
{
1111

1212
TEST_CASE("sum")
1313
{
1414
using shape_type = std::vector<std::size_t>;
15-
using iter_type = xsubgrid_iterator<shape_type>;
15+
using iter_type = xmultiindex_iterator<shape_type>;
1616

1717
shape_type roi_begin{2,3,4};
1818
shape_type roi_end{3,5,6};

0 commit comments

Comments
 (0)