| 1 | // The template and inlines for the -*- C++ -*- gslice_array class. |
| 2 | |
| 3 | // Copyright (C) 1997-2018 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file bits/gslice_array.h |
| 26 | * This is an internal header file, included by other library headers. |
| 27 | * Do not attempt to use it directly. @headername{valarray} |
| 28 | */ |
| 29 | |
| 30 | // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr> |
| 31 | |
| 32 | #ifndef _GSLICE_ARRAY_H |
| 33 | #define _GSLICE_ARRAY_H 1 |
| 34 | |
| 35 | #pragma GCC system_header |
| 36 | |
| 37 | namespace std _GLIBCXX_VISIBILITY(default) |
| 38 | { |
| 39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 40 | |
| 41 | /** |
| 42 | * @addtogroup numeric_arrays |
| 43 | * @{ |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * @brief Reference to multi-dimensional subset of an array. |
| 48 | * |
| 49 | * A gslice_array is a reference to the actual elements of an array |
| 50 | * specified by a gslice. The way to get a gslice_array is to call |
| 51 | * operator[](gslice) on a valarray. The returned gslice_array then |
| 52 | * permits carrying operations out on the referenced subset of elements in |
| 53 | * the original valarray. For example, operator+=(valarray) will add |
| 54 | * values to the subset of elements in the underlying valarray this |
| 55 | * gslice_array refers to. |
| 56 | * |
| 57 | * @param Tp Element type. |
| 58 | */ |
| 59 | template<typename _Tp> |
| 60 | class gslice_array |
| 61 | { |
| 62 | public: |
| 63 | typedef _Tp value_type; |
| 64 | |
| 65 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 66 | // 253. valarray helper functions are almost entirely useless |
| 67 | |
| 68 | /// Copy constructor. Both slices refer to the same underlying array. |
| 69 | gslice_array(const gslice_array&); |
| 70 | |
| 71 | /// Assignment operator. Assigns slice elements to corresponding |
| 72 | /// elements of @a a. |
| 73 | gslice_array& operator=(const gslice_array&); |
| 74 | |
| 75 | /// Assign slice elements to corresponding elements of @a v. |
| 76 | void operator=(const valarray<_Tp>&) const; |
| 77 | /// Multiply slice elements by corresponding elements of @a v. |
| 78 | void operator*=(const valarray<_Tp>&) const; |
| 79 | /// Divide slice elements by corresponding elements of @a v. |
| 80 | void operator/=(const valarray<_Tp>&) const; |
| 81 | /// Modulo slice elements by corresponding elements of @a v. |
| 82 | void operator%=(const valarray<_Tp>&) const; |
| 83 | /// Add corresponding elements of @a v to slice elements. |
| 84 | void operator+=(const valarray<_Tp>&) const; |
| 85 | /// Subtract corresponding elements of @a v from slice elements. |
| 86 | void operator-=(const valarray<_Tp>&) const; |
| 87 | /// Logical xor slice elements with corresponding elements of @a v. |
| 88 | void operator^=(const valarray<_Tp>&) const; |
| 89 | /// Logical and slice elements with corresponding elements of @a v. |
| 90 | void operator&=(const valarray<_Tp>&) const; |
| 91 | /// Logical or slice elements with corresponding elements of @a v. |
| 92 | void operator|=(const valarray<_Tp>&) const; |
| 93 | /// Left shift slice elements by corresponding elements of @a v. |
| 94 | void operator<<=(const valarray<_Tp>&) const; |
| 95 | /// Right shift slice elements by corresponding elements of @a v. |
| 96 | void operator>>=(const valarray<_Tp>&) const; |
| 97 | /// Assign all slice elements to @a t. |
| 98 | void operator=(const _Tp&) const; |
| 99 | |
| 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 | template<class _Dom> |
| 107 | void operator%=(const _Expr<_Dom, _Tp>&) const; |
| 108 | template<class _Dom> |
| 109 | void operator+=(const _Expr<_Dom, _Tp>&) const; |
| 110 | template<class _Dom> |
| 111 | void operator-=(const _Expr<_Dom, _Tp>&) const; |
| 112 | template<class _Dom> |
| 113 | void operator^=(const _Expr<_Dom, _Tp>&) const; |
| 114 | template<class _Dom> |
| 115 | void operator&=(const _Expr<_Dom, _Tp>&) const; |
| 116 | template<class _Dom> |
| 117 | void operator|=(const _Expr<_Dom, _Tp>&) const; |
| 118 | template<class _Dom> |
| 119 | void operator<<=(const _Expr<_Dom, _Tp>&) const; |
| 120 | template<class _Dom> |
| 121 | void operator>>=(const _Expr<_Dom, _Tp>&) const; |
| 122 | |
| 123 | private: |
| 124 | _Array<_Tp> _M_array; |
| 125 | const valarray<size_t>& _M_index; |
| 126 | |
| 127 | friend class valarray<_Tp>; |
| 128 | |
| 129 | gslice_array(_Array<_Tp>, const valarray<size_t>&); |
| 130 | |
| 131 | // not implemented |
| 132 | gslice_array(); |
| 133 | }; |
| 134 | |
| 135 | template<typename _Tp> |
| 136 | inline |
| 137 | gslice_array<_Tp>::gslice_array(_Array<_Tp> __a, |
| 138 | const valarray<size_t>& __i) |
| 139 | : _M_array(__a), _M_index(__i) {} |
| 140 | |
| 141 | template<typename _Tp> |
| 142 | inline |
| 143 | gslice_array<_Tp>::gslice_array(const gslice_array<_Tp>& __a) |
| 144 | : _M_array(__a._M_array), _M_index(__a._M_index) {} |
| 145 | |
| 146 | template<typename _Tp> |
| 147 | inline gslice_array<_Tp>& |
| 148 | gslice_array<_Tp>::operator=(const gslice_array<_Tp>& __a) |
| 149 | { |
| 150 | std::__valarray_copy(_Array<_Tp>(__a._M_array), |
| 151 | _Array<size_t>(__a._M_index), _M_index.size(), |
| 152 | _M_array, _Array<size_t>(_M_index)); |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | template<typename _Tp> |
| 157 | inline void |
| 158 | gslice_array<_Tp>::operator=(const _Tp& __t) const |
| 159 | { |
| 160 | std::__valarray_fill(_M_array, _Array<size_t>(_M_index), |
| 161 | _M_index.size(), __t); |
| 162 | } |
| 163 | |
| 164 | template<typename _Tp> |
| 165 | inline void |
| 166 | gslice_array<_Tp>::operator=(const valarray<_Tp>& __v) const |
| 167 | { |
| 168 | std::__valarray_copy(_Array<_Tp>(__v), __v.size(), |
| 169 | _M_array, _Array<size_t>(_M_index)); |
| 170 | } |
| 171 | |
| 172 | template<typename _Tp> |
| 173 | template<class _Dom> |
| 174 | inline void |
| 175 | gslice_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const |
| 176 | { |
| 177 | std::__valarray_copy (__e, _M_index.size(), _M_array, |
| 178 | _Array<size_t>(_M_index)); |
| 179 | } |
| 180 | |
| 181 | #undef _DEFINE_VALARRAY_OPERATOR |
| 182 | #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name) \ |
| 183 | template<typename _Tp> \ |
| 184 | inline void \ |
| 185 | gslice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \ |
| 186 | { \ |
| 187 | _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), \ |
| 188 | _Array<_Tp>(__v), __v.size()); \ |
| 189 | } \ |
| 190 | \ |
| 191 | template<typename _Tp> \ |
| 192 | template<class _Dom> \ |
| 193 | inline void \ |
| 194 | gslice_array<_Tp>::operator _Op##= (const _Expr<_Dom, _Tp>& __e) const\ |
| 195 | { \ |
| 196 | _Array_augmented_##_Name(_M_array, _Array<size_t>(_M_index), __e,\ |
| 197 | _M_index.size()); \ |
| 198 | } |
| 199 | |
| 200 | _DEFINE_VALARRAY_OPERATOR(*, __multiplies) |
| 201 | _DEFINE_VALARRAY_OPERATOR(/, __divides) |
| 202 | _DEFINE_VALARRAY_OPERATOR(%, __modulus) |
| 203 | _DEFINE_VALARRAY_OPERATOR(+, __plus) |
| 204 | _DEFINE_VALARRAY_OPERATOR(-, __minus) |
| 205 | _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor) |
| 206 | _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and) |
| 207 | _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or) |
| 208 | _DEFINE_VALARRAY_OPERATOR(<<, __shift_left) |
| 209 | _DEFINE_VALARRAY_OPERATOR(>>, __shift_right) |
| 210 | |
| 211 | #undef _DEFINE_VALARRAY_OPERATOR |
| 212 | |
| 213 | // @} group numeric_arrays |
| 214 | |
| 215 | _GLIBCXX_END_NAMESPACE_VERSION |
| 216 | } // namespace |
| 217 | |
| 218 | #endif /* _GSLICE_ARRAY_H */ |
| 219 | |