]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // The template and inlines for the -*- C++ -*- slice_array class. |
2 | ||
d652f226 JJ |
3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2009, |
4 | // 2010 Free Software Foundation, Inc. | |
725dc051 BK |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
725dc051 BK |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | // You should have received a copy of the GNU General Public License and | |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
725dc051 | 25 | |
f910786b | 26 | /** @file bits/slice_array.h |
729e3d3f | 27 | * This is an internal header file, included by other library headers. |
f910786b | 28 | * Do not attempt to use it directly. @headername{valarray} |
729e3d3f PE |
29 | */ |
30 | ||
143c27b0 BK |
31 | // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr> |
32 | ||
3d7c150e BK |
33 | #ifndef _SLICE_ARRAY_H |
34 | #define _SLICE_ARRAY_H 1 | |
725dc051 | 35 | |
b0a85b86 GDR |
36 | #pragma GCC system_header |
37 | ||
3cbc7af0 BK |
38 | _GLIBCXX_BEGIN_NAMESPACE(std) |
39 | ||
5b9daa7e BK |
40 | /** |
41 | * @addtogroup numeric_arrays | |
42 | * @{ | |
43 | */ | |
44 | ||
7fb397a4 JQ |
45 | /** |
46 | * @brief Class defining one-dimensional subset of an array. | |
47 | * | |
48 | * The slice class represents a one-dimensional subset of an array, | |
49 | * specified by three parameters: start offset, size, and stride. The | |
50 | * start offset is the index of the first element of the array that is part | |
51 | * of the subset. The size is the total number of elements in the subset. | |
52 | * Stride is the distance between each successive array element to include | |
53 | * in the subset. | |
54 | * | |
55 | * For example, with an array of size 10, and a slice with offset 1, size 3 | |
56 | * and stride 2, the subset consists of array elements 1, 3, and 5. | |
57 | */ | |
5068f546 GDR |
58 | class slice |
59 | { | |
60 | public: | |
7fb397a4 | 61 | /// Construct an empty slice. |
5068f546 | 62 | slice(); |
7fb397a4 JQ |
63 | |
64 | /** | |
65 | * @brief Construct a slice. | |
66 | * | |
67 | * @param o Offset in array of first element. | |
68 | * @param d Number of elements in slice. | |
69 | * @param s Stride between array elements. | |
70 | */ | |
5068f546 | 71 | slice(size_t, size_t, size_t); |
ed6814f7 | 72 | |
7fb397a4 | 73 | /// Return array offset of first slice element. |
5068f546 | 74 | size_t start() const; |
7fb397a4 | 75 | /// Return size of slice. |
5068f546 | 76 | size_t size() const; |
7fb397a4 | 77 | /// Return array stride of slice. |
5068f546 | 78 | size_t stride() const; |
ed6814f7 | 79 | |
5068f546 GDR |
80 | private: |
81 | size_t _M_off; // offset | |
82 | size_t _M_sz; // size | |
83 | size_t _M_st; // stride unit | |
84 | }; | |
85 | ||
4091894c PC |
86 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
87 | // 543. valarray slice default constructor | |
ed6814f7 | 88 | inline |
4091894c PC |
89 | slice::slice() |
90 | : _M_off(0), _M_sz(0), _M_st(0) {} | |
ed6814f7 BI |
91 | |
92 | inline | |
5068f546 | 93 | slice::slice(size_t __o, size_t __d, size_t __s) |
b714a419 | 94 | : _M_off(__o), _M_sz(__d), _M_st(__s) {} |
ed6814f7 | 95 | |
5068f546 GDR |
96 | inline size_t |
97 | slice::start() const | |
98 | { return _M_off; } | |
ed6814f7 | 99 | |
5068f546 GDR |
100 | inline size_t |
101 | slice::size() const | |
102 | { return _M_sz; } | |
ed6814f7 | 103 | |
5068f546 GDR |
104 | inline size_t |
105 | slice::stride() const | |
106 | { return _M_st; } | |
107 | ||
7fb397a4 JQ |
108 | /** |
109 | * @brief Reference to one-dimensional subset of an array. | |
110 | * | |
111 | * A slice_array is a reference to the actual elements of an array | |
112 | * specified by a slice. The way to get a slice_array is to call | |
113 | * operator[](slice) on a valarray. The returned slice_array then permits | |
114 | * carrying operations out on the referenced subset of elements in the | |
115 | * original valarray. For example, operator+=(valarray) will add values | |
116 | * to the subset of elements in the underlying valarray this slice_array | |
117 | * refers to. | |
118 | * | |
119 | * @param Tp Element type. | |
120 | */ | |
5068f546 | 121 | template<typename _Tp> |
725dc051 BK |
122 | class slice_array |
123 | { | |
124 | public: | |
5068f546 | 125 | typedef _Tp value_type; |
7c301abf | 126 | |
d4cd08dd PC |
127 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
128 | // 253. valarray helper functions are almost entirely useless | |
129 | ||
7fb397a4 | 130 | /// Copy constructor. Both slices refer to the same underlying array. |
5068f546 | 131 | slice_array(const slice_array&); |
7c301abf | 132 | |
7fb397a4 JQ |
133 | /// Assignment operator. Assigns slice elements to corresponding |
134 | /// elements of @a a. | |
5068f546 GDR |
135 | slice_array& operator=(const slice_array&); |
136 | ||
7fb397a4 | 137 | /// Assign slice elements to corresponding elements of @a v. |
5068f546 | 138 | void operator=(const valarray<_Tp>&) const; |
7fb397a4 | 139 | /// Multiply slice elements by corresponding elements of @a v. |
5068f546 | 140 | void operator*=(const valarray<_Tp>&) const; |
7fb397a4 | 141 | /// Divide slice elements by corresponding elements of @a v. |
5068f546 | 142 | void operator/=(const valarray<_Tp>&) const; |
7fb397a4 | 143 | /// Modulo slice elements by corresponding elements of @a v. |
5068f546 | 144 | void operator%=(const valarray<_Tp>&) const; |
7fb397a4 | 145 | /// Add corresponding elements of @a v to slice elements. |
5068f546 | 146 | void operator+=(const valarray<_Tp>&) const; |
7fb397a4 | 147 | /// Subtract corresponding elements of @a v from slice elements. |
5068f546 | 148 | void operator-=(const valarray<_Tp>&) const; |
7fb397a4 | 149 | /// Logical xor slice elements with corresponding elements of @a v. |
5068f546 | 150 | void operator^=(const valarray<_Tp>&) const; |
7fb397a4 | 151 | /// Logical and slice elements with corresponding elements of @a v. |
5068f546 | 152 | void operator&=(const valarray<_Tp>&) const; |
7fb397a4 | 153 | /// Logical or slice elements with corresponding elements of @a v. |
5068f546 | 154 | void operator|=(const valarray<_Tp>&) const; |
7fb397a4 | 155 | /// Left shift slice elements by corresponding elements of @a v. |
5068f546 | 156 | void operator<<=(const valarray<_Tp>&) const; |
7fb397a4 | 157 | /// Right shift slice elements by corresponding elements of @a v. |
5068f546 | 158 | void operator>>=(const valarray<_Tp>&) const; |
7fb397a4 | 159 | /// Assign all slice elements to @a t. |
bb403d69 | 160 | void operator=(const _Tp &) const; |
5068f546 GDR |
161 | // ~slice_array (); |
162 | ||
163 | template<class _Dom> | |
b714a419 | 164 | void operator=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 165 | template<class _Dom> |
b714a419 | 166 | void operator*=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 167 | template<class _Dom> |
b714a419 | 168 | void operator/=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 169 | template<class _Dom> |
b714a419 | 170 | void operator%=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 171 | template<class _Dom> |
b714a419 | 172 | void operator+=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 173 | template<class _Dom> |
b714a419 | 174 | void operator-=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 175 | template<class _Dom> |
b714a419 | 176 | void operator^=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 177 | template<class _Dom> |
b714a419 | 178 | void operator&=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 179 | template<class _Dom> |
b714a419 | 180 | void operator|=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 181 | template<class _Dom> |
b714a419 | 182 | void operator<<=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 183 | template<class _Dom> |
b714a419 | 184 | void operator>>=(const _Expr<_Dom, _Tp>&) const; |
5068f546 | 185 | |
725dc051 | 186 | private: |
5068f546 GDR |
187 | friend class valarray<_Tp>; |
188 | slice_array(_Array<_Tp>, const slice&); | |
189 | ||
b714a419 PC |
190 | const size_t _M_sz; |
191 | const size_t _M_stride; | |
5068f546 | 192 | const _Array<_Tp> _M_array; |
725dc051 | 193 | |
5068f546 GDR |
194 | // not implemented |
195 | slice_array(); | |
725dc051 BK |
196 | }; |
197 | ||
5068f546 | 198 | template<typename _Tp> |
ed6814f7 | 199 | inline |
5068f546 | 200 | slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s) |
d4cd08dd PC |
201 | : _M_sz(__s.size()), _M_stride(__s.stride()), |
202 | _M_array(__a.begin() + __s.start()) {} | |
725dc051 | 203 | |
5068f546 | 204 | template<typename _Tp> |
ed6814f7 | 205 | inline |
5068f546 | 206 | slice_array<_Tp>::slice_array(const slice_array<_Tp>& a) |
d4cd08dd | 207 | : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {} |
ed6814f7 | 208 | |
5068f546 GDR |
209 | // template<typename _Tp> |
210 | // inline slice_array<_Tp>::~slice_array () {} | |
725dc051 | 211 | |
7c301abf | 212 | template<typename _Tp> |
5068f546 GDR |
213 | inline slice_array<_Tp>& |
214 | slice_array<_Tp>::operator=(const slice_array<_Tp>& __a) | |
215 | { | |
5b5bf717 PC |
216 | std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride, |
217 | _M_array, _M_stride); | |
5068f546 GDR |
218 | return *this; |
219 | } | |
7c301abf | 220 | |
5068f546 | 221 | template<typename _Tp> |
725dc051 | 222 | inline void |
bb403d69 | 223 | slice_array<_Tp>::operator=(const _Tp& __t) const |
5b5bf717 | 224 | { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); } |
ed6814f7 | 225 | |
5068f546 | 226 | template<typename _Tp> |
725dc051 | 227 | inline void |
5068f546 | 228 | slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const |
5b5bf717 | 229 | { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); } |
ed6814f7 | 230 | |
5068f546 GDR |
231 | template<typename _Tp> |
232 | template<class _Dom> | |
725dc051 | 233 | inline void |
5068f546 | 234 | slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const |
5b5bf717 | 235 | { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); } |
725dc051 BK |
236 | |
237 | #undef _DEFINE_VALARRAY_OPERATOR | |
926479c2 | 238 | #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \ |
00386a9b GDR |
239 | template<typename _Tp> \ |
240 | inline void \ | |
241 | slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \ | |
242 | { \ | |
243 | _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\ | |
244 | } \ | |
725dc051 | 245 | \ |
00386a9b GDR |
246 | template<typename _Tp> \ |
247 | template<class _Dom> \ | |
248 | inline void \ | |
249 | slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\ | |
250 | { \ | |
251 | _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \ | |
252 | } | |
ed6814f7 | 253 | |
725dc051 | 254 | |
00386a9b GDR |
255 | _DEFINE_VALARRAY_OPERATOR(*, __multiplies) |
256 | _DEFINE_VALARRAY_OPERATOR(/, __divides) | |
257 | _DEFINE_VALARRAY_OPERATOR(%, __modulus) | |
258 | _DEFINE_VALARRAY_OPERATOR(+, __plus) | |
259 | _DEFINE_VALARRAY_OPERATOR(-, __minus) | |
260 | _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor) | |
261 | _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and) | |
262 | _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or) | |
263 | _DEFINE_VALARRAY_OPERATOR(<<, __shift_left) | |
264 | _DEFINE_VALARRAY_OPERATOR(>>, __shift_right) | |
725dc051 BK |
265 | |
266 | #undef _DEFINE_VALARRAY_OPERATOR | |
267 | ||
5b9daa7e BK |
268 | // @} group numeric_arrays |
269 | ||
3cbc7af0 | 270 | _GLIBCXX_END_NAMESPACE |
725dc051 | 271 | |
3d7c150e | 272 | #endif /* _SLICE_ARRAY_H */ |