libstdc++
gslice.h
Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- gslice class.
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2005, 2006, 2009, 2010, 
00004 // 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/gslice.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 _GSLICE_H
00034 #define _GSLICE_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 multi-dimensional subset of an array.
00049    *
00050    *  The slice class represents a multi-dimensional subset of an array,
00051    *  specified by three parameter sets: start offset, size array, and stride
00052    *  array.  The start offset is the index of the first element of the array
00053    *  that is part of the subset.  The size and stride array describe each
00054    *  dimension of the slice.  Size is the number of elements in that
00055    *  dimension, and stride is the distance in the array between successive
00056    *  elements in that dimension.  Each dimension's size and stride is taken
00057    *  to begin at an array element described by the previous dimension.  The
00058    *  size array and stride array must be the same size.
00059    *
00060    *  For example, if you have offset==3, stride[0]==11, size[1]==3,
00061    *  stride[1]==3, then slice[0,0]==array[3], slice[0,1]==array[6],
00062    *  slice[0,2]==array[9], slice[1,0]==array[14], slice[1,1]==array[17],
00063    *  slice[1,2]==array[20].
00064    */
00065   class gslice
00066   {
00067   public:
00068     ///  Construct an empty slice.
00069     gslice();
00070 
00071     /**
00072      *  @brief  Construct a slice.
00073      *
00074      *  Constructs a slice with as many dimensions as the length of the @a l
00075      *  and @a s arrays.
00076      *
00077      *  @param  __o  Offset in array of first element.
00078      *  @param  __l  Array of dimension lengths.
00079      *  @param  __s  Array of dimension strides between array elements.
00080      */
00081     gslice(size_t __o, const valarray<size_t>& __l, 
00082        const valarray<size_t>& __s);
00083 
00084     // XXX: the IS says the copy-ctor and copy-assignment operators are
00085     //      synthesized by the compiler but they are just unsuitable
00086     //      for a ref-counted semantic
00087     ///  Copy constructor.
00088     gslice(const gslice&);
00089 
00090     ///  Destructor.
00091     ~gslice();
00092 
00093     // XXX: See the note above.
00094     ///  Assignment operator.
00095     gslice& operator=(const gslice&);
00096 
00097     ///  Return array offset of first slice element.
00098     size_t           start() const;
00099 
00100     ///  Return array of sizes of slice dimensions.
00101     valarray<size_t> size() const;
00102     
00103     ///  Return array of array strides for each dimension.
00104     valarray<size_t> stride() const;
00105 
00106   private:
00107     struct _Indexer
00108     {
00109       size_t _M_count;
00110       size_t _M_start;
00111       valarray<size_t> _M_size;
00112       valarray<size_t> _M_stride;
00113       valarray<size_t> _M_index; // Linear array of referenced indices
00114 
00115       _Indexer()
00116       : _M_count(1), _M_start(0), _M_size(), _M_stride(), _M_index() {}
00117 
00118       _Indexer(size_t, const valarray<size_t>&,
00119            const valarray<size_t>&);
00120 
00121       void
00122       _M_increment_use()
00123       { ++_M_count; }
00124       
00125       size_t
00126       _M_decrement_use()
00127       { return --_M_count; }
00128     };
00129 
00130     _Indexer* _M_index;
00131 
00132     template<typename _Tp> friend class valarray;
00133   };
00134 
00135   inline size_t
00136   gslice::start() const
00137   { return _M_index ? _M_index->_M_start : 0; }
00138 
00139   inline valarray<size_t>
00140   gslice::size() const
00141   { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
00142 
00143   inline valarray<size_t>
00144   gslice::stride() const
00145   { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
00146 
00147   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00148   // 543. valarray slice default constructor
00149   inline
00150   gslice::gslice()
00151   : _M_index(new gslice::_Indexer()) {}
00152 
00153   inline
00154   gslice::gslice(size_t __o, const valarray<size_t>& __l,
00155          const valarray<size_t>& __s)
00156   : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
00157 
00158   inline
00159   gslice::gslice(const gslice& __g)
00160   : _M_index(__g._M_index)
00161   { if (_M_index) _M_index->_M_increment_use(); }
00162 
00163   inline
00164   gslice::~gslice()
00165   {
00166     if (_M_index && _M_index->_M_decrement_use() == 0)
00167       delete _M_index;
00168   }
00169 
00170   inline gslice&
00171   gslice::operator=(const gslice& __g)
00172   {
00173     if (__g._M_index)
00174       __g._M_index->_M_increment_use();
00175     if (_M_index && _M_index->_M_decrement_use() == 0)
00176       delete _M_index;
00177     _M_index = __g._M_index;
00178     return *this;
00179   }
00180 
00181   // @} group numeric_arrays
00182 
00183 _GLIBCXX_END_NAMESPACE_VERSION
00184 } // namespace
00185 
00186 #endif /* _GSLICE_H */