libstdc++
slice_array.h
Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- slice_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/slice_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 _SLICE_ARRAY_H
00033 #define _SLICE_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  Class defining one-dimensional subset of an array.
00048    *
00049    *  The slice class represents a one-dimensional subset of an array,
00050    *  specified by three parameters: start offset, size, and stride.  The
00051    *  start offset is the index of the first element of the array that is part
00052    *  of the subset.  The size is the total number of elements in the subset.
00053    *  Stride is the distance between each successive array element to include
00054    *  in the subset.
00055    *
00056    *  For example, with an array of size 10, and a slice with offset 1, size 3
00057    *  and stride 2, the subset consists of array elements 1, 3, and 5.
00058    */
00059   class slice
00060   {
00061   public:
00062     ///  Construct an empty slice.
00063     slice();
00064 
00065     /**
00066      *  @brief  Construct a slice.
00067      *
00068      *  @param  __o  Offset in array of first element.
00069      *  @param  __d  Number of elements in slice.
00070      *  @param  __s  Stride between array elements.
00071      */
00072     slice(size_t __o, size_t __d, size_t __s);
00073 
00074     ///  Return array offset of first slice element.
00075     size_t start() const;
00076     ///  Return size of slice.
00077     size_t size() const;
00078     ///  Return array stride of slice.
00079     size_t stride() const;
00080 
00081   private:
00082     size_t _M_off;                      // offset
00083     size_t _M_sz;           // size
00084     size_t _M_st;           // stride unit
00085   };
00086 
00087   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00088   // 543. valarray slice default constructor
00089   inline
00090   slice::slice() 
00091   : _M_off(0), _M_sz(0), _M_st(0) {}
00092 
00093   inline
00094   slice::slice(size_t __o, size_t __d, size_t __s)
00095   : _M_off(__o), _M_sz(__d), _M_st(__s) {}
00096 
00097   inline size_t
00098   slice::start() const
00099   { return _M_off; }
00100 
00101   inline size_t
00102   slice::size() const
00103   { return _M_sz; }
00104 
00105   inline size_t
00106   slice::stride() const
00107   { return _M_st; }
00108 
00109   /**
00110    *  @brief  Reference to one-dimensional subset of an array.
00111    *
00112    *  A slice_array is a reference to the actual elements of an array
00113    *  specified by a slice.  The way to get a slice_array is to call
00114    *  operator[](slice) on a valarray.  The returned slice_array then permits
00115    *  carrying operations out on the referenced subset of elements in the
00116    *  original valarray.  For example, operator+=(valarray) will add values
00117    *  to the subset of elements in the underlying valarray this slice_array
00118    *  refers to.
00119    *
00120    *  @param  Tp  Element type.
00121    */
00122   template<typename _Tp>
00123     class slice_array
00124     {
00125     public:
00126       typedef _Tp value_type;
00127 
00128       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00129       // 253. valarray helper functions are almost entirely useless
00130 
00131       ///  Copy constructor.  Both slices refer to the same underlying array.
00132       slice_array(const slice_array&);
00133 
00134       ///  Assignment operator.  Assigns slice elements to corresponding
00135       ///  elements of @a a.
00136       slice_array& operator=(const slice_array&);
00137 
00138       ///  Assign slice elements to corresponding elements of @a v.
00139       void operator=(const valarray<_Tp>&) const;
00140       ///  Multiply slice elements by corresponding elements of @a v.
00141       void operator*=(const valarray<_Tp>&) const;
00142       ///  Divide slice elements by corresponding elements of @a v.
00143       void operator/=(const valarray<_Tp>&) const;
00144       ///  Modulo slice elements by corresponding elements of @a v.
00145       void operator%=(const valarray<_Tp>&) const;
00146       ///  Add corresponding elements of @a v to slice elements.
00147       void operator+=(const valarray<_Tp>&) const;
00148       ///  Subtract corresponding elements of @a v from slice elements.
00149       void operator-=(const valarray<_Tp>&) const;
00150       ///  Logical xor slice elements with corresponding elements of @a v.
00151       void operator^=(const valarray<_Tp>&) const;
00152       ///  Logical and slice elements with corresponding elements of @a v.
00153       void operator&=(const valarray<_Tp>&) const;
00154       ///  Logical or slice elements with corresponding elements of @a v.
00155       void operator|=(const valarray<_Tp>&) const;
00156       ///  Left shift slice elements by corresponding elements of @a v.
00157       void operator<<=(const valarray<_Tp>&) const;
00158       ///  Right shift slice elements by corresponding elements of @a v.
00159       void operator>>=(const valarray<_Tp>&) const;
00160       ///  Assign all slice elements to @a t.
00161       void operator=(const _Tp &) const;
00162       //        ~slice_array ();
00163 
00164       template<class _Dom>
00165         void operator=(const _Expr<_Dom, _Tp>&) const;
00166       template<class _Dom>
00167     void operator*=(const _Expr<_Dom, _Tp>&) const;
00168       template<class _Dom>
00169     void operator/=(const _Expr<_Dom, _Tp>&) const;
00170       template<class _Dom>
00171     void operator%=(const _Expr<_Dom, _Tp>&) const;
00172       template<class _Dom>
00173     void operator+=(const _Expr<_Dom, _Tp>&) const;
00174       template<class _Dom>
00175     void operator-=(const _Expr<_Dom, _Tp>&) const;
00176       template<class _Dom>
00177     void operator^=(const _Expr<_Dom, _Tp>&) const;
00178       template<class _Dom>
00179     void operator&=(const _Expr<_Dom, _Tp>&) const;
00180       template<class _Dom>
00181     void operator|=(const _Expr<_Dom, _Tp>&) const;
00182       template<class _Dom>
00183     void operator<<=(const _Expr<_Dom, _Tp>&) const;
00184       template<class _Dom>
00185     void operator>>=(const _Expr<_Dom, _Tp>&) const;
00186 
00187     private:
00188       friend class valarray<_Tp>;
00189       slice_array(_Array<_Tp>, const slice&);
00190 
00191       const size_t      _M_sz;
00192       const size_t      _M_stride;
00193       const _Array<_Tp> _M_array;
00194 
00195       // not implemented
00196       slice_array();
00197     };
00198 
00199   template<typename _Tp>
00200     inline
00201     slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
00202     : _M_sz(__s.size()), _M_stride(__s.stride()),
00203       _M_array(__a.begin() + __s.start()) {}
00204 
00205   template<typename _Tp>
00206     inline
00207     slice_array<_Tp>::slice_array(const slice_array<_Tp>& a)
00208     : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
00209 
00210   //    template<typename _Tp>
00211   //    inline slice_array<_Tp>::~slice_array () {}
00212 
00213   template<typename _Tp>
00214     inline slice_array<_Tp>&
00215     slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
00216     {
00217       std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
00218                _M_array, _M_stride);
00219       return *this;
00220     }
00221 
00222   template<typename _Tp>
00223     inline void
00224     slice_array<_Tp>::operator=(const _Tp& __t) const
00225     { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
00226 
00227   template<typename _Tp>
00228     inline void
00229     slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
00230     { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
00231 
00232   template<typename _Tp>
00233   template<class _Dom>
00234     inline void
00235     slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
00236     { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
00237 
00238 #undef _DEFINE_VALARRAY_OPERATOR
00239 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name)                \
00240   template<typename _Tp>                        \
00241     inline void                             \
00242     slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const   \
00243     {                                   \
00244       _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
00245     }                                   \
00246                                     \
00247   template<typename _Tp>                                                \
00248     template<class _Dom>                                \
00249       inline void                           \
00250       slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
00251       {                                 \
00252       _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz);    \
00253       }
00254 
00255 
00256 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
00257 _DEFINE_VALARRAY_OPERATOR(/, __divides)
00258 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
00259 _DEFINE_VALARRAY_OPERATOR(+, __plus)
00260 _DEFINE_VALARRAY_OPERATOR(-, __minus)
00261 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
00262 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
00263 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
00264 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
00265 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
00266 
00267 #undef _DEFINE_VALARRAY_OPERATOR
00268 
00269   // @} group numeric_arrays
00270 
00271 _GLIBCXX_END_NAMESPACE_VERSION
00272 } // namespace
00273 
00274 #endif /* _SLICE_ARRAY_H */