libstdc++
indirect_array.h
Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- indirect_array class.
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2009, 2010
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 bits/indirect_array.h
00027  *  This is an internal header file, included by other library headers.
00028  *  Do not attempt to use it directly. @headername{valarray}
00029  */
00030 
00031 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00032 
00033 #ifndef _INDIRECT_ARRAY_H
00034 #define _INDIRECT_ARRAY_H 1
00035 
00036 #pragma GCC system_header
00037 
00038 namespace std _GLIBCXX_VISIBILITY(default)
00039 {
00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00041 
00042   /**
00043    * @addtogroup numeric_arrays
00044    * @{
00045    */
00046 
00047   /**
00048    *  @brief  Reference to arbitrary subset of an array.
00049    *
00050    *  An indirect_array is a reference to the actual elements of an array
00051    *  specified by an ordered array of indices.  The way to get an
00052    *  indirect_array is to call operator[](valarray<size_t>) on a valarray.
00053    *  The returned indirect_array then permits carrying operations out on the
00054    *  referenced subset of elements in the original valarray.
00055    *
00056    *  For example, if an indirect_array is obtained using the array (4,2,0) as
00057    *  an argument, and then assigned to an array containing (1,2,3), then the
00058    *  underlying array will have array[0]==3, array[2]==2, and array[4]==1.
00059    *
00060    *  @param  Tp  Element type.
00061    */
00062   template <class _Tp>
00063     class indirect_array
00064     {
00065     public:
00066       typedef _Tp value_type;
00067 
00068       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00069       // 253. valarray helper functions are almost entirely useless
00070 
00071       ///  Copy constructor.  Both slices refer to the same underlying array.
00072       indirect_array(const indirect_array&);
00073 
00074       ///  Assignment operator.  Assigns elements to corresponding elements
00075       ///  of @a a.
00076       indirect_array& operator=(const indirect_array&);
00077 
00078       ///  Assign slice elements to corresponding elements of @a v.
00079       void operator=(const valarray<_Tp>&) const;
00080       ///  Multiply slice elements by corresponding elements of @a v.
00081       void operator*=(const valarray<_Tp>&) const;
00082       ///  Divide slice elements by corresponding elements of @a v.
00083       void operator/=(const valarray<_Tp>&) const;
00084       ///  Modulo slice elements by corresponding elements of @a v.
00085       void operator%=(const valarray<_Tp>&) const;
00086       ///  Add corresponding elements of @a v to slice elements.
00087       void operator+=(const valarray<_Tp>&) const;
00088       ///  Subtract corresponding elements of @a v from slice elements.
00089       void operator-=(const valarray<_Tp>&) const;
00090       ///  Logical xor slice elements with corresponding elements of @a v.
00091       void operator^=(const valarray<_Tp>&) const;
00092       ///  Logical and slice elements with corresponding elements of @a v.
00093       void operator&=(const valarray<_Tp>&) const;
00094       ///  Logical or slice elements with corresponding elements of @a v.
00095       void operator|=(const valarray<_Tp>&) const;
00096       ///  Left shift slice elements by corresponding elements of @a v.
00097       void operator<<=(const valarray<_Tp>&) const;
00098       ///  Right shift slice elements by corresponding elements of @a v.
00099       void operator>>=(const valarray<_Tp>&) const;
00100       ///  Assign all slice elements to @a t.
00101       void operator= (const _Tp&) const;
00102       //    ~indirect_array();
00103 
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       template<class _Dom>
00125       void operator>>=(const _Expr<_Dom, _Tp>&) const;
00126 
00127     private:
00128       ///  Copy constructor.  Both slices refer to the same underlying array.
00129       indirect_array(_Array<_Tp>, size_t, _Array<size_t>);
00130 
00131       friend class valarray<_Tp>;
00132       friend class gslice_array<_Tp>;
00133 
00134       const size_t   _M_sz;
00135       const _Array<size_t> _M_index;
00136       const _Array<_Tp>  _M_array;
00137 
00138       // not implemented
00139       indirect_array();
00140     };
00141 
00142   template<typename _Tp>
00143     inline
00144     indirect_array<_Tp>::indirect_array(const indirect_array<_Tp>& __a)
00145     : _M_sz(__a._M_sz), _M_index(__a._M_index), _M_array(__a._M_array) {}
00146 
00147   template<typename _Tp>
00148     inline
00149     indirect_array<_Tp>::indirect_array(_Array<_Tp> __a, size_t __s,
00150                     _Array<size_t> __i)
00151     : _M_sz(__s), _M_index(__i), _M_array(__a) {}
00152 
00153   template<typename _Tp>
00154     inline indirect_array<_Tp>&
00155     indirect_array<_Tp>::operator=(const indirect_array<_Tp>& __a)
00156     {
00157       std::__valarray_copy(__a._M_array, _M_sz, __a._M_index, _M_array,
00158                _M_index);
00159       return *this;
00160     }
00161 
00162   template<typename _Tp>
00163     inline void
00164     indirect_array<_Tp>::operator=(const _Tp& __t) const
00165     { std::__valarray_fill(_M_array, _M_index, _M_sz, __t); }
00166 
00167   template<typename _Tp>
00168     inline void
00169     indirect_array<_Tp>::operator=(const valarray<_Tp>& __v) const
00170     { std::__valarray_copy(_Array<_Tp>(__v), _M_sz, _M_array, _M_index); }
00171 
00172   template<typename _Tp>
00173     template<class _Dom>
00174       inline void
00175       indirect_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
00176       { std::__valarray_copy(__e, _M_sz, _M_array, _M_index); }
00177 
00178 #undef _DEFINE_VALARRAY_OPERATOR
00179 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name)               \
00180   template<typename _Tp>                        \
00181     inline void                             \
00182     indirect_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const\
00183     {                                   \
00184       _Array_augmented_##_Name(_M_array, _M_index, _Array<_Tp>(__v), _M_sz); \
00185     }                                   \
00186                                     \
00187   template<typename _Tp>                                                \
00188     template<class _Dom>                                \
00189       inline void                           \
00190       indirect_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
00191       {                                 \
00192     _Array_augmented_##_Name(_M_array, _M_index, __e, _M_sz);   \
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 /* _INDIRECT_ARRAY_H */