xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/slice_array.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // The template and inlines for the -*- C++ -*- slice_array class.
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 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 slice_array.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 _SLICE_ARRAY_H
39*404b540aSrobert #define _SLICE_ARRAY_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 one-dimensional subset of an array.
47*404b540aSrobert    *
48*404b540aSrobert    *  The slice class represents a one-dimensional subset of an array,
49*404b540aSrobert    *  specified by three parameters: start offset, size, and stride.  The
50*404b540aSrobert    *  start offset is the index of the first element of the array that is part
51*404b540aSrobert    *  of the subset.  The size is the total number of elements in the subset.
52*404b540aSrobert    *  Stride is the distance between each successive array element to include
53*404b540aSrobert    *  in the subset.
54*404b540aSrobert    *
55*404b540aSrobert    *  For example, with an array of size 10, and a slice with offset 1, size 3
56*404b540aSrobert    *  and stride 2, the subset consists of array elements 1, 3, and 5.
57*404b540aSrobert    */
58*404b540aSrobert   class slice
59*404b540aSrobert   {
60*404b540aSrobert   public:
61*404b540aSrobert     ///  Construct an empty slice.
62*404b540aSrobert     slice();
63*404b540aSrobert 
64*404b540aSrobert     /**
65*404b540aSrobert      *  @brief  Construct a slice.
66*404b540aSrobert      *
67*404b540aSrobert      *  @param  o  Offset in array of first element.
68*404b540aSrobert      *  @param  d  Number of elements in slice.
69*404b540aSrobert      *  @param  s  Stride between array elements.
70*404b540aSrobert      */
71*404b540aSrobert     slice(size_t, size_t, size_t);
72*404b540aSrobert 
73*404b540aSrobert     ///  Return array offset of first slice element.
74*404b540aSrobert     size_t start() const;
75*404b540aSrobert     ///  Return size of slice.
76*404b540aSrobert     size_t size() const;
77*404b540aSrobert     ///  Return array stride of slice.
78*404b540aSrobert     size_t stride() const;
79*404b540aSrobert 
80*404b540aSrobert   private:
81*404b540aSrobert     size_t _M_off;                      // offset
82*404b540aSrobert     size_t _M_sz;			// size
83*404b540aSrobert     size_t _M_st;			// stride unit
84*404b540aSrobert   };
85*404b540aSrobert 
86*404b540aSrobert   // The default constructor constructor is not required to initialize
87*404b540aSrobert   // data members with any meaningful values, so we choose to do nothing.
88*404b540aSrobert   inline
slice()89*404b540aSrobert   slice::slice() {}
90*404b540aSrobert 
91*404b540aSrobert   inline
slice(size_t __o,size_t __d,size_t __s)92*404b540aSrobert   slice::slice(size_t __o, size_t __d, size_t __s)
93*404b540aSrobert   : _M_off(__o), _M_sz(__d), _M_st(__s) {}
94*404b540aSrobert 
95*404b540aSrobert   inline size_t
start()96*404b540aSrobert   slice::start() const
97*404b540aSrobert   { return _M_off; }
98*404b540aSrobert 
99*404b540aSrobert   inline size_t
size()100*404b540aSrobert   slice::size() const
101*404b540aSrobert   { return _M_sz; }
102*404b540aSrobert 
103*404b540aSrobert   inline size_t
stride()104*404b540aSrobert   slice::stride() const
105*404b540aSrobert   { return _M_st; }
106*404b540aSrobert 
107*404b540aSrobert   /**
108*404b540aSrobert    *  @brief  Reference to one-dimensional subset of an array.
109*404b540aSrobert    *
110*404b540aSrobert    *  A slice_array is a reference to the actual elements of an array
111*404b540aSrobert    *  specified by a slice.  The way to get a slice_array is to call
112*404b540aSrobert    *  operator[](slice) on a valarray.  The returned slice_array then permits
113*404b540aSrobert    *  carrying operations out on the referenced subset of elements in the
114*404b540aSrobert    *  original valarray.  For example, operator+=(valarray) will add values
115*404b540aSrobert    *  to the subset of elements in the underlying valarray this slice_array
116*404b540aSrobert    *  refers to.
117*404b540aSrobert    *
118*404b540aSrobert    *  @param  Tp  Element type.
119*404b540aSrobert    */
120*404b540aSrobert   template<typename _Tp>
121*404b540aSrobert     class slice_array
122*404b540aSrobert     {
123*404b540aSrobert     public:
124*404b540aSrobert       typedef _Tp value_type;
125*404b540aSrobert 
126*404b540aSrobert       // _GLIBCXX_RESOLVE_LIB_DEFECTS
127*404b540aSrobert       // 253. valarray helper functions are almost entirely useless
128*404b540aSrobert 
129*404b540aSrobert       ///  Copy constructor.  Both slices refer to the same underlying array.
130*404b540aSrobert       slice_array(const slice_array&);
131*404b540aSrobert 
132*404b540aSrobert       ///  Assignment operator.  Assigns slice elements to corresponding
133*404b540aSrobert       ///  elements of @a a.
134*404b540aSrobert       slice_array& operator=(const slice_array&);
135*404b540aSrobert 
136*404b540aSrobert       ///  Assign slice elements to corresponding elements of @a v.
137*404b540aSrobert       void operator=(const valarray<_Tp>&) const;
138*404b540aSrobert       ///  Multiply slice elements by corresponding elements of @a v.
139*404b540aSrobert       void operator*=(const valarray<_Tp>&) const;
140*404b540aSrobert       ///  Divide slice elements by corresponding elements of @a v.
141*404b540aSrobert       void operator/=(const valarray<_Tp>&) const;
142*404b540aSrobert       ///  Modulo slice elements by corresponding elements of @a v.
143*404b540aSrobert       void operator%=(const valarray<_Tp>&) const;
144*404b540aSrobert       ///  Add corresponding elements of @a v to slice elements.
145*404b540aSrobert       void operator+=(const valarray<_Tp>&) const;
146*404b540aSrobert       ///  Subtract corresponding elements of @a v from slice elements.
147*404b540aSrobert       void operator-=(const valarray<_Tp>&) const;
148*404b540aSrobert       ///  Logical xor slice elements with corresponding elements of @a v.
149*404b540aSrobert       void operator^=(const valarray<_Tp>&) const;
150*404b540aSrobert       ///  Logical and slice elements with corresponding elements of @a v.
151*404b540aSrobert       void operator&=(const valarray<_Tp>&) const;
152*404b540aSrobert       ///  Logical or slice elements with corresponding elements of @a v.
153*404b540aSrobert       void operator|=(const valarray<_Tp>&) const;
154*404b540aSrobert       ///  Left shift slice elements by corresponding elements of @a v.
155*404b540aSrobert       void operator<<=(const valarray<_Tp>&) const;
156*404b540aSrobert       ///  Right shift slice elements by corresponding elements of @a v.
157*404b540aSrobert       void operator>>=(const valarray<_Tp>&) const;
158*404b540aSrobert       ///  Assign all slice elements to @a t.
159*404b540aSrobert       void operator=(const _Tp &) const;
160*404b540aSrobert       //        ~slice_array ();
161*404b540aSrobert 
162*404b540aSrobert       template<class _Dom>
163*404b540aSrobert         void operator=(const _Expr<_Dom, _Tp>&) const;
164*404b540aSrobert       template<class _Dom>
165*404b540aSrobert 	void operator*=(const _Expr<_Dom, _Tp>&) const;
166*404b540aSrobert       template<class _Dom>
167*404b540aSrobert 	void operator/=(const _Expr<_Dom, _Tp>&) const;
168*404b540aSrobert       template<class _Dom>
169*404b540aSrobert 	void operator%=(const _Expr<_Dom, _Tp>&) const;
170*404b540aSrobert       template<class _Dom>
171*404b540aSrobert 	void operator+=(const _Expr<_Dom, _Tp>&) const;
172*404b540aSrobert       template<class _Dom>
173*404b540aSrobert 	void operator-=(const _Expr<_Dom, _Tp>&) const;
174*404b540aSrobert       template<class _Dom>
175*404b540aSrobert 	void operator^=(const _Expr<_Dom, _Tp>&) const;
176*404b540aSrobert       template<class _Dom>
177*404b540aSrobert 	void operator&=(const _Expr<_Dom, _Tp>&) const;
178*404b540aSrobert       template<class _Dom>
179*404b540aSrobert 	void operator|=(const _Expr<_Dom, _Tp>&) const;
180*404b540aSrobert       template<class _Dom>
181*404b540aSrobert 	void operator<<=(const _Expr<_Dom, _Tp>&) const;
182*404b540aSrobert       template<class _Dom>
183*404b540aSrobert 	void operator>>=(const _Expr<_Dom, _Tp>&) const;
184*404b540aSrobert 
185*404b540aSrobert     private:
186*404b540aSrobert       friend class valarray<_Tp>;
187*404b540aSrobert       slice_array(_Array<_Tp>, const slice&);
188*404b540aSrobert 
189*404b540aSrobert       const size_t      _M_sz;
190*404b540aSrobert       const size_t      _M_stride;
191*404b540aSrobert       const _Array<_Tp> _M_array;
192*404b540aSrobert 
193*404b540aSrobert       // not implemented
194*404b540aSrobert       slice_array();
195*404b540aSrobert     };
196*404b540aSrobert 
197*404b540aSrobert   template<typename _Tp>
198*404b540aSrobert     inline
slice_array(_Array<_Tp> __a,const slice & __s)199*404b540aSrobert     slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
200*404b540aSrobert     : _M_sz(__s.size()), _M_stride(__s.stride()),
201*404b540aSrobert       _M_array(__a.begin() + __s.start()) {}
202*404b540aSrobert 
203*404b540aSrobert   template<typename _Tp>
204*404b540aSrobert     inline
slice_array(const slice_array<_Tp> & a)205*404b540aSrobert     slice_array<_Tp>::slice_array(const slice_array<_Tp>& a)
206*404b540aSrobert     : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
207*404b540aSrobert 
208*404b540aSrobert   //    template<typename _Tp>
209*404b540aSrobert   //    inline slice_array<_Tp>::~slice_array () {}
210*404b540aSrobert 
211*404b540aSrobert   template<typename _Tp>
212*404b540aSrobert     inline slice_array<_Tp>&
213*404b540aSrobert     slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
214*404b540aSrobert     {
215*404b540aSrobert       std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
216*404b540aSrobert 			   _M_array, _M_stride);
217*404b540aSrobert       return *this;
218*404b540aSrobert     }
219*404b540aSrobert 
220*404b540aSrobert   template<typename _Tp>
221*404b540aSrobert     inline void
222*404b540aSrobert     slice_array<_Tp>::operator=(const _Tp& __t) const
223*404b540aSrobert     { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
224*404b540aSrobert 
225*404b540aSrobert   template<typename _Tp>
226*404b540aSrobert     inline void
227*404b540aSrobert     slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
228*404b540aSrobert     { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
229*404b540aSrobert 
230*404b540aSrobert   template<typename _Tp>
231*404b540aSrobert   template<class _Dom>
232*404b540aSrobert     inline void
233*404b540aSrobert     slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
234*404b540aSrobert     { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
235*404b540aSrobert 
236*404b540aSrobert #undef _DEFINE_VALARRAY_OPERATOR
237*404b540aSrobert #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name)				\
238*404b540aSrobert   template<typename _Tp>						\
239*404b540aSrobert     inline void								\
240*404b540aSrobert     slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const	\
241*404b540aSrobert     {									\
242*404b540aSrobert       _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
243*404b540aSrobert     }									\
244*404b540aSrobert 									\
245*404b540aSrobert   template<typename _Tp>                                                \
246*404b540aSrobert     template<class _Dom>				                \
247*404b540aSrobert       inline void							\
248*404b540aSrobert       slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
249*404b540aSrobert       {									\
250*404b540aSrobert 	  _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz);	\
251*404b540aSrobert       }
252*404b540aSrobert 
253*404b540aSrobert 
254*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
255*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(/, __divides)
256*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(%, __modulus)
257*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(+, __plus)
258*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(-, __minus)
259*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
260*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
261*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
262*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
263*404b540aSrobert _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
264*404b540aSrobert 
265*404b540aSrobert #undef _DEFINE_VALARRAY_OPERATOR
266*404b540aSrobert 
267*404b540aSrobert _GLIBCXX_END_NAMESPACE
268*404b540aSrobert 
269*404b540aSrobert #endif /* _SLICE_ARRAY_H */
270