slice_array.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // The template and inlines for the -*- C++ -*- slice_array class.
  2. // Copyright (C) 1997-2022 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/slice_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 _SLICE_ARRAY_H
  26. #define _SLICE_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 Class defining one-dimensional subset of an array.
  37. *
  38. * The slice class represents a one-dimensional subset of an array,
  39. * specified by three parameters: start offset, size, and stride. The
  40. * start offset is the index of the first element of the array that is part
  41. * of the subset. The size is the total number of elements in the subset.
  42. * Stride is the distance between each successive array element to include
  43. * in the subset.
  44. *
  45. * For example, with an array of size 10, and a slice with offset 1, size 3
  46. * and stride 2, the subset consists of array elements 1, 3, and 5.
  47. */
  48. class slice
  49. {
  50. public:
  51. /// Construct an empty slice.
  52. slice();
  53. /**
  54. * @brief Construct a slice.
  55. *
  56. * @param __o Offset in array of first element.
  57. * @param __d Number of elements in slice.
  58. * @param __s Stride between array elements.
  59. */
  60. slice(size_t __o, size_t __d, size_t __s);
  61. /// Return array offset of first slice element.
  62. size_t start() const;
  63. /// Return size of slice.
  64. size_t size() const;
  65. /// Return array stride of slice.
  66. size_t stride() const;
  67. #if __cpp_impl_three_way_comparison >= 201907L
  68. /// Equality comparison
  69. friend bool operator==(const slice&, const slice&) = default;
  70. #endif
  71. private:
  72. size_t _M_off; // offset
  73. size_t _M_sz; // size
  74. size_t _M_st; // stride unit
  75. };
  76. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  77. // 543. valarray slice default constructor
  78. inline
  79. slice::slice()
  80. : _M_off(0), _M_sz(0), _M_st(0) {}
  81. inline
  82. slice::slice(size_t __o, size_t __d, size_t __s)
  83. : _M_off(__o), _M_sz(__d), _M_st(__s) {}
  84. inline size_t
  85. slice::start() const
  86. { return _M_off; }
  87. inline size_t
  88. slice::size() const
  89. { return _M_sz; }
  90. inline size_t
  91. slice::stride() const
  92. { return _M_st; }
  93. /**
  94. * @brief Reference to one-dimensional subset of an array.
  95. *
  96. * A slice_array is a reference to the actual elements of an array
  97. * specified by a slice. The way to get a slice_array is to call
  98. * operator[](slice) on a valarray. The returned slice_array then permits
  99. * carrying operations out on the referenced subset of elements in the
  100. * original valarray. For example, operator+=(valarray) will add values
  101. * to the subset of elements in the underlying valarray this slice_array
  102. * refers to.
  103. *
  104. * @param Tp Element type.
  105. */
  106. template<typename _Tp>
  107. class slice_array
  108. {
  109. public:
  110. typedef _Tp value_type;
  111. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  112. // 253. valarray helper functions are almost entirely useless
  113. /// Copy constructor. Both slices refer to the same underlying array.
  114. slice_array(const slice_array&);
  115. /// Assignment operator. Assigns slice elements to corresponding
  116. /// elements of @a a.
  117. slice_array& operator=(const slice_array&);
  118. /// Assign slice elements to corresponding elements of @a v.
  119. void operator=(const valarray<_Tp>&) const;
  120. /// Multiply slice elements by corresponding elements of @a v.
  121. void operator*=(const valarray<_Tp>&) const;
  122. /// Divide slice elements by corresponding elements of @a v.
  123. void operator/=(const valarray<_Tp>&) const;
  124. /// Modulo slice elements by corresponding elements of @a v.
  125. void operator%=(const valarray<_Tp>&) const;
  126. /// Add corresponding elements of @a v to slice elements.
  127. void operator+=(const valarray<_Tp>&) const;
  128. /// Subtract corresponding elements of @a v from slice elements.
  129. void operator-=(const valarray<_Tp>&) const;
  130. /// Logical xor slice elements with corresponding elements of @a v.
  131. void operator^=(const valarray<_Tp>&) const;
  132. /// Logical and slice elements with corresponding elements of @a v.
  133. void operator&=(const valarray<_Tp>&) const;
  134. /// Logical or slice elements with corresponding elements of @a v.
  135. void operator|=(const valarray<_Tp>&) const;
  136. /// Left shift slice elements by corresponding elements of @a v.
  137. void operator<<=(const valarray<_Tp>&) const;
  138. /// Right shift slice elements by corresponding elements of @a v.
  139. void operator>>=(const valarray<_Tp>&) const;
  140. /// Assign all slice elements to @a t.
  141. void operator=(const _Tp &) const;
  142. // ~slice_array ();
  143. template<class _Dom>
  144. void operator=(const _Expr<_Dom, _Tp>&) const;
  145. template<class _Dom>
  146. void operator*=(const _Expr<_Dom, _Tp>&) const;
  147. template<class _Dom>
  148. void operator/=(const _Expr<_Dom, _Tp>&) const;
  149. template<class _Dom>
  150. void operator%=(const _Expr<_Dom, _Tp>&) const;
  151. template<class _Dom>
  152. void operator+=(const _Expr<_Dom, _Tp>&) const;
  153. template<class _Dom>
  154. void operator-=(const _Expr<_Dom, _Tp>&) const;
  155. template<class _Dom>
  156. void operator^=(const _Expr<_Dom, _Tp>&) const;
  157. template<class _Dom>
  158. void operator&=(const _Expr<_Dom, _Tp>&) const;
  159. template<class _Dom>
  160. void operator|=(const _Expr<_Dom, _Tp>&) const;
  161. template<class _Dom>
  162. void operator<<=(const _Expr<_Dom, _Tp>&) const;
  163. template<class _Dom>
  164. void operator>>=(const _Expr<_Dom, _Tp>&) const;
  165. private:
  166. friend class valarray<_Tp>;
  167. slice_array(_Array<_Tp>, const slice&);
  168. const size_t _M_sz;
  169. const size_t _M_stride;
  170. const _Array<_Tp> _M_array;
  171. #if __cplusplus < 201103L
  172. // not implemented
  173. slice_array();
  174. #else
  175. public:
  176. slice_array() = delete;
  177. #endif
  178. };
  179. template<typename _Tp>
  180. inline
  181. slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
  182. : _M_sz(__s.size()), _M_stride(__s.stride()),
  183. _M_array(__a.begin() + __s.start()) {}
  184. template<typename _Tp>
  185. inline
  186. slice_array<_Tp>::slice_array(const slice_array<_Tp>& __a)
  187. : _M_sz(__a._M_sz), _M_stride(__a._M_stride), _M_array(__a._M_array) {}
  188. // template<typename _Tp>
  189. // inline slice_array<_Tp>::~slice_array () {}
  190. template<typename _Tp>
  191. inline slice_array<_Tp>&
  192. slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
  193. {
  194. std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
  195. _M_array, _M_stride);
  196. return *this;
  197. }
  198. template<typename _Tp>
  199. inline void
  200. slice_array<_Tp>::operator=(const _Tp& __t) const
  201. { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
  202. template<typename _Tp>
  203. inline void
  204. slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
  205. { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
  206. template<typename _Tp>
  207. template<class _Dom>
  208. inline void
  209. slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
  210. { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
  211. #undef _DEFINE_VALARRAY_OPERATOR
  212. #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
  213. template<typename _Tp> \
  214. inline void \
  215. slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
  216. { \
  217. _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
  218. } \
  219. \
  220. template<typename _Tp> \
  221. template<class _Dom> \
  222. inline void \
  223. slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
  224. { \
  225. _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
  226. }
  227. _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
  228. _DEFINE_VALARRAY_OPERATOR(/, __divides)
  229. _DEFINE_VALARRAY_OPERATOR(%, __modulus)
  230. _DEFINE_VALARRAY_OPERATOR(+, __plus)
  231. _DEFINE_VALARRAY_OPERATOR(-, __minus)
  232. _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
  233. _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
  234. _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
  235. _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
  236. _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
  237. #undef _DEFINE_VALARRAY_OPERATOR
  238. /// @} group numeric_arrays
  239. _GLIBCXX_END_NAMESPACE_VERSION
  240. } // namespace
  241. #endif /* _SLICE_ARRAY_H */