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
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 /** @file gslice.h
00032  *  This is an internal header file, included by other library headers.
00033  *  You should not attempt to use it directly.
00034  */
00035 
00036 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00037 
00038 #ifndef _GSLICE_H
00039 #define _GSLICE_H 1
00040 
00041 #pragma GCC system_header
00042 
00043 _GLIBCXX_BEGIN_NAMESPACE(std)
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     //      synthetized 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       _Indexer(size_t, const valarray<size_t>&,
00112            const valarray<size_t>&);
00113       void
00114       _M_increment_use()
00115       { ++_M_count; }
00116       
00117       size_t
00118       _M_decrement_use()
00119       { return --_M_count; }
00120     };
00121 
00122     _Indexer* _M_index;
00123 
00124     template<typename _Tp> friend class valarray;
00125   };
00126 
00127   inline size_t
00128   gslice::start () const
00129   { return _M_index ? _M_index->_M_start : 0; }
00130 
00131   inline valarray<size_t>
00132   gslice::size () const
00133   { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
00134 
00135   inline valarray<size_t>
00136   gslice::stride () const
00137   { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
00138 
00139   inline gslice::gslice () : _M_index(0) {}
00140 
00141   inline
00142   gslice::gslice(size_t __o, const valarray<size_t>& __l,
00143          const valarray<size_t>& __s)
00144   : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
00145 
00146   inline
00147   gslice::gslice(const gslice& __g) : _M_index(__g._M_index)
00148   { if (_M_index) _M_index->_M_increment_use(); }
00149 
00150   inline
00151   gslice::~gslice()
00152   {
00153     if (_M_index && _M_index->_M_decrement_use() == 0)
00154       delete _M_index;
00155   }
00156 
00157   inline gslice&
00158   gslice::operator= (const gslice& __g)
00159   {
00160     if (__g._M_index)
00161       __g._M_index->_M_increment_use();
00162     if (_M_index && _M_index->_M_decrement_use() == 0)
00163       delete _M_index;
00164     _M_index = __g._M_index;
00165     return *this;
00166   }
00167 
00168 _GLIBCXX_END_NAMESPACE
00169 
00170 #endif /* _GSLICE_H */

Generated on Thu Nov 1 13:11:30 2007 for libstdc++ by  doxygen 1.5.1