@@ -85,11 +85,14 @@ namespace xt
8585 static constexpr bool contiguous_layout = false ;
8686
8787 template <class S = shape_type>
88- void reshape (const S& shape);
88+ void resize (const S& shape);
8989 template <class S = shape_type>
90- void reshape (const S& shape, layout_type l);
90+ void resize (const S& shape, layout_type l);
9191 template <class S = shape_type>
92- void reshape (const S& shape, const strides_type& strides);
92+ void resize (const S& shape, const strides_type& strides);
93+
94+ template <class S = shape_type>
95+ void reshape (S&& shape, layout_type layout = base_type::static_layout);
9396
9497 layout_type layout () const ;
9598
@@ -258,46 +261,79 @@ namespace xt
258261 }
259262
260263 /* *
261- * Reshapes the container.
264+ * resizes the container.
262265 * @param shape the new shape
263266 */
264267 template <class D >
265268 template <class S >
266- inline void pycontainer<D>::reshape (const S& shape)
269+ inline void pycontainer<D>::resize (const S& shape)
267270 {
268271 if (shape.size () != this ->dimension () || !std::equal (std::begin (shape), std::end (shape), std::begin (this ->shape ())))
269272 {
270- reshape (shape, layout_type::row_major);
273+ resize (shape, layout_type::row_major);
271274 }
272275 }
273276
274277 /* *
275- * Reshapes the container.
278+ * resizes the container.
276279 * @param shape the new shape
277280 * @param l the new layout
278281 */
279282 template <class D >
280283 template <class S >
281- inline void pycontainer<D>::reshape (const S& shape, layout_type l)
284+ inline void pycontainer<D>::resize (const S& shape, layout_type l)
282285 {
283286 strides_type strides = xtl::make_sequence<strides_type>(shape.size (), size_type (1 ));
284287 compute_strides (shape, l, strides);
285- reshape (shape, strides);
288+ resize (shape, strides);
286289 }
287290
288291 /* *
289- * Reshapes the container.
292+ * resizes the container.
290293 * @param shape the new shape
291294 * @param strides the new strides
292295 */
293296 template <class D >
294297 template <class S >
295- inline void pycontainer<D>::reshape (const S& shape, const strides_type& strides)
298+ inline void pycontainer<D>::resize (const S& shape, const strides_type& strides)
296299 {
297300 derived_type tmp (xtl::forward_sequence<shape_type>(shape), strides);
298301 *static_cast <derived_type*>(this ) = std::move (tmp);
299302 }
300303
304+ template <class D >
305+ template <class S >
306+ inline void pycontainer<D>::reshape(S&& shape, layout_type layout)
307+ {
308+ if (compute_size (shape) != this ->size ())
309+ {
310+ throw std::runtime_error (" Cannot reshape with incorrect number of elements." );
311+ }
312+
313+ if (layout == layout_type::dynamic || layout == layout_type::any)
314+ {
315+ layout = DEFAULT_LAYOUT;
316+ }
317+
318+ NPY_ORDER npy_layout;
319+ if (layout == layout_type::row_major)
320+ {
321+ npy_layout = NPY_CORDER;
322+ }
323+ else if (layout == layout_type::column_major)
324+ {
325+ npy_layout = NPY_FORTRANORDER;
326+ }
327+ else
328+ {
329+ throw std::runtime_error (" Cannot reshape with unknown layout_type." );
330+ }
331+
332+ std::vector<npy_intp> int_shape (shape.begin (), shape.end ());
333+ PyArray_Dims dims ({int_shape.data (), static_cast <int >(shape.size ())});
334+ PyArray_Newshape ((PyArrayObject*) this ->ptr (), &dims, npy_layout);
335+ }
336+
301337 /* *
302338 * Return the layout_type of the container
303339 * @return layout_type of the container
0 commit comments