1 // The template and inlines for the -*- C++ -*- slice_array class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2009
4 // Free Software Foundation, Inc.
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
9 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
26 /** @file slice_array.h
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
33 #ifndef _SLICE_ARRAY_H
34 #define _SLICE_ARRAY_H 1
36 #pragma GCC system_header
38 _GLIBCXX_BEGIN_NAMESPACE(std)
41 * @addtogroup numeric_arrays
46 * @brief Class defining one-dimensional subset of an array.
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
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.
61 /// Construct an empty slice.
65 * @brief Construct a slice.
67 * @param o Offset in array of first element.
68 * @param d Number of elements in slice.
69 * @param s Stride between array elements.
71 slice(size_t, size_t, size_t);
73 /// Return array offset of first slice element.
75 /// Return size of slice.
77 /// Return array stride of slice.
78 size_t stride() const;
81 size_t _M_off; // offset
83 size_t _M_st; // stride unit
86 // _GLIBCXX_RESOLVE_LIB_DEFECTS
87 // 543. valarray slice default constructor
90 : _M_off(0), _M_sz(0), _M_st(0) {}
93 slice::slice(size_t __o, size_t __d, size_t __s)
94 : _M_off(__o), _M_sz(__d), _M_st(__s) {}
105 slice::stride() const
109 * @brief Reference to one-dimensional subset of an array.
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
119 * @param Tp Element type.
121 template<typename _Tp>
125 typedef _Tp value_type;
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // 253. valarray helper functions are almost entirely useless
130 /// Copy constructor. Both slices refer to the same underlying array.
131 slice_array(const slice_array&);
133 /// Assignment operator. Assigns slice elements to corresponding
134 /// elements of @a a.
135 slice_array& operator=(const slice_array&);
137 /// Assign slice elements to corresponding elements of @a v.
138 void operator=(const valarray<_Tp>&) const;
139 /// Multiply slice elements by corresponding elements of @a v.
140 void operator*=(const valarray<_Tp>&) const;
141 /// Divide slice elements by corresponding elements of @a v.
142 void operator/=(const valarray<_Tp>&) const;
143 /// Modulo slice elements by corresponding elements of @a v.
144 void operator%=(const valarray<_Tp>&) const;
145 /// Add corresponding elements of @a v to slice elements.
146 void operator+=(const valarray<_Tp>&) const;
147 /// Subtract corresponding elements of @a v from slice elements.
148 void operator-=(const valarray<_Tp>&) const;
149 /// Logical xor slice elements with corresponding elements of @a v.
150 void operator^=(const valarray<_Tp>&) const;
151 /// Logical and slice elements with corresponding elements of @a v.
152 void operator&=(const valarray<_Tp>&) const;
153 /// Logical or slice elements with corresponding elements of @a v.
154 void operator|=(const valarray<_Tp>&) const;
155 /// Left shift slice elements by corresponding elements of @a v.
156 void operator<<=(const valarray<_Tp>&) const;
157 /// Right shift slice elements by corresponding elements of @a v.
158 void operator>>=(const valarray<_Tp>&) const;
159 /// Assign all slice elements to @a t.
160 void operator=(const _Tp &) const;
164 void operator=(const _Expr<_Dom, _Tp>&) const;
166 void operator*=(const _Expr<_Dom, _Tp>&) const;
168 void operator/=(const _Expr<_Dom, _Tp>&) const;
170 void operator%=(const _Expr<_Dom, _Tp>&) const;
172 void operator+=(const _Expr<_Dom, _Tp>&) const;
174 void operator-=(const _Expr<_Dom, _Tp>&) const;
176 void operator^=(const _Expr<_Dom, _Tp>&) const;
178 void operator&=(const _Expr<_Dom, _Tp>&) const;
180 void operator|=(const _Expr<_Dom, _Tp>&) const;
182 void operator<<=(const _Expr<_Dom, _Tp>&) const;
184 void operator>>=(const _Expr<_Dom, _Tp>&) const;
187 friend class valarray<_Tp>;
188 slice_array(_Array<_Tp>, const slice&);
191 const size_t _M_stride;
192 const _Array<_Tp> _M_array;
198 template<typename _Tp>
200 slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
201 : _M_sz(__s.size()), _M_stride(__s.stride()),
202 _M_array(__a.begin() + __s.start()) {}
204 template<typename _Tp>
206 slice_array<_Tp>::slice_array(const slice_array<_Tp>& a)
207 : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
209 // template<typename _Tp>
210 // inline slice_array<_Tp>::~slice_array () {}
212 template<typename _Tp>
213 inline slice_array<_Tp>&
214 slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
216 std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
217 _M_array, _M_stride);
221 template<typename _Tp>
223 slice_array<_Tp>::operator=(const _Tp& __t) const
224 { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
226 template<typename _Tp>
228 slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
229 { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
231 template<typename _Tp>
234 slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
235 { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
237 #undef _DEFINE_VALARRAY_OPERATOR
238 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
239 template<typename _Tp> \
241 slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
243 _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
246 template<typename _Tp> \
247 template<class _Dom> \
249 slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
251 _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
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)
266 #undef _DEFINE_VALARRAY_OPERATOR
268 // @} group numeric_arrays
270 _GLIBCXX_END_NAMESPACE
272 #endif /* _SLICE_ARRAY_H */