一个简单的实现matlab数组下标操作的类实现

来源:互联网 发布:php权限修饰符 编辑:程序博客网 时间:2024/05/22 07:49

(sguox002原创作品,欢迎批评,转载请保留作者信息)

#include <iostream>

using namespace std;
template <typename T> class ind_array;
template <typename T> class array
{
public:
    int len; //two dimensional at most
    T *pdata;
    //construct the array
    array(){len=0;pdata=NULL;} //default is empty matrix
    //array(T a){len=1;pdata=new T[1];*pdata=a;} //array for scalar: array a=10;
    array(int m0) {len=m0;pdata=new T[len];}
    array(const array& a,int len0=-1);
    //destructor
    ~array() {delete []pdata;}
    int size() const {return len;}
    //operator overloading
    array<T>& operator+=(T s);
    T& operator[](int i) {return pdata[i];}
    ind_array<T> operator[](const array<int>& ind);//{return (ind_array(ind,pdata));}
    array<T>& operator=(const array<T>& a);
    array<T>& operator=(T a) {for(int i=0;i<len;i++) pdata[i]=a;return *this;}
    array<bool> operator>(T a);
    array<bool> operator<(T a);
    array<bool> operator==(T a);
};


//Index array or similar indirect-array as in valarray
//this class shall keeps the array's address and the index
template <typename T> class ind_array
{
    array<int> ind; //an index array
    T* ptr; //a pointer to the original data
public:
    int size() const {return ind.size();} 
    void operator=(T a){for(int i=0;i<size();i++) ptr[ind[i]]=a;} //assignment a value to a subarray
    //how to construct the indx array then?
    //according to valarry, the default constructor shall be prohibited
    ind_array(const array<int>& indx,T* pt):ind(indx),ptr(pt){} //default constructor
};


//copy a part of the other array
template <typename T> array<T>::array<T>(const array<T>& a,int len0)
{
    if(len0==-1) len0=a.len;
    if(len0==0) {len=0;pdata=NULL;}
    if(len0>0)
    {
        len=len0;
        pdata=new T[len];
        for(int i=0;i<len;i++) pdata[i]=a.pdata[i];
    }
}


template <typename T> array<T>& array<T>::operator +=(T s)
{
    for(int i=0;i<len;i++) pdata[i]+=s;
    return *this;
}


//this function does not meet the purpose, it returns a reference to a temp obj
//now we change it to return a indx_array which stores the original array's address
template <typename T> ind_array<T> array<T>::operator[](const array<int>& ind)
{
    return (ind_array<T>(ind,pdata)); //call the constructor
}


template <typename T> array<bool> array<T>::operator>(T a)
{
    array<bool> res(len);
    for(int i=0;i<len;i++) res.pdata[i]=pdata[i]>a;
    return res;
}


//helper function
array<int> find(array<bool> a)
{
    array<int> ret(a.len); //first use the same size space
    int len=0;
    for(int i=0;i<a.len;i++)
    {
        if(a.pdata[i]) {ret.pdata[len]=i;len++;}
    }
    return array<int>(ret,len);
}


int main()
{
    array<float> a(10);
    for(int i=0;i<10;i++) a[i]=i;
    for(i=0;i<10;i++) cout<<a[i]<<'\t';
    cout<<endl;
    array<int> ind=find(a>5);
    for(i=0;i<ind.len;i++) cout<<ind[i]<<'\t';
    cout<<endl;
array<double> b(10);
    a[ind]=5;//this will not work on the original array
b[ind]=10.0;
    //how do we support this????undefined
    for(i=0;i<10;i++) cout<<a[i]<<'\t';
    cout<<endl;
    for(i=0;i<10;i++) cout<<b[i]<<'\t';
    cout<<endl;
    return 0;

}


一个数组模板类用于构造可变长度的数组,可以使用简单的条件取得下标,对下标组成的数组进行操作。这个展示了C++强大的自由表述功能。

实现a[ind]=b这个操作时有点技巧的。a[ind]我们返回一个下标数组对象,该对象存储了下标以及数组的指针。这样operator=就变成了ind_array的成员操作。类似的概念在std::valarray中广泛得到应用。

这个还有许多可以提高的地方。比如逻辑表达式还不能连写,比如find(a>10 && a<20)等等。

原创粉丝点击