Boost logo

Boost Users :

Subject: Re: [Boost-users] boost lambda for processing std::vector
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-11-29 21:47:49


On Sun, Nov 29, 2009 at 7:14 PM, CR Koudella <ckoudell_at_[hidden]> wrote:
>
> thanks, I have started to look at the doc, but I am not there yet.
>
> imagine we have something like this, without too much thought and excuse
> the basic style laid out here:
>
> struct XMapVec
> {
> public:
> XMapVec(const std::vector<double> & vY, const std::vector<unsigned> & vMap)
> : _vY(vY), _vMap(vMap), _vBuffer(vMap)
> {
> }
>
> std::vector<double> operator()(const std::vector<double> & vX) const
> {
> unsigned nCnt = 0
> for (int nI = 0; nI < _vBuffer.size(); ++nI)
> {
>     if (_vMap[nI])
>     {
>         _vBuffer[nI] = vX[nCnt];
>         ++nCnt;
>     }
> }
>
> return _vBuffer;
> }
> private:
>
> // assume _vY same size as _vMap
> // the state
> std::vector<double> _vY;
> // binary Map: if element = 1, then update corresponding entry in _vBuffer
> std::vector<unsigned> _vMap;
> // set initial state at construction and mutate relevant elements on () call
> mutable std::vector<double> _vBuffer;
> };
>
> this is not nicely written, but more importantly it is not nice to have this
> in
> a dedicated functor. the question is, how to rewrite this in terms of a
> lambda
> expression, which can then be used in a function composition operation?

Looks like an accumulator...
Well a completely lazy version of that could be (using both phoenix and fusion):

std::vector<double> vY;
std::vector<unsigned> vMap;
std::vector<double> vBuffer;

// Assuming vY is the same size as vMap and vBuffer
auto XMapVec =
        let(_a=begin(vY),_b=begin(vMap),_c=begin(vBuffer),_d=end(vY),_e=(unsigned)0)
        [
                for_(nothing,_a!=_d,(++_a,++_b,++_c))
                [
                        if_(*_b)
                        [
                                (*_c) = _1(_e),
                                ++_e
                        ]
                ]
        ];

This is not nicely written either, but demonstrates how it can be used anyway.

This is still completely untested, I am still at work.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net