📄 valarray
字号:
// valarray standard header
#if _MSC_VER > 1000
#pragma once
#endif
#ifndef _VALARRAY_
#define _VALARRAY_
#include <cstring>
#include <xstddef>
#ifdef _MSC_VER
#pragma pack(push,8)
#endif /* _MSC_VER */
_STD_BEGIN
class gslice;
class slice;
template<class _Ty>
class gslice_array;
template<class _Ty>
class indirect_array;
template<class _Ty>
class mask_array;
template<class _Ty>
class slice_array;
template<class _Ty>
class valarray;
typedef valarray<_Bool> _Boolarray;
typedef valarray<size_t> _Sizarray;
// TEMPLATE CLASS valarray
#define _VALOP(TYPE, LENGTH, RHS) \
valarray<TYPE> _Ans(LENGTH); \
for (size_t _I = 0; _I < _Ans.size(); ++_I) \
_Ans[_I] = RHS; \
return (_Ans)
#define _VALGOP(RHS) \
for (size_t _I = 0; _I < size(); ++_I) \
_Ptr[_I] RHS; \
return (*this)
#define _VALGOP2(RHS) \
for (size_t _I = 0; _I < _L.size(); ++_I) \
_L[_I] RHS; \
return (_L)
template<class _Ty>
class valarray {
public:
typedef _Ty value_type;
explicit valarray(size_t _N = 0)
{_Tidy(), _Res = _N, _Grow(_N); }
valarray(const _Ty& _X, size_t _N)
{_Tidy(), _Grow(_N, &_X); }
valarray(const _Ty *_S, size_t _N)
{_Tidy(), _Grow(_N, _S, 1); }
valarray(const valarray<_Ty>& _X)
{_Tidy(), _Grow(_X.size(), _X._Ptr, 1); }
valarray(const slice_array<_Ty>& _Sl)
{_Tidy();
*this = _Sl; }
valarray(const gslice_array<_Ty>& _Gs)
{_Tidy();
*this = _Gs; }
valarray(const mask_array<_Ty>& _Ma)
{_Tidy();
*this = _Ma; }
valarray(const indirect_array<_Ty>& _Ia)
{_Tidy();
*this = _Ia; }
~valarray()
{_Tidy(true); }
valarray<_Ty>& operator=(const valarray<_Ty>& _R)
{if (this == &_R)
return (*this);
_Grow(_R.size(), 0, 0, true);
_VALGOP(= _R[_I]); }
valarray<_Ty>& operator=(const _Ty& _R)
{_VALGOP(= _R); }
void resize(size_t _N, const _Ty& _C = _Ty())
{_Grow(_N, &_C, 0, true); }
valarray<_Ty>& operator=(const slice_array<_Ty>& _Sl);
valarray<_Ty>& operator=(const gslice_array<_Ty>& _Gs);
valarray<_Ty>& operator=(const mask_array<_Ty>& _Ma);
valarray<_Ty>& operator=(const indirect_array<_Ty>& _Ia);
size_t size() const
{return (_Len); }
_Ty operator[](size_t _I) const
{return (_Ptr[_I]); }
_Ty& operator[](size_t _I)
{return (_Ptr[_I]); }
valarray<_Ty> operator[](slice _Sl) const;
slice_array<_Ty> operator[](slice _Sl);
valarray<_Ty> operator[](const gslice& _Gs) const;
gslice_array<_Ty> operator[](const gslice& _Gs);
valarray<_Ty> operator[](
const _Boolarray& _Ba) const;
mask_array<_Ty> operator[](
const _Boolarray& _Ba);
valarray<_Ty> operator[](const _Sizarray& _Ia) const;
indirect_array<_Ty> operator[](const _Sizarray& _Ia);
_Ty sum() const
{_Ty _Sum = _Ptr[0];
for (size_t _I = 0; ++_I < size(); )
_Sum += _Ptr[_I];
return (_Sum); }
_Ty min() const
{_Ty _Min = _Ptr[0];
for (size_t _I = 0; ++_I < size(); )
if (_Ptr[_I] < _Min)
_Min = _Ptr[_I];
return (_Min); }
_Ty max() const
{_Ty _Max = _Ptr[0];
for (size_t _I = 0; ++_I < size(); )
if (_Max < _Ptr[_I])
_Max = _Ptr[_I];
return (_Max); }
valarray<_Ty> shift(int _N) const
{static _Ty _Def;
_VALOP(_Ty, size(),
0 < _N && size() - _I <= _N
|| _N < 0 && _I < -_N
? _Def : _Ptr[_I + _N]); }
valarray<_Ty> cshift(int _N) const
{if (size() == 0)
;
else if (_N < 0)
{if ((_N += size()) < 0)
_N = size() - (-_N) % size(); }
else if (size() <= _N)
_N %= size();
_VALOP(_Ty, size(),
size() - _I <= _N
? _Ptr[_I - size() + _N] : _Ptr[_I + _N]); }
valarray<_Ty> apply(_Ty _F(_Ty)) const
{_VALOP(_Ty, size(), _F(_Ptr[_I])); }
valarray<_Ty> apply(_Ty _F(const _Ty&)) const
{_VALOP(_Ty, size(), _F(_Ptr[_I])); }
void free()
{_Tidy(true); }
private:
void _Grow(size_t _N, const _Ty *_S = 0, size_t _D = 0,
bool _Trim = 0)
{size_t _Os = _Ptr == 0 ? 0 : _Res;
if (_N == 0)
{if (_Trim)
_Tidy(true); }
else if (_N == _Os || _N < _Os && !_Trim)
;
else
{size_t _I, _M = _Ptr == 0 && _N < _Res ? _Res : _N;
_Ty *_Np = new _Ty[_M];
if (_Np == 0)
_Nomemory();
size_t _Nm = _N < _Len ? _N : _Len;
for (_I = 0; _I < _Nm; ++_I)
_Np[_I] = _Ptr[_I];
if (_S != 0)
for (; _I < _M; ++_I, _S += _D)
_Np[_I] = *_S;
_Tidy(true), _Ptr = _Np, _Res = _M; }
_Len = _N; }
void _Tidy(bool _Constructed = 0)
{if (_Constructed && _Ptr != 0)
delete[] _Ptr;
_Len = 0, _Ptr = 0, _Res = 0; }
_Ty *_Ptr;
size_t _Len, _Res;
};
// valarray MEMBER TEMPLATE OPERATORS
template<class _Ty> inline
valarray<_Ty> operator+(const valarray<_Ty>& _L)
{_VALOP(_Ty, _L.size(), +_L[_I]); }
template<class _Ty> inline
valarray<_Ty> operator-(const valarray<_Ty>& _L)
{_VALOP(_Ty, _L.size(), -_L[_I]); }
template<class _Ty> inline
valarray<_Ty> operator~(const valarray<_Ty>& _L)
{_VALOP(_Ty, _L.size(), ~_L[_I]); }
template<class _Ty> inline
_Boolarray operator!(const valarray<_Ty>& _L)
{_VALOP(_Bool, _L.size(), !_L[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator*=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(*= _R); }
template<class _Ty> inline
valarray<_Ty>& operator/=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(/= _R); }
template<class _Ty> inline
valarray<_Ty>& operator%=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(%= _R); }
template<class _Ty> inline
valarray<_Ty>& operator+=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(+= _R); }
template<class _Ty> inline
valarray<_Ty>& operator-=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(-= _R); }
template<class _Ty> inline
valarray<_Ty>& operator^=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(^= _R); }
template<class _Ty> inline
valarray<_Ty>& operator&=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(&= _R); }
template<class _Ty> inline
valarray<_Ty>& operator|=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(|= _R); }
template<class _Ty> inline
valarray<_Ty>& operator<<=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(<<= _R); }
template<class _Ty> inline
valarray<_Ty>& operator>>=(valarray<_Ty>& _L, const _Ty& _R)
{_VALGOP2(>>= _R); }
template<class _Ty> inline
valarray<_Ty>& operator*=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(*= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator/=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(/= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator%=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(%= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator+=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(+= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator-=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(-= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator^=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(^= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator|=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(|= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator&=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(&= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator<<=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(<<= _R[_I]); }
template<class _Ty> inline
valarray<_Ty>& operator>>=(valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALGOP2(>>= _R[_I]); }
// valarray TEMPLATE FUNCTIONS
template<class _Ty> inline
valarray<_Ty> operator*(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] * _R); }
template<class _Ty> inline
valarray<_Ty> operator*(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L * _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator/(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] / _R); }
template<class _Ty> inline
valarray<_Ty> operator/(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L / _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator%(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] % _R); }
template<class _Ty> inline
valarray<_Ty> operator%(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L % _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator+(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] + _R); }
template<class _Ty> inline
valarray<_Ty> operator+(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L + _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator-(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] - _R); }
template<class _Ty> inline
valarray<_Ty> operator-(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L - _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator^(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] ^ _R); }
template<class _Ty> inline
valarray<_Ty> operator^(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L ^ _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator&(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] & _R); }
template<class _Ty> inline
valarray<_Ty> operator&(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L & _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator|(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] | _R); }
template<class _Ty> inline
valarray<_Ty> operator|(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L | _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator<<(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] << _R); }
template<class _Ty> inline
valarray<_Ty> operator<<(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L << _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator>>(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Ty, _L.size(), _L[_I] >> _R); }
template<class _Ty> inline
valarray<_Ty> operator>>(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _R.size(), _L >> _R[_I]); }
template<class _Ty> inline
_Boolarray operator&&(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] && _R); }
template<class _Ty> inline
_Boolarray operator&&(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L && _R[_I]); }
template<class _Ty> inline
_Boolarray operator||(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] || _R); }
template<class _Ty> inline
_Boolarray operator||(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L || _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator*(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] * _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator/(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] / _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator%(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] % _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator+(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] + _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator-(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] - _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator^(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] ^ _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator&(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] & _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator|(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] | _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator<<(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] << _R[_I]); }
template<class _Ty> inline
valarray<_Ty> operator>>(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Ty, _L.size(), _L[_I] >> _R[_I]); }
template<class _Ty> inline
_Boolarray operator&&(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] && _R[_I]); }
template<class _Ty> inline
_Boolarray operator||(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] || _R[_I]); }
template<class _Ty> inline
_Boolarray operator==(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] == _R); }
template<class _Ty> inline
_Boolarray operator==(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L == _R[_I]); }
template<class _Ty> inline
_Boolarray operator==(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] == _R[_I]); }
template<class _Ty> inline
_Boolarray operator!=(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] != _R); }
template<class _Ty> inline
_Boolarray operator!=(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L != _R[_I]); }
template<class _Ty> inline
_Boolarray operator!=(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] != _R[_I]); }
template<class _Ty> inline
_Boolarray operator<(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] < _R); }
template<class _Ty> inline
_Boolarray operator<(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L < _R[_I]); }
template<class _Ty> inline
_Boolarray operator<(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] < _R[_I]); }
template<class _Ty> inline
_Boolarray operator>(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] > _R); }
template<class _Ty> inline
_Boolarray operator>(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L > _R[_I]); }
template<class _Ty> inline
_Boolarray operator>(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] > _R[_I]); }
template<class _Ty> inline
_Boolarray operator<=(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] <= _R); }
template<class _Ty> inline
_Boolarray operator<=(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L <= _R[_I]); }
template<class _Ty> inline
_Boolarray operator<=(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] <= _R[_I]); }
template<class _Ty> inline
_Boolarray operator>=(const valarray<_Ty>& _L,
const _Ty& _R)
{_VALOP(_Bool, _L.size(), _L[_I] >= _R); }
template<class _Ty> inline
_Boolarray operator>=(const _Ty& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _R.size(), _L >= _R[_I]); }
template<class _Ty> inline
_Boolarray operator>=(const valarray<_Ty>& _L,
const valarray<_Ty>& _R)
{_VALOP(_Bool, _L.size(), _L[_I] >= _R[_I]); }
template<class _Ty> inline
valarray<_Ty> abs(const valarray<_Ty>& _X)
{_VALOP(_Ty, _X.size(), ::abs(_X[_I])); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -