|
27 | 27 |
|
28 | 28 | /** Binary search.
|
29 | 29 | *
|
30 |
| - * Statement macro. Find the smallest index i in a sorted array such |
31 |
| - * that the i-th entry is at least as large as a specified key. If no |
32 |
| - * such index exists return N, the size of the array. |
| 30 | + * Statement macro. Find the smallest index i in an ascending sorted |
| 31 | + * array a such that a[i] >= key, i.e., the i-th entry is at least as |
| 32 | + * large as a specified key. If no such index exists, i.e., a[i] < key |
| 33 | + * for all i, return N, the size of the array. |
33 | 34 | *
|
34 | 35 | * @param itype
|
35 | 36 | * integral type used for the return value. itype is also
|
|
50 | 51 | * lvalue of type itype to store the result index.
|
51 | 52 | *
|
52 | 53 | * @note By default the csnip_Bsearch function finds the
|
53 |
| - * lowest index i such that a[i] >= key. To find |
54 |
| - * the lowest index such that a[i] > key, the |
55 |
| - * au_lessthan_key expression can be set to a |
56 |
| - * less-than-or-equal expression. |
| 54 | + * lowest index i such that a[i] >= key. The following |
| 55 | + * alternative uses are possible: |
| 56 | + * |
| 57 | + * - To find the lowest index i such that a[i] > key, the |
| 58 | + * au_lessthan_key expression can be set to a |
| 59 | + * less-than-or-equal expression. |
| 60 | + * |
| 61 | + * - To find the largest index such that a[i] <= key, set |
| 62 | + * the au_lessthan_key expression to a |
| 63 | + * less-than-or-equal, and subtract 1 from the result. |
| 64 | + * Returns (itype)-1 if no such index exists. |
| 65 | + * |
| 66 | + * - To find the largest index i such that a[i] < key, set |
| 67 | + * the au_lessthan_key expression to a less-than |
| 68 | + * expression, and subtract 1 from the result. The |
| 69 | + * return value (itype)-1 means that no such index |
| 70 | + * exists. |
| 71 | + * |
| 72 | + * If the array is sorted in a descending manner, the |
| 73 | + * comparation expression can be reverted. |
57 | 74 | */
|
58 | 75 | #define csnip_Bsearch(itype, u, au_lessthan_key, N, ret) \
|
59 | 76 | csnip__Bsearch(itype, u, au_lessthan_key, (N), (ret), \
|
|
65 | 82 | do { \
|
66 | 83 | /* loop invariants:
|
67 | 84 | * a[0], ..., a[lo - 1] < key;
|
68 |
| - * a[hi], ..., a[N - 1] >= key; |
| 85 | + * a[hi], ..., a[N - 1] >= key. |
69 | 86 | */ \
|
70 | 87 | itype lo = 0; \
|
71 | 88 | itype hi = N; \
|
|
0 commit comments