]>
Commit | Line | Data |
---|---|---|
1d487aca | 1 | // The template and inlines for the -*- C++ -*- slice_array class. |
2 | ||
fbd26352 | 3 | // Copyright (C) 1997-2019 Free Software Foundation, Inc. |
1d487aca | 4 | // |
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 | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
1d487aca | 9 | // any later version. |
10 | ||
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. | |
15 | ||
6bc9506f | 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. | |
19 | ||
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/>. | |
1d487aca | 24 | |
5846aeac | 25 | /** @file bits/slice_array.h |
7a472ef1 | 26 | * This is an internal header file, included by other library headers. |
5846aeac | 27 | * Do not attempt to use it directly. @headername{valarray} |
7a472ef1 | 28 | */ |
29 | ||
944beac5 | 30 | // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr> |
31 | ||
5a64d8cf | 32 | #ifndef _SLICE_ARRAY_H |
33 | #define _SLICE_ARRAY_H 1 | |
1d487aca | 34 | |
cd319190 | 35 | #pragma GCC system_header |
36 | ||
2948dd21 | 37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { | |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
1069247d | 40 | |
97a32de0 | 41 | /** |
42 | * @addtogroup numeric_arrays | |
43 | * @{ | |
44 | */ | |
45 | ||
21d4b1cc | 46 | /** |
47 | * @brief Class defining one-dimensional subset of an array. | |
48 | * | |
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 | |
54 | * in the subset. | |
55 | * | |
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. | |
58 | */ | |
44e8a860 | 59 | class slice |
60 | { | |
61 | public: | |
21d4b1cc | 62 | /// Construct an empty slice. |
44e8a860 | 63 | slice(); |
21d4b1cc | 64 | |
65 | /** | |
66 | * @brief Construct a slice. | |
67 | * | |
e12e4f3b | 68 | * @param __o Offset in array of first element. |
69 | * @param __d Number of elements in slice. | |
70 | * @param __s Stride between array elements. | |
21d4b1cc | 71 | */ |
e12e4f3b | 72 | slice(size_t __o, size_t __d, size_t __s); |
bae9b8af | 73 | |
21d4b1cc | 74 | /// Return array offset of first slice element. |
44e8a860 | 75 | size_t start() const; |
21d4b1cc | 76 | /// Return size of slice. |
44e8a860 | 77 | size_t size() const; |
21d4b1cc | 78 | /// Return array stride of slice. |
44e8a860 | 79 | size_t stride() const; |
bae9b8af | 80 | |
44e8a860 | 81 | private: |
82 | size_t _M_off; // offset | |
83 | size_t _M_sz; // size | |
84 | size_t _M_st; // stride unit | |
85 | }; | |
86 | ||
a7e95a36 | 87 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
88 | // 543. valarray slice default constructor | |
bae9b8af | 89 | inline |
a7e95a36 | 90 | slice::slice() |
91 | : _M_off(0), _M_sz(0), _M_st(0) {} | |
bae9b8af | 92 | |
93 | inline | |
44e8a860 | 94 | slice::slice(size_t __o, size_t __d, size_t __s) |
7558da41 | 95 | : _M_off(__o), _M_sz(__d), _M_st(__s) {} |
bae9b8af | 96 | |
44e8a860 | 97 | inline size_t |
98 | slice::start() const | |
99 | { return _M_off; } | |
bae9b8af | 100 | |
44e8a860 | 101 | inline size_t |
102 | slice::size() const | |
103 | { return _M_sz; } | |
bae9b8af | 104 | |
44e8a860 | 105 | inline size_t |
106 | slice::stride() const | |
107 | { return _M_st; } | |
108 | ||
21d4b1cc | 109 | /** |
110 | * @brief Reference to one-dimensional subset of an array. | |
111 | * | |
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 | |
118 | * refers to. | |
119 | * | |
120 | * @param Tp Element type. | |
121 | */ | |
44e8a860 | 122 | template<typename _Tp> |
1d487aca | 123 | class slice_array |
124 | { | |
125 | public: | |
44e8a860 | 126 | typedef _Tp value_type; |
59327c82 | 127 | |
aba89f4d | 128 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
129 | // 253. valarray helper functions are almost entirely useless | |
130 | ||
21d4b1cc | 131 | /// Copy constructor. Both slices refer to the same underlying array. |
44e8a860 | 132 | slice_array(const slice_array&); |
59327c82 | 133 | |
21d4b1cc | 134 | /// Assignment operator. Assigns slice elements to corresponding |
135 | /// elements of @a a. | |
44e8a860 | 136 | slice_array& operator=(const slice_array&); |
137 | ||
21d4b1cc | 138 | /// Assign slice elements to corresponding elements of @a v. |
44e8a860 | 139 | void operator=(const valarray<_Tp>&) const; |
21d4b1cc | 140 | /// Multiply slice elements by corresponding elements of @a v. |
44e8a860 | 141 | void operator*=(const valarray<_Tp>&) const; |
21d4b1cc | 142 | /// Divide slice elements by corresponding elements of @a v. |
44e8a860 | 143 | void operator/=(const valarray<_Tp>&) const; |
21d4b1cc | 144 | /// Modulo slice elements by corresponding elements of @a v. |
44e8a860 | 145 | void operator%=(const valarray<_Tp>&) const; |
21d4b1cc | 146 | /// Add corresponding elements of @a v to slice elements. |
44e8a860 | 147 | void operator+=(const valarray<_Tp>&) const; |
21d4b1cc | 148 | /// Subtract corresponding elements of @a v from slice elements. |
44e8a860 | 149 | void operator-=(const valarray<_Tp>&) const; |
21d4b1cc | 150 | /// Logical xor slice elements with corresponding elements of @a v. |
44e8a860 | 151 | void operator^=(const valarray<_Tp>&) const; |
21d4b1cc | 152 | /// Logical and slice elements with corresponding elements of @a v. |
44e8a860 | 153 | void operator&=(const valarray<_Tp>&) const; |
21d4b1cc | 154 | /// Logical or slice elements with corresponding elements of @a v. |
44e8a860 | 155 | void operator|=(const valarray<_Tp>&) const; |
21d4b1cc | 156 | /// Left shift slice elements by corresponding elements of @a v. |
44e8a860 | 157 | void operator<<=(const valarray<_Tp>&) const; |
21d4b1cc | 158 | /// Right shift slice elements by corresponding elements of @a v. |
44e8a860 | 159 | void operator>>=(const valarray<_Tp>&) const; |
21d4b1cc | 160 | /// Assign all slice elements to @a t. |
8aa81ba7 | 161 | void operator=(const _Tp &) const; |
44e8a860 | 162 | // ~slice_array (); |
163 | ||
164 | template<class _Dom> | |
7558da41 | 165 | void operator=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 166 | template<class _Dom> |
7558da41 | 167 | void operator*=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 168 | template<class _Dom> |
7558da41 | 169 | void operator/=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 170 | template<class _Dom> |
7558da41 | 171 | void operator%=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 172 | template<class _Dom> |
7558da41 | 173 | void operator+=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 174 | template<class _Dom> |
7558da41 | 175 | void operator-=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 176 | template<class _Dom> |
7558da41 | 177 | void operator^=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 178 | template<class _Dom> |
7558da41 | 179 | void operator&=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 180 | template<class _Dom> |
7558da41 | 181 | void operator|=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 182 | template<class _Dom> |
7558da41 | 183 | void operator<<=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 184 | template<class _Dom> |
7558da41 | 185 | void operator>>=(const _Expr<_Dom, _Tp>&) const; |
44e8a860 | 186 | |
1d487aca | 187 | private: |
44e8a860 | 188 | friend class valarray<_Tp>; |
189 | slice_array(_Array<_Tp>, const slice&); | |
190 | ||
7558da41 | 191 | const size_t _M_sz; |
192 | const size_t _M_stride; | |
44e8a860 | 193 | const _Array<_Tp> _M_array; |
1d487aca | 194 | |
806803f3 | 195 | #if __cplusplus < 201103L |
44e8a860 | 196 | // not implemented |
197 | slice_array(); | |
806803f3 | 198 | #else |
199 | public: | |
200 | slice_array() = delete; | |
201 | #endif | |
1d487aca | 202 | }; |
203 | ||
44e8a860 | 204 | template<typename _Tp> |
bae9b8af | 205 | inline |
44e8a860 | 206 | slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s) |
aba89f4d | 207 | : _M_sz(__s.size()), _M_stride(__s.stride()), |
208 | _M_array(__a.begin() + __s.start()) {} | |
1d487aca | 209 | |
44e8a860 | 210 | template<typename _Tp> |
bae9b8af | 211 | inline |
6cb28338 | 212 | slice_array<_Tp>::slice_array(const slice_array<_Tp>& __a) |
213 | : _M_sz(__a._M_sz), _M_stride(__a._M_stride), _M_array(__a._M_array) {} | |
bae9b8af | 214 | |
44e8a860 | 215 | // template<typename _Tp> |
216 | // inline slice_array<_Tp>::~slice_array () {} | |
1d487aca | 217 | |
59327c82 | 218 | template<typename _Tp> |
44e8a860 | 219 | inline slice_array<_Tp>& |
220 | slice_array<_Tp>::operator=(const slice_array<_Tp>& __a) | |
221 | { | |
033443a4 | 222 | std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride, |
223 | _M_array, _M_stride); | |
44e8a860 | 224 | return *this; |
225 | } | |
59327c82 | 226 | |
44e8a860 | 227 | template<typename _Tp> |
1d487aca | 228 | inline void |
8aa81ba7 | 229 | slice_array<_Tp>::operator=(const _Tp& __t) const |
033443a4 | 230 | { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); } |
bae9b8af | 231 | |
44e8a860 | 232 | template<typename _Tp> |
1d487aca | 233 | inline void |
44e8a860 | 234 | slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const |
033443a4 | 235 | { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); } |
bae9b8af | 236 | |
44e8a860 | 237 | template<typename _Tp> |
238 | template<class _Dom> | |
1d487aca | 239 | inline void |
44e8a860 | 240 | slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const |
033443a4 | 241 | { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); } |
1d487aca | 242 | |
243 | #undef _DEFINE_VALARRAY_OPERATOR | |
e641ee3a | 244 | #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \ |
f75d4c97 | 245 | template<typename _Tp> \ |
246 | inline void \ | |
247 | slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \ | |
248 | { \ | |
249 | _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\ | |
250 | } \ | |
1d487aca | 251 | \ |
f75d4c97 | 252 | template<typename _Tp> \ |
253 | template<class _Dom> \ | |
254 | inline void \ | |
255 | slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\ | |
256 | { \ | |
257 | _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \ | |
258 | } | |
bae9b8af | 259 | |
1d487aca | 260 | |
f75d4c97 | 261 | _DEFINE_VALARRAY_OPERATOR(*, __multiplies) |
262 | _DEFINE_VALARRAY_OPERATOR(/, __divides) | |
263 | _DEFINE_VALARRAY_OPERATOR(%, __modulus) | |
264 | _DEFINE_VALARRAY_OPERATOR(+, __plus) | |
265 | _DEFINE_VALARRAY_OPERATOR(-, __minus) | |
266 | _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor) | |
267 | _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and) | |
268 | _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or) | |
269 | _DEFINE_VALARRAY_OPERATOR(<<, __shift_left) | |
270 | _DEFINE_VALARRAY_OPERATOR(>>, __shift_right) | |
1d487aca | 271 | |
272 | #undef _DEFINE_VALARRAY_OPERATOR | |
273 | ||
97a32de0 | 274 | // @} group numeric_arrays |
275 | ||
2948dd21 | 276 | _GLIBCXX_END_NAMESPACE_VERSION |
277 | } // namespace | |
1d487aca | 278 | |
5a64d8cf | 279 | #endif /* _SLICE_ARRAY_H */ |