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