[gnome-todo] list-store: Cache length



commit 4dd61d2280830fc4dbdc0fc6fff02b952bceb598
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Sep 12 15:38:58 2018 -0300

    list-store: Cache length

 src/contrib/gtd-list-store.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
---
diff --git a/src/contrib/gtd-list-store.c b/src/contrib/gtd-list-store.c
index b195746..fbad702 100644
--- a/src/contrib/gtd-list-store.c
+++ b/src/contrib/gtd-list-store.c
@@ -54,7 +54,8 @@ struct _GtdListStore
   GHashTable         *item_to_iter;
 
   /* cache */
-  guint               last_position;
+  gint64              length;
+  gint64              last_position;
   GSequenceIter      *last_iter;
 };
 
@@ -94,9 +95,12 @@ gtd_list_store_items_changed (GtdListStore *store,
   if (position <= store->last_position)
     {
       store->last_iter = NULL;
-      store->last_position = -1u;
+      store->last_position = -1;
     }
 
+  store->length -= removed;
+  store->length += added;
+
   g_list_model_items_changed (G_LIST_MODEL (store), position, removed, added);
 }
 
@@ -185,7 +189,7 @@ gtd_list_store_get_n_items (GListModel *list)
 {
   GtdListStore *store = GTD_LIST_STORE (list);
 
-  return g_sequence_get_length (store->items);
+  return store->length;
 }
 
 static gpointer
@@ -195,7 +199,7 @@ gtd_list_store_get_item (GListModel *list,
   GtdListStore *store = GTD_LIST_STORE (list);
   GSequenceIter *it = NULL;
 
-  if (store->last_position != -1u)
+  if (store->last_position != -1)
     {
       if (store->last_position == position + 1)
         it = g_sequence_iter_prev (store->last_iter);
@@ -230,7 +234,7 @@ gtd_list_store_init (GtdListStore *store)
 {
   store->item_to_iter = g_hash_table_new (g_direct_hash, g_direct_equal);
   store->items = g_sequence_new (g_object_unref);
-  store->last_position = -1u;
+  store->last_position = -1;
 }
 
 /**


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