summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Nicholson <dbn.lists@gmail.com>2012-11-15 20:12:22 -0800
committerDan Nicholson <dbn.lists@gmail.com>2012-12-04 13:04:57 -0800
commit8f354125d17bca2917badf2c49e6bd6995420159 (patch)
treedfff52d2691606b7f2c6f6f2f589b6e9a2f15f80
parentd0e4ba893a9e00f64efc4ed57a6b037ec1649847 (diff)
downloadpkg-config-8f354125d17bca2917badf2c49e6bd6995420159.tar.gz
Unify string list stripping functions and operate on list in place
There were two string list stripping functions, when the only difference was to operate from the beginning or end of the list. Also, the function was copying the list and operating on that unnecessarily.
-rw-r--r--pkg.c57
1 files changed, 20 insertions, 37 deletions
diff --git a/pkg.c b/pkg.c
index a7c05a5..14a7e39 100644
--- a/pkg.c
+++ b/pkg.c
@@ -425,42 +425,15 @@ get_package_quiet (const char *name)
}
static GList *
-string_list_strip_duplicates (GList *list)
+string_list_strip_duplicates (GList *list, gboolean forward)
{
GHashTable *table;
GList *tmp;
table = g_hash_table_new (g_str_hash, g_str_equal);
- for (tmp = list; tmp != NULL; tmp = g_list_next (tmp))
- {
- if (!g_hash_table_lookup_extended (table, tmp->data, NULL, NULL))
- {
- /* Unique string. Track it and and move to the next. */
- g_hash_table_replace (table, tmp->data, tmp->data);
- }
- else
- {
- GList *dup = tmp;
-
- /* Remove the duplicate string from the list. */
- debug_spew (" removing duplicate \"%s\"\n", tmp->data);
- tmp = tmp->prev;
- list = g_list_remove_link (list, dup);
- }
- }
- g_hash_table_destroy (table);
-
- return list;
-}
-
-static GList *
-string_list_strip_duplicates_from_back (GList *list)
-{
- GHashTable *table;
- GList *tmp;
-
- table = g_hash_table_new (g_str_hash, g_str_equal);
- for (tmp = g_list_last (list); tmp != NULL; tmp = g_list_previous (tmp))
+ for (tmp = forward ? list : g_list_last (list);
+ tmp != NULL;
+ tmp = forward ? g_list_next (tmp) : g_list_previous (tmp))
{
if (!g_hash_table_lookup_extended (table, tmp->data, NULL, NULL))
{
@@ -471,14 +444,24 @@ string_list_strip_duplicates_from_back (GList *list)
{
GList *dup = tmp;
- /* Remove the duplicate string from the list. */
- debug_spew (" removing duplicate (from back) \"%s\"\n", tmp->data);
- tmp = tmp->next;
+ /* Remove the duplicate string from the list and move to the last
+ * element to prepare for the next iteration. */
+ if (forward)
+ {
+ debug_spew (" removing duplicate \"%s\"\n", tmp->data);
+ tmp = g_list_previous (tmp);
+ }
+ else
+ {
+ debug_spew (" removing duplicate (from back) \"%s\"\n",
+ tmp->data);
+ tmp = g_list_next (tmp);
+ }
list = g_list_remove_link (list, dup);
}
}
g_hash_table_destroy (table);
-
+
return list;
}
@@ -1011,7 +994,7 @@ get_multi_merged (GList *pkgs, GetListFunc func, gboolean in_path_order,
char *retval;
list = fill_list (pkgs, func, in_path_order, include_private);
- list = string_list_strip_duplicates (list);
+ list = string_list_strip_duplicates (list, TRUE);
retval = string_list_to_string (list);
g_list_free (list);
@@ -1026,7 +1009,7 @@ get_multi_merged_from_back (GList *pkgs, GetListFunc func,
char *retval;
list = fill_list (pkgs, func, in_path_order, include_private);
- list = string_list_strip_duplicates_from_back (list);
+ list = string_list_strip_duplicates (list, FALSE);
retval = string_list_to_string (list);
g_list_free (list);