1 // The template and inlines for the -*- C++ -*- slice_array class.
3 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/slice_array.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{valarray}
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
32 #ifndef _SLICE_ARRAY_H
33 #define _SLICE_ARRAY_H 1
35 #pragma GCC system_header
37 namespace std
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 * @addtogroup numeric_arrays
47 * @brief Class defining one-dimensional subset of an array.
49 * The slice class represents a one-dimensional subset of an array,
50 * specified by three parameters: start offset, size, and stride. The
51 * start offset is the index of the first element of the array that is part
52 * of the subset. The size is the total number of elements in the subset.
53 * Stride is the distance between each successive array element to include
56 * For example, with an array of size 10, and a slice with offset 1, size 3
57 * and stride 2, the subset consists of array elements 1, 3, and 5.
62 /// Construct an empty slice.
66 * @brief Construct a slice.
68 * @param __o Offset in array of first element.
69 * @param __d Number of elements in slice.
70 * @param __s Stride between array elements.
72 slice(size_t __o
, size_t __d
, size_t __s
);
74 /// Return array offset of first slice element.
76 /// Return size of slice.
78 /// Return array stride of slice.
79 size_t stride() const;
82 size_t _M_off
; // offset
84 size_t _M_st
; // stride unit
87 // _GLIBCXX_RESOLVE_LIB_DEFECTS
88 // 543. valarray slice default constructor
91 : _M_off(0), _M_sz(0), _M_st(0) {}
94 slice::slice(size_t __o
, size_t __d
, size_t __s
)
95 : _M_off(__o
), _M_sz(__d
), _M_st(__s
) {}
106 slice::stride() const
110 * @brief Reference to one-dimensional subset of an array.
112 * A slice_array is a reference to the actual elements of an array
113 * specified by a slice. The way to get a slice_array is to call
114 * operator[](slice) on a valarray. The returned slice_array then permits
115 * carrying operations out on the referenced subset of elements in the
116 * original valarray. For example, operator+=(valarray) will add values
117 * to the subset of elements in the underlying valarray this slice_array
120 * @param Tp Element type.
122 template<typename _Tp
>
126 typedef _Tp value_type
;
128 // _GLIBCXX_RESOLVE_LIB_DEFECTS
129 // 253. valarray helper functions are almost entirely useless
131 /// Copy constructor. Both slices refer to the same underlying array.
132 slice_array(const slice_array
&);
134 /// Assignment operator. Assigns slice elements to corresponding
135 /// elements of @a a.
136 slice_array
& operator=(const slice_array
&);
138 /// Assign slice elements to corresponding elements of @a v.
139 void operator=(const valarray
<_Tp
>&) const;
140 /// Multiply slice elements by corresponding elements of @a v.
141 void operator*=(const valarray
<_Tp
>&) const;
142 /// Divide slice elements by corresponding elements of @a v.
143 void operator/=(const valarray
<_Tp
>&) const;
144 /// Modulo slice elements by corresponding elements of @a v.
145 void operator%=(const valarray
<_Tp
>&) const;
146 /// Add corresponding elements of @a v to slice elements.
147 void operator+=(const valarray
<_Tp
>&) const;
148 /// Subtract corresponding elements of @a v from slice elements.
149 void operator-=(const valarray
<_Tp
>&) const;
150 /// Logical xor slice elements with corresponding elements of @a v.
151 void operator^=(const valarray
<_Tp
>&) const;
152 /// Logical and slice elements with corresponding elements of @a v.
153 void operator&=(const valarray
<_Tp
>&) const;
154 /// Logical or slice elements with corresponding elements of @a v.
155 void operator|=(const valarray
<_Tp
>&) const;
156 /// Left shift slice elements by corresponding elements of @a v.
157 void operator<<=(const valarray
<_Tp
>&) const;
158 /// Right shift slice elements by corresponding elements of @a v.
159 void operator>>=(const valarray
<_Tp
>&) const;
160 /// Assign all slice elements to @a t.
161 void operator=(const _Tp
&) const;
165 void operator=(const _Expr
<_Dom
, _Tp
>&) const;
167 void operator*=(const _Expr
<_Dom
, _Tp
>&) const;
169 void operator/=(const _Expr
<_Dom
, _Tp
>&) const;
171 void operator%=(const _Expr
<_Dom
, _Tp
>&) const;
173 void operator+=(const _Expr
<_Dom
, _Tp
>&) const;
175 void operator-=(const _Expr
<_Dom
, _Tp
>&) const;
177 void operator^=(const _Expr
<_Dom
, _Tp
>&) const;
179 void operator&=(const _Expr
<_Dom
, _Tp
>&) const;
181 void operator|=(const _Expr
<_Dom
, _Tp
>&) const;
183 void operator<<=(const _Expr
<_Dom
, _Tp
>&) const;
185 void operator>>=(const _Expr
<_Dom
, _Tp
>&) const;
188 friend class valarray
<_Tp
>;
189 slice_array(_Array
<_Tp
>, const slice
&);
192 const size_t _M_stride
;
193 const _Array
<_Tp
> _M_array
;
199 template<typename _Tp
>
201 slice_array
<_Tp
>::slice_array(_Array
<_Tp
> __a
, const slice
& __s
)
202 : _M_sz(__s
.size()), _M_stride(__s
.stride()),
203 _M_array(__a
.begin() + __s
.start()) {}
205 template<typename _Tp
>
207 slice_array
<_Tp
>::slice_array(const slice_array
<_Tp
>& __a
)
208 : _M_sz(__a
._M_sz
), _M_stride(__a
._M_stride
), _M_array(__a
._M_array
) {}
210 // template<typename _Tp>
211 // inline slice_array<_Tp>::~slice_array () {}
213 template<typename _Tp
>
214 inline slice_array
<_Tp
>&
215 slice_array
<_Tp
>::operator=(const slice_array
<_Tp
>& __a
)
217 std::__valarray_copy(__a
._M_array
, __a
._M_sz
, __a
._M_stride
,
218 _M_array
, _M_stride
);
222 template<typename _Tp
>
224 slice_array
<_Tp
>::operator=(const _Tp
& __t
) const
225 { std::__valarray_fill(_M_array
, _M_sz
, _M_stride
, __t
); }
227 template<typename _Tp
>
229 slice_array
<_Tp
>::operator=(const valarray
<_Tp
>& __v
) const
230 { std::__valarray_copy(_Array
<_Tp
>(__v
), _M_array
, _M_sz
, _M_stride
); }
232 template<typename _Tp
>
235 slice_array
<_Tp
>::operator=(const _Expr
<_Dom
,_Tp
>& __e
) const
236 { std::__valarray_copy(__e
, _M_sz
, _M_array
, _M_stride
); }
238 #undef _DEFINE_VALARRAY_OPERATOR
239 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
240 template<typename _Tp> \
242 slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
244 _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
247 template<typename _Tp> \
248 template<class _Dom> \
250 slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
252 _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
256 _DEFINE_VALARRAY_OPERATOR(*, __multiplies
)
257 _DEFINE_VALARRAY_OPERATOR(/, __divides
)
258 _DEFINE_VALARRAY_OPERATOR(%, __modulus
)
259 _DEFINE_VALARRAY_OPERATOR(+, __plus
)
260 _DEFINE_VALARRAY_OPERATOR(-, __minus
)
261 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor
)
262 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and
)
263 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or
)
264 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left
)
265 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right
)
267 #undef _DEFINE_VALARRAY_OPERATOR
269 // @} group numeric_arrays
271 _GLIBCXX_END_NAMESPACE_VERSION
274 #endif /* _SLICE_ARRAY_H */