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