# 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
    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 2, 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 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
    00032
    00033 /** @file slice_array.h
    00034  *  This is an internal header file, included by other library headers.
    00035  *  You should not attempt to use it directly.
    00036  */
    00037
    00038 #ifndef _SLICE_ARRAY_H
    00039 #define _SLICE_ARRAY_H 1
    00040
    00041 #pragma GCC system_header
    00042
    00043 namespace std
    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   // The default constructor constructor is not required to initialize
    00087   // data members with any meaningful values, so we choose to do nothing.
    00088   inline
    00089   slice::slice() {}
    00090
    00091   inline
    00092   slice::slice(size_t __o, size_t __d, size_t __s)
    00093     : _M_off(__o), _M_sz(__d), _M_st(__s) {}
    00094
    00095   inline size_t
    00096   slice::start() const
    00097   { return _M_off; }
    00098
    00099   inline size_t
    00100   slice::size() const
    00101   { return _M_sz; }
    00102
    00103   inline size_t
    00104   slice::stride() const
    00105   { return _M_st; }
    00106
    00107   /**
    00108    *  @brief  Reference to one-dimensional subset of an array.
    00109    *
    00110    *  A slice_array is a reference to the actual elements of an array
    00111    *  specified by a slice.  The way to get a slice_array is to call
    00112    *  operator[](slice) on a valarray.  The returned slice_array then permits
    00113    *  carrying operations out on the referenced subset of elements in the
    00114    *  original valarray.  For example, operator+=(valarray) will add values
    00115    *  to the subset of elements in the underlying valarray this slice_array
    00116    *  refers to.
    00117    *
    00118    *  @param  Tp  Element type.
    00119    */
    00120   template<typename _Tp>
    00121     class slice_array
    00122     {
    00123     public:
    00124       typedef _Tp value_type;
    00125
    00126       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00127       // 253. valarray helper functions are almost entirely useless
    00128
    00129       ///  Copy constructor.  Both slices refer to the same underlying array.
    00130       slice_array(const slice_array&);
    00131
    00132       ///  Assignment operator.  Assigns slice elements to corresponding
    00133       ///  elements of @a a.
    00134       slice_array& operator=(const slice_array&);
    00135
    00136       ///  Assign slice elements to corresponding elements of @a v.
    00137       void operator=(const valarray<_Tp>&) const;
    00138       ///  Multiply slice elements by corresponding elements of @a v.
    00139       void operator*=(const valarray<_Tp>&) const;
    00140       ///  Divide slice elements by corresponding elements of @a v.
    00141       void operator/=(const valarray<_Tp>&) const;
    00142       ///  Modulo slice elements by corresponding elements of @a v.
    00143       void operator%=(const valarray<_Tp>&) const;
    00144       ///  Add corresponding elements of @a v to slice elements.
    00145       void operator+=(const valarray<_Tp>&) const;
    00146       ///  Subtract corresponding elements of @a v from slice elements.
    00147       void operator-=(const valarray<_Tp>&) const;
    00148       ///  Logical xor slice elements with corresponding elements of @a v.
    00149       void operator^=(const valarray<_Tp>&) const;
    00150       ///  Logical and slice elements with corresponding elements of @a v.
    00151       void operator&=(const valarray<_Tp>&) const;
    00152       ///  Logical or slice elements with corresponding elements of @a v.
    00153       void operator|=(const valarray<_Tp>&) const;
    00154       ///  Left shift slice elements by corresponding elements of @a v.
    00155       void operator<<=(const valarray<_Tp>&) const;
    00156       ///  Right shift slice elements by corresponding elements of @a v.
    00157       void operator>>=(const valarray<_Tp>&) const;
    00158       ///  Assign all slice elements to @a t.
    00159       void operator=(const _Tp &) const;
    00160       //        ~slice_array ();
    00161
    00162       template<class _Dom>
    00163     void operator=(const _Expr<_Dom,_Tp>&) const;
    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
    00185     private:
    00186       friend class valarray<_Tp>;
    00187       slice_array(_Array<_Tp>, const slice&);
    00188
    00189       const size_t     _M_sz;
    00190       const size_t     _M_stride;
    00191       const _Array<_Tp> _M_array;
    00192
    00193       // not implemented
    00194       slice_array();
    00195     };
    00196
    00197   template<typename _Tp>
    00198     inline
    00199     slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
    00200     : _M_sz(__s.size()), _M_stride(__s.stride()),
    00201       _M_array(__a.begin() + __s.start()) {}
    00202
    00203   template<typename _Tp>
    00204     inline
    00205     slice_array<_Tp>::slice_array(const slice_array<_Tp>& a)
    00206     : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
    00207
    00208   //    template<typename _Tp>
    00209   //    inline slice_array<_Tp>::~slice_array () {}
    00210
    00211   template<typename _Tp>
    00212     inline slice_array<_Tp>&
    00213     slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
    00214     {
    00215       std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
    00216                _M_array, _M_stride);
    00217       return *this;
    00218     }
    00219
    00220   template<typename _Tp>
    00221     inline void
    00222     slice_array<_Tp>::operator=(const _Tp& __t) const
    00223     { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
    00224
    00225   template<typename _Tp>
    00226     inline void
    00227     slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
    00228     { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
    00229
    00230   template<typename _Tp>
    00231   template<class _Dom>
    00232     inline void
    00233     slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
    00234     { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
    00235
    00236 #undef _DEFINE_VALARRAY_OPERATOR
    00237 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name)                \
    00238   template<typename _Tp>                        \
    00239     inline void                             \
    00240     slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const   \
    00241     {                                   \
    00242       _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
    00243     }                                   \
    00244                                     \
    00245   template<typename _Tp>                                                \
    00246     template<class _Dom>                                \
    00247       inline void                           \
    00248       slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
    00249       {                                 \
    00250       _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz);    \
    00251       }
    00252
    00253
    00254 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
    00255 _DEFINE_VALARRAY_OPERATOR(/, __divides)
    00256 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
    00257 _DEFINE_VALARRAY_OPERATOR(+, __plus)
    00258 _DEFINE_VALARRAY_OPERATOR(-, __minus)
    00259 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
    00260 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
    00261 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
    00262 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
    00263 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
    00264
    00265 #undef _DEFINE_VALARRAY_OPERATOR
    00266
    00267 } // std::
    00268
    00269 #endif /* _SLICE_ARRAY_H */
    00270
    00271 // Local Variables:
    00272 // mode:c++
    00273 // End: