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