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