Skip to content

Commit

Permalink
Use the new merge implementation also for global tags
Browse files Browse the repository at this point in the history
  • Loading branch information
techee committed Oct 18, 2014
1 parent 0470f45 commit fe98ea6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 51 deletions.
46 changes: 0 additions & 46 deletions tagmanager/src/tm_tag.c
Expand Up @@ -852,52 +852,6 @@ gboolean tm_tags_custom_dedup(GPtrArray *tags_array, TMTagCompareFunc compare_fu
return TRUE;
}

/* Sorts newly-added tags and merges them in order with existing tags.
* This is much faster than resorting the whole array.
* Note: Having the caller append to the existing array should be faster
* than creating a new array which would likely get resized more than once.
* tags_array: array with new (perhaps unsorted) tags appended.
* orig_len: number of existing tags. */
gboolean tm_tags_merge(GPtrArray *tags_array, gsize orig_len,
TMTagAttrType *sort_attributes, gboolean dedup)
{
gpointer *copy, *a, *b;
gsize copy_len, i;

if ((!tags_array) || (!tags_array->len) || orig_len >= tags_array->len)
return TRUE;
if (!orig_len)
return tm_tags_sort(tags_array, sort_attributes, dedup);
copy_len = tags_array->len - orig_len;
copy = g_memdup(tags_array->pdata + orig_len, copy_len * sizeof(gpointer));
s_sort_attrs = sort_attributes;
s_partial = FALSE;
/* enforce copy sorted with same attributes for merge */
qsort(copy, copy_len, sizeof(gpointer), tm_tag_compare);
a = tags_array->pdata + orig_len - 1;
b = copy + copy_len - 1;
for (i = tags_array->len - 1;; i--)
{
gint cmp = tm_tag_compare(a, b);

tags_array->pdata[i] = (cmp >= 0) ? *a-- : *b--;
if (a < tags_array->pdata)
{
/* include remainder of copy as well as current value of b */
memcpy(tags_array->pdata, copy, ((b + 1) - copy) * sizeof(gpointer));
break;
}
if (b < copy)
break; /* remaining elements of 'a' are in place already */
g_assert(i != 0);
}
s_sort_attrs = NULL;
g_free(copy);
if (dedup)
tm_tags_dedup(tags_array, sort_attributes);
return TRUE;
}

/*
Sort an array of tags on the specified attribuites using the inbuilt comparison
function.
Expand Down
3 changes: 0 additions & 3 deletions tagmanager/src/tm_tag.h
Expand Up @@ -189,9 +189,6 @@ gboolean tm_tag_write(TMTag *tag, FILE *file, guint attrs);

int tm_tag_compare(const void *ptr1, const void *ptr2);

gboolean tm_tags_merge(GPtrArray *tags_array, gsize orig_len,
TMTagAttrType *sort_attributes, gboolean dedup);

GPtrArray *tm_tags_remove_file_tags(TMSourceFile *source_file, GPtrArray *tags_array);

GPtrArray *tm_tags_merge_big_small(GPtrArray *big_array, GPtrArray *small_array, TMTagAttrType *sort_attributes);
Expand Down
14 changes: 12 additions & 2 deletions tagmanager/src/tm_workspace.c
Expand Up @@ -154,6 +154,7 @@ gboolean tm_workspace_load_global_tags(const char *tags_file, gint mode)
gsize orig_len;
guchar buf[BUFSIZ];
FILE *fp;
GPtrArray *file_tags, *new_tags;
TMTag *tag;
TMFileFormat format = TM_FILE_FORMAT_TAGMANAGER;

Expand Down Expand Up @@ -198,12 +199,21 @@ gboolean tm_workspace_load_global_tags(const char *tags_file, gint mode)
}
rewind(fp); /* reset the file pointer, to start reading again from the beginning */
}

file_tags = g_ptr_array_new();
while (NULL != (tag = tm_tag_new_from_file(NULL, fp, mode, format)))
g_ptr_array_add(theWorkspace->global_tags, tag);
g_ptr_array_add(file_tags, tag);
fclose(fp);

tm_tags_sort(file_tags, global_tags_sort_attrs, TRUE);

/* reorder the whole array, because tm_tags_find expects a sorted array */
tm_tags_merge(theWorkspace->global_tags, orig_len, global_tags_sort_attrs, TRUE);
new_tags = tm_tags_merge_big_small(theWorkspace->global_tags,
file_tags, global_tags_sort_attrs);
g_ptr_array_free(theWorkspace->global_tags, TRUE);
g_ptr_array_free(file_tags, TRUE);
theWorkspace->global_tags = new_tags;

return TRUE;
}

Expand Down

0 comments on commit fe98ea6

Please sign in to comment.