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 // 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, const valarray<size_t>&, const valarray<size_t>&);
00082 
00083     // XXX: the IS says the copy-ctor and copy-assignment operators are
00084     //      synthesized by the compiler but they are just unsuitable
00085     //      for a ref-counted semantic
00086     ///  Copy constructor.
00087     gslice(const gslice&);
00088 
00089     ///  Destructor.
00090     ~gslice();
00091 
00092     // XXX: See the note above.
00093     ///  Assignment operator.
00094     gslice& operator=(const gslice&);
00095 
00096     ///  Return array offset of first slice element.
00097     size_t           start() const;
00098 
00099     ///  Return array of sizes of slice dimensions.
00100     valarray<size_t> size() const;
00101     
00102     ///  Return array of array strides for each dimension.
00103     valarray<size_t> stride() const;
00104 
00105   private:
00106     struct _Indexer
00107     {
00108       size_t _M_count;
00109       size_t _M_start;
00110       valarray<size_t> _M_size;
00111       valarray<size_t> _M_stride;
00112       valarray<size_t> _M_index; // Linear array of referenced indices
00113 
00114       _Indexer()
00115       : _M_count(1), _M_start(0), _M_size(), _M_stride(), _M_index() {}
00116 
00117       _Indexer(size_t, const valarray<size_t>&,
00118            const valarray<size_t>&);
00119 
00120       void
00121       _M_increment_use()
00122       { ++_M_count; }
00123       
00124       size_t
00125       _M_decrement_use()
00126       { return --_M_count; }
00127     };
00128 
00129     _Indexer* _M_index;
00130 
00131     template<typename _Tp> friend class valarray;
00132   };
00133 
00134   inline size_t
00135   gslice::start() const
00136   { return _M_index ? _M_index->_M_start : 0; }
00137 
00138   inline valarray<size_t>
00139   gslice::size() const
00140   { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
00141 
00142   inline valarray<size_t>
00143   gslice::stride() const
00144   { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
00145 
00146   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00147   // 543. valarray slice default constructor
00148   inline
00149   gslice::gslice()
00150   : _M_index(new gslice::_Indexer()) {}
00151 
00152   inline
00153   gslice::gslice(size_t __o, const valarray<size_t>& __l,
00154          const valarray<size_t>& __s)
00155   : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
00156 
00157   inline
00158   gslice::gslice(const gslice& __g)
00159   : _M_index(__g._M_index)
00160   { if (_M_index) _M_index->_M_increment_use(); }
00161 
00162   inline
00163   gslice::~gslice()
00164   {
00165     if (_M_index && _M_index->_M_decrement_use() == 0)
00166       delete _M_index;
00167   }
00168 
00169   inline gslice&
00170   gslice::operator=(const gslice& __g)
00171   {
00172     if (__g._M_index)
00173       __g._M_index->_M_increment_use();
00174     if (_M_index && _M_index->_M_decrement_use() == 0)
00175       delete _M_index;
00176     _M_index = __g._M_index;
00177     return *this;
00178   }
00179 
00180   // @} group numeric_arrays
00181 
00182 _GLIBCXX_END_NAMESPACE_VERSION
00183 } // namespace
00184 
00185 #endif /* _GSLICE_H */