[gtk+/filesystemmodel: 22/28] add a hash table to speed up file => index lookups



commit b208642e79007dd89ee4372a1cffa00349c8cf85
Author: Benjamin Otte <otte gnome org>
Date:   Mon Jun 22 20:54:30 2009 +0200

    add a hash table to speed up file => index lookups

 gtk/gtkfilesystemmodel.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkfilesystemmodel.c b/gtk/gtkfilesystemmodel.c
index abcffcf..8562d51 100644
--- a/gtk/gtkfilesystemmodel.c
+++ b/gtk/gtkfilesystemmodel.c
@@ -70,6 +70,7 @@ struct _GtkFileSystemModel
   GCancellable *        cancellable;    /* cancellable in use for all operations - cancelled on dispose */
   GArray *              files;          /* array of FileModelNode containing all our files */
   guint                 n_indexes_valid;/* count of valid indexes */
+  GHashTable *          file_lookup;    /* file => array index table */
 
   guint                 n_columns;      /* number of columns */
   GType *               column_types;   /* types of each column */
@@ -523,12 +524,14 @@ gtk_file_system_model_sort (GtkFileSystemModel *model)
       node_validate_indexes (model, G_MAXUINT, G_MAXUINT);
       n_elements = node_get_index (model, model->files->len - 1) + 1;
       model->n_indexes_valid = 0;
+      g_hash_table_remove_all (model->file_lookup);
       g_qsort_with_data (get_node (model, 1),
                          model->files->len - 1,
                          NODE_SIZE (model),
                          compare_array_element,
                          &data);
       g_assert (model->n_indexes_valid == 0);
+      g_assert (g_hash_table_size (model->file_lookup) == 0);
       if (n_elements)
         {
           int *new_order = g_new (int, n_elements);
@@ -783,6 +786,7 @@ gtk_file_system_model_finalize (GObject *object)
   g_object_unref (model->cancellable);
   g_free (model->attributes);
   g_object_unref (model->dir);
+  g_hash_table_destroy (model->file_lookup);
 
   _gtk_tree_data_list_header_free (model->sort_list);
   if (model->default_sort_destroy)
@@ -837,6 +841,7 @@ _gtk_file_system_model_init (GtkFileSystemModel *model)
 
   model->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
 
+  model->file_lookup = g_hash_table_new (g_file_hash, (GEqualFunc) g_file_equal);
   model->cancellable = g_cancellable_new ();
 }
 
@@ -1258,11 +1263,16 @@ node_get_for_file (GtkFileSystemModel *model,
 {
   guint i;
 
-  /* node 0 is the editable row and has no associated file */
-  for (i = 1; i < model->files->len; i++)
+  i = GPOINTER_TO_UINT (g_hash_table_lookup (model->file_lookup, file));
+  if (i != 0)
+    return i;
+
+  /* node 0 is the editable row and has no associated file or entry in the table */
+  for (i = g_hash_table_size (model->file_lookup) + 1; i < model->files->len; i++)
     {
       FileModelNode *node = get_node (model, i);
 
+      g_hash_table_insert (model->file_lookup, node->file, GUINT_TO_POINTER (i));
       if (g_file_equal (node->file, file))
         return i;
     }



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