Use binary search when removing file tags

Even though the binary search requires expensive string comparisons,
there are just log(n) of them to find the tag in the workspace array
and the result is much faster than scanning the array linearly (this
of course works only under the condition that

len(source_file->tags_array) << len(workspace_array)

This is however satisfied for big projects (and doesn't matter for small
projects).

Also make the tm_tags_find() function more user friendly by returning
tagCount 0 when no tags found.
This commit is contained in:
Jiří Techet 2014-10-30 16:57:33 +01:00
parent be131b00f9
commit 32a3dfab7f

View File

@ -809,13 +809,36 @@ gboolean tm_tags_sort(GPtrArray *tags_array, TMTagAttrType *sort_attributes,
void tm_tags_remove_file_tags(TMSourceFile *source_file, GPtrArray *tags_array) void tm_tags_remove_file_tags(TMSourceFile *source_file, GPtrArray *tags_array)
{ {
guint i; guint i;
for (i = 0; i < tags_array->len; i++) GPtrArray *to_delete = g_ptr_array_sized_new(source_file->tags_array->len);
{
TMTag *tag = tags_array->pdata[i];
if (tag != NULL && tag->file == source_file) for (i = 0; i < source_file->tags_array->len; i++)
tags_array->pdata[i] = NULL; {
guint j;
gint tag_count;
TMTag **found;
TMTag *tag = source_file->tags_array->pdata[i];
found = tm_tags_find(tags_array, tag->name, FALSE, TRUE, &tag_count);
for (j = 0; j < tag_count; j++)
{
if (*found != NULL && (*found)->file == source_file)
{
/* we cannot set the pointer to NULL now because the search wouldn't work */
g_ptr_array_add(to_delete, found);
break;
} }
found++;
}
}
for (i = 0; i < to_delete->len; i++)
{
TMTag **tag = to_delete->pdata[i];
*tag = NULL;
}
g_ptr_array_free(to_delete, TRUE);
tm_tags_prune(tags_array); tm_tags_prune(tags_array);
} }
@ -1055,6 +1078,7 @@ TMTag **tm_tags_find(const GPtrArray *tags_array, const char *name,
int tagMatches=0; int tagMatches=0;
TMSortOptions sort_options; TMSortOptions sort_options;
*tagCount = 0;
if ((!tags_array) || (!tags_array->len)) if ((!tags_array) || (!tags_array->len))
return NULL; return NULL;