[gtk/wip/baedert/for-master: 55/56] shortcutcontroller: Use a GArray instead of a linked list



commit 2c69369010d820f1332beb7e06c046c335d52275
Author: Timm Bäder <mail baedert org>
Date:   Wed Apr 29 16:39:22 2020 +0200

    shortcutcontroller: Use a GArray instead of a linked list

 gtk/gtkshortcutcontroller.c | 39 +++++++++++++++++----------------------
 1 file changed, 17 insertions(+), 22 deletions(-)
---
diff --git a/gtk/gtkshortcutcontroller.c b/gtk/gtkshortcutcontroller.c
index a3c62ead72..c41468c3e2 100644
--- a/gtk/gtkshortcutcontroller.c
+++ b/gtk/gtkshortcutcontroller.c
@@ -277,15 +277,6 @@ typedef struct {
   guint index;
 } ShortcutData;
 
-static void
-shortcut_data_free (gpointer data)
-{
-  ShortcutData *sdata = data;
-
-  g_object_unref (sdata->shortcut);
-  g_free (sdata);
-}
-
 static gboolean
 gtk_shortcut_controller_run_controllers (GtkEventController *controller,
                                          GdkEvent           *event,
@@ -294,13 +285,12 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller,
                                          gboolean            enable_mnemonics)
 {
   GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (controller);
-  guint i;
-  GSList *shortcuts = NULL;
-  GSList *l;
+  int i, p;
+  GArray *shortcuts = NULL;
   gboolean has_exact = FALSE;
   gboolean retval = FALSE;
 
-  for (i = 0; i < g_list_model_get_n_items (self->shortcuts); i++)
+  for (i = 0, p = g_list_model_get_n_items (self->shortcuts); i < p; i++)
     {
       GtkShortcut *shortcut;
       ShortcutData *data;
@@ -325,8 +315,8 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller,
         case GDK_KEY_MATCH_EXACT:
           if (!has_exact)
             {
-              g_slist_free_full (shortcuts, shortcut_data_free);
-              shortcuts = NULL;
+              if (shortcuts)
+                g_array_set_size (shortcuts, 0);
             }
           has_exact = TRUE;
           break;
@@ -353,20 +343,25 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller,
           continue;
         }
 
-      data = g_new0 (ShortcutData, 1);
+      if (G_UNLIKELY (!shortcuts))
+        shortcuts = g_array_sized_new (FALSE, TRUE, sizeof (ShortcutData), 8);
+
+      g_array_set_size (shortcuts, shortcuts->len + 1);
+      data = &g_array_index (shortcuts, ShortcutData, shortcuts->len - 1);
       data->shortcut = shortcut;
       data->index = index;
       data->widget = widget;
-
-      shortcuts = g_slist_append (shortcuts, data);
     }
 
-  for (l = shortcuts; l; l = l->next)
+  if (!shortcuts)
+    return retval;
+
+  for (i = shortcuts->len - 1, p = shortcuts->len; i >= 0; i--)
     {
-      ShortcutData *data = l->data;
+      const ShortcutData *data = &g_array_index (shortcuts, ShortcutData, i);
 
       if (gtk_shortcut_action_activate (gtk_shortcut_get_action (data->shortcut),
-                                        shortcuts->next == NULL ? GTK_SHORTCUT_ACTION_EXCLUSIVE : 0,
+                                        i == p - 1 ? GTK_SHORTCUT_ACTION_EXCLUSIVE : 0,
                                         data->widget,
                                         gtk_shortcut_get_arguments (data->shortcut)))
         {
@@ -376,7 +371,7 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller,
         }
     }
 
-  g_slist_free_full (shortcuts, shortcut_data_free);
+  g_array_free (shortcuts, TRUE);
 
   return retval;
 }


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