1 // The template and inlines for the -*- C++ -*- slice_array class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
33 /** @file slice_array.h
34 * This is an internal header file, included by other library headers.
35 * You should not attempt to use it directly.
38 #ifndef _SLICE_ARRAY_H
39 #define _SLICE_ARRAY_H 1
41 #pragma GCC system_header
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 // The default constructor constructor is not required to initialize
87 // data members with any meaningful values, so we choose to do nothing.
92 slice::slice(size_t __o
, size_t __d
, size_t __s
)
93 : _M_off(__o
), _M_sz(__d
), _M_st(__s
) {}
104 slice::stride() const
108 * @brief Reference to one-dimensional subset of an array.
110 * A slice_array is a reference to the actual elements of an array
111 * specified by a slice. The way to get a slice_array is to call
112 * operator[](slice) on a valarray. The returned slice_array then permits
113 * carrying operations out on the referenced subset of elements in the
114 * original valarray. For example, operator+=(valarray) will add values
115 * to the subset of elements in the underlying valarray this slice_array
118 * @param Tp Element type.
120 template<typename _Tp
>
124 typedef _Tp value_type
;
126 // _GLIBCXX_RESOLVE_LIB_DEFECTS
127 // 253. valarray helper functions are almost entirely useless
129 /// Copy constructor. Both slices refer to the same underlying array.
130 slice_array(const slice_array
&);
132 /// Assignment operator. Assigns slice elements to corresponding
133 /// elements of @a a.
134 slice_array
& operator=(const slice_array
&);
136 /// Assign slice elements to corresponding elements of @a v.
137 void operator=(const valarray
<_Tp
>&) const;
138 /// Multiply slice elements by corresponding elements of @a v.
139 void operator*=(const valarray
<_Tp
>&) const;
140 /// Divide slice elements by corresponding elements of @a v.
141 void operator/=(const valarray
<_Tp
>&) const;
142 /// Modulo slice elements by corresponding elements of @a v.
143 void operator%=(const valarray
<_Tp
>&) const;
144 /// Add corresponding elements of @a v to slice elements.
145 void operator+=(const valarray
<_Tp
>&) const;
146 /// Subtract corresponding elements of @a v from slice elements.
147 void operator-=(const valarray
<_Tp
>&) const;
148 /// Logical xor slice elements with corresponding elements of @a v.
149 void operator^=(const valarray
<_Tp
>&) const;
150 /// Logical and slice elements with corresponding elements of @a v.
151 void operator&=(const valarray
<_Tp
>&) const;
152 /// Logical or slice elements with corresponding elements of @a v.
153 void operator|=(const valarray
<_Tp
>&) const;
154 /// Left shift slice elements by corresponding elements of @a v.
155 void operator<<=(const valarray
<_Tp
>&) const;
156 /// Right shift slice elements by corresponding elements of @a v.
157 void operator>>=(const valarray
<_Tp
>&) const;
158 /// Assign all slice elements to @a t.
159 void operator=(const _Tp
&) const;
163 void operator=(const _Expr
<_Dom
, _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;
186 friend class valarray
<_Tp
>;
187 slice_array(_Array
<_Tp
>, const slice
&);
190 const size_t _M_stride
;
191 const _Array
<_Tp
> _M_array
;
197 template<typename _Tp
>
199 slice_array
<_Tp
>::slice_array(_Array
<_Tp
> __a
, const slice
& __s
)
200 : _M_sz(__s
.size()), _M_stride(__s
.stride()),
201 _M_array(__a
.begin() + __s
.start()) {}
203 template<typename _Tp
>
205 slice_array
<_Tp
>::slice_array(const slice_array
<_Tp
>& a
)
206 : _M_sz(a
._M_sz
), _M_stride(a
._M_stride
), _M_array(a
._M_array
) {}
208 // template<typename _Tp>
209 // inline slice_array<_Tp>::~slice_array () {}
211 template<typename _Tp
>
212 inline slice_array
<_Tp
>&
213 slice_array
<_Tp
>::operator=(const slice_array
<_Tp
>& __a
)
215 std::__valarray_copy(__a
._M_array
, __a
._M_sz
, __a
._M_stride
,
216 _M_array
, _M_stride
);
220 template<typename _Tp
>
222 slice_array
<_Tp
>::operator=(const _Tp
& __t
) const
223 { std::__valarray_fill(_M_array
, _M_sz
, _M_stride
, __t
); }
225 template<typename _Tp
>
227 slice_array
<_Tp
>::operator=(const valarray
<_Tp
>& __v
) const
228 { std::__valarray_copy(_Array
<_Tp
>(__v
), _M_array
, _M_sz
, _M_stride
); }
230 template<typename _Tp
>
233 slice_array
<_Tp
>::operator=(const _Expr
<_Dom
,_Tp
>& __e
) const
234 { std::__valarray_copy(__e
, _M_sz
, _M_array
, _M_stride
); }
236 #undef _DEFINE_VALARRAY_OPERATOR
237 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
238 template<typename _Tp> \
240 slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
242 _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
245 template<typename _Tp> \
246 template<class _Dom> \
248 slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
250 _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
254 _DEFINE_VALARRAY_OPERATOR(*, __multiplies
)
255 _DEFINE_VALARRAY_OPERATOR(/, __divides
)
256 _DEFINE_VALARRAY_OPERATOR(%, __modulus
)
257 _DEFINE_VALARRAY_OPERATOR(+, __plus
)
258 _DEFINE_VALARRAY_OPERATOR(-, __minus
)
259 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor
)
260 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and
)
261 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or
)
262 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left
)
263 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right
)
265 #undef _DEFINE_VALARRAY_OPERATOR
269 #endif /* _SLICE_ARRAY_H */