libstdc++
gslice_array.h
Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- gslice_array class.
00002 
00003 // Copyright (C) 1997-2015 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/gslice_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 _GSLICE_ARRAY_H
00033 #define _GSLICE_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 multi-dimensional subset of an array.
00048    *
00049    *  A gslice_array is a reference to the actual elements of an array
00050    *  specified by a gslice.  The way to get a gslice_array is to call
00051    *  operator[](gslice) on a valarray.  The returned gslice_array then
00052    *  permits carrying operations out on the referenced subset of elements in
00053    *  the original valarray.  For example, operator+=(valarray) will add
00054    *  values to the subset of elements in the underlying valarray this
00055    *  gslice_array refers to.
00056    *
00057    *  @param  Tp  Element type.
00058    */
00059   template<typename _Tp>
00060     class gslice_array
00061     {
00062     public:
00063       typedef _Tp value_type;
00064 
00065       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00066       // 253. valarray helper functions are almost entirely useless
00067 
00068       ///  Copy constructor.  Both slices refer to the same underlying array.
00069       gslice_array(const gslice_array&);
00070 
00071       ///  Assignment operator.  Assigns slice elements to corresponding
00072       ///  elements of @a a.
00073       gslice_array& operator=(const gslice_array&);
00074 
00075       ///  Assign slice elements to corresponding elements of @a v.
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       template<class _Dom>
00101         void operator=(const _Expr<_Dom, _Tp>&) const;
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 
00123     private:
00124       _Array<_Tp>    _M_array;
00125       const valarray<size_t>& _M_index;
00126 
00127       friend class valarray<_Tp>;
00128 
00129       gslice_array(_Array<_Tp>, const valarray<size_t>&);
00130 
00131       // not implemented
00132       gslice_array();
00133     };
00134 
00135   template<typename _Tp>
00136     inline
00137     gslice_array<_Tp>::gslice_array(_Array<_Tp> __a,
00138                                     const valarray<size_t>& __i)
00139     : _M_array(__a), _M_index(__i) {}
00140 
00141   template<typename _Tp>
00142     inline
00143     gslice_array<_Tp>::gslice_array(const gslice_array<_Tp>& __a)
00144     : _M_array(__a._M_array), _M_index(__a._M_index) {}
00145 
00146   template<typename _Tp>
00147     inline gslice_array<_Tp>&
00148     gslice_array<_Tp>::operator=(const gslice_array<_Tp>& __a)
00149     {
00150       std::__valarray_copy(_Array<_Tp>(__a._M_array),
00151                            _Array<size_t>(__a._M_index), _M_index.size(),
00152                            _M_array, _Array<size_t>(_M_index));
00153       return *this;
00154     }
00155 
00156   template<typename _Tp>
00157     inline void
00158     gslice_array<_Tp>::operator=(const _Tp& __t) const
00159     {
00160       std::__valarray_fill(_M_array, _Array<size_t>(_M_index),
00161                            _M_index.size(), __t);
00162     }
00163 
00164   template<typename _Tp>
00165     inline void
00166     gslice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
00167     {
00168       std::__valarray_copy(_Array<_Tp>(__v), __v.size(),
00169                            _M_array, _Array<size_t>(_M_index));
00170     }
00171 
00172   template<typename _Tp>
00173     template<class _Dom>
00174       inline void
00175       gslice_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
00176       {
00177         std::__valarray_copy (__e, _M_index.size(), _M_array,
00178                               _Array<size_t>(_M_index));
00179       }
00180 
00181 #undef _DEFINE_VALARRAY_OPERATOR
00182 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name)                           \
00183   template<typename _Tp>                                                \
00184     inline void                                                         \
00185     gslice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const  \
00186     {                                                                   \
00187       _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index),      \
00188                                _Array<_Tp>(__v), __v.size());           \
00189     }                                                                   \
00190                                                                         \
00191   template<typename _Tp>                                                \
00192     template<class _Dom>                                                \
00193       inline void                                                       \
00194       gslice_array<_Tp>::operator _Op##= (const _Expr<_Dom, _Tp>& __e) const\
00195       {                                                                 \
00196         _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), __e,\
00197                                  _M_index.size());                      \
00198       }
00199 
00200 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
00201 _DEFINE_VALARRAY_OPERATOR(/, __divides)
00202 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
00203 _DEFINE_VALARRAY_OPERATOR(+, __plus)
00204 _DEFINE_VALARRAY_OPERATOR(-, __minus)
00205 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
00206 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
00207 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
00208 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
00209 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
00210 
00211 #undef _DEFINE_VALARRAY_OPERATOR
00212 
00213   // @} group numeric_arrays
00214 
00215 _GLIBCXX_END_NAMESPACE_VERSION
00216 } // namespace
00217 
00218 #endif /* _GSLICE_ARRAY_H */