performs binary search, returns the leftmost index of a match
| 32 | |
| 33 | // performs binary search, returns the leftmost index of a match |
| 34 | static bool _binarySearchLeftmost |
| 35 | ( |
| 36 | uint *idx, |
| 37 | Record *array, |
| 38 | uint array_len, |
| 39 | int join_key_idx, |
| 40 | SIValue v |
| 41 | ) { |
| 42 | ASSERT(idx != NULL); |
| 43 | |
| 44 | SIValue x; |
| 45 | uint pos = 0; |
| 46 | uint left = 0; |
| 47 | uint right = array_len; |
| 48 | |
| 49 | while(left < right) { |
| 50 | pos = (right + left) / 2; |
| 51 | x = Record_Get(array[pos], join_key_idx); |
| 52 | if(SIValue_Compare(x, v, NULL) < 0) left = pos + 1; |
| 53 | else right = pos; |
| 54 | } |
| 55 | |
| 56 | // make sure value was found |
| 57 | *idx = left; |
| 58 | |
| 59 | if(left == array_len) return false; |
| 60 | |
| 61 | x = Record_Get(array[*idx], join_key_idx); |
| 62 | // return false if the value wasn't found or evaluated to NULL |
| 63 | int disjointOrNull = 0; |
| 64 | return (SIValue_Compare(x, v, &disjointOrNull) == 0 && |
| 65 | disjointOrNull != COMPARED_NULL); |
| 66 | } |
| 67 | |
| 68 | // performs binary search, returns the rightmost index of a match |
| 69 | // assuming 'v' exists in 'array' |
no test coverage detected