📄 valarray
字号:
_C_stride (__stride),
_C_reset (true),
_C_r_length (size_t (), __length.size ()) {
_RWSTD_ASSERT (_C_length.size () == _C_stride.size ());
}
gslice (const gslice &__rhs)
: _C_start (__rhs._C_start),
_C_length (__rhs._C_length),
_C_stride (__rhs._C_stride),
_C_reset (true),
_C_r_length (size_t (), __rhs._C_length.size ()) {
_RWSTD_ASSERT (_C_length.size () == _C_stride.size ());
}
// 26.3.6.2
size_t start () const {
return _C_start;
}
valarray<size_t> size () const {
return _C_length;
}
valarray<size_t> stride () const {
return _C_stride;
}
// convenience functions
size_t next_ind ();
bool is_reseted () const {
return _C_reset;
}
size_t ind_numb () const;
private:
size_t _C_start; // starting offset
valarray<size_t> _C_length; // set of lengths (sizes equal)
valarray<size_t> _C_stride; // set of strides (sizes equal)
bool _C_reset;
valarray<size_t> _C_r_length;
};
template <class _TypeT>
class gslice_array
{
friend class valarray<_TypeT>;
public:
typedef _TypeT value_type;
gslice_array (const gslice_array<value_type>& __rhs)
: _C_array (__rhs.get_ref_mem_array ()),
_C_slice (__rhs.get_slice ())
{ }
gslice_array (_RW::__rw_array<value_type>* __a, const gslice & __s)
: _C_array (__a),
_C_slice (__s)
{ }
_RW::__rw_array<value_type>* get_ref_mem_array () const {
return _C_array;
}
gslice get_slice () const {
return _C_slice;
}
void operator= (const valarray<value_type> &__rhs) const;
void operator= (const value_type &__rhs) const;
void operator*= (const valarray<value_type> &__rhs) const;
void operator/= (const valarray<value_type> &__rhs) const;
void operator+= (const valarray<value_type> &__rhs) const;
void operator-= (const valarray<value_type> &__rhs) const;
void operator%= (const valarray<value_type> &__rhs) const;
void operator^= (const valarray<value_type> &__rhs) const;
void operator&= (const valarray<value_type> &__rhs) const;
void operator|= (const valarray<value_type> &__rhs) const;
void operator<<= (const valarray<value_type> &__rhs) const;
void operator>>= (const valarray<value_type> &__rhs) const;
private:
gslice_array ();
gslice_array<value_type>& operator= (const gslice_array<value_type>&);
_RW::__rw_array<value_type>* _C_array;
gslice _C_slice;
};
template <class _TypeT>
class mask_array
{
friend class valarray<_TypeT>;
public:
typedef _TypeT value_type;
mask_array (const mask_array<value_type> &__rhs)
: _C_array (__rhs.get_ref_mem_array ()),
_C_valarray (__rhs.get_array ())
{ }
mask_array (_RW::__rw_array<value_type> *__a, const valarray<bool>& __val)
: _C_array (__a),
_C_valarray (__val)
{ }
_RW::__rw_array<value_type>* get_ref_mem_array () const {
return _C_array;
}
valarray<bool> get_array () const {
return _C_valarray;
}
const valarray<bool>* get_array_pt () const {
return &_C_valarray;
}
void operator= (const value_type&) const;
void operator= (const valarray<value_type> &__rhs) const {
_RW::__rw_unary_function (*this, __rhs, _RW::identity<value_type>());
}
void operator*= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs, multiplies<value_type>());
}
void operator/= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs, divides<value_type>());
}
void operator+= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs, plus<value_type>());
}
void operator-= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs, minus<value_type>());
}
void operator%= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs, modulus<value_type>());
}
void operator^= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::exclusive_or<value_type>());
}
void operator&= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::bitwise_and<value_type>());
}
void operator|= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs, _RW::bitwise_or<value_type>());
}
void operator<<= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs, _RW::shift_left<value_type>());
}
void operator>>= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::shift_right<value_type>());
}
private:
mask_array ();
mask_array<value_type>& operator= (const mask_array<value_type>&);
_RW::__rw_array<value_type>* _C_array;
valarray<bool> _C_valarray;
};
template <class _TypeT>
void mask_array<_TypeT>::operator= (const value_type &__rhs) const
{
_RWSTD_ASSERT (0 != get_ref_mem_array ());
_RW::__rw_array<_TypeT> &__a = *get_ref_mem_array ();
for (size_t __i = 0; __i != _C_valarray.size (); ++__i) {
if (_C_valarray [__i])
__a [__i] = __rhs;
}
}
/****************************************************************
* INDIRECT_ARRAY *
****************************************************************/
template <class _TypeT>
class indirect_array
{
friend class valarray<_TypeT>;
public:
typedef _TypeT value_type;
indirect_array (const indirect_array<value_type>& __sl)
:_C_array (__sl.get_ref_mem_array ()),
_C_valarray (__sl.get_array ())
{ }
indirect_array (_RW::__rw_array<value_type> *__a,
const valarray<size_t> &__v)
:_C_array (__a),
_C_valarray (__v)
{ }
_RW::__rw_array<value_type>* get_ref_mem_array () const {
return _C_array;
}
valarray<size_t> get_array () const {
return _C_valarray;
}
const valarray<size_t>* get_array_pt () const {
return &_C_valarray;
}
void operator= (const value_type&) const;
void operator= (const valarray<value_type> &__rhs) const {
_RW::__rw_unary_function (*this, __rhs,
_RW::identity<value_type>());
}
void operator*= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
multiplies<value_type>());
}
void operator/= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
divides<value_type>());
}
void operator+= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
plus<value_type>());
}
void operator-= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
minus<value_type>());
}
void operator%= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
modulus<value_type>());
}
void operator^= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::exclusive_or<value_type>());
}
void operator&= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::bitwise_and<value_type>());
}
void operator|= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::bitwise_or<value_type>());
}
void operator<<= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::shift_left<value_type>());
}
void operator>>= (const valarray<value_type> &__rhs) const {
_RW::__rw_binary_function (*this, __rhs,
_RW::shift_right<value_type>());
}
private:
indirect_array ();
indirect_array& operator= (const indirect_array&);
_RW::__rw_array<value_type>* _C_array;
valarray<size_t> _C_valarray;
};
template <class _TypeT>
inline void indirect_array<_TypeT>::operator= (const _TypeT &__rhs) const
{
for (size_t __i = 0; __i != _C_valarray.size (); ++__i)
(*_C_array)[_C_valarray [__i]] = __rhs;
}
_RWSTD_NAMESPACE_END // std
_RWSTD_NAMESPACE_BEGIN (__rw)
_USING (_RWSTD_C::size_t);
template <class _TypeT, class _UnaryFunction>
inline void
__rw_unary_function (const _STD::slice_array<_TypeT> &__lhs,
const _STD::valarray<_TypeT> &__rhs,
_UnaryFunction __fun)
{
_RWSTD_ASSERT (0 != __lhs.get_ref_mem_array ());
_STD::slice __slice = __lhs.get_slice ();
_RW::__rw_array<_TypeT> &__ar = *__lhs.get_ref_mem_array ();
for (size_t __i = __slice.start(), __j = 0; __j != __slice.size(); ++__j) {
if (__j < __rhs.size () && __i < __ar.size ())
__ar [__i] = __fun (__rhs [__j]);
__i += __slice.stride ();
}
}
template <class _TypeT, class _BinaryFunction>
inline void
__rw_binary_function (const _STD::slice_array<_TypeT> &__lhs,
const _STD::valarray<_TypeT> &__rhs,
_BinaryFunction __fun)
{
_RWSTD_ASSERT (0 != __lhs.get_ref_mem_array ());
_STD::slice __slice = __lhs.get_slice ();
_RW::__rw_array<_TypeT> &__ar = *__lhs.get_ref_mem_array ();
for (size_t __i = __slice.start(), __j = 0; __j != __slice.size(); ++__j) {
if (__j < __rhs.size () && __i < __ar.size ())
__ar [__i] = __fun (__ar [__i], __rhs [__j]);
__i += __slice.stride ();
}
}
template <class _TypeT, class _UnaryFunction>
inline void
__rw_unary_function (const _STD::mask_array<_TypeT> &__lhs,
const _STD::valarray<_TypeT> &__rhs,
_UnaryFunction __fun)
{
_RWSTD_ASSERT (0 != __lhs.get_ref_mem_array ());
_RW::__rw_array<_TypeT> &__ar = *__lhs.get_ref_mem_array ();
const _STD::valarray<bool> &__vlray = *__lhs.get_array_pt ();
for (size_t __i = 0, __j = 0; __i != __vlray.size (); ++__i)
if (__vlray [__i])
__ar [__i] = __fun (__rhs [__j++]);
}
template <class _TypeT, class _BinaryFunction>
inline void
__rw_binary_function (const _STD::mask_array<_TypeT> &__lhs,
const _STD::valarray<_TypeT> &__rhs,
_BinaryFunction __fun)
{
_RWSTD_ASSERT (0 != __lhs.get_ref_mem_array ());
_RW::__rw_array<_TypeT> &__ar = *__lhs.get_ref_mem_array ();
const _STD::valarray<bool> &__vlray = *__lhs.get_array_pt ();
for (size_t __i = 0, __j = 0; __i != __vlray.size (); ++__i)
if (__vlray [__i])
__ar [__i] = __fun (__ar [__i], __rhs [__j++]);
}
template <class _TypeT, class _UnaryFunction>
inline void
__rw_unary_function (const _STD::indirect_array<_TypeT> &__lhs,
const _STD::valarray<_TypeT> &__rhs,
_UnaryFunction __fun)
{
_RWSTD_ASSERT (0 != __lhs.get_ref_mem_array ());
_RW::__rw_array<_TypeT> &__ar = *__lhs.get_ref_mem_array ();
const _STD::valarray<size_t> &__vlray = *__lhs.get_array_pt ();
for (size_t __i = 0; __i != __vlray.size (); ++__i)
__ar [__vlray [__i]] = __fun (__rhs [__i]);
}
template <class _TypeT, class _BinaryFunction>
inline void
__rw_binary_function (const _STD::indirect_array<_TypeT> &__lhs,
const _STD::valarray<_TypeT> &__rhs,
_BinaryFunction __fun)
{
_RWSTD_ASSERT (0 != __lhs.get_ref_mem_array ());
_RW::__rw_array<_TypeT> &__ar = *__lhs.get_ref_mem_array ();
const _STD::valarray<size_t> &__vlray = *__lhs.get_array_pt ();
for (size_t __i = 0; __i != __vlray.size (); ++__i)
__ar [__vlray [__i]] = __fun (__ar [__vlray [__i]], __rhs [__i]);
}
_RWSTD_NAMESPACE_END // __rw
_RWSTD_NAMESPACE_BEGIN (std)
/*****************************************************************
* *
* GSLICE_ARRAY MEMBER FUNCTIONS *
* *
******************************************************************/
// gslice_array inline member functions
template <class _TypeT>
inline void
gslice_array<_TypeT>::operator= (const valarray<_TypeT>& __rhs) const
{
gslice *gsl = _RWSTD_CONST_CAST (gslice*, &_C_slice);
size_t __i = gsl->next_ind();
size_t __cpt = 0;
while( (!gsl->is_reseted()) && (__cpt < __rhs.size()) )
{
(*_C_array)[__i] = __rhs[__cpt];
__i= gsl->next_ind();
__cpt++;
}
}
template <class _TypeT>
inline void gslice_array<_TypeT>::operator= (const _TypeT& value) const
{
gslice *gsl = _RWSTD_CONST_CAST (gslice*, &_C_slice);
size_t __i = gsl->next_ind();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -