[gnome-builder] ctags: make compare operation more general



commit f89ea85435d203f95e9d67a01eae32ff912ed181
Author: Christian Hergert <christian hergert me>
Date:   Fri May 15 17:21:19 2015 -0700

    ctags: make compare operation more general
    
    This allows us to additionally have a prefix index lookup.

 libide/ctags/ide-ctags-index.c |   52 ++++++++++++++++++++++++++++++++++-----
 libide/ctags/ide-ctags-index.h |   31 +++++++++++++----------
 2 files changed, 62 insertions(+), 21 deletions(-)
---
diff --git a/libide/ctags/ide-ctags-index.c b/libide/ctags/ide-ctags-index.c
index f34dd7c..59bd48f 100644
--- a/libide/ctags/ide-ctags-index.c
+++ b/libide/ctags/ide-ctags-index.c
@@ -64,6 +64,23 @@ ide_ctags_index_entry_compare_keyword (gconstpointer a,
 }
 
 static gint
+ide_ctags_index_entry_compare_prefix (gconstpointer a,
+                                      gconstpointer b)
+{
+  const IdeCtagsIndexEntry *entrya = a;
+  const IdeCtagsIndexEntry *entryb = b;
+
+  /*
+   * With bsearch(), the first element is always the key.
+   */
+
+  if (g_str_has_prefix (entryb->name, entrya->name))
+    return 0;
+  else
+    return g_strcmp0 (entrya->name, entryb->name);
+}
+
+static gint
 ide_ctags_index_entry_compare (gconstpointer a,
                                gconstpointer b)
 {
@@ -401,10 +418,11 @@ ide_ctags_index_get_size (IdeCtagsIndex *self)
   return 0;
 }
 
-const IdeCtagsIndexEntry *
-ide_ctags_index_lookup (IdeCtagsIndex         *self,
-                        const gchar           *keyword,
-                        gsize                 *length)
+static const IdeCtagsIndexEntry *
+ide_ctags_index_lookup_full (IdeCtagsIndex *self,
+                             const gchar   *keyword,
+                             gsize         *length,
+                             GCompareFunc   compare_func)
 {
   IdeCtagsIndexEntry key = { 0 };
   IdeCtagsIndexEntry *ret;
@@ -424,7 +442,7 @@ ide_ctags_index_lookup (IdeCtagsIndex         *self,
                  self->index->data,
                  self->index->len,
                  sizeof (IdeCtagsIndexEntry),
-                 ide_ctags_index_entry_compare_keyword);
+                 compare_func);
 
   if (ret != NULL)
     {
@@ -441,7 +459,7 @@ ide_ctags_index_lookup (IdeCtagsIndex         *self,
        * So let's walk backwards to the first match, being careful not to access the
        * array out of bounds.
        */
-      while ((ret > first) && (ide_str_equal0 (ret[-1].name, keyword)))
+      while ((ret > first) && (compare_func (&key, &ret [-1]) == 0))
         ret--;
 
       /*
@@ -449,8 +467,10 @@ ide_ctags_index_lookup (IdeCtagsIndex         *self,
        */
       for (i = 0; &ret[i] <= last; i++)
         {
-          if (ide_str_equal0 (ret[i].name, keyword))
+          if (compare_func (&key, &ret [i]) == 0)
             count++;
+          else
+            break;
         }
 
       if (length != NULL)
@@ -459,3 +479,21 @@ ide_ctags_index_lookup (IdeCtagsIndex         *self,
 
   return ret;
 }
+
+const IdeCtagsIndexEntry *
+ide_ctags_index_lookup (IdeCtagsIndex         *self,
+                        const gchar           *keyword,
+                        gsize                 *length)
+{
+  return ide_ctags_index_lookup_full (self, keyword, length,
+                                      ide_ctags_index_entry_compare_keyword);
+}
+
+const IdeCtagsIndexEntry *
+ide_ctags_index_lookup_prefix (IdeCtagsIndex         *self,
+                               const gchar           *keyword,
+                               gsize                 *length)
+{
+  return ide_ctags_index_lookup_full (self, keyword, length,
+                                      ide_ctags_index_entry_compare_prefix);
+}
diff --git a/libide/ctags/ide-ctags-index.h b/libide/ctags/ide-ctags-index.h
index e84c946..d6280f5 100644
--- a/libide/ctags/ide-ctags-index.h
+++ b/libide/ctags/ide-ctags-index.h
@@ -55,20 +55,23 @@ typedef struct
   guint8                  padding[3];
 } IdeCtagsIndexEntry;
 
-IdeCtagsIndex            *ide_ctags_index_new         (GFile                *file);
-void                      ide_ctags_index_load_async  (IdeCtagsIndex        *self,
-                                                       GFile                *file,
-                                                       GCancellable         *cancellable,
-                                                       GAsyncReadyCallback   callback,
-                                                       gpointer              user_data);
-gboolean                  ide_ctags_index_load_finish (IdeCtagsIndex        *index,
-                                                       GAsyncResult         *result,
-                                                       GError              **error);
-GFile                    *ide_ctags_index_get_file    (IdeCtagsIndex         *self);
-gsize                     ide_ctags_index_get_size    (IdeCtagsIndex         *self);
-const IdeCtagsIndexEntry *ide_ctags_index_lookup      (IdeCtagsIndex         *self,
-                                                       const gchar           *keyword,
-                                                       gsize                 *length);
+IdeCtagsIndex            *ide_ctags_index_new           (GFile                *file);
+void                      ide_ctags_index_load_async    (IdeCtagsIndex        *self,
+                                                         GFile                *file,
+                                                         GCancellable         *cancellable,
+                                                         GAsyncReadyCallback   callback,
+                                                         gpointer              user_data);
+gboolean                  ide_ctags_index_load_finish   (IdeCtagsIndex        *index,
+                                                         GAsyncResult         *result,
+                                                         GError              **error);
+GFile                    *ide_ctags_index_get_file      (IdeCtagsIndex         *self);
+gsize                     ide_ctags_index_get_size      (IdeCtagsIndex         *self);
+const IdeCtagsIndexEntry *ide_ctags_index_lookup        (IdeCtagsIndex         *self,
+                                                         const gchar           *keyword,
+                                                         gsize                 *length);
+const IdeCtagsIndexEntry *ide_ctags_index_lookup_prefix (IdeCtagsIndex         *self,
+                                                         const gchar           *keyword,
+                                                         gsize                 *length);
 
 G_END_DECLS
 


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