Boost logo

Proto :

Subject: Re: [proto] restructuring expression
From: Eric Niebler (eric_at_[hidden])
Date: 2012-05-30 12:26:17


On 5/30/2012 4:33 AM, Joel Falcou wrote:
> On 05/29/2012 08:21 PM, Eric Niebler wrote:
>> On 5/29/2012 1:44 AM, Karsten Ahnert wrote:
>>> I have an arithmetic expression template where multiplication is
>>> commutative. Is there an easy way to order a chain of multiplications
>>> such that terminals with values (like proto::terminal< double >) appear
>>> at the beginning? For example that
>>>
>>> arg1 * arg1 * 1.5 * arg1
>>>
>>> will be transformed to
>>>
>>> 1.5 * arg1 * arg1 * arg1
>>>
>>> ?
>>>
>>> I can imagine some complicated algorithms swapping expressions and child
>>> expressions but I wonder if there is a simpler way.
>> There is no clever built-in Proto algorithm for commutative
>> transformations like this, I'm afraid. I was going to suggest flattening
>> to a fusion vector and using fusion sort, but I see there is no fusion
>> sort! :-( Nevertheless, that seems like a promising direction to me.
>> Once you have the sorted vector, you should(?) be able to use
>> fusion::fold to build the correct proto tree from it.
>>
>
> Won't having a way to build it properly from the get go be a better
> solution ?
>
> This basically require the feature we spoke about earlier so that
> building a X * Y
> node check which from X or Y is a double and put it in the proper place ?
>
> Then when doing X * Expr, check if there is a double at child<0> of expr
> and restructure the whole tree at generation time ?

That's not a bad suggestion. You can do this today with a custom generator.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Proto list run by eric at boostpro.com