diff --git a/tagmanager/src/tm_source_file.c b/tagmanager/src/tm_source_file.c index a6861626..d5faaf5b 100644 --- a/tagmanager/src/tm_source_file.c +++ b/tagmanager/src/tm_source_file.c @@ -118,8 +118,7 @@ static int tm_source_file_tags(const tagEntryInfo *tag) /* Set the argument list of tag identified by its name */ static void tm_source_file_set_tag_arglist(const char *tag_name, const char *arglist) { - guint count; - TMTag **tags, *tag; + guint i; if (NULL == arglist || NULL == tag_name || @@ -128,13 +127,16 @@ static void tm_source_file_set_tag_arglist(const char *tag_name, const char *arg return; } - tags = tm_tags_find(current_source_file->tags_array, tag_name, FALSE, FALSE, - &count); - if (tags != NULL && count == 1) + /* going in reverse order because the tag was added recently */ + for (i = current_source_file->tags_array->len; i > 0; i--) { - tag = tags[0]; - g_free(tag->arglist); - tag->arglist = g_strdup(arglist); + TMTag *tag = (TMTag *) current_source_file->tags_array->pdata[i - 1]; + if (g_strcmp0(tag->name, tag_name) == 0) + { + g_free(tag->arglist); + tag->arglist = g_strdup(arglist); + break; + } } } diff --git a/tagmanager/src/tm_tag.c b/tagmanager/src/tm_tag.c index 9afc845f..778d052a 100644 --- a/tagmanager/src/tm_tag.c +++ b/tagmanager/src/tm_tag.c @@ -862,7 +862,7 @@ void tm_tags_remove_file_tags(TMSourceFile *source_file, GPtrArray *tags_array) TMTag **found; TMTag *tag = source_file->tags_array->pdata[i]; - found = tm_tags_find(tags_array, tag->name, FALSE, TRUE, &tag_count); + found = tm_tags_find(tags_array, tag->name, FALSE, &tag_count); for (j = 0; j < tag_count; j++) { @@ -1083,42 +1083,16 @@ static gpointer binary_search(gpointer key, gpointer base, size_t nmemb, return NULL; } -static TMTag **tags_search(const GPtrArray *tags_array, TMTag *tag, - gboolean tags_array_sorted, TMSortOptions *sort_options) -{ - if (tags_array_sorted) - { /* fast binary search on sorted tags array */ - return (TMTag **) binary_search(&tag, tags_array->pdata, tags_array->len, - tm_tag_compare, sort_options); - } - else - { /* the slow way: linear search (to make it a bit faster, search reverse assuming - * that the tag to search was added recently) */ - guint i; - TMTag **t; - for (i = tags_array->len; i > 0; i--) - { - t = (TMTag **) &tags_array->pdata[i - 1]; - if (0 == tm_tag_compare(&tag, t, sort_options)) - return t; - } - } - return NULL; -} - /* Returns a pointer to the position of the first matching tag in a (sorted) tags array. - The passed array of tags should be already sorted by name for optimal performance. If - \c tags_array_sorted is set to FALSE, it may be unsorted but the lookup will be slower. - @param tags_array Tag array (may be sorted on name) + The passed array of tags must be already sorted by name (searched with binary search). + @param tags_array Tag array (sorted on name) @param name Name of the tag to locate. @param partial If TRUE, matches the first part of the name instead of doing exact match. - @param tags_array_sorted If TRUE, the passed \c tags_array is sorted by name so it can be - searched with binary search. Otherwise it is searched linear which is obviously slower. @param tagCount Return location of the matched tags. */ TMTag **tm_tags_find(const GPtrArray *tags_array, const char *name, - gboolean partial, gboolean tags_array_sorted, guint * tagCount) + gboolean partial, guint * tagCount) { static TMTag *tag = NULL; TMTag **result; @@ -1135,7 +1109,8 @@ TMTag **tm_tags_find(const GPtrArray *tags_array, const char *name, sort_options.sort_attrs = NULL; sort_options.partial = partial; - result = tags_search(tags_array, tag, tags_array_sorted, &sort_options); + result = (TMTag **)binary_search(&tag, tags_array->pdata, tags_array->len, + tm_tag_compare, &sort_options); /* There can be matches on both sides of result */ if (result) { diff --git a/tagmanager/src/tm_tag.h b/tagmanager/src/tm_tag.h index f1c9bdf4..15f7388e 100644 --- a/tagmanager/src/tm_tag.h +++ b/tagmanager/src/tm_tag.h @@ -179,7 +179,7 @@ gboolean tm_tags_prune(GPtrArray *tags_array); gboolean tm_tags_dedup(GPtrArray *tags_array, TMTagAttrType *sort_attributes, gboolean unref_duplicates); TMTag **tm_tags_find(const GPtrArray *tags_array, const char *name, - gboolean partial, gboolean tags_array_sorted, guint * tagCount); + gboolean partial, guint * tagCount); void tm_tags_array_free(GPtrArray *tags_array, gboolean free_all); diff --git a/tagmanager/src/tm_workspace.c b/tagmanager/src/tm_workspace.c index 4506212a..4f012717 100644 --- a/tagmanager/src/tm_workspace.c +++ b/tagmanager/src/tm_workspace.c @@ -721,7 +721,7 @@ static guint fill_find_tags_array(GPtrArray *dst, const GPtrArray *src, if (!src || !dst || !name || !*name) return 0; - matches = tm_tags_find(src, name, partial, TRUE, &tagCount); + matches = tm_tags_find(src, name, partial, &tagCount); for (tagIter = 0; tagIter < tagCount; ++tagIter) { if ((type & (*matches)->type) &&