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.

213 lines
7.7KB

  1. // The template and inlines for the -*- C++ -*- indirect_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/indirect_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 _INDIRECT_ARRAY_H
  26. #define _INDIRECT_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 arbitrary subset of an array.
  37. *
  38. * An indirect_array is a reference to the actual elements of an array
  39. * specified by an ordered array of indices. The way to get an
  40. * indirect_array is to call operator[](valarray<size_t>) on a valarray.
  41. * The returned indirect_array then permits carrying operations out on the
  42. * referenced subset of elements in the original valarray.
  43. *
  44. * For example, if an indirect_array is obtained using the array (4,2,0) as
  45. * an argument, and then assigned to an array containing (1,2,3), then the
  46. * underlying array will have array[0]==3, array[2]==2, and array[4]==1.
  47. *
  48. * @param Tp Element type.
  49. */
  50. template <class _Tp>
  51. class indirect_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. indirect_array(const indirect_array&);
  59. /// Assignment operator. Assigns elements to corresponding elements
  60. /// of @a a.
  61. indirect_array& operator=(const indirect_array&);
  62. /// Assign slice elements to corresponding elements of @a v.
  63. void operator=(const valarray<_Tp>&) const;
  64. /// Multiply slice elements by corresponding elements of @a v.
  65. void operator*=(const valarray<_Tp>&) const;
  66. /// Divide slice elements by corresponding elements of @a v.
  67. void operator/=(const valarray<_Tp>&) const;
  68. /// Modulo slice elements by corresponding elements of @a v.
  69. void operator%=(const valarray<_Tp>&) const;
  70. /// Add corresponding elements of @a v to slice elements.
  71. void operator+=(const valarray<_Tp>&) const;
  72. /// Subtract corresponding elements of @a v from slice elements.
  73. void operator-=(const valarray<_Tp>&) const;
  74. /// Logical xor slice elements with corresponding elements of @a v.
  75. void operator^=(const valarray<_Tp>&) const;
  76. /// Logical and slice elements with corresponding elements of @a v.
  77. void operator&=(const valarray<_Tp>&) const;
  78. /// Logical or slice elements with corresponding elements of @a v.
  79. void operator|=(const valarray<_Tp>&) const;
  80. /// Left shift slice elements by corresponding elements of @a v.
  81. void operator<<=(const valarray<_Tp>&) const;
  82. /// Right shift slice elements by corresponding elements of @a v.
  83. void operator>>=(const valarray<_Tp>&) const;
  84. /// Assign all slice elements to @a t.
  85. void operator= (const _Tp&) const;
  86. // ~indirect_array();
  87. template<class _Dom>
  88. void operator=(const _Expr<_Dom, _Tp>&) const;
  89. template<class _Dom>
  90. void operator*=(const _Expr<_Dom, _Tp>&) const;
  91. template<class _Dom>
  92. void operator/=(const _Expr<_Dom, _Tp>&) const;
  93. template<class _Dom>
  94. void operator%=(const _Expr<_Dom, _Tp>&) const;
  95. template<class _Dom>
  96. void operator+=(const _Expr<_Dom, _Tp>&) const;
  97. template<class _Dom>
  98. void operator-=(const _Expr<_Dom, _Tp>&) const;
  99. template<class _Dom>
  100. void operator^=(const _Expr<_Dom, _Tp>&) const;
  101. template<class _Dom>
  102. void operator&=(const _Expr<_Dom, _Tp>&) const;
  103. template<class _Dom>
  104. void operator|=(const _Expr<_Dom, _Tp>&) const;
  105. template<class _Dom>
  106. void operator<<=(const _Expr<_Dom, _Tp>&) const;
  107. template<class _Dom>
  108. void operator>>=(const _Expr<_Dom, _Tp>&) const;
  109. private:
  110. /// Copy constructor. Both slices refer to the same underlying array.
  111. indirect_array(_Array<_Tp>, size_t, _Array<size_t>);
  112. friend class valarray<_Tp>;
  113. friend class gslice_array<_Tp>;
  114. const size_t _M_sz;
  115. const _Array<size_t> _M_index;
  116. const _Array<_Tp> _M_array;
  117. // not implemented
  118. indirect_array();
  119. };
  120. template<typename _Tp>
  121. inline
  122. indirect_array<_Tp>::indirect_array(const indirect_array<_Tp>& __a)
  123. : _M_sz(__a._M_sz), _M_index(__a._M_index), _M_array(__a._M_array) {}
  124. template<typename _Tp>
  125. inline
  126. indirect_array<_Tp>::indirect_array(_Array<_Tp> __a, size_t __s,
  127. _Array<size_t> __i)
  128. : _M_sz(__s), _M_index(__i), _M_array(__a) {}
  129. template<typename _Tp>
  130. inline indirect_array<_Tp>&
  131. indirect_array<_Tp>::operator=(const indirect_array<_Tp>& __a)
  132. {
  133. std::__valarray_copy(__a._M_array, _M_sz, __a._M_index, _M_array,
  134. _M_index);
  135. return *this;
  136. }
  137. template<typename _Tp>
  138. inline void
  139. indirect_array<_Tp>::operator=(const _Tp& __t) const
  140. { std::__valarray_fill(_M_array, _M_index, _M_sz, __t); }
  141. template<typename _Tp>
  142. inline void
  143. indirect_array<_Tp>::operator=(const valarray<_Tp>& __v) const
  144. { std::__valarray_copy(_Array<_Tp>(__v), _M_sz, _M_array, _M_index); }
  145. template<typename _Tp>
  146. template<class _Dom>
  147. inline void
  148. indirect_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
  149. { std::__valarray_copy(__e, _M_sz, _M_array, _M_index); }
  150. #undef _DEFINE_VALARRAY_OPERATOR
  151. #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \
  152. template<typename _Tp> \
  153. inline void \
  154. indirect_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const\
  155. { \
  156. _Array_augmented_##_Name(_M_array, _M_index, _Array<_Tp>(__v), _M_sz); \
  157. } \
  158. \
  159. template<typename _Tp> \
  160. template<class _Dom> \
  161. inline void \
  162. indirect_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
  163. { \
  164. _Array_augmented_##_Name(_M_array, _M_index, __e, _M_sz); \
  165. }
  166. _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
  167. _DEFINE_VALARRAY_OPERATOR(/, __divides)
  168. _DEFINE_VALARRAY_OPERATOR(%, __modulus)
  169. _DEFINE_VALARRAY_OPERATOR(+, __plus)
  170. _DEFINE_VALARRAY_OPERATOR(-, __minus)
  171. _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
  172. _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
  173. _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
  174. _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
  175. _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
  176. #undef _DEFINE_VALARRAY_OPERATOR
  177. // @} group numeric_arrays
  178. _GLIBCXX_END_NAMESPACE_VERSION
  179. } // namespace
  180. #endif /* _INDIRECT_ARRAY_H */