# 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
    00033 /** @file gslice.h
    00034  *  This is an internal header file, included by other library headers.
    00035  *  You should not attempt to use it directly.
    00036  */
    00037
    00038 #ifndef _GSLICE_H
    00039 #define _GSLICE_H 1
    00040
    00041 #pragma GCC system_header
    00042
    00043 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     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: