slice_array.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // The template and inlines for the -*- C++ -*- slice_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/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. private:
  68. size_t _M_off; // offset
  69. size_t _M_sz; // size
  70. size_t _M_st; // stride unit
  71. };
  72. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  73. // 543. valarray slice default constructor
  74. inline
  75. slice::slice()
  76. : _M_off(0), _M_sz(0), _M_st(0) {}
  77. inline
  78. slice::slice(size_t __o, size_t __d, size_t __s)
  79. : _M_off(__o), _M_sz(__d), _M_st(__s) {}
  80. inline size_t
  81. slice::start() const
  82. { return _M_off; }
  83. inline size_t
  84. slice::size() const
  85. { return _M_sz; }
  86. inline size_t
  87. slice::stride() const
  88. { return _M_st; }
  89. /**
  90. * @brief Reference to one-dimensional subset of an array.
  91. *
  92. * A slice_array is a reference to the actual elements of an array
  93. * specified by a slice. The way to get a slice_array is to call
  94. * operator[](slice) on a valarray. The returned slice_array then permits
  95. * carrying operations out on the referenced subset of elements in the
  96. * original valarray. For example, operator+=(valarray) will add values
  97. * to the subset of elements in the underlying valarray this slice_array
  98. * refers to.
  99. *
  100. * @param Tp Element type.
  101. */
  102. template<typename _Tp>
  103. class slice_array
  104. {
  105. public:
  106. typedef _Tp value_type;
  107. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  108. // 253. valarray helper functions are almost entirely useless
  109. /// Copy constructor. Both slices refer to the same underlying array.
  110. slice_array(const slice_array&);
  111. /// Assignment operator. Assigns slice elements to corresponding
  112. /// elements of @a a.
  113. slice_array& operator=(const slice_array&);
  114. /// Assign slice elements to corresponding elements of @a v.
  115. void operator=(const valarray<_Tp>&) const;
  116. /// Multiply slice elements by corresponding elements of @a v.
  117. void operator*=(const valarray<_Tp>&) const;
  118. /// Divide slice elements by corresponding elements of @a v.
  119. void operator/=(const valarray<_Tp>&) const;
  120. /// Modulo slice elements by corresponding elements of @a v.
  121. void operator%=(const valarray<_Tp>&) const;
  122. /// Add corresponding elements of @a v to slice elements.
  123. void operator+=(const valarray<_Tp>&) const;
  124. /// Subtract corresponding elements of @a v from slice elements.
  125. void operator-=(const valarray<_Tp>&) const;
  126. /// Logical xor slice elements with corresponding elements of @a v.
  127. void operator^=(const valarray<_Tp>&) const;
  128. /// Logical and slice elements with corresponding elements of @a v.
  129. void operator&=(const valarray<_Tp>&) const;
  130. /// Logical or slice elements with corresponding elements of @a v.
  131. void operator|=(const valarray<_Tp>&) const;
  132. /// Left shift slice elements by corresponding elements of @a v.
  133. void operator<<=(const valarray<_Tp>&) const;
  134. /// Right shift slice elements by corresponding elements of @a v.
  135. void operator>>=(const valarray<_Tp>&) const;
  136. /// Assign all slice elements to @a t.
  137. void operator=(const _Tp &) const;
  138. // ~slice_array ();
  139. template<class _Dom>
  140. void operator=(const _Expr<_Dom, _Tp>&) const;
  141. template<class _Dom>
  142. void operator*=(const _Expr<_Dom, _Tp>&) const;
  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. private:
  162. friend class valarray<_Tp>;
  163. slice_array(_Array<_Tp>, const slice&);
  164. const size_t _M_sz;
  165. const size_t _M_stride;
  166. const _Array<_Tp> _M_array;
  167. // not implemented
  168. slice_array();
  169. };
  170. template<typename _Tp>
  171. inline
  172. slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
  173. : _M_sz(__s.size()), _M_stride(__s.stride()),
  174. _M_array(__a.begin() + __s.start()) {}
  175. template<typename _Tp>
  176. inline
  177. slice_array<_Tp>::slice_array(const slice_array<_Tp>& __a)
  178. : _M_sz(__a._M_sz), _M_stride(__a._M_stride), _M_array(__a._M_array) {}
  179. // template<typename _Tp>
  180. // inline slice_array<_Tp>::~slice_array () {}
  181. template<typename _Tp>
  182. inline slice_array<_Tp>&
  183. slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
  184. {
  185. std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
  186. _M_array, _M_stride);
  187. return *this;
  188. }
  189. template<typename _Tp>
  190. inline void
  191. slice_array<_Tp>::operator=(const _Tp& __t) const
  192. { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
  193. template<typename _Tp>
  194. inline void
  195. slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
  196. { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
  197. template<typename _Tp>
  198. template<class _Dom>
  199. inline void
  200. slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
  201. { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
  202. #undef _DEFINE_VALARRAY_OPERATOR
  203. #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
  204. template<typename _Tp> \
  205. inline void \
  206. slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
  207. { \
  208. _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
  209. } \
  210. \
  211. template<typename _Tp> \
  212. template<class _Dom> \
  213. inline void \
  214. slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
  215. { \
  216. _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
  217. }
  218. _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
  219. _DEFINE_VALARRAY_OPERATOR(/, __divides)
  220. _DEFINE_VALARRAY_OPERATOR(%, __modulus)
  221. _DEFINE_VALARRAY_OPERATOR(+, __plus)
  222. _DEFINE_VALARRAY_OPERATOR(-, __minus)
  223. _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
  224. _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
  225. _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
  226. _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
  227. _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
  228. #undef _DEFINE_VALARRAY_OPERATOR
  229. // @} group numeric_arrays
  230. _GLIBCXX_END_NAMESPACE_VERSION
  231. } // namespace
  232. #endif /* _SLICE_ARRAY_H */