xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/bits/indirect_array.h (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14fee23f9Smrg // The template and inlines for the -*- C++ -*- indirect_array class.
24fee23f9Smrg 
3*b1e83836Smrg // Copyright (C) 1997-2022 Free Software Foundation, Inc.
44fee23f9Smrg //
54fee23f9Smrg // This file is part of the GNU ISO C++ Library.  This library is free
64fee23f9Smrg // software; you can redistribute it and/or modify it under the
74fee23f9Smrg // terms of the GNU General Public License as published by the
84fee23f9Smrg // Free Software Foundation; either version 3, or (at your option)
94fee23f9Smrg // any later version.
104fee23f9Smrg 
114fee23f9Smrg // This library is distributed in the hope that it will be useful,
124fee23f9Smrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
134fee23f9Smrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144fee23f9Smrg // GNU General Public License for more details.
154fee23f9Smrg 
164fee23f9Smrg // Under Section 7 of GPL version 3, you are granted additional
174fee23f9Smrg // permissions described in the GCC Runtime Library Exception, version
184fee23f9Smrg // 3.1, as published by the Free Software Foundation.
194fee23f9Smrg 
204fee23f9Smrg // You should have received a copy of the GNU General Public License and
214fee23f9Smrg // a copy of the GCC Runtime Library Exception along with this program;
224fee23f9Smrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
234fee23f9Smrg // <http://www.gnu.org/licenses/>.
244fee23f9Smrg 
2548fb7bfaSmrg /** @file bits/indirect_array.h
264fee23f9Smrg  *  This is an internal header file, included by other library headers.
2748fb7bfaSmrg  *  Do not attempt to use it directly. @headername{valarray}
284fee23f9Smrg  */
294fee23f9Smrg 
304fee23f9Smrg // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
314fee23f9Smrg 
324fee23f9Smrg #ifndef _INDIRECT_ARRAY_H
334fee23f9Smrg #define _INDIRECT_ARRAY_H 1
344fee23f9Smrg 
354fee23f9Smrg #pragma GCC system_header
364fee23f9Smrg 
_GLIBCXX_VISIBILITY(default)3748fb7bfaSmrg namespace std _GLIBCXX_VISIBILITY(default)
3848fb7bfaSmrg {
3948fb7bfaSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
404fee23f9Smrg 
414fee23f9Smrg   /**
424fee23f9Smrg    * @addtogroup numeric_arrays
434fee23f9Smrg    * @{
444fee23f9Smrg    */
454fee23f9Smrg 
464fee23f9Smrg   /**
474fee23f9Smrg    *  @brief  Reference to arbitrary subset of an array.
484fee23f9Smrg    *
494fee23f9Smrg    *  An indirect_array is a reference to the actual elements of an array
504fee23f9Smrg    *  specified by an ordered array of indices.  The way to get an
514fee23f9Smrg    *  indirect_array is to call operator[](valarray<size_t>) on a valarray.
524fee23f9Smrg    *  The returned indirect_array then permits carrying operations out on the
534fee23f9Smrg    *  referenced subset of elements in the original valarray.
544fee23f9Smrg    *
554fee23f9Smrg    *  For example, if an indirect_array is obtained using the array (4,2,0) as
564fee23f9Smrg    *  an argument, and then assigned to an array containing (1,2,3), then the
574fee23f9Smrg    *  underlying array will have array[0]==3, array[2]==2, and array[4]==1.
584fee23f9Smrg    *
594fee23f9Smrg    *  @param  Tp  Element type.
604fee23f9Smrg    */
614fee23f9Smrg   template <class _Tp>
624fee23f9Smrg     class indirect_array
634fee23f9Smrg     {
644fee23f9Smrg     public:
654fee23f9Smrg       typedef _Tp value_type;
664fee23f9Smrg 
674fee23f9Smrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
684fee23f9Smrg       // 253. valarray helper functions are almost entirely useless
694fee23f9Smrg 
704fee23f9Smrg       ///  Copy constructor.  Both slices refer to the same underlying array.
714fee23f9Smrg       indirect_array(const indirect_array&);
724fee23f9Smrg 
734fee23f9Smrg       ///  Assignment operator.  Assigns elements to corresponding elements
744fee23f9Smrg       ///  of @a a.
754fee23f9Smrg       indirect_array& operator=(const indirect_array&);
764fee23f9Smrg 
774fee23f9Smrg       ///  Assign slice elements to corresponding elements of @a v.
784fee23f9Smrg       void operator=(const valarray<_Tp>&) const;
794fee23f9Smrg       ///  Multiply slice elements by corresponding elements of @a v.
804fee23f9Smrg       void operator*=(const valarray<_Tp>&) const;
814fee23f9Smrg       ///  Divide slice elements by corresponding elements of @a v.
824fee23f9Smrg       void operator/=(const valarray<_Tp>&) const;
834fee23f9Smrg       ///  Modulo slice elements by corresponding elements of @a v.
844fee23f9Smrg       void operator%=(const valarray<_Tp>&) const;
854fee23f9Smrg       ///  Add corresponding elements of @a v to slice elements.
864fee23f9Smrg       void operator+=(const valarray<_Tp>&) const;
874fee23f9Smrg       ///  Subtract corresponding elements of @a v from slice elements.
884fee23f9Smrg       void operator-=(const valarray<_Tp>&) const;
894fee23f9Smrg       ///  Logical xor slice elements with corresponding elements of @a v.
904fee23f9Smrg       void operator^=(const valarray<_Tp>&) const;
914fee23f9Smrg       ///  Logical and slice elements with corresponding elements of @a v.
924fee23f9Smrg       void operator&=(const valarray<_Tp>&) const;
934fee23f9Smrg       ///  Logical or slice elements with corresponding elements of @a v.
944fee23f9Smrg       void operator|=(const valarray<_Tp>&) const;
954fee23f9Smrg       ///  Left shift slice elements by corresponding elements of @a v.
964fee23f9Smrg       void operator<<=(const valarray<_Tp>&) const;
974fee23f9Smrg       ///  Right shift slice elements by corresponding elements of @a v.
984fee23f9Smrg       void operator>>=(const valarray<_Tp>&) const;
994fee23f9Smrg       ///  Assign all slice elements to @a t.
1004fee23f9Smrg       void operator= (const _Tp&) const;
1014fee23f9Smrg       //    ~indirect_array();
1024fee23f9Smrg 
1034fee23f9Smrg       template<class _Dom>
1044fee23f9Smrg       void operator=(const _Expr<_Dom, _Tp>&) const;
1054fee23f9Smrg       template<class _Dom>
1064fee23f9Smrg       void operator*=(const _Expr<_Dom, _Tp>&) const;
1074fee23f9Smrg       template<class _Dom>
1084fee23f9Smrg       void operator/=(const _Expr<_Dom, _Tp>&) const;
1094fee23f9Smrg       template<class _Dom>
1104fee23f9Smrg       void operator%=(const _Expr<_Dom, _Tp>&) const;
1114fee23f9Smrg       template<class _Dom>
1124fee23f9Smrg       void operator+=(const _Expr<_Dom, _Tp>&) const;
1134fee23f9Smrg       template<class _Dom>
1144fee23f9Smrg       void operator-=(const _Expr<_Dom, _Tp>&) const;
1154fee23f9Smrg       template<class _Dom>
1164fee23f9Smrg       void operator^=(const _Expr<_Dom, _Tp>&) const;
1174fee23f9Smrg       template<class _Dom>
1184fee23f9Smrg       void operator&=(const _Expr<_Dom, _Tp>&) const;
1194fee23f9Smrg       template<class _Dom>
1204fee23f9Smrg       void operator|=(const _Expr<_Dom, _Tp>&) const;
1214fee23f9Smrg       template<class _Dom>
1224fee23f9Smrg       void operator<<=(const _Expr<_Dom, _Tp>&) const;
1234fee23f9Smrg       template<class _Dom>
1244fee23f9Smrg       void operator>>=(const _Expr<_Dom, _Tp>&) const;
1254fee23f9Smrg 
1264fee23f9Smrg     private:
1274fee23f9Smrg       ///  Copy constructor.  Both slices refer to the same underlying array.
1284fee23f9Smrg       indirect_array(_Array<_Tp>, size_t, _Array<size_t>);
1294fee23f9Smrg 
1304fee23f9Smrg       friend class valarray<_Tp>;
1314fee23f9Smrg       friend class gslice_array<_Tp>;
1324fee23f9Smrg 
1334fee23f9Smrg       const size_t	 _M_sz;
1344fee23f9Smrg       const _Array<size_t> _M_index;
1354fee23f9Smrg       const _Array<_Tp>	 _M_array;
1364fee23f9Smrg 
1374fee23f9Smrg       // not implemented
1384fee23f9Smrg       indirect_array();
1394fee23f9Smrg     };
1404fee23f9Smrg 
1414fee23f9Smrg   template<typename _Tp>
1424fee23f9Smrg     inline
1434fee23f9Smrg     indirect_array<_Tp>::indirect_array(const indirect_array<_Tp>& __a)
1444fee23f9Smrg     : _M_sz(__a._M_sz), _M_index(__a._M_index), _M_array(__a._M_array) {}
1454fee23f9Smrg 
1464fee23f9Smrg   template<typename _Tp>
1474fee23f9Smrg     inline
1484fee23f9Smrg     indirect_array<_Tp>::indirect_array(_Array<_Tp> __a, size_t __s,
1494fee23f9Smrg 					_Array<size_t> __i)
1504fee23f9Smrg     : _M_sz(__s), _M_index(__i), _M_array(__a) {}
1514fee23f9Smrg 
1524fee23f9Smrg   template<typename _Tp>
1534fee23f9Smrg     inline indirect_array<_Tp>&
1544fee23f9Smrg     indirect_array<_Tp>::operator=(const indirect_array<_Tp>& __a)
1554fee23f9Smrg     {
1564fee23f9Smrg       std::__valarray_copy(__a._M_array, _M_sz, __a._M_index, _M_array,
1574fee23f9Smrg 			   _M_index);
1584fee23f9Smrg       return *this;
1594fee23f9Smrg     }
1604fee23f9Smrg 
1614fee23f9Smrg   template<typename _Tp>
1624fee23f9Smrg     inline void
1634fee23f9Smrg     indirect_array<_Tp>::operator=(const _Tp& __t) const
1644fee23f9Smrg     { std::__valarray_fill(_M_array, _M_index, _M_sz, __t); }
1654fee23f9Smrg 
1664fee23f9Smrg   template<typename _Tp>
1674fee23f9Smrg     inline void
1684fee23f9Smrg     indirect_array<_Tp>::operator=(const valarray<_Tp>& __v) const
1694fee23f9Smrg     { std::__valarray_copy(_Array<_Tp>(__v), _M_sz, _M_array, _M_index); }
1704fee23f9Smrg 
1714fee23f9Smrg   template<typename _Tp>
1724fee23f9Smrg     template<class _Dom>
1734fee23f9Smrg       inline void
1744fee23f9Smrg       indirect_array<_Tp>::operator=(const _Expr<_Dom, _Tp>& __e) const
1754fee23f9Smrg       { std::__valarray_copy(__e, _M_sz, _M_array, _M_index); }
1764fee23f9Smrg 
1777d4dc15bSmrg   /// @cond undocumented
1784fee23f9Smrg #undef _DEFINE_VALARRAY_OPERATOR
1794fee23f9Smrg #define _DEFINE_VALARRAY_OPERATOR(_Op, _Name)				\
1804fee23f9Smrg   template<typename _Tp>						\
1814fee23f9Smrg     inline void								\
1824fee23f9Smrg     indirect_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const\
1834fee23f9Smrg     {									\
1844fee23f9Smrg       _Array_augmented_##_Name(_M_array, _M_index, _Array<_Tp>(__v), _M_sz); \
1854fee23f9Smrg     }									\
1864fee23f9Smrg 									\
1874fee23f9Smrg   template<typename _Tp>                                                \
1884fee23f9Smrg     template<class _Dom>				                \
1894fee23f9Smrg       inline void							\
1904fee23f9Smrg       indirect_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
1914fee23f9Smrg       {									\
1924fee23f9Smrg 	_Array_augmented_##_Name(_M_array, _M_index, __e, _M_sz);	\
1934fee23f9Smrg       }
1944fee23f9Smrg 
1954fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
1964fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(/, __divides)
1974fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(%, __modulus)
1984fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(+, __plus)
1994fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(-, __minus)
2004fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
2014fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
2024fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
2034fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
2044fee23f9Smrg _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
2054fee23f9Smrg 
2064fee23f9Smrg #undef _DEFINE_VALARRAY_OPERATOR
2077d4dc15bSmrg   /// @endcond
2084fee23f9Smrg 
209a448f87cSmrg   /// @} group numeric_arrays
2104fee23f9Smrg 
21148fb7bfaSmrg _GLIBCXX_END_NAMESPACE_VERSION
21248fb7bfaSmrg } // namespace
2134fee23f9Smrg 
2144fee23f9Smrg #endif /* _INDIRECT_ARRAY_H */
215