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
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 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
00032 
00038 #ifndef _GSLICE_H
00039 #define _GSLICE_H 1
00040 
00041 #pragma GCC system_header
00042 
00043 namespace std
00044 {
00063   class gslice
00064   {
00065   public:
00067     gslice ();
00068 
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
00085     gslice(const gslice&);
00086 
00088     ~gslice();
00089 
00090     // XXX: See the note above.
00092     gslice& operator=(const gslice&);
00093 
00095     size_t           start() const;
00096 
00098     valarray<size_t> size() const;
00099     
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 } // std::
00169 
00170 #endif /* _GSLICE_H */
00171 
00172 // Local Variables:
00173 // mode:c++
00174 // End:

Generated on Tue Feb 2 16:55:58 2010 for GNU C++ STL by  doxygen 1.4.7