]> granicus.if.org Git - llvm/commit
[ADT] llvm::bsearch, binary search for mere mortals
authorSam McCall <sam.mccall@gmail.com>
Tue, 16 Apr 2019 23:53:28 +0000 (23:53 +0000)
committerSam McCall <sam.mccall@gmail.com>
Tue, 16 Apr 2019 23:53:28 +0000 (23:53 +0000)
commit7d8cf48c8d1bfd526841db01075c802fef61f93e
tree94005351ec64093e685618c0ee14649f60739e1b
parent12d487290a44d81d969421e084cd8b6db13a315c
[ADT] llvm::bsearch, binary search for mere mortals

Summary:
Add to STLExtras a binary search function with a simple mental model:
You provide a range and a predicate which is true above a certain point.
bsearch() tells you that point.
Overloads are provided for integers, iterators, and containers.

This is more suitable than std:: alternatives in many cases:
 - std::binary_search only indicates presence/absence
 - upper_bound/lower_bound give you the opportunity to pick the wrong one
 - all of the options have confusing names and definitions when your predicate
   doesn't have simple "less than" semantics
 - all of the options require iterators
 - we plumb around a useless `value` parameter that should be a lambda capture

The API is inspired by Go's standard library, but we add an extra parameter as
well as some overloads and templates to show how clever C++ is.

Reviewers: ilya-biryukov, gribozavr

Subscribers: dexonsmith, kristina, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D60779

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358540 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/ADT/STLExtras.h
unittests/ADT/STLExtrasTest.cpp