mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-10-27 03:16:44 +00:00
Initial import
This commit is contained in:
commit
c91b3c5006
14915 changed files with 590219 additions and 0 deletions
31
libc/alg/bisectcarleft.h
Normal file
31
libc/alg/bisectcarleft.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_
|
||||
#define COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
/**
|
||||
* Floor binary search of low 32-bits of 64-bit array items.
|
||||
*
|
||||
* This is well-suited to NexGen-32e, requiring less than 32 bytes of
|
||||
* code. It's particularly useful for frozen maps, requiring less effort
|
||||
* and memory than a perfect hash table.
|
||||
*/
|
||||
forceinline int32_t bisectcarleft(const int32_t (*cons)[2], size_t count,
|
||||
const int32_t key) {
|
||||
size_t left = 0;
|
||||
size_t right = count;
|
||||
while (left < right) {
|
||||
size_t m = (left + right) >> 1;
|
||||
if (cons[m][0] < key) {
|
||||
left = m + 1;
|
||||
} else {
|
||||
right = m;
|
||||
}
|
||||
}
|
||||
if (left && (left == count || cons[left][0] > key)) left--;
|
||||
return left;
|
||||
}
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue