[gtk/matthiasc/arrays: 1/2] multisorter: Use GdkArray for the sorters



commit cf4a8e2152bcfa73cfe0e65513d32842b5fa0153
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jul 16 12:53:29 2020 -0400

    multisorter: Use GdkArray for the sorters

 gtk/gtkmultisorter.c | 89 ++++++++++++++++++++++------------------------------
 1 file changed, 37 insertions(+), 52 deletions(-)
---
diff --git a/gtk/gtkmultisorter.c b/gtk/gtkmultisorter.c
index 66400c50a2..6d1f2120b0 100644
--- a/gtk/gtkmultisorter.c
+++ b/gtk/gtkmultisorter.c
@@ -25,6 +25,13 @@
 #include "gtkintl.h"
 #include "gtktypebuiltins.h"
 
+#define GDK_ARRAY_TYPE_NAME GtkSorters
+#define GDK_ARRAY_NAME gtk_sorters
+#define GDK_ARRAY_ELEMENT_TYPE GtkSorter *
+#define GDK_ARRAY_FREE_FUNC g_object_unref
+
+#include "gdk/gdkarrayimpl.c"
+
 /**
  * SECTION:gtkmultisorter
  * @Title: GtkMultiSorter
@@ -38,7 +45,7 @@ struct _GtkMultiSorter
 {
   GtkSorter parent_instance;
 
-  GSequence *sorters;
+  GtkSorters sorters;
 };
 
 static GType
@@ -52,7 +59,7 @@ gtk_multi_sorter_get_n_items (GListModel *list)
 {
   GtkMultiSorter *self = GTK_MULTI_SORTER (list);
 
-  return g_sequence_get_length (self->sorters);
+  return gtk_sorters_get_size (&self->sorters);
 }
 
 static gpointer
@@ -60,14 +67,11 @@ gtk_multi_sorter_get_item (GListModel *list,
                            guint       position)
 {
   GtkMultiSorter *self = GTK_MULTI_SORTER (list);
-  GSequenceIter *iter;
-
-  iter = g_sequence_get_iter_at_pos (self->sorters, position);
 
-  if (g_sequence_iter_is_end (iter))
-    return NULL;
+  if (position < gtk_sorters_get_size (&self->sorters))
+    return g_object_ref (gtk_sorters_get (&self->sorters, position));
   else
-    return g_object_ref (g_sequence_get (iter));
+    return NULL;
 }
 
 static void
@@ -111,13 +115,11 @@ gtk_multi_sorter_compare (GtkSorter *sorter,
 {
   GtkMultiSorter *self = GTK_MULTI_SORTER (sorter);
   GtkOrdering result = GTK_ORDERING_EQUAL;
-  GSequenceIter *iter;
+  guint i;
 
-  for (iter = g_sequence_get_begin_iter (self->sorters);
-       !g_sequence_iter_is_end (iter);
-       iter = g_sequence_iter_next (iter))
+  for (i = 0; i < gtk_sorters_get_size (&self->sorters); i++)
     {
-      GtkSorter *child = g_sequence_get (iter);
+      GtkSorter *child = gtk_sorters_get (&self->sorters, i);
 
       result = gtk_sorter_compare (child, item1, item2);
       if (result != GTK_ORDERING_EQUAL)
@@ -132,13 +134,11 @@ gtk_multi_sorter_get_order (GtkSorter *sorter)
 {
   GtkMultiSorter *self = GTK_MULTI_SORTER (sorter);
   GtkSorterOrder result = GTK_SORTER_ORDER_NONE;
-  GSequenceIter *iter;
+  guint i;
 
-  for (iter = g_sequence_get_begin_iter (self->sorters);
-       !g_sequence_iter_is_end (iter);
-       iter = g_sequence_iter_next (iter))
+  for (i = 0; i < gtk_sorters_get_size (&self->sorters); i++)
     {
-      GtkSorter *child = g_sequence_get (iter);
+      GtkSorter *child = gtk_sorters_get (&self->sorters, i);
       GtkSorterOrder child_order;
 
       child_order = gtk_sorter_get_order (child);
@@ -166,12 +166,14 @@ gtk_multi_sorter_changed_cb (GtkSorter       *sorter,
                              GtkMultiSorter  *self)
 {
   /* Using an enum on purpose, so gcc complains about this case if
-   * new values are added to the enum */
+   * new values are added to the enum
+   */
   switch (change)
   {
     case GTK_SORTER_CHANGE_INVERTED:
-      /* This could do a lot better with change handling, in particular in cases where self->n_sorters == 1
-       * or if sorter == self->sorters[0] */
+      /* This could do a lot better with change handling, in particular in
+       * cases where self->n_sorters == 1 or if sorter == self->sorters[0]
+       */
       change = GTK_SORTER_CHANGE_DIFFERENT;
       break;
 
@@ -187,39 +189,22 @@ gtk_multi_sorter_changed_cb (GtkSorter       *sorter,
   gtk_sorter_changed (GTK_SORTER (self), change);
 }
 
-static void
-gtk_multi_sorter_remove_iter (GtkMultiSorter *self,
-                              GSequenceIter  *iter)
-{
-  GtkSorter *sorter;
-
-  sorter = g_sequence_get (iter);
-  g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
-  g_object_unref (sorter);
-  g_sequence_remove (iter);
-}
-
 static void
 gtk_multi_sorter_dispose (GObject *object)
 {
   GtkMultiSorter *self = GTK_MULTI_SORTER (object);
+  guint i;
 
-  while (!g_sequence_is_empty (self->sorters))
-    gtk_multi_sorter_remove_iter (self, g_sequence_get_begin_iter (self->sorters));
+  for (i = 0; i < gtk_sorters_get_size (&self->sorters); i++)
+    {
+      GtkSorter *sorter = gtk_sorters_get (&self->sorters, i);
+      g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
+    }
+  gtk_sorters_clear (&self->sorters);
 
   G_OBJECT_CLASS (gtk_multi_sorter_parent_class)->dispose (object);
 }
 
-static void
-gtk_multi_sorter_finalize (GObject *object)
-{
-  GtkMultiSorter *self = GTK_MULTI_SORTER (object);
-
-  g_sequence_free (self->sorters);
-
-  G_OBJECT_CLASS (gtk_multi_sorter_parent_class)->finalize (object);
-}
-
 static void
 gtk_multi_sorter_class_init (GtkMultiSorterClass *class)
 {
@@ -230,13 +215,12 @@ gtk_multi_sorter_class_init (GtkMultiSorterClass *class)
   sorter_class->get_order = gtk_multi_sorter_get_order;
 
   object_class->dispose = gtk_multi_sorter_dispose;
-  object_class->finalize = gtk_multi_sorter_finalize;
 }
 
 static void
 gtk_multi_sorter_init (GtkMultiSorter *self)
 {
-  self->sorters = g_sequence_new (NULL);
+  gtk_sorters_init (&self->sorters);
 }
 
 /**
@@ -274,7 +258,7 @@ gtk_multi_sorter_append (GtkMultiSorter *self,
   g_return_if_fail (GTK_IS_SORTER (sorter));
 
   g_signal_connect (sorter, "changed", G_CALLBACK (gtk_multi_sorter_changed_cb), self);
-  g_sequence_append (self->sorters, sorter);
+  gtk_sorters_append (&self->sorters, sorter);
 
   gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_MORE_STRICT);
 }
@@ -293,17 +277,18 @@ void
 gtk_multi_sorter_remove (GtkMultiSorter *self,
                          guint           position)
 {
-  GSequenceIter *iter;
   guint length;
+  GtkSorter *sorter;
 
   g_return_if_fail (GTK_IS_MULTI_SORTER (self));
 
-  length = g_sequence_get_length (self->sorters);
+  length = gtk_sorters_get_size (&self->sorters);
   if (position >= length)
     return;
 
-  iter = g_sequence_get_iter_at_pos (self->sorters, position);
-  gtk_multi_sorter_remove_iter (self, iter);
+  sorter = gtk_sorters_get (&self->sorters, position);
+  g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
+  gtk_sorters_splice (&self->sorters, position, 1, NULL, 0);
 
   gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_LESS_STRICT);
 }


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