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-2013 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/indirect_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 _INDIRECT_ARRAY_H
00033 #define _INDIRECT_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 arbitrary subset of an array.
00048    *
00049    *  An indirect_array is a reference to the actual elements of an array
00050    *  specified by an ordered array of indices.  The way to get an
00051    *  indirect_array is to call operator[](valarray<size_t>) on a valarray.
00052    *  The returned indirect_array then permits carrying operations out on the
00053    *  referenced subset of elements in the original valarray.
00054    *
00055    *  For example, if an indirect_array is obtained using the array (4,2,0) as
00056    *  an argument, and then assigned to an array containing (1,2,3), then the
00057    *  underlying array will have array[0]==3, array[2]==2, and array[4]==1.
00058    *
00059    *  @param  Tp  Element type.
00060    */
00061   template <class _Tp>
00062     class indirect_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       indirect_array(const indirect_array&);
00072 
00073       ///  Assignment operator.  Assigns elements to corresponding elements
00074       ///  of @a a.
00075       indirect_array& operator=(const indirect_array&);
00076 
00077       ///  Assign slice elements to corresponding elements of @a v.
00078       void operator=(const valarray<_Tp>&) const;
00079       ///  Multiply slice elements by corresponding elements of @a v.
00080       void operator*=(const valarray<_Tp>&) const;
00081       ///  Divide slice elements by corresponding elements of @a v.
00082       void operator/=(const valarray<_Tp>&) const;
00083       ///  Modulo slice elements by corresponding elements of @a v.
00084       void operator%=(const valarray<_Tp>&) const;
00085       ///  Add corresponding elements of @a v to slice elements.
00086       void operator+=(const valarray<_Tp>&) const;
00087       ///  Subtract corresponding elements of @a v from slice elements.
00088       void operator-=(const valarray<_Tp>&) const;
00089       ///  Logical xor slice elements with corresponding elements of @a v.
00090       void operator^=(const valarray<_Tp>&) const;
00091       ///  Logical and slice elements with corresponding elements of @a v.
00092       void operator&=(const valarray<_Tp>&) const;
00093       ///  Logical or slice elements with corresponding elements of @a v.
00094       void operator|=(const valarray<_Tp>&) const;
00095       ///  Left shift slice elements by corresponding elements of @a v.
00096       void operator<<=(const valarray<_Tp>&) const;
00097       ///  Right shift slice elements by corresponding elements of @a v.
00098       void operator>>=(const valarray<_Tp>&) const;
00099       ///  Assign all slice elements to @a t.
00100       void operator= (const _Tp&) const;
00101       //    ~indirect_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       ///  Copy constructor.  Both slices refer to the same underlying array.
00128       indirect_array(_Array<_Tp>, size_t, _Array<size_t>);
00129 
00130       friend class valarray<_Tp>;
00131       friend class gslice_array<_Tp>;
00132 
00133       const size_t   _M_sz;
00134       const _Array<size_t> _M_index;
00135       const _Array<_Tp>  _M_array;
00136 
00137       // not implemented
00138       indirect_array();
00139     };
00140 
00141   template<typename _Tp>
00142     inline
00143     indirect_array<_Tp>::indirect_array(const indirect_array<_Tp>& __a)
00144     : _M_sz(__a._M_sz), _M_index(__a._M_index), _M_array(__a._M_array) {}
00145 
00146   template<typename _Tp>
00147     inline
00148     indirect_array<_Tp>::indirect_array(_Array<_Tp> __a, size_t __s,
00149                     _Array<size_t> __i)
00150     : _M_sz(__s), _M_index(__i), _M_array(__a) {}
00151 
00152   template<typename _Tp>
00153     inline indirect_array<_Tp>&
00154     indirect_array<_Tp>::operator=(const indirect_array<_Tp>& __a)
00155     {
00156       std::__valarray_copy(__a._M_array, _M_sz, __a._M_index, _M_array,
00157                _M_index);
00158       return *this;
00159     }
00160 
00161   template<typename _Tp>
00162     inline void
00163     indirect_array<_Tp>::operator=(const _Tp& __t) const
00164     { std::__valarray_fill(_M_array, _M_index, _M_sz, __t); }
00165 
00166   template<typename _Tp>
00167     inline void
00168     indirect_array<_Tp>::operator=(const valarray<_Tp>& __v) const
00169     { std::__valarray_copy(_Array<_Tp>(__v), _M_sz, _M_array, _M_index); }
00170 
00171   template<typename _Tp>
00172     template<class _Dom>
00173       inline void
00174       indirect_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
00175       { std::__valarray_copy(__e, _M_sz, _M_array, _M_index); }
00176 
00177 #undef _DEFINE_VALARRAY_OPERATOR
00178 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name)               \
00179   template<typename _Tp>                        \
00180     inline void                             \
00181     indirect_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const\
00182     {                                   \
00183       _Array_augmented_##_Name(_M_array, _M_index, _Array<_Tp>(__v), _M_sz); \
00184     }                                   \
00185                                     \
00186   template<typename _Tp>                                                \
00187     template<class _Dom>                                \
00188       inline void                           \
00189       indirect_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
00190       {                                 \
00191     _Array_augmented_##_Name(_M_array, _M_index, __e, _M_sz);   \
00192       }
00193 
00194 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
00195 _DEFINE_VALARRAY_OPERATOR(/, __divides)
00196 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
00197 _DEFINE_VALARRAY_OPERATOR(+, __plus)
00198 _DEFINE_VALARRAY_OPERATOR(-, __minus)
00199 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
00200 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
00201 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
00202 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
00203 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
00204 
00205 #undef _DEFINE_VALARRAY_OPERATOR
00206 
00207   // @} group numeric_arrays
00208 
00209 _GLIBCXX_END_NAMESPACE_VERSION
00210 } // namespace
00211 
00212 #endif /* _INDIRECT_ARRAY_H */