#ifndef VALARRAY_AMV
#define VALARRAY_AMV
#include <new>
namespace amv {
template <class type>
class valarray {
type *ptr;
size_t m_size;
public:
typedef type value_type;
valarray();
explicit valarray(size_t n);
valarray(const type &val, size_t n);
valarray(const type *p, size_t n);
valarray(const valarray &v);
~valarray();
valarray &operator=(const valarray &v);
valarray &operator=(const type &val);
const type &operator[](size_t subscript) const;
type &operator[](size_t subscript);
bool operator==(const valarray &v)
{
if(m_size==v.m_size)
{
for(size_t i=0; i<m_size; ++i)
if(!(ptr[i]==v.ptr[i]))
return false;
}
else
return false;
return true;
}
valarray &operator*=(const type &val);
valarray &operator*=(const valarray &v);
valarray &operator/=(const type &val);
valarray &operator%=(const type &val);
valarray &operator+=(const type &val);
valarray &operator+=(const valarray &v);
valarray &operator-=(const type &val);
valarray &operator-=(const valarray &v);
valarray &operator^=(const type &val);
valarray &operator&=(const type &val);
valarray &operator|=(const type &val);
valarray &operator<<=(size_t n);
valarray &operator>>=(size_t n);
type sum() const;
valarray shift(int i) const;
valarray cshift(int i) const;
valarray apply(type f(type)) const;
valarray apply(type f(const type &)) const;
valarray operator-() const;
valarray operator+() const;
valarray operator~() const;
valarray operator!() const;
size_t size() const;
void resize(size_t n, const type &val=type());
friend valarray<type> operator*(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator*(const valarray<type> &v, const type &val);
friend valarray<type> operator*(const type &val, const valarray<type> &v);
friend valarray<type> operator/(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator/(const valarray<type> &v, const type &val);
friend valarray<type> operator/(const type &val, const valarray<type> &v);
friend valarray<type> operator+(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator+(const valarray<type> &v, const type &val);
friend valarray<type> operator+(const type &val, const valarray<type> &v);
friend valarray<type> operator-(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator-(const valarray<type> &v, const type &val);
friend valarray<type> operator-(const type &val, const valarray<type> &v);
friend valarray<type> operator^(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator^(const valarray<type> &v, const type &val);
friend valarray<type> operator^(const type &val, const valarray<type> &v);
friend valarray<type> operator&(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator&(const valarray<type> &v, const type &val);
friend valarray<type> operator&(const type &val, const valarray<type> &v);
friend valarray<type> operator|(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator|(const valarray<type> &v, const type &val);
friend valarray<type> operator|(const type &val, const valarray<type> &v);
friend valarray<type> operator<<(const valarray<type> &v, size_t n);
friend valarray<type> operator>>(const valarray<type> &v, size_t n);
friend valarray<type> operator&&(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator&&(const valarray<type> &v, const type &val);
friend valarray<type> operator&&(const type &val, const valarray<type> &v);
friend valarray<type> operator||(const valarray<type> &v1, const valarray<type> &v2);
friend valarray<type> operator||(const valarray<type> &v, const type &val);
friend valarray<type> operator||(const type &val, const valarray<type> &v);
friend valarray<type> operator!(const valarray<type> &v);
friend type min(const valarray<type> &v);
friend type max(const valarray<type> &v);
friend valarray<type> abs(const valarray<type> &v);
};
template <class type>
inline valarray<type>::valarray():
ptr(0), m_size(0)
{
}
template <class type>
inline valarray<type>::valarray(size_t n):
ptr(new type[n]), m_size(n)
{
}
template <class type>
inline valarray<type>::valarray(const type &val, size_t n):
ptr(new type[n]), m_size(n)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin=val;
++pBegin;
}
}
template <class type>
inline valarray<type>::valarray(const type *p, size_t n):
ptr(new type[n]), m_size(n)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin=*p;
++pBegin;
++p;
}
}
template <class type>
inline valarray<type>::valarray(const valarray &v):
ptr(new type[v.m_size]), m_size(v.m_size)
{
type *pBegin=ptr, *pEnd=ptr+m_size, *pSource=v.ptr;
while(pBegin!=pEnd)
{
*pBegin=*pSource;
++pBegin;
++pSource;
}
}
template <class type>
inline valarray<type>::~valarray()
{
delete [] ptr;
}
template <class type>
inline valarray<type> &valarray<type>::operator=(const valarray &v)
{
type *pBegin=ptr, *pEnd=ptr+m_size, *pSource=v.ptr;
while(pBegin!=pEnd)
{
*pBegin=*pSource;
++pBegin;
++pSource;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin=val;
++pBegin;
}
return *this;
}
template <class type>
inline const type &valarray<type>::operator[](size_t subscript) const
{
return *(ptr+subscript);
}
template <class type>
inline type &valarray<type>::operator[](size_t subscript)
{
return *(ptr+subscript);
}
template <class type>
inline valarray<type> &valarray<type>::operator*=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin*=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator*=(const valarray &v)
{
type *pBegin=ptr, *pEnd=ptr+m_size, *pBegin2=v.ptr;
while(pBegin!=pEnd)
{
*pBegin*=*pBegin2;
++pBegin;
++pBegin2;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator/=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin/=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator%=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin%=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator+=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin+=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator+=(const valarray &v)
{
type *pBegin=ptr, *pEnd=ptr+m_size, *pBegin2=v.ptr;
while(pBegin!=pEnd)
{
*pBegin+=*pBegin2;
++pBegin;
++pBegin2;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator-=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin-=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator-=(const valarray &v)
{
type *pBegin=ptr, *pEnd=ptr+m_size, *pBegin2=v.ptr;
while(pBegin!=pEnd)
{
*pBegin-=*pBegin2;
++pBegin;
++pBegin2;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator^=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin^=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator&=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin&=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator|=(const type &val)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin|=val;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator<<=(size_t n)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin<<=n;
++pBegin;
}
return *this;
}
template <class type>
inline valarray<type> &valarray<type>::operator>>=(size_t n)
{
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin>>=n;
++pBegin;
}
return *this;
}
template <class type>
inline type valarray<type>::sum() const
{
type temp=type(0);
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
temp+=*pBegin;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::shift(int i) const
{
valarray temp(m_size);
type val=type();
size_t j;
if(0<i)
{
for(j=0; j<m_size-i; ++j)
temp[j]=ptr[j+i];
for(; j<m_size; ++j)
temp[j]=val;
}
else
{
for(j=0; j<m_size-i; ++j)
temp[j+i]=ptr[j];
for(j=0; j<i; ++j)
temp[j]=val;
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::cshift(int i) const
{
valarray temp(m_size), subArray(i);
size_t j,k;
if(0<i)
{
for(j=0; j<i; ++j)
subArray[j]=ptr[j];
for(j=0; j<m_size-i; ++j)
temp[j]=ptr[j+i];
for(k=0; j<m_size; ++j,++k)
temp[j]=subArray[k];
}
else
{
for(k=0,j=m_size-i; j<m_size; ++j,++k)
subArray[k]=ptr[j];
for(j=0; j<m_size-i; ++j)
temp[j+i]=ptr[j];
for(j=0; j<i; ++j)
temp[j]=subArray[j];
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::apply(type f(type)) const
{
valarray temp(m_size);
type *pBegin=ptr, *pEnd=ptr+m_size, *pBeginTemp=temp.ptr;
while(pBegin!=pEnd)
{
*pBeginTemp=f(*pBegin);
++pBegin;
++pBeginTemp;
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::apply(type f(const type &)) const
{
valarray temp(m_size);
type *pBegin=ptr, *pEnd=ptr+m_size, *pBeginTemp=temp.ptr;
while(pBegin!=pEnd)
{
*pBeginTemp=f(*pBegin);
++pBegin;
++pBeginTemp;
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::operator-() const
{
valarray temp(m_size);
type *pBegin=ptr, *pEnd=ptr+m_size, *pBeginTemp=temp.ptr;
while(pBegin!=pEnd)
{
*pBeginTemp=-(*pBegin);
++pBegin;
++pBeginTemp;
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::operator+() const
{
valarray temp(m_size);
type *pBegin=ptr, *pEnd=ptr+m_size, *pBeginTemp=temp.ptr;
while(pBegin!=pEnd)
{
*pBeginTemp=+(*pBegin);
++pBegin;
++pBeginTemp;
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::operator~() const
{
valarray temp(m_size);
type *pBegin=ptr, *pEnd=ptr+m_size, *pBeginTemp=temp.ptr;
while(pBegin!=pEnd)
{
*pBeginTemp=~(*pBegin);
++pBegin;
++pBeginTemp;
}
return temp;
}
template <class type>
inline valarray<type> valarray<type>::operator!() const
{
valarray temp(m_size);
type *pBegin=ptr, *pEnd=ptr+m_size, *pBeginTemp=temp.ptr;
while(pBegin!=pEnd)
{
*pBeginTemp=!(*pBegin);
++pBegin;
++pBeginTemp;
}
return temp;
}
template <class type>
inline size_t valarray<type>::size() const
{
return m_size;
}
template <class type>
inline void valarray<type>::resize(size_t n, const type &val)
{
delete [] ptr;
ptr=new type[n];
m_size=n;
type *pBegin=ptr, *pEnd=ptr+m_size;
while(pBegin!=pEnd)
{
*pBegin=val;
++pBegin;
}
}
template <class type>
inline valarray<type> operator*(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin*=*pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator*(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin*=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator*(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin*=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator/(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin/=*pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator/(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin/=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator/(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin/=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator+(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin+=*pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator+(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin+=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator+(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin+=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator-(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin-=*pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator-(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin-=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator-(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin-=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator^(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin^=*pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator^(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin^=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator^(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin^=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator&(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin&=*pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator&(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin&=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator&(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin&=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator|(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin|=*pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator|(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin|=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator|(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin|=val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator<<(const valarray<type> &v, size_t n)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin<<=n;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator>>(const valarray<type> &v, size_t n)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin>>=n;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator&&(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin=*pBegin && *pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator&&(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin=*pBegin && val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator&&(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin=*pBegin && val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator||(const valarray<type> &v1, const valarray<type> &v2)
{
valarray<type> temp(v1);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v2.ptr;
while(pBegin!=pEnd)
{
*pBegin=*pBegin || *pSource;
++pBegin;
++pSource;
}
return temp;
}
template <class type>
inline valarray<type> operator||(const valarray<type> &v, const type &val)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin=*pBegin || val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator||(const type &val, const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin=*pBegin || val;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> operator!(const valarray<type> &v)
{
valarray<type> temp(v);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size;
while(pBegin!=pEnd)
{
*pBegin=!(*pBegin);
++pBegin;
}
return temp;
}
template <class type>
inline type min(const valarray<type> &v)
{
type temp=*ptr;
type *pBegin=v.ptr, *pEnd=v.ptr+v.m_size;
++pBegin;
while(pBegin!=pEnd)
{
if(*pBegin<temp)
temp=*pBegin;
++pBegin;
}
return temp;
}
template <class type>
inline type max(const valarray<type> &v)
{
type temp=*ptr;
type *pBegin=v.ptr, *pEnd=v.ptr+v.m_size;
++pBegin;
while(pBegin!=pEnd)
{
if(*pBegin>temp)
temp=*pBegin;
++pBegin;
}
return temp;
}
template <class type>
inline valarray<type> abs(const valarray<type> &v)
{
valarray<type> temp;
temp.resize(v.m_size);
type *pBegin=temp.ptr, *pEnd=temp.ptr+temp.m_size, *pSource=v.ptr;
while(pBegin!=pEnd)
{
if(*pSource<0)
*pBegin=-*pSource;
else
*pBegin=*pSource;
++pBegin;
++pSource;
}
return temp;
}
}
#endif