Skip to content

Commit dd00a76

Browse files
committed
search: Improve Bsearch documentation.
The documentation did not clearly explain how to use the macro to search for slightly modified boundary conditions.
1 parent e4ad7a3 commit dd00a76

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/csnip/search.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727

2828
/** Binary search.
2929
*
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.
3334
*
3435
* @param itype
3536
* integral type used for the return value. itype is also
@@ -50,10 +51,26 @@
5051
* lvalue of type itype to store the result index.
5152
*
5253
* @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.
5774
*/
5875
#define csnip_Bsearch(itype, u, au_lessthan_key, N, ret) \
5976
csnip__Bsearch(itype, u, au_lessthan_key, (N), (ret), \
@@ -65,7 +82,7 @@
6582
do { \
6683
/* loop invariants:
6784
* a[0], ..., a[lo - 1] < key;
68-
* a[hi], ..., a[N - 1] >= key;
85+
* a[hi], ..., a[N - 1] >= key.
6986
*/ \
7087
itype lo = 0; \
7188
itype hi = N; \

0 commit comments

Comments
 (0)