[virt-tools-list] [PATCH 22/47] Replace some temporary GTree objects with GHashTable
Daniel P. Berrange
berrange at redhat.com
Wed Aug 25 19:37:17 UTC 2010
Using a GHashTable + GList combination instead of GTree
and GPtrArray results in shorter, simpler code and is
more inline with normal GLib practice.
* osinfo/osinfo_common.h, osinfo/osinfo_db.c,
osinfo/osinfo_db.h, osinfo/osinfo_list.c: Replace
several GTree objects with GHashTable
---
osinfo/osinfo_common.h | 5 ----
osinfo/osinfo_db.c | 50 +++++++++++++++++++++--------------------------
osinfo/osinfo_db.h | 6 ++--
osinfo/osinfo_list.c | 26 ++++++++++++------------
4 files changed, 38 insertions(+), 49 deletions(-)
diff --git a/osinfo/osinfo_common.h b/osinfo/osinfo_common.h
index f4e829e..6118819 100644
--- a/osinfo/osinfo_common.h
+++ b/osinfo/osinfo_common.h
@@ -67,11 +67,6 @@ struct __osinfoPopulateListArgs {
OsinfoList *list;
};
-struct __osinfoPopulateValuesArgs {
- GTree *values;
- gchar *property;
-};
-
struct __osinfoOsCheckRelationshipArgs {
OsinfoList *list;
osinfoRelationship relshp;
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index 4ecf8bf..0bee6c1 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -216,10 +216,15 @@ OsinfoDeviceList *osinfo_db_get_device_list(OsinfoDb *self)
return self->priv->devices;
}
+struct osinfo_db_populate_values_args {
+ GHashTable *values;
+ gchar *property;
+};
+
static gboolean osinfo_db_get_property_values_in_entity(OsinfoList *list, OsinfoEntity *entity, gpointer data)
{
- struct __osinfoPopulateValuesArgs *args = data;
- GTree *values = args->values;
+ struct osinfo_db_populate_values_args *args = data;
+ GHashTable *values = args->values;
gchar *property = args->property;
GPtrArray *valueArray = NULL;
@@ -230,45 +235,34 @@ static gboolean osinfo_db_get_property_values_in_entity(OsinfoList *list, Osinfo
int i;
for (i = 0; i < valueArray->len; i++) {
gchar *currValue = g_ptr_array_index(valueArray, i);
- void *test = g_tree_lookup(values, currValue);
+ void *test = g_hash_table_lookup(values, currValue);
if (test)
continue;
- gchar *dupValue = g_strdup(currValue);
- // Add to tree with dummy value
- g_tree_insert(values, dupValue, (gpointer) 1);
+ g_hash_table_insert(values,
+ g_strdup(currValue),
+ GINT_TO_POINTER(1));
}
return FALSE; // Continue iterating
}
-static gboolean __osinfoPutKeysInList(gpointer key, gpointer value, gpointer data)
+static GList *osinfo_db_unique_values_for_property_in_entity(OsinfoList *entities, gchar *propName)
{
- gchar *currValue = (gchar *) key;
- GPtrArray *valuesList = (GPtrArray *) data;
-
- g_ptr_array_add(valuesList, currValue);
- return FALSE; // keep iterating
-}
-
+ /* Delibrately no free func for key, since we return those to caller */
+ GHashTable *values = g_hash_table_new(g_str_hash, g_str_equal);
+ GList *ret;
+ struct osinfo_db_populate_values_args args = { values, propName};
-static GPtrArray *osinfo_db_unique_values_for_property_in_entity(OsinfoList *entities, gchar *propName)
-{
- GTree *values = g_tree_new(__osinfoStringCompareBase);
-
- struct __osinfoPopulateValuesArgs args = { values, propName};
osinfo_list_foreach(entities, osinfo_db_get_property_values_in_entity, &args);
- // For each key in tree, add to gptrarray
- GPtrArray *valuesList = g_ptr_array_sized_new(g_tree_nnodes(values));
-
- g_tree_foreach(values, __osinfoPutKeysInList, valuesList);
- g_tree_destroy(values);
- return valuesList;
+ ret = g_hash_table_get_keys(values);
+ g_hash_table_unref(values);
+ return ret;
}
// Get me all unique values for property "vendor" among operating systems
-GPtrArray *osinfo_db_unique_values_for_property_in_os(OsinfoDb *self, gchar *propName)
+GList *osinfo_db_unique_values_for_property_in_os(OsinfoDb *self, gchar *propName)
{
g_return_val_if_fail(OSINFO_IS_DB(self), NULL);
g_return_val_if_fail(propName != NULL, NULL);
@@ -277,7 +271,7 @@ GPtrArray *osinfo_db_unique_values_for_property_in_os(OsinfoDb *self, gchar *pro
}
// Get me all unique values for property "vendor" among hypervisors
-GPtrArray *osinfo_db_unique_values_for_property_in_hv(OsinfoDb *self, gchar *propName)
+GList *osinfo_db_unique_values_for_property_in_hv(OsinfoDb *self, gchar *propName)
{
g_return_val_if_fail(OSINFO_IS_DB(self), NULL);
g_return_val_if_fail(propName != NULL, NULL);
@@ -286,7 +280,7 @@ GPtrArray *osinfo_db_unique_values_for_property_in_hv(OsinfoDb *self, gchar *pro
}
// Get me all unique values for property "vendor" among devices
-GPtrArray *osinfo_db_unique_values_for_property_in_dev(OsinfoDb *self, gchar *propName)
+GList *osinfo_db_unique_values_for_property_in_dev(OsinfoDb *self, gchar *propName)
{
g_return_val_if_fail(OSINFO_IS_DB(self), NULL);
g_return_val_if_fail(propName != NULL, NULL);
diff --git a/osinfo/osinfo_db.h b/osinfo/osinfo_db.h
index d435ed3..55f4620 100644
--- a/osinfo/osinfo_db.h
+++ b/osinfo/osinfo_db.h
@@ -73,13 +73,13 @@ OsinfoHypervisorList *osinfo_db_get_hypervisor_list(OsinfoDb *self);
OsinfoDeviceList *osinfo_db_get_device_list(OsinfoDb *self);
// Get me all unique values for property "vendor" among operating systems
-GPtrArray *osinfo_db_unique_values_for_property_in_os(OsinfoDb *self, gchar *propName);
+GList *osinfo_db_unique_values_for_property_in_os(OsinfoDb *self, gchar *propName);
// Get me all unique values for property "vendor" among hypervisors
-GPtrArray *osinfo_db_unique_values_for_property_in_hv(OsinfoDb *self, gchar *propName);
+GList *osinfo_db_unique_values_for_property_in_hv(OsinfoDb *self, gchar *propName);
// Get me all unique values for property "vendor" among devices
-GPtrArray *osinfo_db_unique_values_for_property_in_dev(OsinfoDb *self, gchar *propName);
+GList *osinfo_db_unique_values_for_property_in_dev(OsinfoDb *self, gchar *propName);
// Get me all OSes that 'upgrade' another OS (or whatever relationship is specified)
OsinfoOsList *osinfo_db_unique_values_for_os_relationship(OsinfoDb *self, osinfoRelationship relshp);
diff --git a/osinfo/osinfo_list.c b/osinfo/osinfo_list.c
index 1200ef0..1cf0275 100644
--- a/osinfo/osinfo_list.c
+++ b/osinfo/osinfo_list.c
@@ -87,15 +87,15 @@ void osinfo_list_add_intersection(OsinfoList *self, OsinfoList *sourceOne, Osinf
int i, len;
// Make set representation of otherList and newList
- GTree *otherSet = g_tree_new(__osinfoStringCompareBase);
- GTree *newSet = g_tree_new(__osinfoStringCompareBase);
+ GHashTable *otherSet = g_hash_table_new(g_str_hash, g_str_equal);
+ GHashTable *newSet = g_hash_table_new(g_str_hash, g_str_equal);
// Add all from otherList to otherSet
len = osinfo_list_get_length(sourceTwo);
for (i = 0; i < len; i++) {
OsinfoEntity *entity = osinfo_list_get_nth(sourceTwo, i);
gchar *id = entity->priv->id;
- g_tree_insert(otherSet, id, entity);
+ g_hash_table_insert(otherSet, id, entity);
}
// If other contains entity, and new list does not, add to new list
@@ -104,22 +104,22 @@ void osinfo_list_add_intersection(OsinfoList *self, OsinfoList *sourceOne, Osinf
OsinfoEntity *entity = osinfo_list_get_nth(sourceOne, i);
gchar *id = entity->priv->id;
- if (g_tree_lookup(otherSet, entity->priv->id) &&
- !g_tree_lookup(newSet, entity->priv->id)) {
- g_tree_insert(newSet, id, entity);
+ if (g_hash_table_lookup(otherSet, entity->priv->id) &&
+ !g_hash_table_lookup(newSet, entity->priv->id)) {
+ g_hash_table_insert(newSet, id, entity);
osinfo_list_add(self, entity);
}
}
- g_tree_destroy(otherSet);
- g_tree_destroy(newSet);
+ g_hash_table_destroy(otherSet);
+ g_hash_table_destroy(newSet);
}
void osinfo_list_add_union(OsinfoList *self, OsinfoList *sourceOne, OsinfoList *sourceTwo)
{
// Make set version of new list
- GTree *newSet = g_tree_new(__osinfoStringCompareBase);
+ GHashTable *newSet = g_hash_table_new(g_str_hash, g_str_equal);
// Add all from other list to new list
int i, len;
@@ -128,7 +128,7 @@ void osinfo_list_add_union(OsinfoList *self, OsinfoList *sourceOne, OsinfoList *
OsinfoEntity *entity = osinfo_list_get_nth(sourceTwo, i);
gchar *id = entity->priv->id;
osinfo_list_add(self, entity);
- g_tree_insert(newSet, id, entity);
+ g_hash_table_insert(newSet, id, entity);
}
// Add remaining elements from this list to new list
@@ -137,13 +137,13 @@ void osinfo_list_add_union(OsinfoList *self, OsinfoList *sourceOne, OsinfoList *
OsinfoEntity *entity = osinfo_list_get_nth(sourceOne, i);
gchar *id = entity->priv->id;
// If new list does not contain element, add to new list
- if (!g_tree_lookup(newSet, id)) {
+ if (!g_hash_table_lookup(newSet, id)) {
osinfo_list_add(self, entity);
- g_tree_insert(newSet, id, entity);
+ g_hash_table_insert(newSet, id, entity);
}
}
- g_tree_destroy(newSet);
+ g_hash_table_unref(newSet);
}
--
1.7.2.1
More information about the virt-tools-list
mailing list