1
0
mirror of https://github.com/moparisthebest/wget synced 2024-07-03 16:38:41 -04:00

[svn] Use a marginally faster implementation of binary search.

Published in <sxs662uf7l9.fsf@florida.arsdigita.de>.
This commit is contained in:
hniksic 2002-04-14 11:39:43 -07:00
parent 0eee8f8b25
commit 081e4eb4f4
2 changed files with 18 additions and 12 deletions

View 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'.

View 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;
}