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