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

Generated on Thu Jul 23 21:16:06 2009 for libstdc++ by  doxygen 1.5.8