xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/bits/mask_array.h (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14fee23f9Smrg // The template and inlines for the -*- C++ -*- mask_array class.
24fee23f9Smrg 
3*b1e83836Smrg // Copyright (C) 1997-2022 Free Software Foundation, Inc.
44fee23f9Smrg //
54fee23f9Smrg // This file is part of the GNU ISO C++ Library.  This library is free
64fee23f9Smrg // software; you can redistribute it and/or modify it under the
74fee23f9Smrg // terms of the GNU General Public License as published by the
84fee23f9Smrg // Free Software Foundation; either version 3, or (at your option)
94fee23f9Smrg // any later version.
104fee23f9Smrg 
114fee23f9Smrg // This library is distributed in the hope that it will be useful,
124fee23f9Smrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
134fee23f9Smrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144fee23f9Smrg // GNU General Public License for more details.
154fee23f9Smrg 
164fee23f9Smrg // Under Section 7 of GPL version 3, you are granted additional
174fee23f9Smrg // permissions described in the GCC Runtime Library Exception, version
184fee23f9Smrg // 3.1, as published by the Free Software Foundation.
194fee23f9Smrg 
204fee23f9Smrg // You should have received a copy of the GNU General Public License and
214fee23f9Smrg // a copy of the GCC Runtime Library Exception along with this program;
224fee23f9Smrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
234fee23f9Smrg // <http://www.gnu.org/licenses/>.
244fee23f9Smrg 
2548fb7bfaSmrg /** @file bits/mask_array.h
264fee23f9Smrg  *  This is an internal header file, included by other library headers.
2748fb7bfaSmrg  *  Do not attempt to use it directly. @headername{valarray}
284fee23f9Smrg  */
294fee23f9Smrg 
304fee23f9Smrg // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
314fee23f9Smrg 
324fee23f9Smrg #ifndef _MASK_ARRAY_H
334fee23f9Smrg #define _MASK_ARRAY_H 1
344fee23f9Smrg 
354fee23f9Smrg #pragma GCC system_header
364fee23f9Smrg 
_GLIBCXX_VISIBILITY(default)3748fb7bfaSmrg namespace std _GLIBCXX_VISIBILITY(default)
3848fb7bfaSmrg {
3948fb7bfaSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
404fee23f9Smrg 
414fee23f9Smrg   /**
424fee23f9Smrg    * @addtogroup numeric_arrays
434fee23f9Smrg    * @{
444fee23f9Smrg    */
454fee23f9Smrg 
464fee23f9Smrg   /**
474fee23f9Smrg    *  @brief  Reference to selected subset of an array.
484fee23f9Smrg    *
494fee23f9Smrg    *  A mask_array is a reference to the actual elements of an array specified
504fee23f9Smrg    *  by a bitmask in the form of an array of bool.  The way to get a
514fee23f9Smrg    *  mask_array is to call operator[](valarray<bool>) on a valarray.  The
524fee23f9Smrg    *  returned mask_array then permits carrying operations out on the
534fee23f9Smrg    *  referenced subset of elements in the original valarray.
544fee23f9Smrg    *
554fee23f9Smrg    *  For example, if a mask_array is obtained using the array (false, true,
564fee23f9Smrg    *  false, true) as an argument, the mask array has two elements referring
574fee23f9Smrg    *  to array[1] and array[3] in the underlying array.
584fee23f9Smrg    *
594fee23f9Smrg    *  @param  Tp  Element type.
604fee23f9Smrg    */
614fee23f9Smrg   template <class _Tp>
624fee23f9Smrg     class mask_array
634fee23f9Smrg     {
644fee23f9Smrg     public:
654fee23f9Smrg       typedef _Tp value_type;
664fee23f9Smrg 
674fee23f9Smrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
684fee23f9Smrg       // 253. valarray helper functions are almost entirely useless
694fee23f9Smrg 
704fee23f9Smrg       ///  Copy constructor.  Both slices refer to the same underlying array.
714fee23f9Smrg       mask_array (const mask_array&);
724fee23f9Smrg 
734fee23f9Smrg       ///  Assignment operator.  Assigns elements to corresponding elements
744fee23f9Smrg       ///  of @a a.
754fee23f9Smrg       mask_array& operator=(const mask_array&);
764fee23f9Smrg 
774fee23f9Smrg       void operator=(const valarray<_Tp>&) const;
784fee23f9Smrg       ///  Multiply slice elements by corresponding elements of @a v.
794fee23f9Smrg       void operator*=(const valarray<_Tp>&) const;
804fee23f9Smrg       ///  Divide slice elements by corresponding elements of @a v.
814fee23f9Smrg       void operator/=(const valarray<_Tp>&) const;
824fee23f9Smrg       ///  Modulo slice elements by corresponding elements of @a v.
834fee23f9Smrg       void operator%=(const valarray<_Tp>&) const;
844fee23f9Smrg       ///  Add corresponding elements of @a v to slice elements.
854fee23f9Smrg       void operator+=(const valarray<_Tp>&) const;
864fee23f9Smrg       ///  Subtract corresponding elements of @a v from slice elements.
874fee23f9Smrg       void operator-=(const valarray<_Tp>&) const;
884fee23f9Smrg       ///  Logical xor slice elements with corresponding elements of @a v.
894fee23f9Smrg       void operator^=(const valarray<_Tp>&) const;
904fee23f9Smrg       ///  Logical and slice elements with corresponding elements of @a v.
914fee23f9Smrg       void operator&=(const valarray<_Tp>&) const;
924fee23f9Smrg       ///  Logical or slice elements with corresponding elements of @a v.
934fee23f9Smrg       void operator|=(const valarray<_Tp>&) const;
944fee23f9Smrg       ///  Left shift slice elements by corresponding elements of @a v.
954fee23f9Smrg       void operator<<=(const valarray<_Tp>&) const;
964fee23f9Smrg       ///  Right shift slice elements by corresponding elements of @a v.
974fee23f9Smrg       void operator>>=(const valarray<_Tp>&) const;
984fee23f9Smrg       ///  Assign all slice elements to @a t.
994fee23f9Smrg       void operator=(const _Tp&) const;
1004fee23f9Smrg 
1014fee23f9Smrg         //        ~mask_array ();
1024fee23f9Smrg 
1034fee23f9Smrg       template<class _Dom>
1044fee23f9Smrg         void operator=(const _Expr<_Dom,_Tp>&) const;
1054fee23f9Smrg       template<class _Dom>
1064fee23f9Smrg         void operator*=(const _Expr<_Dom,_Tp>&) const;
1074fee23f9Smrg       template<class _Dom>
1084fee23f9Smrg         void operator/=(const _Expr<_Dom,_Tp>&) const;
1094fee23f9Smrg       template<class _Dom>
1104fee23f9Smrg         void operator%=(const _Expr<_Dom,_Tp>&) const;
1114fee23f9Smrg       template<class _Dom>
1124fee23f9Smrg         void operator+=(const _Expr<_Dom,_Tp>&) const;
1134fee23f9Smrg       template<class _Dom>
1144fee23f9Smrg         void operator-=(const _Expr<_Dom,_Tp>&) const;
1154fee23f9Smrg       template<class _Dom>
1164fee23f9Smrg         void operator^=(const _Expr<_Dom,_Tp>&) const;
1174fee23f9Smrg       template<class _Dom>
1184fee23f9Smrg         void operator&=(const _Expr<_Dom,_Tp>&) const;
1194fee23f9Smrg       template<class _Dom>
1204fee23f9Smrg         void operator|=(const _Expr<_Dom,_Tp>&) const;
1214fee23f9Smrg       template<class _Dom>
1224fee23f9Smrg         void operator<<=(const _Expr<_Dom,_Tp>&) const;
1234fee23f9Smrg       template<class _Dom>
1244fee23f9Smrg         void operator>>=(const _Expr<_Dom,_Tp>&) const;
1254fee23f9Smrg 
1264fee23f9Smrg     private:
1274fee23f9Smrg       mask_array(_Array<_Tp>, size_t, _Array<bool>);
1284fee23f9Smrg       friend class valarray<_Tp>;
1294fee23f9Smrg 
1304fee23f9Smrg       const size_t       _M_sz;
1314fee23f9Smrg       const _Array<bool> _M_mask;
1324fee23f9Smrg       const _Array<_Tp>  _M_array;
1334fee23f9Smrg 
134181254a7Smrg #if __cplusplus < 201103L
1354fee23f9Smrg       // not implemented
1364fee23f9Smrg       mask_array();
137181254a7Smrg #else
138181254a7Smrg     public:
139181254a7Smrg       mask_array() = delete;
140181254a7Smrg #endif
1414fee23f9Smrg     };
1424fee23f9Smrg 
1434fee23f9Smrg   template<typename _Tp>
1443f4ceed9Smrg     inline mask_array<_Tp>::mask_array(const mask_array<_Tp>& __a)
1453f4ceed9Smrg     : _M_sz(__a._M_sz), _M_mask(__a._M_mask), _M_array(__a._M_array) {}
1464fee23f9Smrg 
1474fee23f9Smrg   template<typename _Tp>
1484fee23f9Smrg     inline
1494fee23f9Smrg     mask_array<_Tp>::mask_array(_Array<_Tp> __a, size_t __s, _Array<bool> __m)
1504fee23f9Smrg     : _M_sz(__s), _M_mask(__m), _M_array(__a) {}
1514fee23f9Smrg 
1524fee23f9Smrg   template<typename _Tp>
1534fee23f9Smrg     inline mask_array<_Tp>&
1544fee23f9Smrg     mask_array<_Tp>::operator=(const mask_array<_Tp>& __a)
1554fee23f9Smrg     {
1564fee23f9Smrg       std::__valarray_copy(__a._M_array, __a._M_mask,
1574fee23f9Smrg 			   _M_sz, _M_array, _M_mask);
1584fee23f9Smrg       return *this;
1594fee23f9Smrg     }
1604fee23f9Smrg 
1614fee23f9Smrg   template<typename _Tp>
1624fee23f9Smrg     inline void
1634fee23f9Smrg     mask_array<_Tp>::operator=(const _Tp& __t) const
1644fee23f9Smrg     { std::__valarray_fill(_M_array, _M_sz, _M_mask, __t); }
1654fee23f9Smrg 
1664fee23f9Smrg   template<typename _Tp>
1674fee23f9Smrg     inline void
1684fee23f9Smrg     mask_array<_Tp>::operator=(const valarray<_Tp>& __v) const
1694fee23f9Smrg     { std::__valarray_copy(_Array<_Tp>(__v), __v.size(), _M_array, _M_mask); }
1704fee23f9Smrg 
1714fee23f9Smrg   template<typename _Tp>
1724fee23f9Smrg     template<class _Ex>
1734fee23f9Smrg       inline void
1744fee23f9Smrg       mask_array<_Tp>::operator=(const _Expr<_Ex, _Tp>& __e) const
1754fee23f9Smrg       { std::__valarray_copy(__e, __e.size(), _M_array, _M_mask); }
1764fee23f9Smrg 
1777d4dc15bSmrg   /// @cond undocumented
1784fee23f9Smrg #undef _DEFINE_VALARRAY_OPERATOR
1794fee23f9Smrg #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name)				\
1804fee23f9Smrg   template<typename _Tp>						\
1814fee23f9Smrg     inline void								\
1824fee23f9Smrg     mask_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const	\
1834fee23f9Smrg     {									\
1844fee23f9Smrg       _Array_augmented_##_Name(_M_array, _M_mask,			\
1854fee23f9Smrg 			       _Array<_Tp>(__v), __v.size());		\
1864fee23f9Smrg     }									\
1874fee23f9Smrg 									\
1884fee23f9Smrg   template<typename _Tp>                                                \
1894fee23f9Smrg     template<class _Dom>			                        \
1904fee23f9Smrg       inline void							\
1914fee23f9Smrg       mask_array<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) const\
1924fee23f9Smrg       {									\
1934fee23f9Smrg 	_Array_augmented_##_Name(_M_array, _M_mask, __e, __e.size());   \
1944fee23f9Smrg       }
1954fee23f9Smrg 
1964fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
1974fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(/, __divides)
1984fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(%, __modulus)
1994fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(+, __plus)
2004fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(-, __minus)
2014fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
2024fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
2034fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
2044fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
2054fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
2064fee23f9Smrg 
2074fee23f9Smrg #undef _DEFINE_VALARRAY_OPERATOR
2087d4dc15bSmrg   /// @endcond
2094fee23f9Smrg 
210a448f87cSmrg   /// @} group numeric_arrays
2114fee23f9Smrg 
21248fb7bfaSmrg _GLIBCXX_END_NAMESPACE_VERSION
21348fb7bfaSmrg } // namespace
2144fee23f9Smrg 
2154fee23f9Smrg #endif /* _MASK_ARRAY_H */
216