[gnome-todo/gbsneto/task-model: 7/14] list-store: Introduce gtd_list_store_get_item_position



commit dedc18d0185b83b551b92393714309153ed32323
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Sep 10 21:46:56 2018 -0300

    list-store: Introduce gtd_list_store_get_item_position
    
    A nice API to get the position of an item. It is an O(1)
    operation, and will be used in the future.

 src/contrib/gtd-list-store.c | 24 ++++++++++++++++++++++++
 src/contrib/gtd-list-store.h |  3 +++
 2 files changed, 27 insertions(+)
---
diff --git a/src/contrib/gtd-list-store.c b/src/contrib/gtd-list-store.c
index 5a4f631..b195746 100644
--- a/src/contrib/gtd-list-store.c
+++ b/src/contrib/gtd-list-store.c
@@ -534,3 +534,27 @@ gtd_list_store_splice (GtdListStore *store,
 
   gtd_list_store_items_changed (store, position, n_removals, n_additions);
 }
+
+/**
+ * gtd_list_store_get_item_position:
+ * @store: a #GtdListStore
+ * @item: the item to retrieve the position
+ *
+ * Retrieves the position of @items inside @store. It is a programming
+ * error to pass an @item that is not contained in @store.
+ *
+ * Returns: the position of @item in @store.
+ */
+guint
+gtd_list_store_get_item_position (GtdListStore *store,
+                                  gpointer      item)
+{
+  GSequenceIter *iter;
+
+  g_return_val_if_fail (GTD_IS_LIST_STORE (store), 0);
+
+  iter = g_hash_table_lookup (store->item_to_iter, item);
+  g_assert (iter != NULL);
+
+  return g_sequence_iter_get_position (iter);
+}
diff --git a/src/contrib/gtd-list-store.h b/src/contrib/gtd-list-store.h
index a97eb0b..84e8882 100644
--- a/src/contrib/gtd-list-store.h
+++ b/src/contrib/gtd-list-store.h
@@ -60,4 +60,7 @@ void                 gtd_list_store_splice                       (GtdListStore
                                                                   gpointer           *additions,
                                                                   guint               n_additions);
 
+guint                gtd_list_store_get_item_position            (GtdListStore       *store,
+                                                                  gpointer            item);
+
 G_END_DECLS


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