Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL functions with 3-way comparison predicate

Is there any library with STL functions like std::sort(), std::binary_search(), std::lower_bound(), std::upper_bound() accepting 3-way comparison predicates (which return -1 on less, 0 on equal, 1 on great) instead of less predicate (true on less, false on equal or great) ?

Of course, the less predicate can be easily made out from existing 3-way predicate (like [](A a, B b) { return compare3(a,b)<0; }) but this results in extra number of calls to the predicate.

like image 224
user222202 Avatar asked Oct 11 '22 23:10

user222202


1 Answers

If you look at the implementation of the above algorithms, you'll see that lower/upper_bound don't do 3-way branches at all, binary_search does only in the last iteration to check equality and about sort() I don't know but I'm almost sure it doesn't do 3-way branches too. So your 'optimization' won't give you any boost. The opposite is true, your comparisons will be slower.

like image 124
Yakov Galka Avatar answered Nov 03 '22 01:11

Yakov Galka