Last active
November 12, 2022 21:30
-
-
Save slode/5ce2a6eb9be1b185b584d2b7f3b94422 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT License | |
Copyright (c) 2017 Stian Lode, | |
[email protected] | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <stdint.h> | |
#include <vector> | |
#include <cassert> | |
#ifdef _MSC_VER | |
#include <intrin.h> | |
#define FORCEINLINE __forceinline | |
FORCEINLINE uint32_t bsr(uint32_t x) { | |
unsigned long res; | |
_BitScanReverse(&res, x); | |
return res; | |
} | |
#else | |
#define FORCEINLINE __attribute__((always_inline)) inline | |
FORCEINLINE uint32_t bsr(uint32_t x) { | |
return 31 - __builtin_clz(x); | |
} | |
#endif | |
/**** | |
* Binary search algorithm based on ideas from a blog post on binary search | |
* at https://github.com/stgatilov/linear-vs-binary-search. The algorithm | |
* should behave like binary search implemented using std::lower_bound() but | |
* slightly faster, according to my measurements. | |
* | |
* Let me know if you found it useful at [email protected]. | |
*/ | |
int binary_search(const std::vector<int> &arr, const int key) { | |
const unsigned n = arr.size(); | |
if (n < 2) return 0; | |
if (arr[n-1] < key) return n; | |
unsigned step = 1 << bsr(n-1); | |
int pos = arr[step - 1] < key ? n - step - 1 : -1; | |
while ((step >>= 1) > 0) { | |
pos = (arr[pos + step] < key ? pos + step : pos); | |
} | |
return pos + 1; | |
} | |
int main(int argc, char **argv) { | |
std::vector<int> vec; | |
for (int i = 0; i < 10; i++) { | |
vec.push_back(i); | |
vec.push_back(i); | |
vec.push_back(i); | |
} | |
assert(binary_search(vec, -1) == 0); | |
assert(binary_search(vec, 0) == 0); | |
assert(binary_search(vec, 3) == 9); | |
assert(binary_search(vec, 7) == 21); | |
assert(binary_search(vec, 10) == 30); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment