开发者

Multiple predicates specified during runtime

开发者 https://www.devze.com 2023-04-11 04:48 出处:网络
There are operator classes in STL like less, equal_to, greater_equal etc.How to easily comb开发者_如何学运维ine them to use with for example remove_if function?

There are operator classes in STL like less, equal_to, greater_equal etc. How to easily comb开发者_如何学运维ine them to use with for example remove_if function?

For example I want to remove in vector elements which are greater than 0 AND less than 3 AND not equal to 2, then it would be something like:

remove_if (v.begin(), v.end(), bind2nd(greater<int>(),0) + bind2nd(less<int>(),3) + not1(bind2nd(equal_to<int>(), 2)));

User during running of program can specify filtering options for example he can write: remove if x > 0 && x < 3 && x != 2, or he can write: remove if x > 5 || x == 3. Then command is parsed and appropriate operators with their arguments are combined together to one predicate.


This is rather simple in your case, actually.

You first need to parse the statement the user gave, and turn it into an AST (Abstract Syntax Tree). It turns out that this AST is nearly already suitable.

x > 0 && x < 3 && x != 2

Can be expressed as a tree:

      AND
     /   \
    >     AND
   / \   /    \
  x  0   <    !=
        / \   / \
       x   3 x   2

All nodes should inherit from a common base class, and you should implement a Visitor to evaluate the x parameter for a given value.


I guess by "dynamic combining" you refer to the ability to "compose" the predicate "in-line". In your example, the predicate is known at compile-time, so, "dynamic" is not the right term for that.

Consider "Boost.Bind". It offers operator overloading for composition.

bind(greater<int>(),_1,0) && bind(less<int>(),_1,3) && !bind(equal_to<int>(), _1, 2)

Edit: Cosidering your explanation of "dynamic composition", the above code is not what you want. You need an expression parser and evaluator. But this has little to do with your actual code example. There is no easy solution to this. You could try to check out some expression parser libraries and see if / which of them are suitable for your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号