libstdc++: gslice_array.h Source File

libstdc++

gslice_array.h
Go to the documentation of this file.
1// The template and inlines for the -*- C++ -*- gslice_array class.
2
3// Copyright (C) 1997-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file bits/gslice_array.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{valarray}
28 */
29
30// Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31
32#ifndef _GSLICE_ARRAY_H
33#define _GSLICE_ARRAY_H 1
34
35#ifdef _GLIBCXX_SYSHDR
36#pragma GCC system_header
37#endif
38
39namespace std _GLIBCXX_VISIBILITY(default)
40{
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
42
43 /**
44 * @addtogroup numeric_arrays
45 * @{
46 */
47
48 /**
49 * @brief Reference to multi-dimensional subset of an array.
50 *
51 * A gslice_array is a reference to the actual elements of an array
52 * specified by a gslice. The way to get a gslice_array is to call
53 * operator[](gslice) on a valarray. The returned gslice_array then
54 * permits carrying operations out on the referenced subset of elements in
55 * the original valarray. For example, operator+=(valarray) will add
56 * values to the subset of elements in the underlying valarray this
57 * gslice_array refers to.
58 *
59 * @param Tp Element type.
60 */
61 template<typename _Tp>
62 class gslice_array
63 {
64 public:
65 typedef _Tp value_type;
66
67 // _GLIBCXX_RESOLVE_LIB_DEFECTS
68 // 253. valarray helper functions are almost entirely useless
69
70 /// Copy constructor. Both slices refer to the same underlying array.
71 gslice_array(const gslice_array&);
72
73 /// Assignment operator. Assigns slice elements to corresponding
74 /// elements of @a a.
75 gslice_array& operator=(const gslice_array&);
76
77 /// Assign slice elements to corresponding elements of @a v.
78 void operator=(const valarray<_Tp>&) const;
79 /// Multiply slice elements by corresponding elements of @a v.
80 void operator*=(const valarray<_Tp>&) const;
81 /// Divide slice elements by corresponding elements of @a v.
82 void operator/=(const valarray<_Tp>&) const;
83 /// Modulo slice elements by corresponding elements of @a v.
84 void operator%=(const valarray<_Tp>&) const;
85 /// Add corresponding elements of @a v to slice elements.
86 void operator+=(const valarray<_Tp>&) const;
87 /// Subtract corresponding elements of @a v from slice elements.
88 void operator-=(const valarray<_Tp>&) const;
89 /// Logical xor slice elements with corresponding elements of @a v.
90 void operator^=(const valarray<_Tp>&) const;
91 /// Logical and slice elements with corresponding elements of @a v.
92 void operator&=(const valarray<_Tp>&) const;
93 /// Logical or slice elements with corresponding elements of @a v.
94 void operator|=(const valarray<_Tp>&) const;
95 /// Left shift slice elements by corresponding elements of @a v.
96 void operator<<=(const valarray<_Tp>&) const;
97 /// Right shift slice elements by corresponding elements of @a v.
98 void operator>>=(const valarray<_Tp>&) const;
99 /// Assign all slice elements to @a t.
100 void operator=(const _Tp&) const;
101
102 template<class _Dom>
103 void operator=(const _Expr<_Dom, _Tp>&) const;
104 template<class _Dom>
105 void operator*=(const _Expr<_Dom, _Tp>&) const;
106 template<class _Dom>
107 void operator/=(const _Expr<_Dom, _Tp>&) const;
108 template<class _Dom>
109 void operator%=(const _Expr<_Dom, _Tp>&) const;
110 template<class _Dom>
111 void operator+=(const _Expr<_Dom, _Tp>&) const;
112 template<class _Dom>
113 void operator-=(const _Expr<_Dom, _Tp>&) const;
114 template<class _Dom>
115 void operator^=(const _Expr<_Dom, _Tp>&) const;
116 template<class _Dom>
117 void operator&=(const _Expr<_Dom, _Tp>&) const;
118 template<class _Dom>
119 void operator|=(const _Expr<_Dom, _Tp>&) const;
120 template<class _Dom>
121 void operator<<=(const _Expr<_Dom, _Tp>&) const;
122 template<class _Dom>
123 void operator>>=(const _Expr<_Dom, _Tp>&) const;
124
125 private:
126 _Array<_Tp> _M_array;
127 const valarray<size_t>& _M_index;
128
129 friend class valarray<_Tp>;
130
131 gslice_array(_Array<_Tp>, const valarray<size_t>&);
132
133#if __cplusplus < 201103L
134 // not implemented
135 gslice_array();
136#else
137 public:
138 gslice_array() = delete;
139#endif
140 };
141
142 template<typename _Tp>
143 inline
144 gslice_array<_Tp>::gslice_array(_Array<_Tp> __a,
145 const valarray<size_t>& __i)
146 : _M_array(__a), _M_index(__i) {}
147
148 template<typename _Tp>
149 inline
150 gslice_array<_Tp>::gslice_array(const gslice_array<_Tp>& __a)
151 : _M_array(__a._M_array), _M_index(__a._M_index) {}
152
153 template<typename _Tp>
154 inline gslice_array<_Tp>&
155 gslice_array<_Tp>::operator=(const gslice_array<_Tp>& __a)
156 {
157 std::__valarray_copy(_Array<_Tp>(__a._M_array),
158 _Array<size_t>(__a._M_index), _M_index.size(),
159 _M_array, _Array<size_t>(_M_index));
160 return *this;
161 }
162
163 template<typename _Tp>
164 inline void
165 gslice_array<_Tp>::operator=(const _Tp& __t) const
166 {
167 std::__valarray_fill(_M_array, _Array<size_t>(_M_index),
168 _M_index.size(), __t);
169 }
170
171 template<typename _Tp>
172 inline void
173 gslice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
174 {
175 std::__valarray_copy(_Array<_Tp>(__v), __v.size(),
176 _M_array, _Array<size_t>(_M_index));
177 }
178
179 template<typename _Tp>
180 template<class _Dom>
181 inline void
182 gslice_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
183 {
184 std::__valarray_copy (__e, _M_index.size(), _M_array,
185 _Array<size_t>(_M_index));
186 }
187
188 /// @cond undocumented
189#undef _DEFINE_VALARRAY_OPERATOR
190#define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
191 template<typename _Tp> \
192 inline void \
193 gslice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
194 { \
195 _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), \
196 _Array<_Tp>(__v), __v.size()); \
197 } \
198 \
199 template<typename _Tp> \
200 template<class _Dom> \
201 inline void \
202 gslice_array<_Tp>::operator _Op##= (const _Expr<_Dom, _Tp>& __e) const\
203 { \
204 _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), __e,\
205 _M_index.size()); \
206 }
207
208_DEFINE_VALARRAY_OPERATOR(*, __multiplies)
209_DEFINE_VALARRAY_OPERATOR(/, __divides)
210_DEFINE_VALARRAY_OPERATOR(%, __modulus)
211_DEFINE_VALARRAY_OPERATOR(+, __plus)
212_DEFINE_VALARRAY_OPERATOR(-, __minus)
213_DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
214_DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
215_DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
216_DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
217_DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
218
219#undef _DEFINE_VALARRAY_OPERATOR
220 /// @endcond
221
222 /// @} group numeric_arrays
223
224_GLIBCXX_END_NAMESPACE_VERSION
225} // namespace
226
227#endif /* _GSLICE_ARRAY_H */
std::valarray::size
size_t size() const
Return the number of elements in array.
Definition valarray:949
std::gslice_array::gslice_array
gslice_array(const gslice_array &)
Copy constructor. Both slices refer to the same underlying array.
Definition gslice_array.h:150
std::gslice_array::operator=
gslice_array & operator=(const gslice_array &)
Assignment operator. Assigns slice elements to corresponding elements of a.
Definition gslice_array.h:155
std
ISO C++ entities toplevel namespace is std.
std::valarray
Smart array designed to support numeric processing.
Definition valarray:132
std::gslice_array
Reference to multi-dimensional subset of an array.
Definition gslice_array.h:63
std::gslice_array::operator<<=
void operator<<=(const valarray< _Tp > &) const
Left shift slice elements by corresponding elements of v.
std::gslice_array::operator/=
void operator/=(const valarray< _Tp > &) const
Divide slice elements by corresponding elements of v.
std::gslice_array::operator%=
void operator%=(const valarray< _Tp > &) const
Modulo slice elements by corresponding elements of v.
std::gslice_array::operator>>=
void operator>>=(const valarray< _Tp > &) const
Right shift slice elements by corresponding elements of v.
std::gslice_array::operator*=
void operator*=(const valarray< _Tp > &) const
Multiply slice elements by corresponding elements of v.
std::gslice_array::operator|=
void operator|=(const valarray< _Tp > &) const
Logical or slice elements with corresponding elements of v.
std::gslice_array::operator^=
void operator^=(const valarray< _Tp > &) const
Logical xor slice elements with corresponding elements of v.
std::gslice_array::operator-=
void operator-=(const valarray< _Tp > &) const
Subtract corresponding elements of v from slice elements.
std::gslice_array::operator+=
void operator+=(const valarray< _Tp > &) const
Add corresponding elements of v to slice elements.
std::gslice_array::operator&=
void operator&=(const valarray< _Tp > &) const
Logical and slice elements with corresponding elements of v.