[gimp/soc-2011-gimpunitentry] GimpUnitEntryTable: use GHashTable for storing entries



commit b69cac0bda897388842c3438fc2b149b290ec7dd
Author: Enrico SchroÌder <enni schroeder gmail com>
Date:   Sun Jul 10 16:23:12 2011 +0200

    GimpUnitEntryTable: use GHashTable for storing entries
    
    GimpUnitEntryTable now uses a GHashTable instead of a GList for storing its entries. The id string of an entry is not stored in the entry itself any more, but via the hash table.

 libgimpwidgets/gimpunitentry.c      |   10 +-----
 libgimpwidgets/gimpunitentry.h      |    5 +--
 libgimpwidgets/gimpunitentrytable.c |   61 +++++++++++++++++++++-------------
 libgimpwidgets/gimpunitentrytable.h |    2 +-
 libgimpwidgets/gimpwidgets.def      |    1 -
 libgimpwidgets/test-unitentry.c     |    4 +-
 6 files changed, 43 insertions(+), 40 deletions(-)
---
diff --git a/libgimpwidgets/gimpunitentry.c b/libgimpwidgets/gimpunitentry.c
index 59888c9..78e69d1 100644
--- a/libgimpwidgets/gimpunitentry.c
+++ b/libgimpwidgets/gimpunitentry.c
@@ -145,12 +145,10 @@ gimp_unit_entry_class_init (GimpUnitEntryClass *class)
 }
 
 GtkWidget*
-gimp_unit_entry_new (const gchar *id)
+gimp_unit_entry_new (void)
 {
   GtkWidget *entry = g_object_new (GIMP_TYPE_UNIT_ENTRY, NULL);
 
-  GIMP_UNIT_ENTRY (entry)->id = id;
-
   return entry;
 }
 
@@ -416,12 +414,6 @@ gimp_unit_entry_key_release (GtkWidget          *widget,
 }
 
 /* convenience getters/setters */
-const gchar* 
-gimp_unit_entry_get_id (GimpUnitEntry *entry)
-{
-  return entry->id;
-}
-
 void 
 gimp_unit_entry_set_unit (GimpUnitEntry *entry, GimpUnit unit)
 {
diff --git a/libgimpwidgets/gimpunitentry.h b/libgimpwidgets/gimpunitentry.h
index 696850f..39ed115 100644
--- a/libgimpwidgets/gimpunitentry.h
+++ b/libgimpwidgets/gimpunitentry.h
@@ -60,8 +60,6 @@ struct _GimpUnitEntry
 
   /* input mode */
   GimpUnitEntryMode   mode;
-  /* identifier string of unit entry (used by GimpUnitEntryTable) */
-  const gchar        *id;
 };
 
 struct _GimpUnitEntryClass
@@ -70,10 +68,9 @@ struct _GimpUnitEntryClass
 };
 
 GType                 gimp_unit_entry_get_type          (void);
-GtkWidget *           gimp_unit_entry_new               (const gchar        *id);
+GtkWidget *           gimp_unit_entry_new               (void);
 
 GimpUnitAdjustment *  gimp_unit_entry_get_adjustment    (GimpUnitEntry      *entry);
-const gchar *         gimp_unit_entry_get_id            (GimpUnitEntry      *entry);
 gdouble               gimp_unit_entry_get_value         (GimpUnitEntry      *entry);
 gdouble               gimp_unit_entry_get_value_in_unit (GimpUnitEntry      *entry, 
                                                          GimpUnit            unit);
diff --git a/libgimpwidgets/gimpunitentrytable.c b/libgimpwidgets/gimpunitentrytable.c
index 02ee184..dfe5dfb 100644
--- a/libgimpwidgets/gimpunitentrytable.c
+++ b/libgimpwidgets/gimpunitentrytable.c
@@ -47,8 +47,8 @@ static void
 gimp_unit_entry_table_init (GimpUnitEntryTable *table)
 {
    /* initialize our fields */
-  table->table        = gtk_table_new (1, 1, FALSE);
-  table->entries      = NULL;
+  table->table        = gtk_table_new     (1, 1, FALSE);
+  table->entries      = g_hash_table_new  (NULL, NULL);;
   table->bottom       = 0;
   table->right        = 0;
 }
