]> sjero.net Git - wget/commitdiff
[svn] Use a marginally faster implementation of binary search.
authorhniksic <devnull@localhost>
Sun, 14 Apr 2002 18:39:43 +0000 (11:39 -0700)
committerhniksic <devnull@localhost>
Sun, 14 Apr 2002 18:39:43 +0000 (11:39 -0700)
Published in <sxs662uf7l9.fsf@florida.arsdigita.de>.

src/ChangeLog
src/init.c

index ec98aaf04d432cfc749f117fd65eac6eda0f38e3..09d4bc700dd8556c22c5b79d0904f99ba1a52188 100644 (file)
@@ -1,3 +1,9 @@
+2002-04-14  Hrvoje Niksic  <hniksic@arsdigita.com>
+
+       * init.c (comind): Use a marginally faster implementation of
+       binary search.  To quote Martin Buchholz, "a nanosecond saved is a
+       nanosecond earned."
+
 2002-04-14  Hrvoje Niksic  <hniksic@arsdigita.com>
 
        * main.c (print_help): Document `--post-data' and `--post-file'.
index 0a5bc633d70befb4668dfa2d2e2d2c89304e73dc..e860b6717531e0d4d5292e944841e708146904ce 100644 (file)
@@ -194,25 +194,25 @@ static struct {
   { "waitretry",       &opt.waitretry,         cmd_time }
 };
 
-/* Return index of COM if it is a valid command, or -1 otherwise.  COM
-   is looked up in `commands' using binary search algorithm.  */
+/* Look up COM in the commands[] array and return its index.  If COM
+   is not found, -1 is returned.  This function uses binary search.  */
+
 static int
 comind (const char *com)
 {
-  int min = 0, max = ARRAY_SIZE (commands) - 1;
+  int lo = 0, hi = ARRAY_SIZE (commands) - 1;
 
-  do
+  while (lo <= hi)
     {
-      int i = (min + max) / 2;
-      int cmp = strcasecmp (com, commands[i].name);
-      if (cmp == 0)
-       return i;
-      else if (cmp < 0)
-       max = i - 1;
+      int mid = (lo + hi) >> 1;
+      int cmp = strcasecmp (com, commands[mid].name);
+      if (cmp < 0)
+       hi = mid - 1;
+      else if (cmp > 0)
+       lo = mid + 1;
       else
-       min = i + 1;
+       return mid;
     }
-  while (min <= max);
   return -1;
 }
 \f