GListStore: add sorted insert function

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
This commit is contained in:
Ryan Lortie 2015-02-03 13:18:10 +01:00
parent 6161b285da
commit 3f3eac474b
3 changed files with 46 additions and 0 deletions

View 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

View File

@ -268,6 +268,45 @@ g_list_store_insert (GListStore *store,
g_list_store_items_changed (store, position, 0, 1);
}
/**
* 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

View File

@ -43,6 +43,12 @@ void g_list_store_insert (GListSt
guint position,
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);