libstdc++
mask_array.h
Go to the documentation of this file.
1 // The template and inlines for the -*- C++ -*- mask_array class.
2 
3 // Copyright (C) 1997-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/mask_array.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{valarray}
28  */
29 
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31 
32 #ifndef _MASK_ARRAY_H
33 #define _MASK_ARRAY_H 1
34 
35 #ifdef _GLIBCXX_SYSHDR
36 #pragma GCC system_header
37 #endif
38 
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  /**
44  * @addtogroup numeric_arrays
45  * @{
46  */
47 
48  /**
49  * @brief Reference to selected subset of an array.
50  *
51  * A mask_array is a reference to the actual elements of an array specified
52  * by a bitmask in the form of an array of bool. The way to get a
53  * mask_array is to call operator[](valarray<bool>) on a valarray. The
54  * returned mask_array then permits carrying operations out on the
55  * referenced subset of elements in the original valarray.
56  *
57  * For example, if a mask_array is obtained using the array (false, true,
58  * false, true) as an argument, the mask array has two elements referring
59  * to array[1] and array[3] in the underlying array.
60  *
61  * @param Tp Element type.
62  */
63  template <class _Tp>
64  class mask_array
65  {
66  public:
67  typedef _Tp value_type;
68 
69  // _GLIBCXX_RESOLVE_LIB_DEFECTS
70  // 253. valarray helper functions are almost entirely useless
71 
72  /// Copy constructor. Both slices refer to the same underlying array.
73  mask_array (const mask_array&);
74 
75  /// Assignment operator. Assigns elements to corresponding elements
76  /// of @a a.
78 
79  void operator=(const valarray<_Tp>&) const;
80  /// Multiply slice elements by corresponding elements of @a v.
81  void operator*=(const valarray<_Tp>&) const;
82  /// Divide slice elements by corresponding elements of @a v.
83  void operator/=(const valarray<_Tp>&) const;
84  /// Modulo slice elements by corresponding elements of @a v.
85  void operator%=(const valarray<_Tp>&) const;
86  /// Add corresponding elements of @a v to slice elements.
87  void operator+=(const valarray<_Tp>&) const;
88  /// Subtract corresponding elements of @a v from slice elements.
89  void operator-=(const valarray<_Tp>&) const;
90  /// Logical xor slice elements with corresponding elements of @a v.
91  void operator^=(const valarray<_Tp>&) const;
92  /// Logical and slice elements with corresponding elements of @a v.
93  void operator&=(const valarray<_Tp>&) const;
94  /// Logical or slice elements with corresponding elements of @a v.
95  void operator|=(const valarray<_Tp>&) const;
96  /// Left shift slice elements by corresponding elements of @a v.
97  void operator<<=(const valarray<_Tp>&) const;
98  /// Right shift slice elements by corresponding elements of @a v.
99  void operator>>=(const valarray<_Tp>&) const;
100  /// Assign all slice elements to @a t.
101  void operator=(const _Tp&) const;
102 
103  // ~mask_array ();
104 
105  template<class _Dom>
106  void operator=(const _Expr<_Dom,_Tp>&) const;
107  template<class _Dom>
108  void operator*=(const _Expr<_Dom,_Tp>&) const;
109  template<class _Dom>
110  void operator/=(const _Expr<_Dom,_Tp>&) const;
111  template<class _Dom>
112  void operator%=(const _Expr<_Dom,_Tp>&) const;
113  template<class _Dom>
114  void operator+=(const _Expr<_Dom,_Tp>&) const;
115  template<class _Dom>
116  void operator-=(const _Expr<_Dom,_Tp>&) const;
117  template<class _Dom>
118  void operator^=(const _Expr<_Dom,_Tp>&) const;
119  template<class _Dom>
120  void operator&=(const _Expr<_Dom,_Tp>&) const;
121  template<class _Dom>
122  void operator|=(const _Expr<_Dom,_Tp>&) const;
123  template<class _Dom>
124  void operator<<=(const _Expr<_Dom,_Tp>&) const;
125  template<class _Dom>
126  void operator>>=(const _Expr<_Dom,_Tp>&) const;
127 
128  private:
129  mask_array(_Array<_Tp>, size_t, _Array<bool>);
130  friend class valarray<_Tp>;
131 
132  const size_t _M_sz;
133  const _Array<bool> _M_mask;
134  const _Array<_Tp> _M_array;
135 
136 #if __cplusplus < 201103L
137  // not implemented
138  mask_array();
139 #else
140  public:
141  mask_array() = delete;
142 #endif
143  };
144 
145  template<typename _Tp>
147  : _M_sz(__a._M_sz), _M_mask(__a._M_mask), _M_array(__a._M_array) {}
148 
149  template<typename _Tp>
150  inline
151  mask_array<_Tp>::mask_array(_Array<_Tp> __a, size_t __s, _Array<bool> __m)
152  : _M_sz(__s), _M_mask(__m), _M_array(__a) {}
153 
154  template<typename _Tp>
155  inline mask_array<_Tp>&
157  {
158  __glibcxx_assert(__a._M_sz == _M_sz);
159  std::__valarray_copy(__a._M_array, __a._M_mask,
160  _M_sz, _M_array, _M_mask);
161  return *this;
162  }
163 
164  template<typename _Tp>
165  inline void
166  mask_array<_Tp>::operator=(const _Tp& __t) const
167  { std::__valarray_fill(_M_array, _M_sz, _M_mask, __t); }
168 
169  template<typename _Tp>
170  inline void
171  mask_array<_Tp>::operator=(const valarray<_Tp>& __v) const
172  {
173  __glibcxx_assert(__v.size() == _M_sz);
174  std::__valarray_copy(_Array<_Tp>(__v), __v.size(), _M_array, _M_mask);
175  }
176 
177  template<typename _Tp>
178  template<class _Ex>
179  inline void
180  mask_array<_Tp>::operator=(const _Expr<_Ex, _Tp>& __e) const
181  {
182  __glibcxx_assert(__e.size() == _M_sz);
183  std::__valarray_copy(__e, __e.size(), _M_array, _M_mask);
184  }
185 
186  /// @cond undocumented
187 #undef _DEFINE_VALARRAY_OPERATOR
188 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
189  template<typename _Tp> \
190  inline void \
191  mask_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
192  { \
193  __glibcxx_assert(__v.size() == _M_sz); \
194  _Array_augmented_##_Name(_M_array, _M_mask, \
195  _Array<_Tp>(__v), __v.size()); \
196  } \
197  \
198  template<typename _Tp> \
199  template<class _Dom> \
200  inline void \
201  mask_array<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) const\
202  { \
203  __glibcxx_assert(__e.size() == _M_sz); \
204  _Array_augmented_##_Name(_M_array, _M_mask, __e, __e.size()); \
205  }
206 
207 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
208 _DEFINE_VALARRAY_OPERATOR(/, __divides)
209 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
210 _DEFINE_VALARRAY_OPERATOR(+, __plus)
211 _DEFINE_VALARRAY_OPERATOR(-, __minus)
212 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
213 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
214 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
215 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
216 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
217 
218 #undef _DEFINE_VALARRAY_OPERATOR
219  /// @endcond
220 
221  /// @} group numeric_arrays
222 
223 _GLIBCXX_END_NAMESPACE_VERSION
224 } // namespace
225 
226 #endif /* _MASK_ARRAY_H */
size_t size() const
Return the number of elements in array.
Definition: valarray:949
mask_array & operator=(const mask_array &)
Assignment operator. Assigns elements to corresponding elements of a.
Definition: mask_array.h:156
ISO C++ entities toplevel namespace is std.
Smart array designed to support numeric processing.
Definition: valarray:132
Reference to selected subset of an array.
Definition: mask_array.h:65
void operator*=(const valarray< _Tp > &) const
Multiply slice elements by corresponding elements of v.
void operator+=(const valarray< _Tp > &) const
Add corresponding elements of v to slice elements.
void operator&=(const valarray< _Tp > &) const
Logical and slice elements with corresponding elements of v.
void operator|=(const valarray< _Tp > &) const
Logical or slice elements with corresponding elements of v.
void operator-=(const valarray< _Tp > &) const
Subtract corresponding elements of v from slice elements.
void operator>>=(const valarray< _Tp > &) const
Right shift slice elements by corresponding elements of v.
void operator%=(const valarray< _Tp > &) const
Modulo slice elements by corresponding elements of v.
void operator^=(const valarray< _Tp > &) const
Logical xor slice elements with corresponding elements of v.
void operator/=(const valarray< _Tp > &) const
Divide slice elements by corresponding elements of v.
void operator<<=(const valarray< _Tp > &) const
Left shift slice elements by corresponding elements of v.