1*38fd1498Szrj // The template and inlines for the -*- C++ -*- indirect_array class.
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 1997-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /** @file bits/indirect_array.h
26*38fd1498Szrj * This is an internal header file, included by other library headers.
27*38fd1498Szrj * Do not attempt to use it directly. @headername{valarray}
28*38fd1498Szrj */
29*38fd1498Szrj
30*38fd1498Szrj // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31*38fd1498Szrj
32*38fd1498Szrj #ifndef _INDIRECT_ARRAY_H
33*38fd1498Szrj #define _INDIRECT_ARRAY_H 1
34*38fd1498Szrj
35*38fd1498Szrj #pragma GCC system_header
36*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)37*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
38*38fd1498Szrj {
39*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
40*38fd1498Szrj
41*38fd1498Szrj /**
42*38fd1498Szrj * @addtogroup numeric_arrays
43*38fd1498Szrj * @{
44*38fd1498Szrj */
45*38fd1498Szrj
46*38fd1498Szrj /**
47*38fd1498Szrj * @brief Reference to arbitrary subset of an array.
48*38fd1498Szrj *
49*38fd1498Szrj * An indirect_array is a reference to the actual elements of an array
50*38fd1498Szrj * specified by an ordered array of indices. The way to get an
51*38fd1498Szrj * indirect_array is to call operator[](valarray<size_t>) on a valarray.
52*38fd1498Szrj * The returned indirect_array then permits carrying operations out on the
53*38fd1498Szrj * referenced subset of elements in the original valarray.
54*38fd1498Szrj *
55*38fd1498Szrj * For example, if an indirect_array is obtained using the array (4,2,0) as
56*38fd1498Szrj * an argument, and then assigned to an array containing (1,2,3), then the
57*38fd1498Szrj * underlying array will have array[0]==3, array[2]==2, and array[4]==1.
58*38fd1498Szrj *
59*38fd1498Szrj * @param Tp Element type.
60*38fd1498Szrj */
61*38fd1498Szrj template <class _Tp>
62*38fd1498Szrj class indirect_array
63*38fd1498Szrj {
64*38fd1498Szrj public:
65*38fd1498Szrj typedef _Tp value_type;
66*38fd1498Szrj
67*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
68*38fd1498Szrj // 253. valarray helper functions are almost entirely useless
69*38fd1498Szrj
70*38fd1498Szrj /// Copy constructor. Both slices refer to the same underlying array.
71*38fd1498Szrj indirect_array(const indirect_array&);
72*38fd1498Szrj
73*38fd1498Szrj /// Assignment operator. Assigns elements to corresponding elements
74*38fd1498Szrj /// of @a a.
75*38fd1498Szrj indirect_array& operator=(const indirect_array&);
76*38fd1498Szrj
77*38fd1498Szrj /// Assign slice elements to corresponding elements of @a v.
78*38fd1498Szrj void operator=(const valarray<_Tp>&) const;
79*38fd1498Szrj /// Multiply slice elements by corresponding elements of @a v.
80*38fd1498Szrj void operator*=(const valarray<_Tp>&) const;
81*38fd1498Szrj /// Divide slice elements by corresponding elements of @a v.
82*38fd1498Szrj void operator/=(const valarray<_Tp>&) const;
83*38fd1498Szrj /// Modulo slice elements by corresponding elements of @a v.
84*38fd1498Szrj void operator%=(const valarray<_Tp>&) const;
85*38fd1498Szrj /// Add corresponding elements of @a v to slice elements.
86*38fd1498Szrj void operator+=(const valarray<_Tp>&) const;
87*38fd1498Szrj /// Subtract corresponding elements of @a v from slice elements.
88*38fd1498Szrj void operator-=(const valarray<_Tp>&) const;
89*38fd1498Szrj /// Logical xor slice elements with corresponding elements of @a v.
90*38fd1498Szrj void operator^=(const valarray<_Tp>&) const;
91*38fd1498Szrj /// Logical and slice elements with corresponding elements of @a v.
92*38fd1498Szrj void operator&=(const valarray<_Tp>&) const;
93*38fd1498Szrj /// Logical or slice elements with corresponding elements of @a v.
94*38fd1498Szrj void operator|=(const valarray<_Tp>&) const;
95*38fd1498Szrj /// Left shift slice elements by corresponding elements of @a v.
96*38fd1498Szrj void operator<<=(const valarray<_Tp>&) const;
97*38fd1498Szrj /// Right shift slice elements by corresponding elements of @a v.
98*38fd1498Szrj void operator>>=(const valarray<_Tp>&) const;
99*38fd1498Szrj /// Assign all slice elements to @a t.
100*38fd1498Szrj void operator= (const _Tp&) const;
101*38fd1498Szrj // ~indirect_array();
102*38fd1498Szrj
103*38fd1498Szrj template<class _Dom>
104*38fd1498Szrj void operator=(const _Expr<_Dom, _Tp>&) const;
105*38fd1498Szrj template<class _Dom>
106*38fd1498Szrj void operator*=(const _Expr<_Dom, _Tp>&) const;
107*38fd1498Szrj template<class _Dom>
108*38fd1498Szrj void operator/=(const _Expr<_Dom, _Tp>&) const;
109*38fd1498Szrj template<class _Dom>
110*38fd1498Szrj void operator%=(const _Expr<_Dom, _Tp>&) const;
111*38fd1498Szrj template<class _Dom>
112*38fd1498Szrj void operator+=(const _Expr<_Dom, _Tp>&) const;
113*38fd1498Szrj template<class _Dom>
114*38fd1498Szrj void operator-=(const _Expr<_Dom, _Tp>&) const;
115*38fd1498Szrj template<class _Dom>
116*38fd1498Szrj void operator^=(const _Expr<_Dom, _Tp>&) const;
117*38fd1498Szrj template<class _Dom>
118*38fd1498Szrj void operator&=(const _Expr<_Dom, _Tp>&) const;
119*38fd1498Szrj template<class _Dom>
120*38fd1498Szrj void operator|=(const _Expr<_Dom, _Tp>&) const;
121*38fd1498Szrj template<class _Dom>
122*38fd1498Szrj void operator<<=(const _Expr<_Dom, _Tp>&) const;
123*38fd1498Szrj template<class _Dom>
124*38fd1498Szrj void operator>>=(const _Expr<_Dom, _Tp>&) const;
125*38fd1498Szrj
126*38fd1498Szrj private:
127*38fd1498Szrj /// Copy constructor. Both slices refer to the same underlying array.
128*38fd1498Szrj indirect_array(_Array<_Tp>, size_t, _Array<size_t>);
129*38fd1498Szrj
130*38fd1498Szrj friend class valarray<_Tp>;
131*38fd1498Szrj friend class gslice_array<_Tp>;
132*38fd1498Szrj
133*38fd1498Szrj const size_t _M_sz;
134*38fd1498Szrj const _Array<size_t> _M_index;
135*38fd1498Szrj const _Array<_Tp> _M_array;
136*38fd1498Szrj
137*38fd1498Szrj // not implemented
138*38fd1498Szrj indirect_array();
139*38fd1498Szrj };
140*38fd1498Szrj
141*38fd1498Szrj template<typename _Tp>
142*38fd1498Szrj inline
143*38fd1498Szrj indirect_array<_Tp>::indirect_array(const indirect_array<_Tp>& __a)
144*38fd1498Szrj : _M_sz(__a._M_sz), _M_index(__a._M_index), _M_array(__a._M_array) {}
145*38fd1498Szrj
146*38fd1498Szrj template<typename _Tp>
147*38fd1498Szrj inline
148*38fd1498Szrj indirect_array<_Tp>::indirect_array(_Array<_Tp> __a, size_t __s,
149*38fd1498Szrj _Array<size_t> __i)
150*38fd1498Szrj : _M_sz(__s), _M_index(__i), _M_array(__a) {}
151*38fd1498Szrj
152*38fd1498Szrj template<typename _Tp>
153*38fd1498Szrj inline indirect_array<_Tp>&
154*38fd1498Szrj indirect_array<_Tp>::operator=(const indirect_array<_Tp>& __a)
155*38fd1498Szrj {
156*38fd1498Szrj std::__valarray_copy(__a._M_array, _M_sz, __a._M_index, _M_array,
157*38fd1498Szrj _M_index);
158*38fd1498Szrj return *this;
159*38fd1498Szrj }
160*38fd1498Szrj
161*38fd1498Szrj template<typename _Tp>
162*38fd1498Szrj inline void
163*38fd1498Szrj indirect_array<_Tp>::operator=(const _Tp& __t) const
164*38fd1498Szrj { std::__valarray_fill(_M_array, _M_index, _M_sz, __t); }
165*38fd1498Szrj
166*38fd1498Szrj template<typename _Tp>
167*38fd1498Szrj inline void
168*38fd1498Szrj indirect_array<_Tp>::operator=(const valarray<_Tp>& __v) const
169*38fd1498Szrj { std::__valarray_copy(_Array<_Tp>(__v), _M_sz, _M_array, _M_index); }
170*38fd1498Szrj
171*38fd1498Szrj template<typename _Tp>
172*38fd1498Szrj template<class _Dom>
173*38fd1498Szrj inline void
174*38fd1498Szrj indirect_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
175*38fd1498Szrj { std::__valarray_copy(__e, _M_sz, _M_array, _M_index); }
176*38fd1498Szrj
177*38fd1498Szrj #undef _DEFINE_VALARRAY_OPERATOR
178*38fd1498Szrj #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
179*38fd1498Szrj template<typename _Tp> \
180*38fd1498Szrj inline void \
181*38fd1498Szrj indirect_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const\
182*38fd1498Szrj { \
183*38fd1498Szrj _Array_augmented_##_Name(_M_array, _M_index, _Array<_Tp>(__v), _M_sz); \
184*38fd1498Szrj } \
185*38fd1498Szrj \
186*38fd1498Szrj template<typename _Tp> \
187*38fd1498Szrj template<class _Dom> \
188*38fd1498Szrj inline void \
189*38fd1498Szrj indirect_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
190*38fd1498Szrj { \
191*38fd1498Szrj _Array_augmented_##_Name(_M_array, _M_index, __e, _M_sz); \
192*38fd1498Szrj }
193*38fd1498Szrj
194*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
195*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(/, __divides)
196*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(%, __modulus)
197*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(+, __plus)
198*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(-, __minus)
199*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
200*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
201*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
202*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
203*38fd1498Szrj _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
204*38fd1498Szrj
205*38fd1498Szrj #undef _DEFINE_VALARRAY_OPERATOR
206*38fd1498Szrj
207*38fd1498Szrj // @} group numeric_arrays
208*38fd1498Szrj
209*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
210*38fd1498Szrj } // namespace
211*38fd1498Szrj
212*38fd1498Szrj #endif /* _INDIRECT_ARRAY_H */
213