Boost logo

Boost :

From: Pavel Chikulaev (pavel.chikulaev_at_[hidden])
Date: 2005-03-07 10:39:40


This text not as silly as my "Herb Sutter's operator = can be
optimized", so please read it.

I'm currently working on library "Lazy" which eases creation
of classes which needs lazy evalution and n-arity operators to
improve speed.

The library's sample usage placed below.
Please let me know what do you think about it. I'll appreciate
any feedback on my library.

Library description:
    Library eases creation of classes which needs lazy evalution
and n-arity operators.

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.

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 &);

    Matrix & operator = (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)

    //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_);

    //operator 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 at all
};

//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.

I believe that my library should be very useful with big_int.

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

Thanks in advance.

--
Pavel Chikulaev

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