]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/doc/binsearch.txt
Merge branch 'v3.12.4'
[libucw.git] / ucw / doc / binsearch.txt
index 6cd45fd2493e7906902f5f68fc2bc14554a70200..211bd735ad1732c4b4da53a729cade1ade678211 100644 (file)
@@ -3,8 +3,6 @@ Binary search
 
 * <<defs,Definitions>>
 * <<examples,Examples>>
 
 * <<defs,Definitions>>
 * <<examples,Examples>>
-  - <<ex_numbers,Numbers>>
-  - <<ex_strings,Strings>>
 
 !!ucw/binsearch.h
 
 
 !!ucw/binsearch.h
 
@@ -16,11 +14,8 @@ You can find few examples of binary search usage. Although we define only few ma
 for several different cases, for example to find lower elements in a (non-)decreasing array or even to find 
 elements in a (non-)increasing array.
 
 for several different cases, for example to find lower elements in a (non-)decreasing array or even to find 
 elements in a (non-)increasing array.
 
-[[ex_num]]
-Numbers
-~~~~~~~
-
   static int inc[10] = { 1, 4, 4, 5, 6, 10, 11, 20, 25, 50 };
   static int inc[10] = { 1, 4, 4, 5, 6, 10, 11, 20, 25, 50 };
+  static const char *str[5] = { "aaa", "abc", "bflmpsvz", "rep", "rep" };
   static int dec[3] = { 5, 2, 1 };
 
   // find the first equal element
   static int dec[3] = { 5, 2, 1 };
 
   // find the first equal element
@@ -33,28 +28,24 @@ Numbers
   printf("%d\n", BIN_SEARCH_GE(inc, 10, 4));                           // prints 1
   printf("%d\n", BIN_SEARCH_GE(inc, 10, 99));                          // prints 10 (not found)
 
   printf("%d\n", BIN_SEARCH_GE(inc, 10, 4));                           // prints 1
   printf("%d\n", BIN_SEARCH_GE(inc, 10, 99));                          // prints 10 (not found)
 
-  // find the first greater element
+  // find the last equal element (or -1 if does not exist)
   #define CMP_LE(ary, i, x) ((ary[i]) <= (x))
   #define CMP_LE(ary, i, x) ((ary[i]) <= (x))
+  int i = BIN_SEARCH_FIRST_GE_CMP(inc, 10, 4, CMP_LE);
+  printf("%d\n", (i && inc[i - 1] == 4) ? i - 1 : -1);                 // prints 2
+
+  // find the first greater element
   printf("%d\n", BIN_SEARCH_FIRST_GE_CMP(inc, 10, 25, CMP_LE));                // prints 9
 
   printf("%d\n", BIN_SEARCH_FIRST_GE_CMP(inc, 10, 25, CMP_LE));                // prints 9
 
-  // find the last lower or equal element (or -1 if not found)
+  // find the last lower or equal element (or -1 if does not exist)
   printf("%d\n", BIN_SEARCH_FIRST_GE_CMP(inc, 10, 25, CMP_LE) - 1);    // prints 8
 
   printf("%d\n", BIN_SEARCH_FIRST_GE_CMP(inc, 10, 25, CMP_LE) - 1);    // prints 8
 
-  // find the last lower element (or -1 if not found)
+  // find the last lower element (or -1 if does not exist)
   printf("%d\n", BIN_SEARCH_FIRST_GE(inc, 10, 25) - 1);                        // prints 7
 
   printf("%d\n", BIN_SEARCH_FIRST_GE(inc, 10, 25) - 1);                        // prints 7
 
+  // find the first greater or equal string
+  #define CMP_STR(ary, i, x) (strcmp((ary[i]), (x)) < 0)
+  printf("%d\n", BIN_SEARCH_GE_CMP(str, 5, "bfl", CMP_STR));            // prints 2
+
   // find the first lower or equal element in the non-increasing array
   #define CMP_GT(ary, i, x) ((ary[i]) > (x))
   printf("%d\n", BIN_SEARCH_FIRST_GE_CMP(dec, 3, 4, CMP_GT));          // prints 1
   // find the first lower or equal element in the non-increasing array
   #define CMP_GT(ary, i, x) ((ary[i]) > (x))
   printf("%d\n", BIN_SEARCH_FIRST_GE_CMP(dec, 3, 4, CMP_GT));          // prints 1
-
-  // ...
-
-[[ex_str]]
-Strings
-~~~~~~~
-
-  static char *str[5] = { "aaa", "abc", "bflmpsvz", "rep", "rep" };
-
-  #define CMP_STR(ary, i, x) (strcmp((ary[i]), (x)) < 0)
-
-  printf("%d\n", BIN_SEARCH_GE_CMP(str, 5, "bfl", CMP_STR));  // prints 2