xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/gslice.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // The template and inlines for the -*- C++ -*- gslice class.
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2005
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert 
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert 
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert 
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License.  This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert 
31*404b540aSrobert /** @file gslice.h
32*404b540aSrobert  *  This is an internal header file, included by other library headers.
33*404b540aSrobert  *  You should not attempt to use it directly.
34*404b540aSrobert  */
35*404b540aSrobert 
36*404b540aSrobert // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
37*404b540aSrobert 
38*404b540aSrobert #ifndef _GSLICE_H
39*404b540aSrobert #define _GSLICE_H 1
40*404b540aSrobert 
41*404b540aSrobert #pragma GCC system_header
42*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(std)43*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
44*404b540aSrobert 
45*404b540aSrobert   /**
46*404b540aSrobert    *  @brief  Class defining multi-dimensional subset of an array.
47*404b540aSrobert    *
48*404b540aSrobert    *  The slice class represents a multi-dimensional subset of an array,
49*404b540aSrobert    *  specified by three parameter sets: start offset, size array, and stride
50*404b540aSrobert    *  array.  The start offset is the index of the first element of the array
51*404b540aSrobert    *  that is part of the subset.  The size and stride array describe each
52*404b540aSrobert    *  dimension of the slice.  Size is the number of elements in that
53*404b540aSrobert    *  dimension, and stride is the distance in the array between successive
54*404b540aSrobert    *  elements in that dimension.  Each dimension's size and stride is taken
55*404b540aSrobert    *  to begin at an array element described by the previous dimension.  The
56*404b540aSrobert    *  size array and stride array must be the same size.
57*404b540aSrobert    *
58*404b540aSrobert    *  For example, if you have offset==3, stride[0]==11, size[1]==3,
59*404b540aSrobert    *  stride[1]==3, then slice[0,0]==array[3], slice[0,1]==array[6],
60*404b540aSrobert    *  slice[0,2]==array[9], slice[1,0]==array[14], slice[1,1]==array[17],
61*404b540aSrobert    *  slice[1,2]==array[20].
62*404b540aSrobert    */
63*404b540aSrobert   class gslice
64*404b540aSrobert   {
65*404b540aSrobert   public:
66*404b540aSrobert     ///  Construct an empty slice.
67*404b540aSrobert     gslice ();
68*404b540aSrobert 
69*404b540aSrobert     /**
70*404b540aSrobert      *  @brief  Construct a slice.
71*404b540aSrobert      *
72*404b540aSrobert      *  Constructs a slice with as many dimensions as the length of the @a l
73*404b540aSrobert      *  and @a s arrays.
74*404b540aSrobert      *
75*404b540aSrobert      *  @param  o  Offset in array of first element.
76*404b540aSrobert      *  @param  l  Array of dimension lengths.
77*404b540aSrobert      *  @param  s  Array of dimension strides between array elements.
78*404b540aSrobert      */
79*404b540aSrobert     gslice(size_t, const valarray<size_t>&, const valarray<size_t>&);
80*404b540aSrobert 
81*404b540aSrobert     // XXX: the IS says the copy-ctor and copy-assignment operators are
82*404b540aSrobert     //      synthetized by the compiler but they are just unsuitable
83*404b540aSrobert     //      for a ref-counted semantic
84*404b540aSrobert     ///  Copy constructor.
85*404b540aSrobert     gslice(const gslice&);
86*404b540aSrobert 
87*404b540aSrobert     ///  Destructor.
88*404b540aSrobert     ~gslice();
89*404b540aSrobert 
90*404b540aSrobert     // XXX: See the note above.
91*404b540aSrobert     ///  Assignment operator.
92*404b540aSrobert     gslice& operator=(const gslice&);
93*404b540aSrobert 
94*404b540aSrobert     ///  Return array offset of first slice element.
95*404b540aSrobert     size_t           start() const;
96*404b540aSrobert 
97*404b540aSrobert     ///  Return array of sizes of slice dimensions.
98*404b540aSrobert     valarray<size_t> size() const;
99*404b540aSrobert 
100*404b540aSrobert     ///  Return array of array strides for each dimension.
101*404b540aSrobert     valarray<size_t> stride() const;
102*404b540aSrobert 
103*404b540aSrobert   private:
104*404b540aSrobert     struct _Indexer
105*404b540aSrobert     {
106*404b540aSrobert       size_t _M_count;
107*404b540aSrobert       size_t _M_start;
108*404b540aSrobert       valarray<size_t> _M_size;
109*404b540aSrobert       valarray<size_t> _M_stride;
110*404b540aSrobert       valarray<size_t> _M_index; // Linear array of referenced indices
111*404b540aSrobert       _Indexer(size_t, const valarray<size_t>&,
112*404b540aSrobert 	       const valarray<size_t>&);
113*404b540aSrobert       void
114*404b540aSrobert       _M_increment_use()
115*404b540aSrobert       { ++_M_count; }
116*404b540aSrobert 
117*404b540aSrobert       size_t
118*404b540aSrobert       _M_decrement_use()
119*404b540aSrobert       { return --_M_count; }
120*404b540aSrobert     };
121*404b540aSrobert 
122*404b540aSrobert     _Indexer* _M_index;
123*404b540aSrobert 
124*404b540aSrobert     template<typename _Tp> friend class valarray;
125*404b540aSrobert   };
126*404b540aSrobert 
127*404b540aSrobert   inline size_t
start()128*404b540aSrobert   gslice::start () const
129*404b540aSrobert   { return _M_index ? _M_index->_M_start : 0; }
130*404b540aSrobert 
131*404b540aSrobert   inline valarray<size_t>
size()132*404b540aSrobert   gslice::size () const
133*404b540aSrobert   { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
134*404b540aSrobert 
135*404b540aSrobert   inline valarray<size_t>
stride()136*404b540aSrobert   gslice::stride () const
137*404b540aSrobert   { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
138*404b540aSrobert 
gslice()139*404b540aSrobert   inline gslice::gslice () : _M_index(0) {}
140*404b540aSrobert 
141*404b540aSrobert   inline
gslice(size_t __o,const valarray<size_t> & __l,const valarray<size_t> & __s)142*404b540aSrobert   gslice::gslice(size_t __o, const valarray<size_t>& __l,
143*404b540aSrobert 		 const valarray<size_t>& __s)
144*404b540aSrobert   : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
145*404b540aSrobert 
146*404b540aSrobert   inline
gslice(const gslice & __g)147*404b540aSrobert   gslice::gslice(const gslice& __g) : _M_index(__g._M_index)
148*404b540aSrobert   { if (_M_index) _M_index->_M_increment_use(); }
149*404b540aSrobert 
150*404b540aSrobert   inline
~gslice()151*404b540aSrobert   gslice::~gslice()
152*404b540aSrobert   {
153*404b540aSrobert     if (_M_index && _M_index->_M_decrement_use() == 0)
154*404b540aSrobert       delete _M_index;
155*404b540aSrobert   }
156*404b540aSrobert 
157*404b540aSrobert   inline gslice&
158*404b540aSrobert   gslice::operator= (const gslice& __g)
159*404b540aSrobert   {
160*404b540aSrobert     if (__g._M_index)
161*404b540aSrobert       __g._M_index->_M_increment_use();
162*404b540aSrobert     if (_M_index && _M_index->_M_decrement_use() == 0)
163*404b540aSrobert       delete _M_index;
164*404b540aSrobert     _M_index = __g._M_index;
165*404b540aSrobert     return *this;
166*404b540aSrobert   }
167*404b540aSrobert 
168*404b540aSrobert _GLIBCXX_END_NAMESPACE
169*404b540aSrobert 
170*404b540aSrobert #endif /* _GSLICE_H */
171