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