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, 1998, 1999, 2000, 2001, 2004, 2005, 2009
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file gslice_array.h
00027  *  This is an internal header file, included by other library headers.
00028  *  You should not attempt to use it directly.
00029  */
00030 
00031 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00032 
00033 #ifndef _GSLICE_ARRAY_H
00034 #define _GSLICE_ARRAY_H 1
00035 
00036 #pragma GCC system_header
00037 
00038 _GLIBCXX_BEGIN_NAMESPACE(std)
00039 
00040   /**
00041    * @addtogroup numeric_arrays
00042    * @{
00043    */
00044 
00045   /**
00046    *  @brief  Reference to multi-dimensional subset of an array.
00047    *
00048    *  A gslice_array is a reference to the actual elements of an array
00049    *  specified by a gslice.  The way to get a gslice_array is to call
00050    *  operator[](gslice) on a valarray.  The returned gslice_array then
00051    *  permits carrying operations out on the referenced subset of elements in
00052    *  the original valarray.  For example, operator+=(valarray) will add
00053    *  values to the subset of elements in the underlying valarray this
00054    *  gslice_array refers to.
00055    *
00056    *  @param  Tp  Element type.
00057    */
00058   template<typename _Tp>
00059     class gslice_array
00060     {
00061     public:
00062       typedef _Tp value_type;
00063 
00064       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00065       // 253. valarray helper functions are almost entirely useless
00066 
00067       ///  Copy constructor.  Both slices refer to the same underlying array.
00068       gslice_array(const gslice_array&);
00069 
00070       ///  Assignment operator.  Assigns slice elements to corresponding
00071       ///  elements of @a a.
00072       gslice_array& operator=(const gslice_array&);
00073 
00074       ///  Assign slice elements to corresponding elements of @a v.
00075       void operator=(const valarray<_Tp>&) const;
00076       ///  Multiply slice elements by corresponding elements of @a v.
00077       void operator*=(const valarray<_Tp>&) const;
00078       ///  Divide slice elements by corresponding elements of @a v.
00079       void operator/=(const valarray<_Tp>&) const;
00080       ///  Modulo slice elements by corresponding elements of @a v.
00081       void operator%=(const valarray<_Tp>&) const;
00082       ///  Add corresponding elements of @a v to slice elements.
00083       void operator+=(const valarray<_Tp>&) const;
00084       ///  Subtract corresponding elements of @a v from slice elements.
00085       void operator-=(const valarray<_Tp>&) const;
00086       ///  Logical xor slice elements with corresponding elements of @a v.
00087       void operator^=(const valarray<_Tp>&) const;
00088       ///  Logical and slice elements with corresponding elements of @a v.
00089       void operator&=(const valarray<_Tp>&) const;
00090       ///  Logical or slice elements with corresponding elements of @a v.
00091       void operator|=(const valarray<_Tp>&) const;
00092       ///  Left shift slice elements by corresponding elements of @a v.
00093       void operator<<=(const valarray<_Tp>&) const;
00094       ///  Right shift slice elements by corresponding elements of @a v.
00095       void operator>>=(const valarray<_Tp>&) const;
00096       ///  Assign all slice elements to @a t.
00097       void operator=(const _Tp&) const;
00098 
00099       template<class _Dom>
00100         void operator=(const _Expr<_Dom, _Tp>&) const;
00101       template<class _Dom>
00102         void operator*=(const _Expr<_Dom, _Tp>&) const;
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 
00122     private:
00123       _Array<_Tp>    _M_array;
00124       const valarray<size_t>& _M_index;
00125 
00126       friend class valarray<_Tp>;
00127 
00128       gslice_array(_Array<_Tp>, const valarray<size_t>&);
00129 
00130       // not implemented
00131       gslice_array();
00132     };
00133 
00134   template<typename _Tp>
00135     inline
00136     gslice_array<_Tp>::gslice_array(_Array<_Tp> __a,
00137                     const valarray<size_t>& __i)
00138     : _M_array(__a), _M_index(__i) {}
00139 
00140   template<typename _Tp>
00141     inline
00142     gslice_array<_Tp>::gslice_array(const gslice_array<_Tp>& __a)
00143     : _M_array(__a._M_array), _M_index(__a._M_index) {}
00144 
00145   template<typename _Tp>
00146     inline gslice_array<_Tp>&
00147     gslice_array<_Tp>::operator=(const gslice_array<_Tp>& __a)
00148     {
00149       std::__valarray_copy(_Array<_Tp>(__a._M_array),
00150                _Array<size_t>(__a._M_index), _M_index.size(),
00151                _M_array, _Array<size_t>(_M_index));
00152       return *this;
00153     }
00154 
00155   template<typename _Tp>
00156     inline void
00157     gslice_array<_Tp>::operator=(const _Tp& __t) const
00158     {
00159       std::__valarray_fill(_M_array, _Array<size_t>(_M_index),
00160                _M_index.size(), __t);
00161     }
00162 
00163   template<typename _Tp>
00164     inline void
00165     gslice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
00166     {
00167       std::__valarray_copy(_Array<_Tp>(__v), __v.size(),
00168                _M_array, _Array<size_t>(_M_index));
00169     }
00170 
00171   template<typename _Tp>
00172     template<class _Dom>
00173       inline void
00174       gslice_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
00175       {
00176     std::__valarray_copy (__e, _M_index.size(), _M_array,
00177                   _Array<size_t>(_M_index));
00178       }
00179 
00180 #undef _DEFINE_VALARRAY_OPERATOR
00181 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name)               \
00182   template<typename _Tp>                        \
00183     inline void                             \
00184     gslice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const  \
00185     {                                   \
00186       _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index),  \
00187                    _Array<_Tp>(__v), __v.size());       \
00188     }                                   \
00189                                     \
00190   template<typename _Tp>                                                \
00191     template<class _Dom>                                \
00192       inline void                           \
00193       gslice_array<_Tp>::operator _Op##= (const _Expr<_Dom, _Tp>& __e) const\
00194       {                                 \
00195     _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), __e,\
00196                  _M_index.size());          \
00197       }
00198 
00199 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
00200 _DEFINE_VALARRAY_OPERATOR(/, __divides)
00201 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
00202 _DEFINE_VALARRAY_OPERATOR(+, __plus)
00203 _DEFINE_VALARRAY_OPERATOR(-, __minus)
00204 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
00205 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
00206 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
00207 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
00208 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
00209 
00210 #undef _DEFINE_VALARRAY_OPERATOR
00211 
00212   // @} group numeric_arrays
00213 
00214 _GLIBCXX_END_NAMESPACE
00215 
00216 #endif /* _GSLICE_ARRAY_H */

Generated on Thu Jul 23 21:16:07 2009 for libstdc++ by  doxygen 1.5.8