Boost logo

Boost :

From: Pavel Chikulaev (pavel.chikulaev_at_[hidden])
Date: 2005-03-06 09:59:01


I'm currently working on library "Lazy".

Library description:
    Library eases creation of classes which needs lazy evalution and n-arity
operators
    and I guess will be as efficient as Gnu MP class but with pretty syntax.
(Not yet tested
    but I do believe it is so).

Library features are:
    * n-arity operators support;
    * operator aliasing, such as a + -b will be computed as a - b;
    * custom operators;
    * no unnecessary temporaries during evalution and assigning;
    * easy usage.

Library's sample usage:

//Matrix.h

#include "lazy.hpp"

class transpose_tag {}; //Tag class

class Matrix
{
    LAZY_CLASS(Matrix)
    static lazy::type<Matrix> Matrix_; //Similar to Andrei Alexandrescu's
Type2Type template
public:
    Matrix();
    Matrix(const Matrix &);
    void swap(void Matrix &);
    //Libray won't work without swap function.
    //If name of your swap function isn't swap then use
LAZY_CLASS_EX(Matrix, YourSwapFunctionName)
    Matrix & operator = (const Matrix &);

    //lazy constuctors which enables lazy operators
    Matrix(LAZY_OP(Matix_ + Matrix_)); //Matrix operator +(Matrix, Matrix)
    Matrix(LAZY_OP(Matrix_ * Matrix_)); //...
    Matrix(LAZY_OP(-Matrix_);
    Matrix(LAZY_OP(Matrix_ + Matrix_ * Matrix_)); //ternary operator

    //Now enable operators
    LAZY_ENABLE_OP(Matrix_ = Matrix_ + Matrix_);
    LAZY_ENABLE_OP(Matrix_ = Matrix_ * Matrix_);
    LAZY_ENABLE_OP(Matrix_ = -Matrix_);
    LAZY_ENABLE_OP(Matrix_ = Matrix_ + Matrix_ * Matrix_);

    //Actually we can make declaration of lazy operators and enabling them
using only one macro e.g.
    LAZY_CONSTUCTOR(Matrix_ = Matrix_ * Matrix_ * Matrix_);

    //Aliasing
    LAZY_ALIAS(Matrix + -Matrix_ = Matrix_ - Matrix_);

    //Custom operation
    Matrix(LAZY_CUSTOM_OP(transpose_tag, (Matrix_)));
    LAZY_ENABLE_CUSTOM_OP(transpose, Matrix_ =
lazy::custom_op<transpose_tag>(Matrix_));
    //same as Matrix transpose (const Matrix &) but there won't be any
temporary objects
};

//Matrix.cpp

Matrix::Matrix(LAZY_OP(Matix_ + Matrix_) op)
{
    //Construct matrix object
    //lhs matrix = op._1
    //rhs matrix = op._2
}

Matrix::Matrix(LAZY_CUSTOM_OP(transpose_tag, (Matrix_) op)
{
    //Construct matrix object tranposed to op._1
}
//etc

//Now the use of Matrix class

Matrix a, b, c, d;
    //...
a = b + c;
    //No tempories at all
    //Using normal practice it will take two temporary objects (no NRVO or
RVO )
a = b + -c;
    //Will be evaluates as a = b - c; No temporaries again
    //Using normal practice it will take three temporary objects (no NRVO or
RVO)
a = b + c * d;
    //Will use ternary operator. No temporaries again
a = transpose(b);
    //Custom lazy operation. No tempories again

Maybe the library is very similar to uBLAS's matrix_expression and
vector_expression.
But with my approach there is no need to code matrix_expression and
vector_expression
at all and it is easy to create new classes which needs lazy evaluation and
n-arity operators.

So what do you think?
Is there any similar approaches available?
Is there any need of such library?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk