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, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2009
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file slice_array.h
27  * This is an internal header file, included by other library headers.
28  * You should not attempt to use it directly.
29  */
30 
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
32 
33 #ifndef _SLICE_ARRAY_H
34 #define _SLICE_ARRAY_H 1
35 
36 #pragma GCC system_header
37 
38 _GLIBCXX_BEGIN_NAMESPACE(std)
39 
40  /**
41  * @addtogroup numeric_arrays
42  * @{
43  */
44 
45  /**
46  * @brief Class defining one-dimensional subset of an array.
47  *
48  * The slice class represents a one-dimensional subset of an array,
49  * specified by three parameters: start offset, size, and stride. The
50  * start offset is the index of the first element of the array that is part
51  * of the subset. The size is the total number of elements in the subset.
52  * Stride is the distance between each successive array element to include
53  * in the subset.
54  *
55  * For example, with an array of size 10, and a slice with offset 1, size 3
56  * and stride 2, the subset consists of array elements 1, 3, and 5.
57  */
58  class slice
59  {
60  public:
61  /// Construct an empty slice.
62  slice();
63 
64  /**
65  * @brief Construct a slice.
66  *
67  * @param o Offset in array of first element.
68  * @param d Number of elements in slice.
69  * @param s Stride between array elements.
70  */
71  slice(size_t, size_t, size_t);
72 
73  /// Return array offset of first slice element.
74  size_t start() const;
75  /// Return size of slice.
76  size_t size() const;
77  /// Return array stride of slice.
78  size_t stride() const;
79 
80  private:
81  size_t _M_off; // offset
82  size_t _M_sz; // size
83  size_t _M_st; // stride unit
84  };
85 
86  // _GLIBCXX_RESOLVE_LIB_DEFECTS
87  // 543. valarray slice default constructor
88  inline
89  slice::slice()
90  : _M_off(0), _M_sz(0), _M_st(0) {}
91 
92  inline
93  slice::slice(size_t __o, size_t __d, size_t __s)
94  : _M_off(__o), _M_sz(__d), _M_st(__s) {}
95 
96  inline size_t
97  slice::start() const
98  { return _M_off; }
99 
100  inline size_t
101  slice::size() const
102  { return _M_sz; }
103 
104  inline size_t
106  { return _M_st; }
107 
108  /**
109  * @brief Reference to one-dimensional subset of an array.
110  *
111  * A slice_array is a reference to the actual elements of an array
112  * specified by a slice. The way to get a slice_array is to call
113  * operator[](slice) on a valarray. The returned slice_array then permits
114  * carrying operations out on the referenced subset of elements in the
115  * original valarray. For example, operator+=(valarray) will add values
116  * to the subset of elements in the underlying valarray this slice_array
117  * refers to.
118  *
119  * @param Tp Element type.
120  */
121  template<typename _Tp>
122  class slice_array
123  {
124  public:
125  typedef _Tp value_type;
126 
127  // _GLIBCXX_RESOLVE_LIB_DEFECTS
128  // 253. valarray helper functions are almost entirely useless
129 
130  /// Copy constructor. Both slices refer to the same underlying array.
131  slice_array(const slice_array&);
132 
133  /// Assignment operator. Assigns slice elements to corresponding
134  /// elements of @a a.
136 
137  /// Assign slice elements to corresponding elements of @a v.
138  void operator=(const valarray<_Tp>&) const;
139  /// Multiply slice elements by corresponding elements of @a v.
140  void operator*=(const valarray<_Tp>&) const;
141  /// Divide slice elements by corresponding elements of @a v.
142  void operator/=(const valarray<_Tp>&) const;
143  /// Modulo slice elements by corresponding elements of @a v.
144  void operator%=(const valarray<_Tp>&) const;
145  /// Add corresponding elements of @a v to slice elements.
146  void operator+=(const valarray<_Tp>&) const;
147  /// Subtract corresponding elements of @a v from slice elements.
148  void operator-=(const valarray<_Tp>&) const;
149  /// Logical xor slice elements with corresponding elements of @a v.
150  void operator^=(const valarray<_Tp>&) const;
151  /// Logical and slice elements with corresponding elements of @a v.
152  void operator&=(const valarray<_Tp>&) const;
153  /// Logical or slice elements with corresponding elements of @a v.
154  void operator|=(const valarray<_Tp>&) const;
155  /// Left shift slice elements by corresponding elements of @a v.
156  void operator<<=(const valarray<_Tp>&) const;
157  /// Right shift slice elements by corresponding elements of @a v.
158  void operator>>=(const valarray<_Tp>&) const;
159  /// Assign all slice elements to @a t.
160  void operator=(const _Tp &) const;
161  // ~slice_array ();
162 
163  template<class _Dom>
164  void operator=(const _Expr<_Dom, _Tp>&) const;
165  template<class _Dom>
166  void operator*=(const _Expr<_Dom, _Tp>&) const;
167  template<class _Dom>
168  void operator/=(const _Expr<_Dom, _Tp>&) const;
169  template<class _Dom>
170  void operator%=(const _Expr<_Dom, _Tp>&) const;
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 
186  private:
187  friend class valarray<_Tp>;
188  slice_array(_Array<_Tp>, const slice&);
189 
190  const size_t _M_sz;
191  const size_t _M_stride;
192  const _Array<_Tp> _M_array;
193 
194  // not implemented
195  slice_array();
196  };
197 
198  template<typename _Tp>
199  inline
200  slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
201  : _M_sz(__s.size()), _M_stride(__s.stride()),
202  _M_array(__a.begin() + __s.start()) {}
203 
204  template<typename _Tp>
205  inline
207  : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
208 
209  // template<typename _Tp>
210  // inline slice_array<_Tp>::~slice_array () {}
211 
212  template<typename _Tp>
213  inline slice_array<_Tp>&
215  {
216  std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
217  _M_array, _M_stride);
218  return *this;
219  }
220 
221  template<typename _Tp>
222  inline void
223  slice_array<_Tp>::operator=(const _Tp& __t) const
224  { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
225 
226  template<typename _Tp>
227  inline void
229  { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
230 
231  template<typename _Tp>
232  template<class _Dom>
233  inline void
234  slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
235  { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
236 
237 #undef _DEFINE_VALARRAY_OPERATOR
238 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
239  template<typename _Tp> \
240  inline void \
241  slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
242  { \
243  _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
244  } \
245  \
246  template<typename _Tp> \
247  template<class _Dom> \
248  inline void \
249  slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
250  { \
251  _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
252  }
253 
254 
255 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
256 _DEFINE_VALARRAY_OPERATOR(/, __divides)
257 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
258 _DEFINE_VALARRAY_OPERATOR(+, __plus)
259 _DEFINE_VALARRAY_OPERATOR(-, __minus)
260 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
261 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
262 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
263 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
264 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
265 
266 #undef _DEFINE_VALARRAY_OPERATOR
267 
268  // @} group numeric_arrays
269 
270 _GLIBCXX_END_NAMESPACE
271 
272 #endif /* _SLICE_ARRAY_H */