GListStore: add sorted insert function
authorRyan Lortie <desrt@desrt.ca>
Tue, 3 Feb 2015 12:18:10 +0000 (13:18 +0100)
committerRyan Lortie <desrt@desrt.ca>
Tue, 3 Feb 2015 14:46:48 +0000 (15:46 +0100)
Add g_list_store_insert_sorted() which takes a GCompareDataFunc to
decide where to insert.  This ends up being a very trivial function,
thanks to GSequence.

https://bugzilla.gnome.org/show_bug.cgi?id=743927

docs/reference/gio/gio-sections.txt
gio/gliststore.c
gio/gliststore.h

index a487391..8460894 100644 (file)
@@ -4287,6 +4287,7 @@ GListStore
 g_list_store_get_type
 g_list_store_new
 g_list_store_insert
+g_list_store_insert_sorted
 g_list_store_append
 g_list_store_remove
 g_list_store_remove_all
index 407dcbf..990c794 100644 (file)
@@ -269,6 +269,45 @@ g_list_store_insert (GListStore *store,
 }
 
 /**
+ * g_list_store_insert_sorted:
+ * @store: a #GListStore
+ * @item: the new item
+ *
+ * Inserts @item into @store at a position to be determined by the
+ * @compare_func.
+ *
+ * The list must already be sorted before calling this function or the
+ * result is undefined.  Usually you would approach this by only ever
+ * inserting items by way of this function.
+ *
+ * This function takes a ref on @item.
+ *
+ * Returns: the position at which @item was inserted
+ *
+ * Since: 2.44
+ */
+guint
+g_list_store_insert_sorted (GListStore       *store,
+                            gpointer          item,
+                            GCompareDataFunc  compare_func,
+                            gpointer          user_data)
+{
+  GSequenceIter *it;
+  guint position;
+
+  g_return_if_fail (G_IS_LIST_STORE (store));
+  g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (item), store->item_type));
+  g_return_if_fail (compare_func != NULL);
+
+  it = g_sequence_insert_sorted (store->items, g_object_ref (item), compare_func, user_data);
+  position = g_sequence_iter_get_position (it);
+
+  g_list_store_items_changed (store, position, 0, 1);
+
+  return position;
+}
+
+/**
  * g_list_store_append:
  * @store: a #GListStore
  * @item: the new item
index f62b378..88ce941 100644 (file)
@@ -44,6 +44,12 @@ void                    g_list_store_insert                             (GListSt
                                                                          gpointer    item);
 
 GLIB_AVAILABLE_IN_2_44
+guint                   g_list_store_insert_sorted                      (GListStore       *store,
+                                                                         gpointer          item,
+                                                                         GCompareDataFunc  compare_func,
+                                                                         gpointer          user_data);
+
+GLIB_AVAILABLE_IN_2_44
 void                    g_list_store_append                             (GListStore *store,
                                                                          gpointer    item);