xtensor
Loading...
Searching...
No Matches
xmasked_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_XMASKED_VIEW_HPP
11#define XTENSOR_XMASKED_VIEW_HPP
12
13#include "xaccessible.hpp"
14#include "xexpression.hpp"
15#include "xiterable.hpp"
16#include "xsemantic.hpp"
17#include "xshape.hpp"
18#include "xtensor_forward.hpp"
19#include "xtl/xmasked_value.hpp"
20#include "xutils.hpp"
21
22namespace xt
23{
24 /****************************
25 * xmasked_view declaration *
26 *****************************/
27
28 template <class CTD, class CTM>
29 class xmasked_view;
30
31 template <class D, bool is_const>
32 class xmasked_view_stepper;
33
34 template <class T>
35 struct xcontainer_inner_types;
36
37 template <class CTD, class CTM>
39 {
40 using data_type = std::decay_t<CTD>;
41 using mask_type = std::decay_t<CTM>;
42 using base_value_type = typename data_type::value_type;
43 using flag_type = typename mask_type::value_type;
46 using value_type = xtl::xmasked_value<base_value_type, flag_type>;
47 using reference = xtl::xmasked_value<val_reference, mask_reference>;
48 using const_reference = xtl::xmasked_value<typename data_type::const_reference, typename mask_type::const_reference>;
49 using size_type = typename data_type::size_type;
51 };
52
53 template <class CTD, class CTM>
55 {
57 using inner_shape_type = typename std::decay_t<CTD>::inner_shape_type;
60 };
61
77 template <class CTD, class CTM>
78 class xmasked_view : public xview_semantic<xmasked_view<CTD, CTM>>,
79 private xaccessible<xmasked_view<CTD, CTM>>,
80 private xiterable<xmasked_view<CTD, CTM>>
81 {
82 public:
83
88 using temporary_type = typename inner_types::temporary_type;
89
90 using data_type = typename inner_types::data_type;
91 using mask_type = typename inner_types::mask_type;
92 using value_expression = CTD;
93 using mask_expression = CTM;
94
95 static constexpr bool is_data_const = std::is_const<std::remove_reference_t<value_expression>>::value;
96
97 using base_value_type = typename inner_types::base_value_type;
98 using base_reference = typename data_type::reference;
99 using base_const_reference = typename data_type::const_reference;
100
101 using flag_type = typename inner_types::flag_type;
102 using flag_reference = typename mask_type::reference;
103 using flag_const_reference = typename mask_type::const_reference;
104
105 using val_reference = typename inner_types::val_reference;
106 using mask_reference = typename inner_types::mask_reference;
107
108 using value_type = typename inner_types::value_type;
109 using reference = typename inner_types::reference;
110 using const_reference = typename inner_types::const_reference;
111
112 using pointer = xtl::xclosure_pointer<reference>;
113 using const_pointer = xtl::xclosure_pointer<const_reference>;
114
115 using size_type = typename inner_types::size_type;
116 using difference_type = typename data_type::difference_type;
117
118 using bool_load_type = xtl::xmasked_value<typename data_type::bool_load_type, mask_type>;
119
120 using shape_type = typename data_type::shape_type;
121 using strides_type = typename data_type::strides_type;
122
123 static constexpr layout_type static_layout = data_type::static_layout;
124 static constexpr bool contiguous_layout = false;
125
126 using inner_shape_type = typename data_type::inner_shape_type;
127 using inner_strides_type = typename data_type::inner_strides_type;
128 using inner_backstrides_type = typename data_type::inner_backstrides_type;
129
131
133 using stepper = typename iterable_base::stepper;
134 using const_stepper = typename iterable_base::const_stepper;
135
136 template <layout_type L>
137 using layout_iterator = typename iterable_base::template layout_iterator<L>;
138 template <layout_type L>
139 using const_layout_iterator = typename iterable_base::template const_layout_iterator<L>;
140 template <layout_type L>
141 using reverse_layout_iterator = typename iterable_base::template reverse_layout_iterator<L>;
142 template <layout_type L>
143 using const_reverse_layout_iterator = typename iterable_base::template const_reverse_layout_iterator<L>;
144
145 template <class S, layout_type L>
146 using broadcast_iterator = typename iterable_base::template broadcast_iterator<S, L>;
147 template <class S, layout_type L>
148 using const_broadcast_iterator = typename iterable_base::template const_broadcast_iterator<S, L>;
149 template <class S, layout_type L>
150 using reverse_broadcast_iterator = typename iterable_base::template reverse_broadcast_iterator<S, L>;
151 template <class S, layout_type L>
152 using const_reverse_broadcast_iterator = typename iterable_base::template const_reverse_broadcast_iterator<S, L>;
153
154 using iterator = typename iterable_base::iterator;
155 using const_iterator = typename iterable_base::const_iterator;
156 using reverse_iterator = typename iterable_base::reverse_iterator;
157 using const_reverse_iterator = typename iterable_base::const_reverse_iterator;
158
159 template <class D, class M>
160 xmasked_view(D&& data, M&& mask);
161
162 xmasked_view(const xmasked_view&) = default;
163
164 size_type size() const noexcept;
165 const inner_shape_type& shape() const noexcept;
166 const inner_strides_type& strides() const noexcept;
167 const inner_backstrides_type& backstrides() const noexcept;
170
171 layout_type layout() const noexcept;
172 bool is_contiguous() const noexcept;
173
174 template <class T>
175 void fill(const T& value);
176
177 template <class... Args>
178 reference operator()(Args... args);
179
180 template <class... Args>
181 const_reference operator()(Args... args) const;
182
183 template <class... Args>
184 reference unchecked(Args... args);
185
186 template <class... Args>
187 const_reference unchecked(Args... args) const;
188
189 using accessible_base::at;
190 using accessible_base::operator[];
194 using accessible_base::periodic;
195
196 template <class It>
197 reference element(It first, It last);
198
199 template <class It>
200 const_reference element(It first, It last) const;
201
202 template <class S>
203 bool has_linear_assign(const S& strides) const noexcept;
204
205 data_type& value() noexcept;
206 const data_type& value() const noexcept;
207
208 mask_type& visible() noexcept;
209 const mask_type& visible() const noexcept;
210
211 using iterable_base::begin;
212 using iterable_base::cbegin;
213 using iterable_base::cend;
214 using iterable_base::crbegin;
215 using iterable_base::crend;
216 using iterable_base::end;
217 using iterable_base::rbegin;
218 using iterable_base::rend;
219
220 template <class S>
221 stepper stepper_begin(const S& shape) noexcept;
222 template <class S>
223 stepper stepper_end(const S& shape, layout_type l) noexcept;
224
225 template <class S>
226 const_stepper stepper_begin(const S& shape) const noexcept;
227 template <class S>
228 const_stepper stepper_end(const S& shape, layout_type l) const noexcept;
229
230 self_type& operator=(const self_type& rhs);
231
232 template <class E>
233 self_type& operator=(const xexpression<E>& e);
234
235 template <class E>
236 disable_xexpression<E, self_type>& operator=(const E& e);
237
238 private:
239
240 CTD m_data;
241 CTM m_mask;
242
243 void assign_temporary_impl(temporary_type&& tmp);
244
245 friend class xiterable<self_type>;
246 friend class xconst_iterable<self_type>;
247 friend class xview_semantic<self_type>;
248 friend class xaccessible<self_type>;
249 friend class xconst_accessible<self_type>;
250 };
251
252 template <class D, bool is_const>
254 {
255 public:
256
258 using masked_view_type = std::decay_t<D>;
259 using value_type = typename masked_view_type::value_type;
260 using reference = std::
261 conditional_t<is_const, typename masked_view_type::const_reference, typename masked_view_type::reference>;
262 using pointer = std::
263 conditional_t<is_const, typename masked_view_type::const_pointer, typename masked_view_type::pointer>;
264 using size_type = typename masked_view_type::size_type;
265 using difference_type = typename masked_view_type::difference_type;
266 using data_type = typename masked_view_type::data_type;
267 using mask_type = typename masked_view_type::mask_type;
268 using value_stepper = std::conditional_t<is_const, typename data_type::const_stepper, typename data_type::stepper>;
269 using mask_stepper = std::conditional_t<is_const, typename mask_type::const_stepper, typename mask_type::stepper>;
270
271 xmasked_view_stepper(value_stepper vs, mask_stepper fs) noexcept;
272
273
274 void step(size_type dim);
275 void step_back(size_type dim);
276 void step(size_type dim, size_type n);
277 void step_back(size_type dim, size_type n);
278 void reset(size_type dim);
279 void reset_back(size_type dim);
280
281 void to_begin();
282 void to_end(layout_type l);
283
284 reference operator*() const;
285
286 private:
287
288 value_stepper m_vs;
289 mask_stepper m_ms;
290 };
291
292 /*******************************
293 * xmasked_view implementation *
294 *******************************/
295
307 template <class CTD, class CTM>
308 template <class D, class M>
310 : m_data(std::forward<D>(data))
311 , m_mask(std::forward<M>(mask))
312 {
313 }
314
322 template <class CTD, class CTM>
323 inline auto xmasked_view<CTD, CTM>::size() const noexcept -> size_type
324 {
325 return m_data.size();
326 }
327
331 template <class CTD, class CTM>
332 inline auto xmasked_view<CTD, CTM>::shape() const noexcept -> const inner_shape_type&
333 {
334 return m_data.shape();
335 }
336
340 template <class CTD, class CTM>
341 inline auto xmasked_view<CTD, CTM>::strides() const noexcept -> const inner_strides_type&
342 {
343 return m_data.strides();
344 }
345
349 template <class CTD, class CTM>
350 inline auto xmasked_view<CTD, CTM>::backstrides() const noexcept -> const inner_backstrides_type&
351 {
352 return m_data.backstrides();
353 }
354
356
361 template <class CTD, class CTM>
363 {
364 return m_data.layout();
365 }
366
367 template <class CTD, class CTM>
369 {
370 return false;
371 }
372
377 template <class CTD, class CTM>
378 template <class T>
379 inline void xmasked_view<CTD, CTM>::fill(const T& value)
380 {
381 std::fill(this->begin(), this->end(), value);
382 }
383
394 template <class CTD, class CTM>
395 template <class... Args>
396 inline auto xmasked_view<CTD, CTM>::operator()(Args... args) -> reference
397 {
398 return reference(m_data(args...), m_mask(args...));
399 }
400
407 template <class CTD, class CTM>
408 template <class... Args>
409 inline auto xmasked_view<CTD, CTM>::operator()(Args... args) const -> const_reference
410 {
411 return const_reference(m_data(args...), m_mask(args...));
412 }
413
433 template <class CTD, class CTM>
434 template <class... Args>
435 inline auto xmasked_view<CTD, CTM>::unchecked(Args... args) -> reference
436 {
437 return reference(m_data.unchecked(args...), m_mask.unchecked(args...));
438 }
439
459 template <class CTD, class CTM>
460 template <class... Args>
461 inline auto xmasked_view<CTD, CTM>::unchecked(Args... args) const -> const_reference
462 {
463 return const_reference(m_data.unchecked(args...), m_mask.unchecked(args...));
464 }
465
473 template <class CTD, class CTM>
474 template <class It>
475 inline auto xmasked_view<CTD, CTM>::element(It first, It last) -> reference
476 {
477 return reference(m_data.element(first, last), m_mask.element(first, last));
478 }
479
487 template <class CTD, class CTM>
488 template <class It>
489 inline auto xmasked_view<CTD, CTM>::element(It first, It last) const -> const_reference
490 {
491 return const_reference(m_data.element(first, last), m_mask.element(first, last));
492 }
493
495
496 template <class CTD, class CTM>
497 template <class S>
498 inline bool xmasked_view<CTD, CTM>::has_linear_assign(const S& strides) const noexcept
499 {
500 return m_data.has_linear_assign(strides) && m_mask.has_linear_assign(strides);
501 }
502
506 template <class CTD, class CTM>
507 inline auto xmasked_view<CTD, CTM>::value() noexcept -> data_type&
508 {
509 return m_data;
510 }
511
515 template <class CTD, class CTM>
517 {
518 return m_data;
519 }
520
524 template <class CTD, class CTM>
525 inline auto xmasked_view<CTD, CTM>::visible() noexcept -> mask_type&
526 {
527 return m_mask;
528 }
529
533 template <class CTD, class CTM>
535 {
536 return m_mask;
537 }
538
539 template <class CTD, class CTM>
540 template <class S>
541 inline auto xmasked_view<CTD, CTM>::stepper_begin(const S& shape) noexcept -> stepper
542 {
543 return stepper(value().stepper_begin(shape), visible().stepper_begin(shape));
544 }
545
546 template <class CTD, class CTM>
547 template <class S>
548 inline auto xmasked_view<CTD, CTM>::stepper_end(const S& shape, layout_type l) noexcept -> stepper
549 {
550 return stepper(value().stepper_end(shape, l), visible().stepper_end(shape, l));
551 }
552
553 template <class CTD, class CTM>
554 template <class S>
555 inline auto xmasked_view<CTD, CTM>::stepper_begin(const S& shape) const noexcept -> const_stepper
556 {
557 return const_stepper(value().stepper_begin(shape), visible().stepper_begin(shape));
558 }
559
560 template <class CTD, class CTM>
561 template <class S>
562 inline auto xmasked_view<CTD, CTM>::stepper_end(const S& shape, layout_type l) const noexcept
563 -> const_stepper
564 {
565 return const_stepper(value().stepper_end(shape, l), visible().stepper_end(shape, l));
566 }
567
568 template <class CTD, class CTM>
569 inline auto xmasked_view<CTD, CTM>::operator=(const self_type& rhs) -> self_type&
570 {
571 temporary_type tmp(rhs);
572 return this->assign_temporary(std::move(tmp));
573 }
574
575 template <class CTD, class CTM>
576 template <class E>
577 inline auto xmasked_view<CTD, CTM>::operator=(const xexpression<E>& e) -> self_type&
578 {
579 return semantic_base::operator=(e);
580 }
581
582 template <class CTD, class CTM>
583 template <class E>
584 inline auto xmasked_view<CTD, CTM>::operator=(const E& e) -> disable_xexpression<E, self_type>&
585 {
586 std::fill(this->begin(), this->end(), e);
587 return *this;
588 }
589
590 template <class CTD, class CTM>
591 inline void xmasked_view<CTD, CTM>::assign_temporary_impl(temporary_type&& tmp)
592 {
593 std::copy(tmp.cbegin(), tmp.cend(), this->begin());
594 }
595
596 template <class CTD, class CTM>
597 inline xmasked_view<CTD, CTM> masked_view(CTD&& data, CTM&& mask)
598 {
599 return xmasked_view<CTD, CTM>(std::forward<CTD>(data), std::forward<CTM>(mask));
600 }
601
602 /***************************************
603 * xmasked_view_stepper implementation *
604 ***************************************/
605
606 template <class D, bool C>
607 inline xmasked_view_stepper<D, C>::xmasked_view_stepper(value_stepper vs, mask_stepper ms) noexcept
608 : m_vs(vs)
609 , m_ms(ms)
610 {
611 }
612
613 template <class D, bool C>
614 inline void xmasked_view_stepper<D, C>::step(size_type dim)
615 {
616 m_vs.step(dim);
617 m_ms.step(dim);
618 }
619
620 template <class D, bool C>
621 inline void xmasked_view_stepper<D, C>::step_back(size_type dim)
622 {
623 m_vs.step_back(dim);
624 m_ms.step_back(dim);
625 }
626
627 template <class D, bool C>
628 inline void xmasked_view_stepper<D, C>::step(size_type dim, size_type n)
629 {
630 m_vs.step(dim, n);
631 m_ms.step(dim, n);
632 }
633
634 template <class D, bool C>
635 inline void xmasked_view_stepper<D, C>::step_back(size_type dim, size_type n)
636 {
637 m_vs.step_back(dim, n);
638 m_ms.step_back(dim, n);
639 }
640
641 template <class D, bool C>
642 inline void xmasked_view_stepper<D, C>::reset(size_type dim)
643 {
644 m_vs.reset(dim);
645 m_ms.reset(dim);
646 }
647
648 template <class D, bool C>
649 inline void xmasked_view_stepper<D, C>::reset_back(size_type dim)
650 {
651 m_vs.reset_back(dim);
652 m_ms.reset_back(dim);
653 }
654
655 template <class D, bool C>
656 inline void xmasked_view_stepper<D, C>::to_begin()
657 {
658 m_vs.to_begin();
659 m_ms.to_begin();
660 }
661
662 template <class D, bool C>
663 inline void xmasked_view_stepper<D, C>::to_end(layout_type l)
664 {
665 m_vs.to_end(l);
666 m_ms.to_end(l);
667 }
668
669 template <class D, bool C>
670 inline auto xmasked_view_stepper<D, C>::operator*() const -> reference
671 {
672 return reference(*m_vs, *m_ms);
673 }
674}
675
676#endif
Base class for implementation of common expression access methods.
reference back()
Returns a reference to the last element of the expression.
reference front()
Returns a reference to the first element of the expression.
Base class for implementation of common expression constant access methods.
size_type dimension() const noexcept
Returns the number of dimensions of the expression.
bool in_bounds(Args... args) const
Returns true only if the the specified position is a valid entry in the expression.
size_type shape(size_type index) const
Returns the i-th dimension of the expression.
Base class for multidimensional iterable constant expressions.
Definition xiterable.hpp:37
Base class for multidimensional iterable expressions.
View on an xoptional_assembly or xoptional_assembly_adaptor hiding values depending on a given mask.
const inner_strides_type & strides() const noexcept
Returns the strides of the xmasked_view.
const inner_backstrides_type & backstrides() const noexcept
Returns the backstrides of the xmasked_view.
const inner_shape_type & shape() const noexcept
Returns the shape of the xmasked_view.
mask_type & visible() noexcept
Return an expression for the mask of the xmasked_view.
void fill(const T &value)
Fills the data with the given value.
layout_type layout() const noexcept
Return the layout_type of the xmasked_view.
data_type & value() noexcept
Return an expression for the values of the xmasked_view.
size_type size() const noexcept
Returns the number of elements in the xmasked_view.
xmasked_view(D &&data, M &&mask)
Creates an xmasked_view, given the xoptional_assembly or xoptional_assembly_adaptor and the mask.
Implementation of the xsemantic_base interface for multidimensional views.
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
Definition xstrides.hpp:248
standard mathematical functions for xexpressions
layout_type
Definition xlayout.hpp:24