xtensor
Loading...
Searching...
No Matches
xstrided_view.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_STRIDED_VIEW_HPP
11#define XTENSOR_STRIDED_VIEW_HPP
12
13#include <algorithm>
14#include <cstddef>
15#include <type_traits>
16#include <utility>
17#include <variant>
18
19#include <xtl/xsequence.hpp>
20
21#include "../containers/xstorage.hpp"
22#include "../core/xexpression.hpp"
23#include "../core/xiterable.hpp"
24#include "../core/xlayout.hpp"
25#include "../core/xsemantic.hpp"
26#include "../utils/xutils.hpp"
27#include "../views/xstrided_view_base.hpp"
28
29namespace xt
30{
31 /***************************
32 * xstrided_view extension *
33 ***************************/
34
35 namespace extension
36 {
37 template <class Tag, class CT, class S, layout_type L, class FST>
39
40 template <class CT, class S, layout_type L, class FST>
42 {
43 using type = xtensor_empty_base;
44 };
45
46 template <class CT, class S, layout_type L, class FST>
47 struct xstrided_view_base : xstrided_view_base_impl<xexpression_tag_t<CT>, CT, S, L, FST>
48 {
49 };
50
51 template <class CT, class S, layout_type L, class FST>
52 using xstrided_view_base_t = typename xstrided_view_base<CT, S, L, FST>::type;
53 }
54
55 template <layout_type L1, layout_type L2, class T>
57 {
58 using type = std::conditional_t<L1 == L2 && L1 != layout_type::dynamic, xcontiguous_iterable<T>, xiterable<T>>;
59 };
60
61 template <layout_type L1, layout_type L2, class T>
62 using select_iterable_base_t = typename select_iterable_base<L1, L2, T>::type;
63
64
65 template <class CT, class S, layout_type L, class FST>
66 class xstrided_view;
67
68 template <class CT, class S, layout_type L, class FST>
70 {
71 using xexpression_type = std::decay_t<CT>;
72 using undecay_expression = CT;
73 using reference = inner_reference_t<undecay_expression>;
74 using const_reference = typename xexpression_type::const_reference;
75 using size_type = typename xexpression_type::size_type;
76 using shape_type = std::decay_t<S>;
77 using undecay_shape = S;
78 using storage_getter = FST;
79 using inner_storage_type = typename storage_getter::type;
80 using temporary_type = typename detail::xtype_for_shape<
81 S>::template type<typename xexpression_type::value_type, xexpression_type::static_layout>;
82 using storage_type = std::remove_reference_t<inner_storage_type>;
83 static constexpr layout_type layout = L;
84 };
85
86 template <class CT, class S, layout_type L, class FST>
87 struct xiterable_inner_types<xstrided_view<CT, S, L, FST>>
88 {
89 using inner_shape_type = std::decay_t<S>;
90 using inner_strides_type = get_strides_t<inner_shape_type>;
91 using inner_backstrides_type_type = inner_strides_type;
92
93 using const_stepper = std::conditional_t<
97
98 using stepper = std::conditional_t<
102 };
103
104 template <class CT, class S, layout_type L, class FST, class RHS>
105 struct can_assign<xstrided_view<CT, S, L, FST>, RHS> : can_assign<CT, RHS>
106 {
107 };
108
109 /*****************
110 * xstrided_view *
111 *****************/
112
127 template <class CT, class S, layout_type L = layout_type::dynamic, class FST = detail::flat_storage_getter<CT, XTENSOR_DEFAULT_TRAVERSAL>>
129 : public xview_semantic<xstrided_view<CT, S, L, FST>>,
130 public select_iterable_base_t<L, std::decay_t<CT>::static_layout, xstrided_view<CT, S, L, FST>>,
131 private xstrided_view_base<xstrided_view<CT, S, L, FST>>,
132 public extension::xstrided_view_base_t<CT, S, L, FST>
133 {
134 public:
135
136 using self_type = xstrided_view<CT, S, L, FST>;
137 using base_type = xstrided_view_base<self_type>;
138 using semantic_base = xview_semantic<self_type>;
139 using extension_base = extension::xstrided_view_base_t<CT, S, L, FST>;
140 using expression_tag = typename extension_base::expression_tag;
141
142 using xexpression_type = typename base_type::xexpression_type;
143 using base_type::is_const;
144
145 using value_type = typename base_type::value_type;
146 using reference = typename base_type::reference;
147 using const_reference = typename base_type::const_reference;
148 using pointer = typename base_type::pointer;
149 using const_pointer = typename base_type::const_pointer;
150 using size_type = typename base_type::size_type;
151 using difference_type = typename base_type::difference_type;
152
153 using inner_storage_type = typename base_type::inner_storage_type;
154 using storage_type = typename base_type::storage_type;
155 using linear_iterator = typename storage_type::iterator;
156 using const_linear_iterator = typename storage_type::const_iterator;
157 using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
158 using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
159
160 using iterable_base = select_iterable_base_t<L, xexpression_type::static_layout, self_type>;
161 using inner_shape_type = typename base_type::inner_shape_type;
162 using inner_strides_type = typename base_type::inner_strides_type;
163 using inner_backstrides_type = typename base_type::inner_backstrides_type;
164 using shape_type = typename base_type::shape_type;
165 using strides_type = typename base_type::strides_type;
166 using backstrides_type = typename base_type::backstrides_type;
167
168 using stepper = typename iterable_base::stepper;
169 using const_stepper = typename iterable_base::const_stepper;
170
171 using base_type::contiguous_layout;
172 using base_type::static_layout;
173
174 using temporary_type = typename xcontainer_inner_types<self_type>::temporary_type;
175 using base_index_type = xindex_type_t<shape_type>;
176
177 using data_alignment = xt_simd::container_alignment_t<storage_type>;
178 using simd_type = xt_simd::simd_type<value_type>;
179 using simd_value_type = xt_simd::simd_type<value_type>;
180 using bool_load_type = typename base_type::bool_load_type;
181
182 // load_simd/store_simd take the address of the flat storage, which requires the
183 // storage to expose lvalue references (not the case for lazy expressions wrapped
184 // in a flat_expression_adaptor).
185 static constexpr bool provides_simd_interface = has_simd_interface<xexpression_type>::value
187 && std::is_lvalue_reference_v<
188 decltype(std::declval<const storage_type&>()[0])>;
189
190 template <class CTA, class SA>
192 CTA&& e,
193 SA&& view_shape,
194 strides_type&& view_strides,
195 std::size_t offset,
196 layout_type view_layout
197 ) noexcept;
198
199 xstrided_view(const xstrided_view& rhs) = default;
200
201 self_type& operator=(const self_type&);
202
203 template <class E>
204 self_type& operator=(const xexpression<E>& e);
205
206 template <class E>
207 disable_xexpression<E, self_type>& operator=(const E& e);
208
211 using base_type::is_contiguous;
212 using base_type::layout;
213 using base_type::shape;
214 using base_type::size;
215 using base_type::strides;
216
217 using base_type::operator();
218 using base_type::at;
220 using base_type::operator[];
221 using base_type::data;
223 using base_type::element;
225 using base_type::storage;
226
229
230 template <class T>
231 void fill(const T& value);
232
233 linear_iterator linear_begin();
234 linear_iterator linear_end();
235 const_linear_iterator linear_begin() const;
236 const_linear_iterator linear_end() const;
237 const_linear_iterator linear_cbegin() const;
238 const_linear_iterator linear_cend() const;
239
240 reverse_linear_iterator linear_rbegin();
241 reverse_linear_iterator linear_rend();
242 const_reverse_linear_iterator linear_rbegin() const;
243 const_reverse_linear_iterator linear_rend() const;
244 const_reverse_linear_iterator linear_crbegin() const;
245 const_reverse_linear_iterator linear_crend() const;
246
247 template <class ST, class STEP = stepper>
248 disable_indexed_stepper_t<STEP> stepper_begin(const ST& iteration_shape);
249 template <class ST, class STEP = stepper>
250 disable_indexed_stepper_t<STEP> stepper_end(const ST& iteration_shape, layout_type l);
251
252 template <class ST, class STEP = stepper>
253 enable_indexed_stepper_t<STEP> stepper_begin(const ST& iteration_shape);
254 template <class ST, class STEP = stepper>
255 enable_indexed_stepper_t<STEP> stepper_end(const ST& iteration_shape, layout_type l);
256
257 template <class ST, class STEP = const_stepper>
258 disable_indexed_stepper_t<STEP> stepper_begin(const ST& iteration_shape) const;
259 template <class ST, class STEP = const_stepper>
260 disable_indexed_stepper_t<STEP> stepper_end(const ST& iteration_shape, layout_type l) const;
261
262 template <class ST, class STEP = const_stepper>
263 enable_indexed_stepper_t<STEP> stepper_begin(const ST& iteration_shape) const;
264 template <class ST, class STEP = const_stepper>
265 enable_indexed_stepper_t<STEP> stepper_end(const ST& iteration_shape, layout_type l) const;
266
267 template <class requested_type>
268 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
269
270 template <class align, class simd>
271 void store_simd(size_type i, const simd& e)
272 requires provides_simd_interface;
273
274 template <class align, class requested_type = value_type, std::size_t N = xt_simd::simd_traits<requested_type>::size>
275 simd_return_type<requested_type> load_simd(size_type i) const
276 requires provides_simd_interface;
277
278 reference data_element(size_type i);
279 const_reference data_element(size_type i) const;
280
281 reference flat(size_type i);
282 const_reference flat(size_type i) const;
283
284 using container_iterator = std::
285 conditional_t<is_const, typename storage_type::const_iterator, typename storage_type::iterator>;
286 using const_container_iterator = typename storage_type::const_iterator;
287
288 template <class E>
290
291 template <class E>
292 rebind_t<E> build_view(E&& e) const;
293
294 private:
295
296 container_iterator data_xbegin() noexcept;
297 const_container_iterator data_xbegin() const noexcept;
298 container_iterator data_xend(layout_type l, size_type offset) noexcept;
299 const_container_iterator data_xend(layout_type l, size_type offset) const noexcept;
300
301 template <class It>
302 It data_xbegin_impl(It begin) const noexcept;
303
304 template <class It>
305 It data_xend_impl(It end, layout_type l, size_type offset) const noexcept;
306
307 void assign_temporary_impl(temporary_type&& tmp);
308
309 using base_type::set_offset;
310
311 template <class C>
312 friend class xstepper;
313 friend class xview_semantic<self_type>;
314 friend class xaccessible<self_type>;
315 friend class xconst_accessible<self_type>;
316 template <class D>
317 friend class xaxis_iterator;
318 template <class D>
319 friend class xaxis_slice_iterator;
320 };
321
322 /**************************
323 * xstrided_view builders *
324 **************************/
325
326 template <class T>
327 using xstrided_slice = std::variant<
328 T,
329
330 xrange_adaptor<placeholders::xtuph, T, T>,
331 xrange_adaptor<T, placeholders::xtuph, T>,
332 xrange_adaptor<T, T, placeholders::xtuph>,
333
334 xrange_adaptor<T, placeholders::xtuph, placeholders::xtuph>,
335 xrange_adaptor<placeholders::xtuph, T, placeholders::xtuph>,
336 xrange_adaptor<placeholders::xtuph, placeholders::xtuph, T>,
337
338 xrange_adaptor<T, T, T>,
339 xrange_adaptor<placeholders::xtuph, placeholders::xtuph, placeholders::xtuph>,
340
341 xrange<T>,
343
344 xall_tag,
347
352 using xstrided_slice_vector = std::vector<xstrided_slice<std::ptrdiff_t>>;
353
354 template <layout_type L = layout_type::dynamic, class E, class S, class X>
355 auto strided_view(E&& e, S&& shape, X&& stride, std::size_t offset = 0, layout_type layout = L) noexcept;
356
357 template <class E>
358 auto strided_view(E&& e, const xstrided_slice_vector& slices);
359
360 /********************************
361 * xstrided_view implementation *
362 ********************************/
363
368
377 template <class CT, class S, layout_type L, class FST>
378 template <class CTA, class SA>
379 inline xstrided_view<CT, S, L, FST>::xstrided_view(
380 CTA&& e,
381 SA&& view_shape,
382 strides_type&& view_strides,
383 std::size_t offset,
384 layout_type view_layout
385 ) noexcept
386 : base_type(std::forward<CTA>(e), std::forward<SA>(view_shape), std::move(view_strides), offset, view_layout)
387 {
388 }
389
391
392 template <class CT, class S, layout_type L, class FST>
393 inline auto xstrided_view<CT, S, L, FST>::operator=(const self_type& rhs) -> self_type&
394 {
395 temporary_type tmp(rhs);
396 return this->assign_temporary(std::move(tmp));
397 }
398
403
406 template <class CT, class S, layout_type L, class FST>
407 template <class E>
408 inline auto xstrided_view<CT, S, L, FST>::operator=(const xexpression<E>& e) -> self_type&
409 {
410 return semantic_base::operator=(e);
411 }
412
414
415 template <class CT, class S, layout_type L, class FST>
416 template <class E>
417 inline auto xstrided_view<CT, S, L, FST>::operator=(const E& e) -> disable_xexpression<E, self_type>&
418 {
419 this->fill(e);
420 return *this;
421 }
422
423 template <class CT, class S, layout_type L, class FST>
424 inline void xstrided_view<CT, S, L, FST>::assign_temporary_impl(temporary_type&& tmp)
425 {
426 constexpr bool
427 fast_assign = xassign_traits<xstrided_view<CT, S, L, FST>, temporary_type>::simd_strided_assign();
428 if constexpr (fast_assign)
429 {
430 using self_type = xstrided_view<CT, S, L, FST>;
431 parallel_assigner_t<self_type, temporary_type, default_parallel_tag> assigner;
432 assigner.strided_loop_assign(*this, tmp);
433 }
434 else
435 {
436 std::copy(tmp.cbegin(), tmp.cend(), this->begin());
437 }
438 }
439
444
449 template <class CT, class S, layout_type L, class FST>
450 template <class T>
451 inline void xstrided_view<CT, S, L, FST>::fill(const T& value)
452 {
454 {
455 std::fill(this->linear_begin(), this->linear_end(), value);
456 }
457 else
458 {
459 std::fill(this->begin(), this->end(), value);
460 }
461 }
462
464
465 template <class CT, class S, layout_type L, class FST>
466 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) -> reference
467 {
468 return storage()[i];
469 }
470
471 template <class CT, class S, layout_type L, class FST>
472 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) const -> const_reference
473 {
474 return storage()[i];
475 }
476
477 template <class CT, class S, layout_type L, class FST>
478 inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) -> reference
479 {
480 return storage()[i];
481 }
482
483 template <class CT, class S, layout_type L, class FST>
484 inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) const -> const_reference
485 {
486 return storage()[i];
487 }
488
489 template <class CT, class S, layout_type L, class FST>
490 inline auto xstrided_view<CT, S, L, FST>::linear_begin() -> linear_iterator
491 {
492 return this->storage().begin() + static_cast<std::ptrdiff_t>(data_offset());
493 }
494
495 template <class CT, class S, layout_type L, class FST>
496 inline auto xstrided_view<CT, S, L, FST>::linear_end() -> linear_iterator
497 {
498 return this->storage().begin() + static_cast<std::ptrdiff_t>(data_offset() + size());
499 }
500
501 template <class CT, class S, layout_type L, class FST>
502 inline auto xstrided_view<CT, S, L, FST>::linear_begin() const -> const_linear_iterator
503 {
504 return this->linear_cbegin();
505 }
506
507 template <class CT, class S, layout_type L, class FST>
508 inline auto xstrided_view<CT, S, L, FST>::linear_end() const -> const_linear_iterator
509 {
510 return this->linear_cend();
511 }
512
513 template <class CT, class S, layout_type L, class FST>
514 inline auto xstrided_view<CT, S, L, FST>::linear_cbegin() const -> const_linear_iterator
515 {
516 return this->storage().cbegin() + static_cast<std::ptrdiff_t>(data_offset());
517 }
518
519 template <class CT, class S, layout_type L, class FST>
520 inline auto xstrided_view<CT, S, L, FST>::linear_cend() const -> const_linear_iterator
521 {
522 return this->storage().cbegin() + static_cast<std::ptrdiff_t>(data_offset() + size());
523 }
524
525 template <class CT, class S, layout_type L, class FST>
526 inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() -> reverse_linear_iterator
527 {
528 return reverse_linear_iterator(this->linear_begin());
529 }
530
531 template <class CT, class S, layout_type L, class FST>
532 inline auto xstrided_view<CT, S, L, FST>::linear_rend() -> reverse_linear_iterator
533 {
534 return reverse_linear_iterator(this->linear_end());
535 }
536
537 template <class CT, class S, layout_type L, class FST>
538 inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() const -> const_reverse_linear_iterator
539 {
540 return this->linear_crbegin();
541 }
542
543 template <class CT, class S, layout_type L, class FST>
544 inline auto xstrided_view<CT, S, L, FST>::linear_rend() const -> const_reverse_linear_iterator
545 {
546 return this->linear_crend();
547 }
548
549 template <class CT, class S, layout_type L, class FST>
550 inline auto xstrided_view<CT, S, L, FST>::linear_crbegin() const -> const_reverse_linear_iterator
551 {
552 return const_reverse_linear_iterator(this->linear_cbegin());
553 }
554
555 template <class CT, class S, layout_type L, class FST>
556 inline auto xstrided_view<CT, S, L, FST>::linear_crend() const -> const_reverse_linear_iterator
557 {
558 return const_reverse_linear_iterator(this->linear_cend());
559 }
560
561 /***************
562 * stepper api *
563 ***************/
564
565 template <class CT, class S, layout_type L, class FST>
566 template <class ST, class STEP>
567 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& iteration_shape)
568 -> disable_indexed_stepper_t<STEP>
569 {
570 size_type offset = iteration_shape.size() - dimension();
571 return stepper(this, data_xbegin(), offset);
572 }
573
574 template <class CT, class S, layout_type L, class FST>
575 template <class ST, class STEP>
576 inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& iteration_shape, layout_type l)
577 -> disable_indexed_stepper_t<STEP>
578 {
579 size_type offset = iteration_shape.size() - dimension();
580 return stepper(this, data_xend(l, offset), offset);
581 }
582
583 template <class CT, class S, layout_type L, class FST>
584 template <class ST, class STEP>
585 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& iteration_shape)
586 -> enable_indexed_stepper_t<STEP>
587 {
588 size_type offset = iteration_shape.size() - dimension();
589 return stepper(this, offset);
590 }
591
592 template <class CT, class S, layout_type L, class FST>
593 template <class ST, class STEP>
594 inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& iteration_shape, layout_type /*l*/)
595 -> enable_indexed_stepper_t<STEP>
596 {
597 size_type offset = iteration_shape.size() - dimension();
598 return stepper(this, offset, true);
599 }
600
601 template <class CT, class S, layout_type L, class FST>
602 template <class ST, class STEP>
603 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& iteration_shape) const
604 -> disable_indexed_stepper_t<STEP>
605 {
606 size_type offset = iteration_shape.size() - dimension();
607 return const_stepper(this, data_xbegin(), offset);
608 }
609
610 template <class CT, class S, layout_type L, class FST>
611 template <class ST, class STEP>
612 inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& iteration_shape, layout_type l) const
613 -> disable_indexed_stepper_t<STEP>
614 {
615 size_type offset = iteration_shape.size() - dimension();
616 return const_stepper(this, data_xend(l, offset), offset);
617 }
618
619 template <class CT, class S, layout_type L, class FST>
620 template <class ST, class STEP>
621 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& iteration_shape) const
622 -> enable_indexed_stepper_t<STEP>
623 {
624 size_type offset = iteration_shape.size() - dimension();
625 return const_stepper(this, offset);
626 }
627
628 template <class CT, class S, layout_type L, class FST>
629 template <class ST, class STEP>
630 inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& iteration_shape, layout_type /*l*/) const
631 -> enable_indexed_stepper_t<STEP>
632 {
633 size_type offset = iteration_shape.size() - dimension();
634 return const_stepper(this, offset, true);
635 }
636
637 template <class CT, class S, layout_type L, class FST>
638 template <class It>
639 inline It xstrided_view<CT, S, L, FST>::data_xbegin_impl(It begin) const noexcept
640 {
641 return begin + static_cast<std::ptrdiff_t>(this->data_offset());
642 }
643
644 template <class CT, class S, layout_type L, class FST>
645 template <class It>
646 inline It
647 xstrided_view<CT, S, L, FST>::data_xend_impl(It begin, layout_type l, size_type offset) const noexcept
648 {
649 return strided_data_end(*this, begin + std::ptrdiff_t(this->data_offset()), l, offset);
650 }
651
652 template <class CT, class S, layout_type L, class FST>
653 inline auto xstrided_view<CT, S, L, FST>::data_xbegin() noexcept -> container_iterator
654 {
655 return data_xbegin_impl(this->storage().begin());
656 }
657
658 template <class CT, class S, layout_type L, class FST>
659 inline auto xstrided_view<CT, S, L, FST>::data_xbegin() const noexcept -> const_container_iterator
660 {
661 return data_xbegin_impl(this->storage().cbegin());
662 }
663
664 template <class CT, class S, layout_type L, class FST>
665 inline auto xstrided_view<CT, S, L, FST>::data_xend(layout_type l, size_type offset) noexcept
666 -> container_iterator
667 {
668 return data_xend_impl(this->storage().begin(), l, offset);
669 }
670
671 template <class CT, class S, layout_type L, class FST>
672 inline auto xstrided_view<CT, S, L, FST>::data_xend(layout_type l, size_type offset) const noexcept
673 -> const_container_iterator
674 {
675 return data_xend_impl(this->storage().cbegin(), l, offset);
676 }
677
678 template <class CT, class S, layout_type L, class FST>
679 template <class alignment, class simd>
680 inline void xstrided_view<CT, S, L, FST>::store_simd(size_type i, const simd& e)
681 requires provides_simd_interface
682 {
683 using align_mode = driven_align_mode_t<alignment, data_alignment>;
684 xt_simd::store_as(&(storage()[i]), e, align_mode());
685 }
686
687 template <class CT, class S, layout_type L, class FST>
688 template <class alignment, class requested_type, std::size_t N>
689 inline auto xstrided_view<CT, S, L, FST>::load_simd(size_type i) const -> simd_return_type<requested_type>
690 requires provides_simd_interface
691 {
692 using align_mode = driven_align_mode_t<alignment, data_alignment>;
693 return xt_simd::load_as<requested_type>(&(storage()[i]), align_mode());
694 }
695
696 template <class CT, class S, layout_type L, class FST>
697 template <class E>
698 inline auto xstrided_view<CT, S, L, FST>::build_view(E&& e) const -> rebind_t<E>
699 {
700 inner_shape_type sh(this->shape());
701 inner_strides_type str(this->strides());
702 return rebind_t<E>(
703 std::forward<E>(e),
704 std::move(sh),
705 std::move(str),
707 this->layout()
708 );
709 }
710
711 /*****************************************
712 * xstrided_view builders implementation *
713 *****************************************/
714
731 template <layout_type L, class E, class S, class X>
732 inline auto strided_view(E&& e, S&& shape, X&& strides, std::size_t offset, layout_type layout) noexcept
733 {
734 using view_type = xstrided_view<xclosure_t<E>, S, L>;
735 return view_type(std::forward<E>(e), std::forward<S>(shape), std::forward<X>(strides), offset, layout);
736 }
737
738 namespace detail
739 {
740 struct no_adj_strides_policy
741 {
742 protected:
743
744 inline void resize(std::size_t)
745 {
746 }
747
748 inline void set_fake_slice(std::size_t)
749 {
750 }
751
752 template <class ST, class S>
753 bool fill_args(
754 const xstrided_slice_vector& /*slices*/,
755 std::size_t /*sl_idx*/,
756 std::size_t /*i*/,
757 std::size_t /*old_shape*/,
758 const ST& /*old_stride*/,
759 S& /*shape*/,
760 get_strides_t<S>& /*strides*/
761 )
762 {
763 return false;
764 }
765 };
766 }
767
793 template <class E>
794 inline auto strided_view(E&& e, const xstrided_slice_vector& slices)
795 {
796 detail::strided_view_args<detail::no_adj_strides_policy> args;
797 args.fill_args(
798 e.shape(),
799 detail::get_strides<XTENSOR_DEFAULT_TRAVERSAL>(e),
800 detail::get_offset<XTENSOR_DEFAULT_TRAVERSAL>(e),
801 e.layout(),
802 slices
803 );
804 using view_type = xstrided_view<xclosure_t<E>, decltype(args.new_shape)>;
805 return view_type(
806 std::forward<E>(e),
807 std::move(args.new_shape),
808 std::move(args.new_strides),
809 args.new_offset,
810 args.new_layout
811 );
812 }
813
814 namespace detail
815 {
816 template <typename S>
817 struct rebind_shape;
818
819 template <std::size_t... X>
820 struct rebind_shape<xt::fixed_shape<X...>>
821 {
822 using type = xt::fixed_shape<X...>;
823 };
824
825 template <class S>
826 struct rebind_shape
827 {
828 using type = rebind_container_t<size_t, S>;
829 };
830
831 template <class S>
832 inline void recalculate_shape_impl(S& shape, size_t size)
833 {
834 if constexpr (std::is_signed_v<get_value_type_t<typename std::decay<S>::type>>)
835 {
836 using value_type = get_value_type_t<typename std::decay_t<S>>;
837 XTENSOR_ASSERT(std::count(shape.cbegin(), shape.cend(), -1) <= 1);
838 auto iter = std::find(shape.begin(), shape.end(), -1);
839 if (iter != std::end(shape))
840 {
841 const auto total = std::accumulate(shape.cbegin(), shape.cend(), -1, std::multiplies<int>{});
842 const auto missing_dimension = size / static_cast<size_t>(total);
843 (*iter) = static_cast<value_type>(missing_dimension);
844 }
845 }
846 }
847
848 template <class S>
849 inline auto recalculate_shape(S&& shape, size_t size)
850 {
851 return recalculate_shape_impl(shape, size);
852 }
853 }
854
855 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class S>
856 inline auto reshape_view(E&& e, S&& shape)
857 {
858 static_assert(
860 "traversal has to be row or column major"
861 );
862
863 using shape_type = std::decay_t<decltype(shape)>;
864 using unsigned_shape_type = typename detail::rebind_shape<shape_type>::type;
865 get_strides_t<unsigned_shape_type> strides;
866
867 detail::recalculate_shape(shape, e.size());
868 xt::resize_container(strides, shape.size());
870 constexpr auto computed_layout = std::decay_t<E>::static_layout == L ? L : layout_type::dynamic;
871 using view_type = xstrided_view<
872 xclosure_t<E>,
873 unsigned_shape_type,
874 computed_layout,
875 detail::flat_adaptor_getter<xclosure_t<E>, L>>;
876 return view_type(
877 std::forward<E>(e),
878 xtl::forward_sequence<unsigned_shape_type, S>(shape),
879 std::move(strides),
880 0,
881 e.layout()
882 );
883 }
884
898 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class S>
899 inline auto reshape_view(E&& e, S&& shape, layout_type /*order*/)
900 {
901 return reshape_view<L>(std::forward<E>(e), std::forward<S>(shape));
902 }
903
904 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class I, std::size_t N>
905 inline auto reshape_view(E&& e, const I (&shape)[N], layout_type order)
906 {
907 using shape_type = std::array<std::size_t, N>;
908 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type, decltype(shape)>(shape), order);
909 }
910
911 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class I, std::size_t N>
912 inline auto reshape_view(E&& e, const I (&shape)[N])
913 {
914 using shape_type = std::array<I, N>;
915 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type, decltype(shape)>(shape));
916 }
917}
918
919#endif
Fixed shape implementation for compile time defined arrays.
Base class for implementation of common expression access methods.
Base class for implementation of common expression constant access methods.
size_type size() const noexcept(noexcept(derived_cast().shape()))
size_type dimension() const noexcept
const_reference at(Args... args) const
Base class for xexpressions.
Base class for multidimensional iterable expressions.
layout_type layout() const noexcept
reference unchecked(Args... args)
xstrided_view_base(CTA &&e, SA &&shape, strides_type &&strides, size_type offset, layout_type layout) noexcept
Constructs an xstrided_view_base.
bool has_linear_assign(const O &strides) const noexcept
const inner_strides_type & strides() const noexcept
bool broadcast_shape(O &shape, bool reuse_cache=false) const
const inner_backstrides_type & backstrides() const noexcept
const inner_shape_type & shape() const noexcept
size_type data_offset() const noexcept
storage_type & storage() noexcept
xexpression_type & expression() noexcept
reference element(It first, It last)
View of an xexpression using strides.
xstrided_view(CTA &&e, SA &&view_shape, strides_type &&view_strides, std::size_t offset, layout_type view_layout) noexcept
Constructs an xstrided_view.
void fill(const T &value)
Fills the view with the given value.
storage_type & storage() noexcept
Returns a reference to the buffer containing the elements of the view.
size_type data_offset() const noexcept
Returns the offset to the first element in the view.
const inner_shape_type & shape() const noexcept
Returns the shape of the xtrided_view_base.
self_type & operator=(const xexpression< E > &e)
The extended assignment operator.
layout_type layout() const noexcept
Returns the layout of the xtrided_view_base.
const inner_strides_type & strides() const noexcept
Returns the strides of the xtrided_view_base.
derived_type & assign_temporary(temporary_type &&)
Assigns the temporary tmp to *this.
std::size_t compute_strides(const shape_type &shape, layout_type l, strides_type &strides)
Compute the strides given the shape and the layout of an array.
Definition xstrides.hpp:574
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
Definition xstrides.hpp:254
standard mathematical functions for xexpressions
std::vector< xstrided_slice< std::ptrdiff_t > > xstrided_slice_vector
vector of slices used to build a xstrided_view
layout_type
Definition xlayout.hpp:24
auto strided_view(E &&e, S &&shape, X &&stride, std::size_t offset=0, layout_type layout=L) noexcept
Construct a strided view from an xexpression, shape, strides and offset.