@@ -87,7 +87,7 @@ gimp_unit_entry_table_add_entry (GimpUnitEntryTable *table,
                                  gint x,
                                  gint y)
 {
-  GimpUnitEntry *entry = GIMP_UNIT_ENTRY (gimp_unit_entry_new (id)); 
+  GimpUnitEntry *entry = GIMP_UNIT_ENTRY (gimp_unit_entry_new ()); 
   GtkWidget     *label;
 
   /* position of the entry (leave one row/column empty for labels etc) */
@@ -136,7 +136,7 @@ gimp_unit_entry_table_add_entry (GimpUnitEntryTable *table,
 
   gtk_widget_show_all (table->table); 
 
-  table->entries = g_list_append (table->entries, (gpointer) entry);
+  g_hash_table_insert (table->entries, (gpointer) id, (gpointer) entry);
 
   return GTK_WIDGET (entry);
 }
@@ -156,7 +156,7 @@ gimp_unit_entry_table_add_entry_defaults (GimpUnitEntryTable *table,
                                                            table->bottom));
 
   /* connect entry to others */
-  for (i = 0; i < g_list_length (table->entries); i++)
+  for (i = 0; i < g_hash_table_size (table->entries); i++)
   {
     entry2 = gimp_unit_entry_table_get_nth_entry (table, i);
     gimp_unit_entry_connect (GIMP_UNIT_ENTRY (entry), GIMP_UNIT_ENTRY (entry2));
@@ -257,17 +257,15 @@ gimp_unit_entry_table_get_entry (GimpUnitEntryTable *table,
                                  const gchar *id)
 {
   GimpUnitEntry *entry;
-  gint i, count = g_list_length (table->entries);
 
-  /* iterate over list to find our entry */
-  for (i = 0; i < count; i++) 
+  entry = GIMP_UNIT_ENTRY (g_hash_table_lookup (table->entries, id));
+
+  if (entry == NULL)
   {
-    entry = gimp_unit_entry_table_get_nth_entry (table, i);
-    if (g_strcmp0 (gimp_unit_entry_get_id (entry), id) == 0)
-      return entry;
+    g_warning ("gimp_unit_entry_table_get_entry: entry with id '%s' does not exist", id);
   }
-  g_warning ("gimp_unit_entry_table_get_entry: entry with id '%s' does not exist", id);
-  return NULL;
+
+  return entry;
 }
 
 /* get UnitEntry by index */
@@ -275,12 +273,29 @@ GimpUnitEntry*
 gimp_unit_entry_table_get_nth_entry (GimpUnitEntryTable *table, 
                                      gint index)
 {
-  if (g_list_length (table->entries) <= index)
+  GHashTableIter   iter;
+  gpointer         key, value;
+  gint             i;
+
+  if (g_hash_table_size (table->entries) <= index || index < 0)
   {
+    g_warning ("gimp_unit_entry_table_get_nth_entry: index < 0 or hash table size smaller than index");
     return NULL;
   }
 
-  return GIMP_UNIT_ENTRY (g_list_nth (table->entries, index)->data);
+  /* reverse order because first added element is last in g_hash_table */
+  i = g_hash_table_size (table->entries) - 1;
+
+  g_hash_table_iter_init (&iter, table->entries);
+
+  while (g_hash_table_iter_next (&iter, &key, &value))
+  {
+    if (i == index)
+      return GIMP_UNIT_ENTRY (value);
+    i--;
+  }
+
+  return NULL;
 }
 
 /* updates the text of the preview label */
@@ -311,9 +326,9 @@ void label_updater (GtkAdjustment *adj, gpointer userData)
 static 
 void on_entry_changed  (GtkAdjustment *adj, gpointer userData)
 {
-  GimpUnitEntryTable *table = GIMP_UNIT_ENTRY_TABLE (userData);
-  GimpUnitEntry *entry;
-  gint i, count = g_list_length (table->entries);
+  GimpUnitEntryTable *table     = GIMP_UNIT_ENTRY_TABLE (userData);
+  GimpUnitEntry      *entry;
+  gint                i, count  = gimp_unit_entry_table_get_entry_count (table);
 
   /* find corresponding entry */
   for (i = 0; i < count; i++) 
@@ -331,7 +346,7 @@ void on_entry_changed  (GtkAdjustment *adj, gpointer userData)
 gint
 gimp_unit_entry_table_get_entry_count (GimpUnitEntryTable *table)
 {
-  return g_list_length (table->entries);
+  return g_hash_table_size (table->entries);
 }
 
 /* get value of given entry in pixels */
@@ -359,7 +374,7 @@ void
 gimp_unit_entry_table_set_unit (GimpUnitEntryTable *table, GimpUnit unit)
 {
   GimpUnitEntry *entry;
-  gint i, count = g_list_length (table->entries);
+  gint           i, count = gimp_unit_entry_table_get_entry_count (table);
 
   /* iterate over list of entries */
   for (i = 0; i < count; i++) 
@@ -374,7 +389,7 @@ void
 gimp_unit_entry_table_set_resolution (GimpUnitEntryTable *table, gdouble res)
 {
   GimpUnitEntry *entry;
-  gint i, count = g_list_length (table->entries);
+  gint           i, count = gimp_unit_entry_table_get_entry_count (table);
 
   /* iterate over list of entries */
   for (i = 0; i < count; i++) 
@@ -390,7 +405,7 @@ gimp_unit_entry_table_set_mode (GimpUnitEntryTable *table,
                                 GimpUnitEntryMode   mode)
 {
   GimpUnitEntry *entry;
-  gint i, count = g_list_length (table->entries);
+  gint           i, count = gimp_unit_entry_table_get_entry_count (table);
 
   /* iterate over list of entries */
   for (i = 0; i < count; i++) 
@@ -406,7 +421,7 @@ gimp_unit_entry_table_set_activates_default (GimpUnitEntryTable *table,
                                              gboolean setting)
 {
   GimpUnitEntry *entry;
-  gint i, count = g_list_length (table->entries);
+  gint           i, count = gimp_unit_entry_table_get_entry_count (table);
 
   /* iterate over list of entries */
   for (i = 0; i < count; i++) 
diff --git a/libgimpwidgets/gimpunitentrytable.h b/libgimpwidgets/gimpunitentrytable.h
index 0c94e0e..121e12e 100644
--- a/libgimpwidgets/gimpunitentrytable.h
+++ b/libgimpwidgets/gimpunitentrytable.h
@@ -49,7 +49,7 @@ struct _GimpUnitEntryTable
 
   /* private */
   GtkWidget  *table;
-  GList      *entries;        /* list of entries */
+  GHashTable *entries;
 
   /* dimensions of "sub-table" containing the actual entries */ 
   gint       bottom, right;
diff --git a/libgimpwidgets/gimpwidgets.def b/libgimpwidgets/gimpwidgets.def
index 4805d3b..c3b9294 100644
--- a/libgimpwidgets/gimpwidgets.def
+++ b/libgimpwidgets/gimpwidgets.def
@@ -376,7 +376,6 @@ EXPORTS
 	gimp_unit_combo_box_set_active
 	gimp_unit_entry_connect
 	gimp_unit_entry_get_adjustment
-	gimp_unit_entry_get_id
 	gimp_unit_entry_get_type
 	gimp_unit_entry_get_unit
 	gimp_unit_entry_get_value
diff --git a/libgimpwidgets/test-unitentry.c b/libgimpwidgets/test-unitentry.c
index 260ac14..f2be515 100644
--- a/libgimpwidgets/test-unitentry.c
+++ b/libgimpwidgets/test-unitentry.c
@@ -27,8 +27,8 @@ static void
 gimp_test_unitentry_setup (GimpTestFixture *fixture,
                            gconstpointer    data)
 {
-  fixture->entry1 = GIMP_UNIT_ENTRY (gimp_unit_entry_new ("entry1"));
-  fixture->entry2 = GIMP_UNIT_ENTRY (gimp_unit_entry_new ("entry2"));
+  fixture->entry1 = GIMP_UNIT_ENTRY (gimp_unit_entry_new ());
+  fixture->entry2 = GIMP_UNIT_ENTRY (gimp_unit_entry_new ());
   gimp_unit_entry_connect (fixture->entry1, fixture->entry2);
   gimp_unit_entry_connect (fixture->entry2, fixture->entry1);
 }



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