libstdc++
slice_array.h
Go to the documentation of this file.
1 // The template and inlines for the -*- C++ -*- slice_array class.
2 
3 // Copyright (C) 1997-2025 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/slice_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 _SLICE_ARRAY_H
33 #define _SLICE_ARRAY_H 1
34 
35 #ifdef _GLIBCXX_SYSHDR
36 #pragma GCC system_header
37 #endif
38 
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  /**
44  * @addtogroup numeric_arrays
45  * @{
46  */
47 
48  /**
49  * @brief Class defining one-dimensional subset of an array.
50  *
51  * The slice class represents a one-dimensional subset of an array,
52  * specified by three parameters: start offset, size, and stride. The
53  * start offset is the index of the first element of the array that is part
54  * of the subset. The size is the total number of elements in the subset.
55  * Stride is the distance between each successive array element to include
56  * in the subset.
57  *
58  * For example, with an array of size 10, and a slice with offset 1, size 3
59  * and stride 2, the subset consists of array elements 1, 3, and 5.
60  */
61  class slice
62  {
63  public:
64  /// Construct an empty slice.
65  slice();
66 
67  /**
68  * @brief Construct a slice.
69  *
70  * @param __o Offset in array of first element.
71  * @param __d Number of elements in slice.
72  * @param __s Stride between array elements.
73  */
74  slice(size_t __o, size_t __d, size_t __s);
75 
76  /// Return array offset of first slice element.
77  size_t start() const;
78  /// Return size of slice.
79  size_t size() const;
80  /// Return array stride of slice.
81  size_t stride() const;
82 
83 #if __cpp_impl_three_way_comparison >= 201907L
84  /// Equality comparison
85  friend bool operator==(const slice&, const slice&) = default;
86 #endif
87 
88  private:
89  size_t _M_off; // offset
90  size_t _M_sz; // size
91  size_t _M_st; // stride unit
92  };
93 
94  // _GLIBCXX_RESOLVE_LIB_DEFECTS
95  // 543. valarray slice default constructor
96  inline
98  : _M_off(0), _M_sz(0), _M_st(0) {}
99 
100  inline
101  slice::slice(size_t __o, size_t __d, size_t __s)
102  : _M_off(__o), _M_sz(__d), _M_st(__s) {}
103 
104  inline size_t
105  slice::start() const
106  { return _M_off; }
107 
108  inline size_t
109  slice::size() const
110  { return _M_sz; }
111 
112  inline size_t
114  { return _M_st; }
115 
116  /**
117  * @brief Reference to one-dimensional subset of an array.
118  *
119  * A slice_array is a reference to the actual elements of an array
120  * specified by a slice. The way to get a slice_array is to call
121  * operator[](slice) on a valarray. The returned slice_array then permits
122  * carrying operations out on the referenced subset of elements in the
123  * original valarray. For example, operator+=(valarray) will add values
124  * to the subset of elements in the underlying valarray this slice_array
125  * refers to.
126  *
127  * @param Tp Element type.
128  */
129  template<typename _Tp>
131  {
132  public:
133  typedef _Tp value_type;
134 
135  // _GLIBCXX_RESOLVE_LIB_DEFECTS
136  // 253. valarray helper functions are almost entirely useless
137 
138  /// Copy constructor. Both slices refer to the same underlying array.
139  slice_array(const slice_array&);
140 
141  /// Assignment operator. Assigns slice elements to corresponding
142  /// elements of @a a.
144 
145  /// Assign slice elements to corresponding elements of @a v.
146  void operator=(const valarray<_Tp>&) const;
147  /// Multiply slice elements by corresponding elements of @a v.
148  void operator*=(const valarray<_Tp>&) const;
149  /// Divide slice elements by corresponding elements of @a v.
150  void operator/=(const valarray<_Tp>&) const;
151  /// Modulo slice elements by corresponding elements of @a v.
152  void operator%=(const valarray<_Tp>&) const;
153  /// Add corresponding elements of @a v to slice elements.
154  void operator+=(const valarray<_Tp>&) const;
155  /// Subtract corresponding elements of @a v from slice elements.
156  void operator-=(const valarray<_Tp>&) const;
157  /// Logical xor slice elements with corresponding elements of @a v.
158  void operator^=(const valarray<_Tp>&) const;
159  /// Logical and slice elements with corresponding elements of @a v.
160  void operator&=(const valarray<_Tp>&) const;
161  /// Logical or slice elements with corresponding elements of @a v.
162  void operator|=(const valarray<_Tp>&) const;
163  /// Left shift slice elements by corresponding elements of @a v.
164  void operator<<=(const valarray<_Tp>&) const;
165  /// Right shift slice elements by corresponding elements of @a v.
166  void operator>>=(const valarray<_Tp>&) const;
167  /// Assign all slice elements to @a t.
168  void operator=(const _Tp &) const;
169  // ~slice_array ();
170 
171  template<class _Dom>
172  void operator=(const _Expr<_Dom, _Tp>&) const;
173  template<class _Dom>
174  void operator*=(const _Expr<_Dom, _Tp>&) const;
175  template<class _Dom>
176  void operator/=(const _Expr<_Dom, _Tp>&) const;
177  template<class _Dom>
178  void operator%=(const _Expr<_Dom, _Tp>&) const;
179  template<class _Dom>
180  void operator+=(const _Expr<_Dom, _Tp>&) const;
181  template<class _Dom>
182  void operator-=(const _Expr<_Dom, _Tp>&) const;
183  template<class _Dom>
184  void operator^=(const _Expr<_Dom, _Tp>&) const;
185  template<class _Dom>
186  void operator&=(const _Expr<_Dom, _Tp>&) const;
187  template<class _Dom>
188  void operator|=(const _Expr<_Dom, _Tp>&) const;
189  template<class _Dom>
190  void operator<<=(const _Expr<_Dom, _Tp>&) const;
191  template<class _Dom>
192  void operator>>=(const _Expr<_Dom, _Tp>&) const;
193 
194  private:
195  friend class valarray<_Tp>;
196  slice_array(_Array<_Tp>, const slice&);
197 
198  const size_t _M_sz;
199  const size_t _M_stride;
200  const _Array<_Tp> _M_array;
201 
202 #if __cplusplus < 201103L
203  // not implemented
204  slice_array();
205 #else
206  public:
207  slice_array() = delete;
208 #endif
209  };
210 
211  template<typename _Tp>
212  inline
213  slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
214  : _M_sz(__s.size()), _M_stride(__s.stride()),
215  _M_array(__a.begin() + __s.start()) {}
216 
217  template<typename _Tp>
218  inline
220  : _M_sz(__a._M_sz), _M_stride(__a._M_stride), _M_array(__a._M_array) {}
221 
222  // template<typename _Tp>
223  // inline slice_array<_Tp>::~slice_array () {}
224 
225  template<typename _Tp>
226  inline slice_array<_Tp>&
228  {
229  std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
230  _M_array, _M_stride);
231  return *this;
232  }
233 
234  template<typename _Tp>
235  inline void
236  slice_array<_Tp>::operator=(const _Tp& __t) const
237  { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
238 
239  template<typename _Tp>
240  inline void
242  { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
243 
244  template<typename _Tp>
245  template<class _Dom>
246  inline void
247  slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
248  { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
249 
250  /// @cond undocumented
251 #undef _DEFINE_VALARRAY_OPERATOR
252 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
253  template<typename _Tp> \
254  inline void \
255  slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
256  { \
257  _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
258  } \
259  \
260  template<typename _Tp> \
261  template<class _Dom> \
262  inline void \
263  slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
264  { \
265  _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
266  }
267 
268 
269 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
270 _DEFINE_VALARRAY_OPERATOR(/, __divides)
271 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
272 _DEFINE_VALARRAY_OPERATOR(+, __plus)
273 _DEFINE_VALARRAY_OPERATOR(-, __minus)
274 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
275 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
276 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
277 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
278 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
279 
280 #undef _DEFINE_VALARRAY_OPERATOR
281  /// @endcond
282 
283  /// @} group numeric_arrays
284 
285 _GLIBCXX_END_NAMESPACE_VERSION
286 } // namespace
287 
288 #endif /* _SLICE_ARRAY_H */
size_t size() const
Return size of slice.
Definition: slice_array.h:109
slice_array & operator=(const slice_array &)
Assignment operator. Assigns slice elements to corresponding elements of a.
Definition: slice_array.h:227
slice()
Construct an empty slice.
Definition: slice_array.h:97
size_t stride() const
Return array stride of slice.
Definition: slice_array.h:113
size_t start() const
Return array offset of first slice element.
Definition: slice_array.h:105
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1229
ISO C++ entities toplevel namespace is std.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition: range_access.h:274
Smart array designed to support numeric processing.
Definition: valarray:132
Reference to one-dimensional subset of an array.
Definition: slice_array.h:131
void operator|=(const valarray< _Tp > &) const
Logical or slice elements with corresponding elements of v.
void operator<<=(const valarray< _Tp > &) const
Left shift slice elements by corresponding elements of v.
void operator^=(const valarray< _Tp > &) const
Logical xor slice elements with corresponding elements of v.
void operator*=(const valarray< _Tp > &) const
Multiply slice elements by corresponding elements of v.
void operator%=(const valarray< _Tp > &) const
Modulo slice elements by corresponding elements of v.
void operator>>=(const valarray< _Tp > &) const
Right shift slice elements by corresponding elements of v.
void operator&=(const valarray< _Tp > &) const
Logical and slice elements with corresponding elements of v.
void operator-=(const valarray< _Tp > &) const
Subtract corresponding elements of v from slice elements.
void operator+=(const valarray< _Tp > &) const
Add corresponding elements of v to slice elements.
void operator/=(const valarray< _Tp > &) const
Divide slice elements by corresponding elements of v.
Class defining one-dimensional subset of an array.
Definition: slice_array.h:62
friend bool operator==(const slice &, const slice &)=default
Equality comparison.