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