gslice_array.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // The template and inlines for the -*- C++ -*- gslice_array class.
  2. // Copyright (C) 1997-2018 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. // not implemented
  112. gslice_array();
  113. };
  114. template<typename _Tp>
  115. inline
  116. gslice_array<_Tp>::gslice_array(_Array<_Tp> __a,
  117. const valarray<size_t>& __i)
  118. : _M_array(__a), _M_index(__i) {}
  119. template<typename _Tp>
  120. inline
  121. gslice_array<_Tp>::gslice_array(const gslice_array<_Tp>& __a)
  122. : _M_array(__a._M_array), _M_index(__a._M_index) {}
  123. template<typename _Tp>
  124. inline gslice_array<_Tp>&
  125. gslice_array<_Tp>::operator=(const gslice_array<_Tp>& __a)
  126. {
  127. std::__valarray_copy(_Array<_Tp>(__a._M_array),
  128. _Array<size_t>(__a._M_index), _M_index.size(),
  129. _M_array, _Array<size_t>(_M_index));
  130. return *this;
  131. }
  132. template<typename _Tp>
  133. inline void
  134. gslice_array<_Tp>::operator=(const _Tp& __t) const
  135. {
  136. std::__valarray_fill(_M_array, _Array<size_t>(_M_index),
  137. _M_index.size(), __t);
  138. }
  139. template<typename _Tp>
  140. inline void
  141. gslice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
  142. {
  143. std::__valarray_copy(_Array<_Tp>(__v), __v.size(),
  144. _M_array, _Array<size_t>(_M_index));
  145. }
  146. template<typename _Tp>
  147. template<class _Dom>
  148. inline void
  149. gslice_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
  150. {
  151. std::__valarray_copy (__e, _M_index.size(), _M_array,
  152. _Array<size_t>(_M_index));
  153. }
  154. #undef _DEFINE_VALARRAY_OPERATOR
  155. #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
  156. template<typename _Tp> \
  157. inline void \
  158. gslice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
  159. { \
  160. _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), \
  161. _Array<_Tp>(__v), __v.size()); \
  162. } \
  163. \
  164. template<typename _Tp> \
  165. template<class _Dom> \
  166. inline void \
  167. gslice_array<_Tp>::operator _Op##= (const _Expr<_Dom, _Tp>& __e) const\
  168. { \
  169. _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), __e,\
  170. _M_index.size()); \
  171. }
  172. _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
  173. _DEFINE_VALARRAY_OPERATOR(/, __divides)
  174. _DEFINE_VALARRAY_OPERATOR(%, __modulus)
  175. _DEFINE_VALARRAY_OPERATOR(+, __plus)
  176. _DEFINE_VALARRAY_OPERATOR(-, __minus)
  177. _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
  178. _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
  179. _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
  180. _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
  181. _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
  182. #undef _DEFINE_VALARRAY_OPERATOR
  183. // @} group numeric_arrays
  184. _GLIBCXX_END_NAMESPACE_VERSION
  185. } // namespace
  186. #endif /* _GSLICE_ARRAY_H */