You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
7.5KB

  1. // The template and inlines for the -*- C++ -*- mask_array class.
  2. // Copyright (C) 1997-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/mask_array.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{valarray}
  23. */
  24. // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
  25. #ifndef _MASK_ARRAY_H
  26. #define _MASK_ARRAY_H 1
  27. #pragma GCC system_header
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. /**
  32. * @addtogroup numeric_arrays
  33. * @{
  34. */
  35. /**
  36. * @brief Reference to selected subset of an array.
  37. *
  38. * A mask_array is a reference to the actual elements of an array specified
  39. * by a bitmask in the form of an array of bool. The way to get a
  40. * mask_array is to call operator[](valarray<bool>) on a valarray. The
  41. * returned mask_array then permits carrying operations out on the
  42. * referenced subset of elements in the original valarray.
  43. *
  44. * For example, if a mask_array is obtained using the array (false, true,
  45. * false, true) as an argument, the mask array has two elements referring
  46. * to array[1] and array[3] in the underlying array.
  47. *
  48. * @param Tp Element type.
  49. */
  50. template <class _Tp>
  51. class mask_array
  52. {
  53. public:
  54. typedef _Tp value_type;
  55. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  56. // 253. valarray helper functions are almost entirely useless
  57. /// Copy constructor. Both slices refer to the same underlying array.
  58. mask_array (const mask_array&);
  59. /// Assignment operator. Assigns elements to corresponding elements
  60. /// of @a a.
  61. mask_array& operator=(const mask_array&);
  62. void operator=(const valarray<_Tp>&) const;
  63. /// Multiply slice elements by corresponding elements of @a v.
  64. void operator*=(const valarray<_Tp>&) const;
  65. /// Divide slice elements by corresponding elements of @a v.
  66. void operator/=(const valarray<_Tp>&) const;
  67. /// Modulo slice elements by corresponding elements of @a v.
  68. void operator%=(const valarray<_Tp>&) const;
  69. /// Add corresponding elements of @a v to slice elements.
  70. void operator+=(const valarray<_Tp>&) const;
  71. /// Subtract corresponding elements of @a v from slice elements.
  72. void operator-=(const valarray<_Tp>&) const;
  73. /// Logical xor slice elements with corresponding elements of @a v.
  74. void operator^=(const valarray<_Tp>&) const;
  75. /// Logical and slice elements with corresponding elements of @a v.
  76. void operator&=(const valarray<_Tp>&) const;
  77. /// Logical or slice elements with corresponding elements of @a v.
  78. void operator|=(const valarray<_Tp>&) const;
  79. /// Left shift slice elements by corresponding elements of @a v.
  80. void operator<<=(const valarray<_Tp>&) const;
  81. /// Right shift slice elements by corresponding elements of @a v.
  82. void operator>>=(const valarray<_Tp>&) const;
  83. /// Assign all slice elements to @a t.
  84. void operator=(const _Tp&) const;
  85. // ~mask_array ();
  86. template<class _Dom>
  87. void operator=(const _Expr<_Dom,_Tp>&) const;
  88. template<class _Dom>
  89. void operator*=(const _Expr<_Dom,_Tp>&) const;
  90. template<class _Dom>
  91. void operator/=(const _Expr<_Dom,_Tp>&) const;
  92. template<class _Dom>
  93. void operator%=(const _Expr<_Dom,_Tp>&) const;
  94. template<class _Dom>
  95. void operator+=(const _Expr<_Dom,_Tp>&) const;
  96. template<class _Dom>
  97. void operator-=(const _Expr<_Dom,_Tp>&) const;
  98. template<class _Dom>
  99. void operator^=(const _Expr<_Dom,_Tp>&) const;
  100. template<class _Dom>
  101. void operator&=(const _Expr<_Dom,_Tp>&) const;
  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. private:
  109. mask_array(_Array<_Tp>, size_t, _Array<bool>);
  110. friend class valarray<_Tp>;
  111. const size_t _M_sz;
  112. const _Array<bool> _M_mask;
  113. const _Array<_Tp> _M_array;
  114. #if __cplusplus < 201103L
  115. // not implemented
  116. mask_array();
  117. #else
  118. public:
  119. mask_array() = delete;
  120. #endif
  121. };
  122. template<typename _Tp>
  123. inline mask_array<_Tp>::mask_array(const mask_array<_Tp>& __a)
  124. : _M_sz(__a._M_sz), _M_mask(__a._M_mask), _M_array(__a._M_array) {}
  125. template<typename _Tp>
  126. inline
  127. mask_array<_Tp>::mask_array(_Array<_Tp> __a, size_t __s, _Array<bool> __m)
  128. : _M_sz(__s), _M_mask(__m), _M_array(__a) {}
  129. template<typename _Tp>
  130. inline mask_array<_Tp>&
  131. mask_array<_Tp>::operator=(const mask_array<_Tp>& __a)
  132. {
  133. std::__valarray_copy(__a._M_array, __a._M_mask,
  134. _M_sz, _M_array, _M_mask);
  135. return *this;
  136. }
  137. template<typename _Tp>
  138. inline void
  139. mask_array<_Tp>::operator=(const _Tp& __t) const
  140. { std::__valarray_fill(_M_array, _M_sz, _M_mask, __t); }
  141. template<typename _Tp>
  142. inline void
  143. mask_array<_Tp>::operator=(const valarray<_Tp>& __v) const
  144. { std::__valarray_copy(_Array<_Tp>(__v), __v.size(), _M_array, _M_mask); }
  145. template<typename _Tp>
  146. template<class _Ex>
  147. inline void
  148. mask_array<_Tp>::operator=(const _Expr<_Ex, _Tp>& __e) const
  149. { std::__valarray_copy(__e, __e.size(), _M_array, _M_mask); }
  150. #undef _DEFINE_VALARRAY_OPERATOR
  151. #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
  152. template<typename _Tp> \
  153. inline void \
  154. mask_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
  155. { \
  156. _Array_augmented_##_Name(_M_array, _M_mask, \
  157. _Array<_Tp>(__v), __v.size()); \
  158. } \
  159. \
  160. template<typename _Tp> \
  161. template<class _Dom> \
  162. inline void \
  163. mask_array<_Tp>::operator _Op##=(const _Expr<_Dom, _Tp>& __e) const\
  164. { \
  165. _Array_augmented_##_Name(_M_array, _M_mask, __e, __e.size()); \
  166. }
  167. _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
  168. _DEFINE_VALARRAY_OPERATOR(/, __divides)
  169. _DEFINE_VALARRAY_OPERATOR(%, __modulus)
  170. _DEFINE_VALARRAY_OPERATOR(+, __plus)
  171. _DEFINE_VALARRAY_OPERATOR(-, __minus)
  172. _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
  173. _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
  174. _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
  175. _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
  176. _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
  177. #undef _DEFINE_VALARRAY_OPERATOR
  178. // @} group numeric_arrays
  179. _GLIBCXX_END_NAMESPACE_VERSION
  180. } // namespace
  181. #endif /* _MASK_ARRAY_H */