libstdc++
mask_array.h
Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- mask_array class.
00002 
00003 // Copyright (C) 1997-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/mask_array.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{valarray}
00028  */
00029 
00030 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00031 
00032 #ifndef _MASK_ARRAY_H
00033 #define _MASK_ARRAY_H 1
00034 
00035 #pragma GCC system_header
00036 
00037 namespace std _GLIBCXX_VISIBILITY(default)
00038 {
00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00040 
00041   /**
00042    * @addtogroup numeric_arrays
00043    * @{
00044    */
00045 
00046   /**
00047    *  @brief  Reference to selected subset of an array.
00048    *
00049    *  A mask_array is a reference to the actual elements of an array specified
00050    *  by a bitmask in the form of an array of bool.  The way to get a
00051    *  mask_array is to call operator[](valarray<bool>) on a valarray.  The
00052    *  returned mask_array then permits carrying operations out on the
00053    *  referenced subset of elements in the original valarray.
00054    *
00055    *  For example, if a mask_array is obtained using the array (false, true,
00056    *  false, true) as an argument, the mask array has two elements referring
00057    *  to array[1] and array[3] in the underlying array.
00058    *
00059    *  @param  Tp  Element type.
00060    */
00061   template <class _Tp>
00062     class mask_array
00063     {
00064     public:
00065       typedef _Tp value_type;
00066 
00067       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00068       // 253. valarray helper functions are almost entirely useless
00069 
00070       ///  Copy constructor.  Both slices refer to the same underlying array.
00071       mask_array (const mask_array&);
00072       
00073       ///  Assignment operator.  Assigns elements to corresponding elements
00074       ///  of @a a.
00075       mask_array& operator=(const mask_array&);
00076 
00077       void operator=(const valarray<_Tp>&) const;
00078       ///  Multiply slice elements by corresponding elements of @a v.
00079       void operator*=(const valarray<_Tp>&) const;
00080       ///  Divide slice elements by corresponding elements of @a v.
00081       void operator/=(const valarray<_Tp>&) const;
00082       ///  Modulo slice elements by corresponding elements of @a v.
00083       void operator%=(const valarray<_Tp>&) const;
00084       ///  Add corresponding elements of @a v to slice elements.
00085       void operator+=(const valarray<_Tp>&) const;
00086       ///  Subtract corresponding elements of @a v from slice elements.
00087       void operator-=(const valarray<_Tp>&) const;
00088       ///  Logical xor slice elements with corresponding elements of @a v.
00089       void operator^=(const valarray<_Tp>&) const;
00090       ///  Logical and slice elements with corresponding elements of @a v.
00091       void operator&=(const valarray<_Tp>&) const;
00092       ///  Logical or slice elements with corresponding elements of @a v.
00093       void operator|=(const valarray<_Tp>&) const;
00094       ///  Left shift slice elements by corresponding elements of @a v.
00095       void operator<<=(const valarray<_Tp>&) const;
00096       ///  Right shift slice elements by corresponding elements of @a v.
00097       void operator>>=(const valarray<_Tp>&) const;
00098       ///  Assign all slice elements to @a t.
00099       void operator=(const _Tp&) const;
00100 
00101         //        ~mask_array ();
00102 
00103       template<class _Dom>
00104         void operator=(const _Expr<_Dom,_Tp>&) const;
00105       template<class _Dom>
00106         void operator*=(const _Expr<_Dom,_Tp>&) const;
00107       template<class _Dom>
00108         void operator/=(const _Expr<_Dom,_Tp>&) const;
00109       template<class _Dom>
00110         void operator%=(const _Expr<_Dom,_Tp>&) const;
00111       template<class _Dom>
00112         void operator+=(const _Expr<_Dom,_Tp>&) const;
00113       template<class _Dom>
00114         void operator-=(const _Expr<_Dom,_Tp>&) const;
00115       template<class _Dom>
00116         void operator^=(const _Expr<_Dom,_Tp>&) const;
00117       template<class _Dom>
00118         void operator&=(const _Expr<_Dom,_Tp>&) const;
00119       template<class _Dom>
00120         void operator|=(const _Expr<_Dom,_Tp>&) const;
00121       template<class _Dom>
00122         void operator<<=(const _Expr<_Dom,_Tp>&) const;
00123       template<class _Dom>
00124         void operator>>=(const _Expr<_Dom,_Tp>&) const;
00125 
00126     private:
00127       mask_array(_Array<_Tp>, size_t, _Array<bool>);
00128       friend class valarray<_Tp>;
00129 
00130       const size_t       _M_sz;
00131       const _Array<bool> _M_mask;
00132       const _Array<_Tp>  _M_array;
00133 
00134 #if __cplusplus < 201103L
00135       // not implemented
00136       mask_array();
00137 #else
00138     public:
00139       mask_array() = delete;
00140 #endif
00141     };
00142 
00143   template<typename _Tp>
00144     inline mask_array<_Tp>::mask_array(const mask_array<_Tp>& __a)
00145     : _M_sz(__a._M_sz), _M_mask(__a._M_mask), _M_array(__a._M_array) {}
00146 
00147   template<typename _Tp>
00148     inline
00149     mask_array<_Tp>::mask_array(_Array<_Tp> __a, size_t __s, _Array<bool> __m)
00150     : _M_sz(__s), _M_mask(__m), _M_array(__a) {}
00151 
00152   template<typename _Tp>
00153     inline mask_array<_Tp>&
00154     mask_array<_Tp>::operator=(const mask_array<_Tp>& __a)
00155     {
00156       std::__valarray_copy(__a._M_array, __a._M_mask,
00157                            _M_sz, _M_array, _M_mask);
00158       return *this;
00159     }
00160 
00161   template<typename _Tp>
00162     inline void
00163     mask_array<_Tp>::operator=(const _Tp& __t) const
00164     { std::__valarray_fill(_M_array, _M_sz, _M_mask, __t); }
00165 
00166   template<typename _Tp>
00167     inline void
00168     mask_array<_Tp>::operator=(const valarray<_Tp>& __v) const
00169     { std::__valarray_copy(_Array<_Tp>(__v), __v.size(), _M_array, _M_mask); }
00170 
00171   template<typename _Tp>
00172     template<class _Ex>
00173       inline void
00174       mask_array<_Tp>::operator=(const _Expr<_Ex, _Tp>& __e) const
00175       { std::__valarray_copy(__e, __e.size(), _M_array, _M_mask); }
00176 
00177 #undef _DEFINE_VALARRAY_OPERATOR
00178 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name)                           \
00179   template<typename _Tp>                                                \
00180     inline void                                                         \
00181     mask_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const    \
00182     {                                                                   \
00183       _Array_augmented_##_Name(_M_array, _M_mask,                       \
00184                                _Array<_Tp>(__v), __v.size());           \
00185     }                                                                   \
00186                                                                         \
00187   template<typename _Tp>                                                \
00188     template<class _Dom>                                                \
00189       inline void                                                       \
00190       mask_array<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) const\
00191       {                                                                 \
00192         _Array_augmented_##_Name(_M_array, _M_mask, __e, __e.size());   \
00193       }
00194 
00195 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
00196 _DEFINE_VALARRAY_OPERATOR(/, __divides)
00197 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
00198 _DEFINE_VALARRAY_OPERATOR(+, __plus)
00199 _DEFINE_VALARRAY_OPERATOR(-, __minus)
00200 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
00201 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
00202 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
00203 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
00204 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
00205 
00206 #undef _DEFINE_VALARRAY_OPERATOR
00207 
00208   // @} group numeric_arrays
00209 
00210 _GLIBCXX_END_NAMESPACE_VERSION
00211 } // namespace
00212 
00213 #endif /* _MASK_ARRAY_H */