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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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     size_t _M_count;
00106     size_t _M_start;
00107     valarray<size_t> _M_size;
00108     valarray<size_t> _M_stride;
00109     valarray<size_t> _M_index; // Linear array of referenced indices
00110     _Indexer(size_t, const valarray<size_t>&,
00111          const valarray<size_t>&);
00112     void _M_increment_use() { ++_M_count; }
00113     size_t _M_decrement_use() { return --_M_count; }
00114       };
00115 
00116       _Indexer* _M_index;
00117 
00118       template<typename _Tp> friend class valarray;
00119     };
00120 
00121     inline size_t
00122     gslice::start () const
00123     { return _M_index ? _M_index->_M_start : 0; }
00124 
00125     inline valarray<size_t>
00126     gslice::size () const
00127     { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
00128 
00129     inline valarray<size_t>
00130     gslice::stride () const
00131     { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
00132 
00133     inline gslice::gslice () : _M_index(0) {}
00134 
00135     inline
00136     gslice::gslice(size_t __o, const valarray<size_t>& __l,
00137                    const valarray<size_t>& __s)
00138             : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
00139 
00140     inline
00141     gslice::gslice(const gslice& __g) : _M_index(__g._M_index)
00142     { if (_M_index) _M_index->_M_increment_use(); }
00143 
00144     inline
00145     gslice::~gslice()
00146     { if (_M_index && _M_index->_M_decrement_use() == 0) delete _M_index; }
00147 
00148     inline gslice&
00149     gslice::operator= (const gslice& __g)
00150     {
00151         if (__g._M_index) __g._M_index->_M_increment_use();
00152         if (_M_index && _M_index->_M_decrement_use() == 0) delete _M_index;
00153         _M_index = __g._M_index;
00154         return *this;
00155     }
00156 
00157 
00158 } // std::
00159 
00160 
00161 #endif /* _GSLICE_H */
00162 
00163 // Local Variables:
00164 // mode:c++
00165 // End:

Generated on Tue Jan 30 17:31:49 2007 for GNU C++ STL by doxygen 1.3.6