Hello.
I was looking for a library to simplify building of simple and complex
predicates for sorting.
For example sort structure like this by member or by several members in
specific order.
struct data
{
int a; int b; int c;
int get_b() const { return b; }
};
And have std::vector<data> v to sort.
Boost.Bind or Boost.Lambda can simplify sorting by one member, like this:
std::sort(v.begin(), v.end(), boost::bind(&data::b, _1) <
boost::bind(&data::b, _2));
But not sorting by several ones, there's no easy way to concatenate sorting
predicates for usability in sorting.
For example, this is illegal:
std::sort(v.begin(), v.end(),
(boost::bind(&data::b, _1) < boost::bind(&data::b, _2))
|| (boost::bind(&data::a, _1) < boost::bind(&data::a, _2)));
, because both predicate(_1, _2) and predicate(_2, _1) can be true then.
So I come to solution to this problem. Sorting by a, then by b and finally
by c can look like:
std::sort(v.begin(), v.end(),
by(get_a) || by(&data::get_b) || by(&data::c));
where get_b is member function, c is data member, and get_a is a free
function defined like this:
int get_a(const data& d)
{
return d.a;
}
"by" function can also take predicate, like std::greater as its second
parameter.
The code with "by" function and a few more examples is attached.
Is there already another solution to this problem which I have missed ?
If not, is my code useful for someone else ?
begin 666 test.cpp
M(VEN8VQU9&4@/&%L9V]R:71H;3X-"B-I;F-L=61E(#QV96-T;W(^#0HC:6YC
M;'5D92 \PT*("!S=&0Z
M.G9E8W1OW)A;F0H*2 E(#,L(')A;F0H*2 E
M(#,L(')A;F0H*2 E(#-].PT*(" @('8N<'5S:%]B86-K*&0I.PT*("!]#0H-
M"B @2@F9&%T83HZ8RDI.PT*("!S=&0Z.G-O2@F9&%T83HZ9V5T7V(I*3L-"@T*(" O
M+R!3;W)T(&)Y('-E=F5R86P@9FEE;&1S+"!I;B!T:&4@2AG971?82D@?'P@8GDH)F1A=&$Z.F=E=%]B*2!\?"!B>2@F9&%T83HZ8RDI
M.PT*#0H@("\O(%-O2@F
M9&%T83HZ9V5T7V(L('-T9#HZ9W)E871E2P@9F]R(&%N(&%S'1R82!L979E;"!O9B!I;F1IPT*(" @(')E='5R;B!I;7!L7RAL:',L(')HPT*(" @(&1A=&$@9" ]('MR86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"!0('!R960I#0I[
M#0H@(')E='5R;B!D971A:6PZ.F)Y7VUE;5]F;E]PPT*
M("!R971U5]F=6YC=&EO;E]PF%T:6]N(&UE86YS('1H870@='EP92!O9B!4('=I
M;&P@8F4@9&5D=6-E9"!I;B!P;&%C92!O9B H*2!C86QL+@T*("!T96UP;&%T
M93QC;&%SPT*
M(" @('T-"@T*(" @('1E;7!L871E/'1Y<&5N86UE(%0^#0H@(" @8F]O;"!O
M<&5R871OPT*#0IN86UEPT*(" O+R!&;W)W87)D(&1E8VQA5]M96UB
M97(\5"P@53X@/@T*("![#0H@('!R:79A=&4Z#0H@(" @52!4.CHJ(&U?.PT*
M("!P=6)L:6,Z#0H@(" @97AP;&EC:70@8GE?;65M8F5R*%4@5#HZ*FTI#0H@
M(" @(" Z(&U?*&TI#0H@(" @>PT*(" @('T-"B @("!B;V]L(&]P97)A=&]R
M*"DH8V]NPT*
M(" @(" @2!C;VYS="!M
M96UB97(@9G5N8W1I;VXL("@I('1A:V5S('1W;R!4)W,@8GD@8V]N5]M
M96U?9FY?8PT*(" @(#H@<'5B;&EC(&]R9&5R7V)APT*("!P'!L:6-I="!B
M>5]M96U?9FXH32!M*0T*(" @(" @.B!M7RAM*0T*(" @(" @+"!P71H:6YG(&-A;&QA8FQE+@T*("!T
M96UP;&%T93QT>7!E;F%M92!&/@T*("!C;&%S5]F=6YC=&EO;@T*(" @
M(#H@<'5B;&EC(&]R9&5R7V)A5]M96UB97)?<'(-"B @(" Z('!U8FQI8R!OPT*(" @('T-"B @("!B;V]L
M(&]P97)A=&]R*"DH8V]NPT*(" @(" @2!C;VYS="!M96UB97(@9G5N8W1I;VXL("@I('1A:V5S('1W;R!4
M)W,@8GD@8V]N5]M96U?9FY?8U]P<@T*(" @(#H@
M<'5B;&EC(&]R9&5R7V)APT*(" @
M('T-"B @("!B;V]L(&]P97)A=&]R*"DH8V]NPT*(" @(" @5]M96U?
M9FY?8R!C;&%SPT*("!PPT*(" @(" @5]M96U?9FY?<'(@
M8VQA71H:6YG(&-A;&QA8FQE+@T*
M("!T96UP;&%T93QT>7!E;F%M92!&+"!C;&%S5]F=6YC=&EO;E]P<@T*(" @(#H@<'5B;&EC(&]R9&5R7V)APT*(" @('T-"B @("!T96UP;&%T
M93QT>7!E;F%M92!4/@T*(" @(&)O;VP@;W!EPT*("!P'!L:6-I="!B>5]PPT*(" @(" @