libstdc++
mask_array.h
Go to the documentation of this file.
1 // The template and inlines for the -*- C++ -*- mask_array class.
2 
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2009, 2010
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 bits/mask_array.h
27  * This is an internal header file, included by other library headers.
28  * Do not attempt to use it directly. @headername{valarray}
29  */
30 
31 // Written by Gabriel Dos Reis <[email protected]>
32 
33 #ifndef _MASK_ARRAY_H
34 #define _MASK_ARRAY_H 1
35 
36 #pragma GCC system_header
37 
38 namespace std _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 
42  /**
43  * @addtogroup numeric_arrays
44  * @{
45  */
46 
47  /**
48  * @brief Reference to selected subset of an array.
49  *
50  * A mask_array is a reference to the actual elements of an array specified
51  * by a bitmask in the form of an array of bool. The way to get a
52  * mask_array is to call operator[](valarray<bool>) on a valarray. The
53  * returned mask_array then permits carrying operations out on the
54  * referenced subset of elements in the original valarray.
55  *
56  * For example, if a mask_array is obtained using the array (false, true,
57  * false, true) as an argument, the mask array has two elements referring
58  * to array[1] and array[3] in the underlying array.
59  *
60  * @param Tp Element type.
61  */
62  template <class _Tp>
63  class mask_array
64  {
65  public:
66  typedef _Tp value_type;
67 
68  // _GLIBCXX_RESOLVE_LIB_DEFECTS
69  // 253. valarray helper functions are almost entirely useless
70 
71  /// Copy constructor. Both slices refer to the same underlying array.
72  mask_array (const mask_array&);
73 
74  /// Assignment operator. Assigns elements to corresponding elements
75  /// of @a a.
76  mask_array& operator=(const mask_array&);
77 
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  // ~mask_array ();
103 
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  template<class _Dom>
125  void operator>>=(const _Expr<_Dom,_Tp>&) const;
126 
127  private:
128  mask_array(_Array<_Tp>, size_t, _Array<bool>);
129  friend class valarray<_Tp>;
130 
131  const size_t _M_sz;
132  const _Array<bool> _M_mask;
133  const _Array<_Tp> _M_array;
134 
135  // not implemented
136  mask_array();
137  };
138 
139  template<typename _Tp>
141  : _M_sz(a._M_sz), _M_mask(a._M_mask), _M_array(a._M_array) {}
142 
143  template<typename _Tp>
144  inline
145  mask_array<_Tp>::mask_array(_Array<_Tp> __a, size_t __s, _Array<bool> __m)
146  : _M_sz(__s), _M_mask(__m), _M_array(__a) {}
147 
148  template<typename _Tp>
149  inline mask_array<_Tp>&
151  {
152  std::__valarray_copy(__a._M_array, __a._M_mask,
153  _M_sz, _M_array, _M_mask);
154  return *this;
155  }
156 
157  template<typename _Tp>
158  inline void
159  mask_array<_Tp>::operator=(const _Tp& __t) const
160  { std::__valarray_fill(_M_array, _M_sz, _M_mask, __t); }
161 
162  template<typename _Tp>
163  inline void
164  mask_array<_Tp>::operator=(const valarray<_Tp>& __v) const
165  { std::__valarray_copy(_Array<_Tp>(__v), __v.size(), _M_array, _M_mask); }
166 
167  template<typename _Tp>
168  template<class _Ex>
169  inline void
170  mask_array<_Tp>::operator=(const _Expr<_Ex, _Tp>& __e) const
171  { std::__valarray_copy(__e, __e.size(), _M_array, _M_mask); }
172 
173 #undef _DEFINE_VALARRAY_OPERATOR
174 #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
175  template<typename _Tp> \
176  inline void \
177  mask_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
178  { \
179  _Array_augmented_##_Name(_M_array, _M_mask, \
180  _Array<_Tp>(__v), __v.size()); \
181  } \
182  \
183  template<typename _Tp> \
184  template<class _Dom> \
185  inline void \
186  mask_array<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) const\
187  { \
188  _Array_augmented_##_Name(_M_array, _M_mask, __e, __e.size()); \
189  }
190 
191 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
192 _DEFINE_VALARRAY_OPERATOR(/, __divides)
193 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
194 _DEFINE_VALARRAY_OPERATOR(+, __plus)
195 _DEFINE_VALARRAY_OPERATOR(-, __minus)
196 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
197 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
198 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
199 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
200 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
201 
202 #undef _DEFINE_VALARRAY_OPERATOR
203 
204  // @} group numeric_arrays
205 
206 _GLIBCXX_END_NAMESPACE_VERSION
207 } // namespace
208 
209 #endif /* _MASK_ARRAY_H */
Reference to selected subset of an array.
Definition: valarray:85
Smart array designed to support numeric processing.
Definition: valarray:80
mask_array(const mask_array &)
Copy constructor. Both slices refer to the same underlying array.
Definition: mask_array.h:140
void operator^=(const valarray< _Tp > &) const
Logical xor slice elements with corresponding elements of v.
Definition: mask_array.h:196
mask_array & operator=(const mask_array &)
Assignment operator. Assigns elements to corresponding elements of a.
Definition: mask_array.h:150
void operator|=(const valarray< _Tp > &) const
Logical or slice elements with corresponding elements of v.
Definition: mask_array.h:198
void operator+=(const valarray< _Tp > &) const
Add corresponding elements of v to slice elements.
Definition: mask_array.h:194
void operator&=(const valarray< _Tp > &) const
Logical and slice elements with corresponding elements of v.
Definition: mask_array.h:197
size_t size() const
Return the number of elements in array.
Definition: valarray:910
void operator>>=(const valarray< _Tp > &) const
Right shift slice elements by corresponding elements of v.
Definition: mask_array.h:200
void operator/=(const valarray< _Tp > &) const
Divide slice elements by corresponding elements of v.
Definition: mask_array.h:192
void operator-=(const valarray< _Tp > &) const
Subtract corresponding elements of v from slice elements.
Definition: mask_array.h:195
void operator*=(const valarray< _Tp > &) const
Multiply slice elements by corresponding elements of v.
Definition: mask_array.h:191
void operator%=(const valarray< _Tp > &) const
Modulo slice elements by corresponding elements of v.
Definition: mask_array.h:193