1 // The template and inlines for the -*- C++ -*- indirect_array class.
3 // Copyright (C) 1997-2025 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/indirect_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 _INDIRECT_ARRAY_H
33 #define _INDIRECT_ARRAY_H 1
35 #ifdef _GLIBCXX_SYSHDR
36 #pragma GCC system_header
39 namespace std
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @addtogroup numeric_arrays
49 * @brief Reference to arbitrary subset of an array.
51 * An indirect_array is a reference to the actual elements of an array
52 * specified by an ordered array of indices. The way to get an
53 * indirect_array is to call operator[](valarray<size_t>) on a valarray.
54 * The returned indirect_array then permits carrying operations out on the
55 * referenced subset of elements in the original valarray.
57 * For example, if an indirect_array is obtained using the array (4,2,0) as
58 * an argument, and then assigned to an array containing (1,2,3), then the
59 * underlying array will have array[0]==3, array[2]==2, and array[4]==1.
61 * @param Tp Element type.
67 typedef _Tp value_type
;
69 // _GLIBCXX_RESOLVE_LIB_DEFECTS
70 // 253. valarray helper functions are almost entirely useless
72 /// Copy constructor. Both slices refer to the same underlying array.
73 indirect_array(const indirect_array
&);
75 /// Assignment operator. Assigns elements to corresponding elements
77 indirect_array
& operator=(const indirect_array
&);
79 /// Assign slice elements to corresponding elements of @a v.
80 void operator=(const valarray
<_Tp
>&) const;
81 /// Multiply slice elements by corresponding elements of @a v.
82 void operator*=(const valarray
<_Tp
>&) const;
83 /// Divide slice elements by corresponding elements of @a v.
84 void operator/=(const valarray
<_Tp
>&) const;
85 /// Modulo slice elements by corresponding elements of @a v.
86 void operator%=(const valarray
<_Tp
>&) const;
87 /// Add corresponding elements of @a v to slice elements.
88 void operator+=(const valarray
<_Tp
>&) const;
89 /// Subtract corresponding elements of @a v from slice elements.
90 void operator-=(const valarray
<_Tp
>&) const;
91 /// Logical xor slice elements with corresponding elements of @a v.
92 void operator^=(const valarray
<_Tp
>&) const;
93 /// Logical and slice elements with corresponding elements of @a v.
94 void operator&=(const valarray
<_Tp
>&) const;
95 /// Logical or slice elements with corresponding elements of @a v.
96 void operator|=(const valarray
<_Tp
>&) const;
97 /// Left shift slice elements by corresponding elements of @a v.
98 void operator<<=(const valarray
<_Tp
>&) const;
99 /// Right shift slice elements by corresponding elements of @a v.
100 void operator>>=(const valarray
<_Tp
>&) const;
101 /// Assign all slice elements to @a t.
102 void operator= (const _Tp
&) const;
103 // ~indirect_array();
106 void operator=(const _Expr
<_Dom
, _Tp
>&) const;
108 void operator*=(const _Expr
<_Dom
, _Tp
>&) const;
110 void operator/=(const _Expr
<_Dom
, _Tp
>&) const;
112 void operator%=(const _Expr
<_Dom
, _Tp
>&) const;
114 void operator+=(const _Expr
<_Dom
, _Tp
>&) const;
116 void operator-=(const _Expr
<_Dom
, _Tp
>&) const;
118 void operator^=(const _Expr
<_Dom
, _Tp
>&) const;
120 void operator&=(const _Expr
<_Dom
, _Tp
>&) const;
122 void operator|=(const _Expr
<_Dom
, _Tp
>&) const;
124 void operator<<=(const _Expr
<_Dom
, _Tp
>&) const;
126 void operator>>=(const _Expr
<_Dom
, _Tp
>&) const;
129 /// Copy constructor. Both slices refer to the same underlying array.
130 indirect_array(_Array
<_Tp
>, size_t, _Array
<size_t>);
132 friend class valarray
<_Tp
>;
133 friend class gslice_array
<_Tp
>;
136 const _Array
<size_t> _M_index
;
137 const _Array
<_Tp
> _M_array
;
143 template<typename _Tp
>
145 indirect_array
<_Tp
>::indirect_array(const indirect_array
<_Tp
>& __a
)
146 : _M_sz(__a
._M_sz
), _M_index(__a
._M_index
), _M_array(__a
._M_array
) {}
148 template<typename _Tp
>
150 indirect_array
<_Tp
>::indirect_array(_Array
<_Tp
> __a
, size_t __s
,
152 : _M_sz(__s
), _M_index(__i
), _M_array(__a
) {}
154 template<typename _Tp
>
155 inline indirect_array
<_Tp
>&
156 indirect_array
<_Tp
>::operator=(const indirect_array
<_Tp
>& __a
)
158 std::__valarray_copy(__a
._M_array
, _M_sz
, __a
._M_index
, _M_array
,
163 template<typename _Tp
>
165 indirect_array
<_Tp
>::operator=(const _Tp
& __t
) const
166 { std::__valarray_fill(_M_array
, _M_index
, _M_sz
, __t
); }
168 template<typename _Tp
>
170 indirect_array
<_Tp
>::operator=(const valarray
<_Tp
>& __v
) const
171 { std::__valarray_copy(_Array
<_Tp
>(__v
), _M_sz
, _M_array
, _M_index
); }
173 template<typename _Tp
>
176 indirect_array
<_Tp
>::operator=(const _Expr
<_Dom
, _Tp
>& __e
) const
177 { std::__valarray_copy(__e
, _M_sz
, _M_array
, _M_index
); }
179 /// @cond undocumented
180 #undef _DEFINE_VALARRAY_OPERATOR
181 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
182 template<typename _Tp> \
184 indirect_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const\
186 _Array_augmented_##_Name(_M_array, _M_index, _Array<_Tp>(__v), _M_sz); \
189 template<typename _Tp> \
190 template<class _Dom> \
192 indirect_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
194 _Array_augmented_##_Name(_M_array, _M_index, __e, _M_sz); \
197 _DEFINE_VALARRAY_OPERATOR(*, __multiplies
)
198 _DEFINE_VALARRAY_OPERATOR(/, __divides
)
199 _DEFINE_VALARRAY_OPERATOR(%, __modulus
)
200 _DEFINE_VALARRAY_OPERATOR(+, __plus
)
201 _DEFINE_VALARRAY_OPERATOR(-, __minus
)
202 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor
)
203 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and
)
204 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or
)
205 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left
)
206 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right
)
208 #undef _DEFINE_VALARRAY_OPERATOR
211 /// @} group numeric_arrays
213 _GLIBCXX_END_NAMESPACE_VERSION
216 #endif /* _INDIRECT_ARRAY_H */