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 static constexpr bool provides_simd_interface = has_simd_interface<xexpression_type>::value
183 && L != layout_type::dynamic;
184
185 template <class CTA, class SA>
186 xstrided_view(CTA&& e, SA&& shape, strides_type&& strides, std::size_t offset, layout_type layout) noexcept;
187
188 xstrided_view(const xstrided_view& rhs) = default;
189
190 self_type& operator=(const self_type&);
191
192 template <class E>
193 self_type& operator=(const xexpression<E>& e);
194
195 template <class E>
196 disable_xexpression<E, self_type>& operator=(const E& e);
197
200 using base_type::is_contiguous;
201 using base_type::layout;
202 using base_type::shape;
203 using base_type::size;
204 using base_type::strides;
205
206 using base_type::operator();
207 using base_type::at;
208 using base_type::unchecked;
209 using base_type::operator[];
210 using base_type::data;
212 using base_type::element;
214 using base_type::storage;
215
218
219 template <class T>
220 void fill(const T& value);
221
222 linear_iterator linear_begin();
223 linear_iterator linear_end();
224 const_linear_iterator linear_begin() const;
225 const_linear_iterator linear_end() const;
226 const_linear_iterator linear_cbegin() const;
227 const_linear_iterator linear_cend() const;
228
229 reverse_linear_iterator linear_rbegin();
230 reverse_linear_iterator linear_rend();
231 const_reverse_linear_iterator linear_rbegin() const;
232 const_reverse_linear_iterator linear_rend() const;
233 const_reverse_linear_iterator linear_crbegin() const;
234 const_reverse_linear_iterator linear_crend() const;
235
236 template <class ST, class STEP = stepper>
237 disable_indexed_stepper_t<STEP> stepper_begin(const ST& shape);
238 template <class ST, class STEP = stepper>
239 disable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l);
240
241 template <class ST, class STEP = stepper>
242 enable_indexed_stepper_t<STEP> stepper_begin(const ST& shape);
243 template <class ST, class STEP = stepper>
244 enable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l);
245
246 template <class ST, class STEP = const_stepper>
247 disable_indexed_stepper_t<STEP> stepper_begin(const ST& shape) const;
248 template <class ST, class STEP = const_stepper>
249 disable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l) const;
250
251 template <class ST, class STEP = const_stepper>
252 enable_indexed_stepper_t<STEP> stepper_begin(const ST& shape) const;
253 template <class ST, class STEP = const_stepper>
254 enable_indexed_stepper_t<STEP> stepper_end(const ST& shape, layout_type l) const;
255
256 template <class requested_type>
257 using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
258
259 template <class align, class simd>
260 void store_simd(size_type i, const simd& e)
261 requires provides_simd_interface;
262
263 template <class align, class requested_type = value_type, std::size_t N = xt_simd::simd_traits<requested_type>::size>
264 simd_return_type<requested_type> load_simd(size_type i) const
265 requires provides_simd_interface;
266
267 reference data_element(size_type i);
268 const_reference data_element(size_type i) const;
269
270 reference flat(size_type i);
271 const_reference flat(size_type i) const;
272
273 using container_iterator = std::
274 conditional_t<is_const, typename storage_type::const_iterator, typename storage_type::iterator>;
275 using const_container_iterator = typename storage_type::const_iterator;
276
277 template <class E>
279
280 template <class E>
281 rebind_t<E> build_view(E&& e) const;
282
283 private:
284
285 container_iterator data_xbegin() noexcept;
286 const_container_iterator data_xbegin() const noexcept;
287 container_iterator data_xend(layout_type l, size_type offset) noexcept;
288 const_container_iterator data_xend(layout_type l, size_type offset) const noexcept;
289
290 template <class It>
291 It data_xbegin_impl(It begin) const noexcept;
292
293 template <class It>
294 It data_xend_impl(It end, layout_type l, size_type offset) const noexcept;
295
296 void assign_temporary_impl(temporary_type&& tmp);
297
298 using base_type::set_offset;
299
300 template <class C>
301 friend class xstepper;
302 friend class xview_semantic<self_type>;
303 friend class xaccessible<self_type>;
304 friend class xconst_accessible<self_type>;
305 template <class D>
306 friend class xaxis_iterator;
307 template <class D>
308 friend class xaxis_slice_iterator;
309 };
310
311 /**************************
312 * xstrided_view builders *
313 **************************/
314
315 template <class T>
316 using xstrided_slice = std::variant<
317 T,
318
319 xrange_adaptor<placeholders::xtuph, T, T>,
320 xrange_adaptor<T, placeholders::xtuph, T>,
321 xrange_adaptor<T, T, placeholders::xtuph>,
322
323 xrange_adaptor<T, placeholders::xtuph, placeholders::xtuph>,
324 xrange_adaptor<placeholders::xtuph, T, placeholders::xtuph>,
325 xrange_adaptor<placeholders::xtuph, placeholders::xtuph, T>,
326
327 xrange_adaptor<T, T, T>,
328 xrange_adaptor<placeholders::xtuph, placeholders::xtuph, placeholders::xtuph>,
329
330 xrange<T>,
332
333 xall_tag,
336
341 using xstrided_slice_vector = std::vector<xstrided_slice<std::ptrdiff_t>>;
342
343 template <layout_type L = layout_type::dynamic, class E, class S, class X>
344 auto strided_view(E&& e, S&& shape, X&& stride, std::size_t offset = 0, layout_type layout = L) noexcept;
345
346 template <class E>
347 auto strided_view(E&& e, const xstrided_slice_vector& slices);
348
349 /********************************
350 * xstrided_view implementation *
351 ********************************/
352
357
366 template <class CT, class S, layout_type L, class FST>
367 template <class CTA, class SA>
368 inline xstrided_view<CT, S, L, FST>::xstrided_view(
369 CTA&& e,
370 SA&& shape,
371 strides_type&& strides,
372 std::size_t offset,
374 ) noexcept
375 : base_type(std::forward<CTA>(e), std::forward<SA>(shape), std::move(strides), offset, layout)
376 {
377 }
378
380
381 template <class CT, class S, layout_type L, class FST>
382 inline auto xstrided_view<CT, S, L, FST>::operator=(const self_type& rhs) -> self_type&
383 {
384 temporary_type tmp(rhs);
385 return this->assign_temporary(std::move(tmp));
386 }
387
392
395 template <class CT, class S, layout_type L, class FST>
396 template <class E>
397 inline auto xstrided_view<CT, S, L, FST>::operator=(const xexpression<E>& e) -> self_type&
398 {
399 return semantic_base::operator=(e);
400 }
401
403
404 template <class CT, class S, layout_type L, class FST>
405 template <class E>
406 inline auto xstrided_view<CT, S, L, FST>::operator=(const E& e) -> disable_xexpression<E, self_type>&
407 {
408 this->fill(e);
409 return *this;
410 }
411
412 namespace xstrided_view_detail
413 {
414 template <class V, class T>
415 inline void run_assign_temporary_impl(V& v, const T& t, std::true_type /* enable strided assign */)
416 {
417 strided_loop_assigner<true>::run(v, t);
418 }
419
420 template <class V, class T>
421 inline void
422 run_assign_temporary_impl(V& v, const T& t, std::false_type /* fallback to iterator assign */)
423 {
424 std::copy(t.cbegin(), t.cend(), v.begin());
425 }
426 }
427
428 template <class CT, class S, layout_type L, class FST>
429 inline void xstrided_view<CT, S, L, FST>::assign_temporary_impl(temporary_type&& tmp)
430 {
431 constexpr bool
432 fast_assign = xassign_traits<xstrided_view<CT, S, L, FST>, temporary_type>::simd_strided_assign();
433 xstrided_view_detail::run_assign_temporary_impl(*this, tmp, std::integral_constant<bool, fast_assign>{});
434 }
435
440
445 template <class CT, class S, layout_type L, class FST>
446 template <class T>
447 inline void xstrided_view<CT, S, L, FST>::fill(const T& value)
448 {
450 {
451 std::fill(this->linear_begin(), this->linear_end(), value);
452 }
453 else
454 {
455 std::fill(this->begin(), this->end(), value);
456 }
457 }
458
460
461 template <class CT, class S, layout_type L, class FST>
462 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) -> reference
463 {
464 return storage()[i];
465 }
466
467 template <class CT, class S, layout_type L, class FST>
468 inline auto xstrided_view<CT, S, L, FST>::data_element(size_type i) const -> const_reference
469 {
470 return storage()[i];
471 }
472
473 template <class CT, class S, layout_type L, class FST>
474 inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) -> reference
475 {
476 return storage()[i];
477 }
478
479 template <class CT, class S, layout_type L, class FST>
480 inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) const -> const_reference
481 {
482 return storage()[i];
483 }
484
485 template <class CT, class S, layout_type L, class FST>
486 inline auto xstrided_view<CT, S, L, FST>::linear_begin() -> linear_iterator
487 {
488 return this->storage().begin() + static_cast<std::ptrdiff_t>(data_offset());
489 }
490
491 template <class CT, class S, layout_type L, class FST>
492 inline auto xstrided_view<CT, S, L, FST>::linear_end() -> linear_iterator
493 {
494 return this->storage().begin() + static_cast<std::ptrdiff_t>(data_offset() + size());
495 }
496
497 template <class CT, class S, layout_type L, class FST>
498 inline auto xstrided_view<CT, S, L, FST>::linear_begin() const -> const_linear_iterator
499 {
500 return this->linear_cbegin();
501 }
502
503 template <class CT, class S, layout_type L, class FST>
504 inline auto xstrided_view<CT, S, L, FST>::linear_end() const -> const_linear_iterator
505 {
506 return this->linear_cend();
507 }
508
509 template <class CT, class S, layout_type L, class FST>
510 inline auto xstrided_view<CT, S, L, FST>::linear_cbegin() const -> const_linear_iterator
511 {
512 return this->storage().cbegin() + static_cast<std::ptrdiff_t>(data_offset());
513 }
514
515 template <class CT, class S, layout_type L, class FST>
516 inline auto xstrided_view<CT, S, L, FST>::linear_cend() const -> const_linear_iterator
517 {
518 return this->storage().cbegin() + static_cast<std::ptrdiff_t>(data_offset() + size());
519 }
520
521 template <class CT, class S, layout_type L, class FST>
522 inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() -> reverse_linear_iterator
523 {
524 return reverse_linear_iterator(this->linear_begin());
525 }
526
527 template <class CT, class S, layout_type L, class FST>
528 inline auto xstrided_view<CT, S, L, FST>::linear_rend() -> reverse_linear_iterator
529 {
530 return reverse_linear_iterator(this->linear_end());
531 }
532
533 template <class CT, class S, layout_type L, class FST>
534 inline auto xstrided_view<CT, S, L, FST>::linear_rbegin() const -> const_reverse_linear_iterator
535 {
536 return this->linear_crbegin();
537 }
538
539 template <class CT, class S, layout_type L, class FST>
540 inline auto xstrided_view<CT, S, L, FST>::linear_rend() const -> const_reverse_linear_iterator
541 {
542 return this->linear_crend();
543 }
544
545 template <class CT, class S, layout_type L, class FST>
546 inline auto xstrided_view<CT, S, L, FST>::linear_crbegin() const -> const_reverse_linear_iterator
547 {
548 return const_reverse_linear_iterator(this->linear_cbegin());
549 }
550
551 template <class CT, class S, layout_type L, class FST>
552 inline auto xstrided_view<CT, S, L, FST>::linear_crend() const -> const_reverse_linear_iterator
553 {
554 return const_reverse_linear_iterator(this->linear_cend());
555 }
556
557 /***************
558 * stepper api *
559 ***************/
560
561 template <class CT, class S, layout_type L, class FST>
562 template <class ST, class STEP>
563 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) -> disable_indexed_stepper_t<STEP>
564 {
565 size_type offset = shape.size() - dimension();
566 return stepper(this, data_xbegin(), offset);
567 }
568
569 template <class CT, class S, layout_type L, class FST>
570 template <class ST, class STEP>
572 -> disable_indexed_stepper_t<STEP>
573 {
574 size_type offset = shape.size() - dimension();
575 return stepper(this, data_xend(l, offset), offset);
576 }
577
578 template <class CT, class S, layout_type L, class FST>
579 template <class ST, class STEP>
580 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) -> enable_indexed_stepper_t<STEP>
581 {
582 size_type offset = shape.size() - dimension();
583 return stepper(this, offset);
584 }
585
586 template <class CT, class S, layout_type L, class FST>
587 template <class ST, class STEP>
589 -> enable_indexed_stepper_t<STEP>
590 {
591 size_type offset = shape.size() - dimension();
592 return stepper(this, offset, true);
593 }
594
595 template <class CT, class S, layout_type L, class FST>
596 template <class ST, class STEP>
597 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) const
598 -> disable_indexed_stepper_t<STEP>
599 {
600 size_type offset = shape.size() - dimension();
601 return const_stepper(this, data_xbegin(), offset);
602 }
603
604 template <class CT, class S, layout_type L, class FST>
605 template <class ST, class STEP>
606 inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& shape, layout_type l) const
607 -> disable_indexed_stepper_t<STEP>
608 {
609 size_type offset = shape.size() - dimension();
610 return const_stepper(this, data_xend(l, offset), offset);
611 }
612
613 template <class CT, class S, layout_type L, class FST>
614 template <class ST, class STEP>
615 inline auto xstrided_view<CT, S, L, FST>::stepper_begin(const ST& shape) const
616 -> enable_indexed_stepper_t<STEP>
617 {
618 size_type offset = shape.size() - dimension();
619 return const_stepper(this, offset);
620 }
621
622 template <class CT, class S, layout_type L, class FST>
623 template <class ST, class STEP>
624 inline auto xstrided_view<CT, S, L, FST>::stepper_end(const ST& shape, layout_type /*l*/) const
625 -> enable_indexed_stepper_t<STEP>
626 {
627 size_type offset = shape.size() - dimension();
628 return const_stepper(this, offset, true);
629 }
630
631 template <class CT, class S, layout_type L, class FST>
632 template <class It>
633 inline It xstrided_view<CT, S, L, FST>::data_xbegin_impl(It begin) const noexcept
634 {
635 return begin + static_cast<std::ptrdiff_t>(this->data_offset());
636 }
637
638 template <class CT, class S, layout_type L, class FST>
639 template <class It>
640 inline It
641 xstrided_view<CT, S, L, FST>::data_xend_impl(It begin, layout_type l, size_type offset) const noexcept
642 {
643 return strided_data_end(*this, begin + std::ptrdiff_t(this->data_offset()), l, offset);
644 }
645
646 template <class CT, class S, layout_type L, class FST>
647 inline auto xstrided_view<CT, S, L, FST>::data_xbegin() noexcept -> container_iterator
648 {
649 return data_xbegin_impl(this->storage().begin());
650 }
651
652 template <class CT, class S, layout_type L, class FST>
653 inline auto xstrided_view<CT, S, L, FST>::data_xbegin() const noexcept -> const_container_iterator
654 {
655 return data_xbegin_impl(this->storage().cbegin());
656 }
657
658 template <class CT, class S, layout_type L, class FST>
659 inline auto xstrided_view<CT, S, L, FST>::data_xend(layout_type l, size_type offset) noexcept
660 -> container_iterator
661 {
662 return data_xend_impl(this->storage().begin(), l, offset);
663 }
664
665 template <class CT, class S, layout_type L, class FST>
666 inline auto xstrided_view<CT, S, L, FST>::data_xend(layout_type l, size_type offset) const noexcept
667 -> const_container_iterator
668 {
669 return data_xend_impl(this->storage().cbegin(), l, offset);
670 }
671
672 template <class CT, class S, layout_type L, class FST>
673 template <class alignment, class simd>
674 inline void xstrided_view<CT, S, L, FST>::store_simd(size_type i, const simd& e)
675 requires provides_simd_interface
676 {
677 using align_mode = driven_align_mode_t<alignment, data_alignment>;
678 xt_simd::store_as(&(storage()[i]), e, align_mode());
679 }
680
681 template <class CT, class S, layout_type L, class FST>
682 template <class alignment, class requested_type, std::size_t N>
683 inline auto xstrided_view<CT, S, L, FST>::load_simd(size_type i) const -> simd_return_type<requested_type>
684 requires provides_simd_interface
685 {
686 using align_mode = driven_align_mode_t<alignment, data_alignment>;
687 return xt_simd::load_as<requested_type>(&(storage()[i]), align_mode());
688 }
689
690 template <class CT, class S, layout_type L, class FST>
691 template <class E>
692 inline auto xstrided_view<CT, S, L, FST>::build_view(E&& e) const -> rebind_t<E>
693 {
694 inner_shape_type sh(this->shape());
695 inner_strides_type str(this->strides());
696 return rebind_t<E>(
697 std::forward<E>(e),
698 std::move(sh),
699 std::move(str),
701 this->layout()
702 );
703 }
704
705 /*****************************************
706 * xstrided_view builders implementation *
707 *****************************************/
708
725 template <layout_type L, class E, class S, class X>
726 inline auto strided_view(E&& e, S&& shape, X&& strides, std::size_t offset, layout_type layout) noexcept
727 {
728 using view_type = xstrided_view<xclosure_t<E>, S, L>;
729 return view_type(std::forward<E>(e), std::forward<S>(shape), std::forward<X>(strides), offset, layout);
730 }
731
732 namespace detail
733 {
734 struct no_adj_strides_policy
735 {
736 protected:
737
738 inline void resize(std::size_t)
739 {
740 }
741
742 inline void set_fake_slice(std::size_t)
743 {
744 }
745
746 template <class ST, class S>
747 bool fill_args(
748 const xstrided_slice_vector& /*slices*/,
749 std::size_t /*sl_idx*/,
750 std::size_t /*i*/,
751 std::size_t /*old_shape*/,
752 const ST& /*old_stride*/,
753 S& /*shape*/,
754 get_strides_t<S>& /*strides*/
755 )
756 {
757 return false;
758 }
759 };
760 }
761
787 template <class E>
788 inline auto strided_view(E&& e, const xstrided_slice_vector& slices)
789 {
790 detail::strided_view_args<detail::no_adj_strides_policy> args;
791 args.fill_args(
792 e.shape(),
793 detail::get_strides<XTENSOR_DEFAULT_TRAVERSAL>(e),
794 detail::get_offset<XTENSOR_DEFAULT_TRAVERSAL>(e),
795 e.layout(),
796 slices
797 );
798 using view_type = xstrided_view<xclosure_t<E>, decltype(args.new_shape)>;
799 return view_type(
800 std::forward<E>(e),
801 std::move(args.new_shape),
802 std::move(args.new_strides),
803 args.new_offset,
804 args.new_layout
805 );
806 }
807
808 namespace detail
809 {
810 template <typename S>
811 struct rebind_shape;
812
813 template <std::size_t... X>
814 struct rebind_shape<xt::fixed_shape<X...>>
815 {
816 using type = xt::fixed_shape<X...>;
817 };
818
819 template <class S>
820 struct rebind_shape
821 {
822 using type = rebind_container_t<size_t, S>;
823 };
824
825 template <class S>
826 inline void recalculate_shape_impl(S& shape, size_t size)
827 {
828 if constexpr (std::is_signed_v<get_value_type_t<typename std::decay<S>::type>>)
829 {
830 using value_type = get_value_type_t<typename std::decay_t<S>>;
831 XTENSOR_ASSERT(std::count(shape.cbegin(), shape.cend(), -1) <= 1);
832 auto iter = std::find(shape.begin(), shape.end(), -1);
833 if (iter != std::end(shape))
834 {
835 const auto total = std::accumulate(shape.cbegin(), shape.cend(), -1, std::multiplies<int>{});
836 const auto missing_dimension = size / static_cast<size_t>(total);
837 (*iter) = static_cast<value_type>(missing_dimension);
838 }
839 }
840 }
841
842 template <class S>
843 inline auto recalculate_shape(S&& shape, size_t size)
844 {
845 return recalculate_shape_impl(shape, size);
846 }
847 }
848
849 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class S>
850 inline auto reshape_view(E&& e, S&& shape)
851 {
852 static_assert(
854 "traversal has to be row or column major"
855 );
856
857 using shape_type = std::decay_t<decltype(shape)>;
858 using unsigned_shape_type = typename detail::rebind_shape<shape_type>::type;
859 get_strides_t<unsigned_shape_type> strides;
860
861 detail::recalculate_shape(shape, e.size());
862 xt::resize_container(strides, shape.size());
864 constexpr auto computed_layout = std::decay_t<E>::static_layout == L ? L : layout_type::dynamic;
865 using view_type = xstrided_view<
866 xclosure_t<E>,
867 unsigned_shape_type,
868 computed_layout,
869 detail::flat_adaptor_getter<xclosure_t<E>, L>>;
870 return view_type(
871 std::forward<E>(e),
872 xtl::forward_sequence<unsigned_shape_type, S>(shape),
873 std::move(strides),
874 0,
875 e.layout()
876 );
877 }
878
892 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class S>
893 inline auto reshape_view(E&& e, S&& shape, layout_type /*order*/)
894 {
895 return reshape_view<L>(std::forward<E>(e), std::forward<S>(shape));
896 }
897
898 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class I, std::size_t N>
899 inline auto reshape_view(E&& e, const I (&shape)[N], layout_type order)
900 {
901 using shape_type = std::array<std::size_t, N>;
902 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type, decltype(shape)>(shape), order);
903 }
904
905 template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class I, std::size_t N>
906 inline auto reshape_view(E&& e, const I (&shape)[N])
907 {
908 using shape_type = std::array<I, N>;
909 return reshape_view<L>(std::forward<E>(e), xtl::forward_sequence<shape_type, decltype(shape)>(shape));
910 }
911}
912
913#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
Base class for xexpressions.
Base class for multidimensional iterable expressions.
layout_type layout() const noexcept
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
View of an xexpression using strides.
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.
xstrided_view(CTA &&e, SA &&shape, strides_type &&strides, std::size_t offset, layout_type layout) noexcept
Constructs an xstrided_view.
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:570
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
Definition xstrides.hpp:250
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.