next up previous
Next: Hiding Templates Up: C++ Design For Research Previous: Examples (AppModel Library)

Iterators

class TC_VectorDataBase {
public:
    virtual ~TC_VectorDataBase();

    virtual int nSamples() const =0;
    virtual int nDims()	const =0;
    virtual const RD_Vector& first() =0;
    virtual const RD_Vector& current() =0;
    virtual const RD_Vector& next() = 0;

    ...
};

// Example:
int n = data->nSamples();
RD_Vector sum = data->first();
for (int i=1;i<n;i++)
    sum += data->next();
RD_Vector mean = sum/n;

Processing data on the fly:

class TC_NormalisedVectorData : public TC_VectorDataBase {
public:
    TC_NormalisedVectorData(const TC_VectorDataBase& data,
                     const TC_VTransformBase& normaliser);
    TC_VectorDataBase& data();
    TC_VTransformBase& normaliser();

    virtual int nSamples() const { return data().nSamples(); }
    virtual int nDims()	const { return data().nDims(); }

    virtual const RD_Vector& first()
       { _v = data().first(); 
         normaliser().normalise(_v); 
         return _v; }
    ...
};



Tim Cootes
2/24/1998