No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

224 líneas
7.7KB

  1. // The template and inlines for the -*- C++ -*- gslice_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/gslice_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 _GSLICE_ARRAY_H
  26. #define _GSLICE_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 multi-dimensional subset of an array.
  37. *
  38. * A gslice_array is a reference to the actual elements of an array
  39. * specified by a gslice. The way to get a gslice_array is to call
  40. * operator[](gslice) on a valarray. The returned gslice_array then
  41. * permits carrying operations out on the referenced subset of elements in
  42. * the original valarray. For example, operator+=(valarray) will add
  43. * values to the subset of elements in the underlying valarray this
  44. * gslice_array refers to.
  45. *
  46. * @param Tp Element type.
  47. */
  48. template<typename _Tp>
  49. class gslice_array
  50. {
  51. public:
  52. typedef _Tp value_type;
  53. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  54. // 253. valarray helper functions are almost entirely useless
  55. /// Copy constructor. Both slices refer to the same underlying array.
  56. gslice_array(const gslice_array&);
  57. /// Assignment operator. Assigns slice elements to corresponding
  58. /// elements of @a a.
  59. gslice_array& operator=(const gslice_array&);
  60. /// Assign slice elements to corresponding elements of @a v.
  61. void operator=(const valarray<_Tp>&) const;
  62. /// Multiply slice elements by corresponding elements of @a v.
  63. void operator*=(const valarray<_Tp>&) const;
  64. /// Divide slice elements by corresponding elements of @a v.
  65. void operator/=(const valarray<_Tp>&) const;
  66. /// Modulo slice elements by corresponding elements of @a v.
  67. void operator%=(const valarray<_Tp>&) const;
  68. /// Add corresponding elements of @a v to slice elements.
  69. void operator+=(const valarray<_Tp>&) const;
  70. /// Subtract corresponding elements of @a v from slice elements.
  71. void operator-=(const valarray<_Tp>&) const;
  72. /// Logical xor slice elements with corresponding elements of @a v.
  73. void operator^=(const valarray<_Tp>&) const;
  74. /// Logical and slice elements with corresponding elements of @a v.
  75. void operator&=(const valarray<_Tp>&) const;
  76. /// Logical or slice elements with corresponding elements of @a v.
  77. void operator|=(const valarray<_Tp>&) const;
  78. /// Left shift slice elements by corresponding elements of @a v.
  79. void operator<<=(const valarray<_Tp>&) const;
  80. /// Right shift slice elements by corresponding elements of @a v.
  81. void operator>>=(const valarray<_Tp>&) const;
  82. /// Assign all slice elements to @a t.
  83. void operator=(const _Tp&) const;
  84. template<class _Dom>
  85. void operator=(const _Expr<_Dom, _Tp>&) const;
  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. private:
  107. _Array<_Tp> _M_array;
  108. const valarray<size_t>& _M_index;
  109. friend class valarray<_Tp>;
  110. gslice_array(_Array<_Tp>, const valarray<size_t>&);
  111. #if __cplusplus < 201103L
  112. // not implemented
  113. gslice_array();
  114. #else
  115. public:
  116. gslice_array() = delete;
  117. #endif
  118. };
  119. template<typename _Tp>
  120. inline
  121. gslice_array<_Tp>::gslice_array(_Array<_Tp> __a,
  122. const valarray<size_t>& __i)
  123. : _M_array(__a), _M_index(__i) {}
  124. template<typename _Tp>
  125. inline
  126. gslice_array<_Tp>::gslice_array(const gslice_array<_Tp>& __a)
  127. : _M_array(__a._M_array), _M_index(__a._M_index) {}
  128. template<typename _Tp>
  129. inline gslice_array<_Tp>&
  130. gslice_array<_Tp>::operator=(const gslice_array<_Tp>& __a)
  131. {
  132. std::__valarray_copy(_Array<_Tp>(__a._M_array),
  133. _Array<size_t>(__a._M_index), _M_index.size(),
  134. _M_array, _Array<size_t>(_M_index));
  135. return *this;
  136. }
  137. template<typename _Tp>
  138. inline void
  139. gslice_array<_Tp>::operator=(const _Tp& __t) const
  140. {
  141. std::__valarray_fill(_M_array, _Array<size_t>(_M_index),
  142. _M_index.size(), __t);
  143. }
  144. template<typename _Tp>
  145. inline void
  146. gslice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
  147. {
  148. std::__valarray_copy(_Array<_Tp>(__v), __v.size(),
  149. _M_array, _Array<size_t>(_M_index));
  150. }
  151. template<typename _Tp>
  152. template<class _Dom>
  153. inline void
  154. gslice_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
  155. {
  156. std::__valarray_copy (__e, _M_index.size(), _M_array,
  157. _Array<size_t>(_M_index));
  158. }
  159. #undef _DEFINE_VALARRAY_OPERATOR
  160. #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
  161. template<typename _Tp> \
  162. inline void \
  163. gslice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
  164. { \
  165. _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), \
  166. _Array<_Tp>(__v), __v.size()); \
  167. } \
  168. \
  169. template<typename _Tp> \
  170. template<class _Dom> \
  171. inline void \
  172. gslice_array<_Tp>::operator _Op##= (const _Expr<_Dom, _Tp>& __e) const\
  173. { \
  174. _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), __e,\
  175. _M_index.size()); \
  176. }
  177. _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
  178. _DEFINE_VALARRAY_OPERATOR(/, __divides)
  179. _DEFINE_VALARRAY_OPERATOR(%, __modulus)
  180. _DEFINE_VALARRAY_OPERATOR(+, __plus)
  181. _DEFINE_VALARRAY_OPERATOR(-, __minus)
  182. _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
  183. _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
  184. _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
  185. _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
  186. _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
  187. #undef _DEFINE_VALARRAY_OPERATOR
  188. // @} group numeric_arrays
  189. _GLIBCXX_END_NAMESPACE_VERSION
  190. } // namespace
  191. #endif /* _GSLICE_ARRAY_H */