[svn] Switch to binary search for find_tag.

This commit is contained in:
hniksic 2003-10-08 09:00:10 -07:00
parent 72820b092f
commit a9c3c58c9f
2 changed files with 21 additions and 11 deletions

View File

@ -1,3 +1,7 @@
2003-10-08 Hrvoje Niksic <hniksic@xemacs.org>
* html-url.c (find_tag): Switch to binary search.
2003-10-08 Hrvoje Niksic <hniksic@xemacs.org> 2003-10-08 Hrvoje Niksic <hniksic@xemacs.org>
* main.c (print_help): Fix typo; stured -> stored. * main.c (print_help): Fix typo; stured -> stored.

View File

@ -270,30 +270,36 @@ init_interesting (void)
} }
} }
/* Find tag with name TAG_NAME in KNOWN_TAGS and return its index. */
static int static int
find_tag (const char *tag_name) find_tag (const char *tag_name)
{ {
int i; /* Originally implemented as linear search. In Wget 1.9 known_tags
contains 21 elements, for which binary search requires max. 5
comparisons, whereas linear search performs 10 on average. */
/* This is linear search; if the number of tags grow, we can switch int lo = 0, hi = countof (known_tags) - 1;
to binary search. */
for (i = 0; i < countof (known_tags); i++) while (lo <= hi)
{ {
int cmp = strcasecmp (known_tags[i].name, tag_name); int mid = (lo + hi) >> 1;
/* known_tags are sorted alphabetically, so we can int cmp = strcasecmp (tag_name, known_tags[mid].name);
micro-optimize. */ if (cmp < 0)
if (cmp > 0) hi = mid - 1;
break; else if (cmp > 0)
else if (cmp == 0) lo = mid + 1;
return i; else
return mid;
} }
return -1; return -1;
} }
/* Find the value of attribute named NAME in the taginfo TAG. If the /* Find the value of attribute named NAME in the taginfo TAG. If the
attribute is not present, return NULL. If ATTRIND is non-NULL, the attribute is not present, return NULL. If ATTRIND is non-NULL, the
index of the attribute in TAG will be stored there. */ index of the attribute in TAG will be stored there. */
static char * static char *
find_attr (struct taginfo *tag, const char *name, int *attrind) find_attr (struct taginfo *tag, const char *name, int *attrind)
{ {