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