[gnome-db] Use of hash table improvement in gda-data-model.c



	Hi!

	Attached is a patch that uses the number of column as the key
	for the hash table in gda-data-model.c

	It works fine and avoid memory allocation/freeing for the keys
	in the hash table.

	Commit?

-- 
Gonzalo Paniagua Javier <gonzalo gnome-db org>
http://www.gnome-db.org/~gonzalo/

Index: gda-data-model.c
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-data-model.c,v
retrieving revision 1.16
diff -u -r1.16 gda-data-model.c
--- gda-data-model.c	9 May 2002 04:47:25 -0000	1.16
+++ gda-data-model.c	9 May 2002 06:01:56 -0000
@@ -106,6 +106,20 @@
 	klass->get_value_at = NULL;
 }
 
+
+static gboolean
+my_int_equal (gconstpointer v1,
+	      gconstpointer v2)
+{
+  return v1 == v2;
+}
+
+static guint
+my_int_hash (gconstpointer v)
+{
+  return (guint) v;
+}
+
 static void
 gda_data_model_init (GdaDataModel *model, GdaDataModelClass *klass)
 {
@@ -113,20 +127,16 @@
 
 	model->priv = g_new (GdaDataModelPrivate, 1);
 	model->priv->notify_changes = TRUE;
-	model->priv->column_titles = g_hash_table_new (g_str_hash, g_str_equal);
+	model->priv->column_titles = g_hash_table_new_full (my_int_hash,
+							    my_int_equal,
+							    NULL,
+							    g_free);
 	model->priv->editing = FALSE;
 	model->priv->cmd_text = NULL;
 	model->priv->cmd_type = GDA_COMMAND_TYPE_INVALID;
 }
 
 static void
-free_hash_string (gpointer key, gpointer value, gpointer user_data)
-{
-	g_free (key);
-	g_free (value);
-}
-
-static void
 gda_data_model_finalize (GObject *object)
 {
 	GdaDataModel *model = (GdaDataModel *) object;
@@ -134,7 +144,6 @@
 	g_return_if_fail (GDA_IS_DATA_MODEL (model));
 
 	/* free memory */
-	g_hash_table_foreach (model->priv->column_titles, (GHFunc) free_hash_string, NULL);
 	g_hash_table_destroy (model->priv->column_titles);
 
 	g_free (model->priv->cmd_text);
@@ -300,11 +309,9 @@
 	g_return_val_if_fail (GDA_IS_DATA_MODEL (model), NULL);
 
 	n_cols = gda_data_model_get_n_columns (model);
-	if (col < n_cols && col >= 0) {
-		gchar *col_str = g_strdup_printf ("%d", col);
-		title = g_hash_table_lookup (model->priv->column_titles, col_str);
-		g_free (col_str);
-	}
+	if (col < n_cols && col >= 0)
+		title = g_hash_table_lookup (model->priv->column_titles,
+					     GINT_TO_POINTER (col));
 	else
 		title = "";
 
@@ -326,19 +333,14 @@
 
 	n_cols = gda_data_model_get_n_columns (model);
 	if (col >= 0 && col < n_cols) {
-		gpointer orig_key;
-		gpointer orig_value;
-		gchar *col_str;
-
-		col_str = g_strdup_printf ("%d", col);
-		if (g_hash_table_lookup_extended (model->priv->column_titles, col_str,
-			&orig_key, &orig_value)) {
-			g_hash_table_remove (model->priv->column_titles, &col);
-			g_free (orig_key);
-			g_free (orig_value);
-		}
+		gpointer value;
+
+		value = g_hash_table_lookup (model->priv->column_titles,
+					     GINT_TO_POINTER (col));
+		g_free (value);
 
-		g_hash_table_insert (model->priv->column_titles, col_str, g_strdup (title));
+		g_hash_table_insert (model->priv->column_titles, 
+				     GINT_TO_POINTER (col), g_strdup (title));
 	}
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]