From e79db92d8d1ec067e629247b32a8d674ffceb4e7 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Mon, 22 Feb 2021 18:17:02 +0800 Subject: [PATCH] [Split.c] xaccSplitListGetUniqueTransactionsReversed Same as xaccSplitListGetUniqueTransactions but doesn't reverse the list prior to returning. To be used by gnc-tree-model-split-reg.c Several optimizations * doesn't call g_list_find and g_list_append for every iteration * uses g_hash_table to cache list of txns already added instead of g_list_find * does not reverse the result, thereby returning a reversed list. --- libgnucash/engine/Split.c | 23 +++++++++++++++++------ libgnucash/engine/Split.h | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/libgnucash/engine/Split.c b/libgnucash/engine/Split.c index 4f74b37094..5b3ecd6a37 100644 --- a/libgnucash/engine/Split.c +++ b/libgnucash/engine/Split.c @@ -893,21 +893,32 @@ xaccSplitEqual(const Split *sa, const Split *sb, * xaccSplitListGetUniqueTransactions ********************************************************************/ GList * -xaccSplitListGetUniqueTransactions(const GList *splits) +xaccSplitListGetUniqueTransactionsReversed (const GList *splits) { - const GList *snode; + GHashTable *txn_hash = g_hash_table_new (NULL, NULL); GList *transList = NULL; + const GList *snode; - for(snode = splits; snode; snode = snode->next) + for (snode = splits; snode; snode = snode->next) { Transaction *trans = xaccSplitGetParent((Split *)(snode->data)); - GList *item = g_list_find (transList, trans); - if (item == NULL) - transList = g_list_append (transList, trans); + if (g_hash_table_contains (txn_hash, trans)) + continue; + + g_hash_table_insert (txn_hash, trans, NULL); + transList = g_list_prepend (transList, trans); } + g_hash_table_destroy (txn_hash); return transList; } + +GList * +xaccSplitListGetUniqueTransactions(const GList *splits) +{ + return g_list_reverse (xaccSplitListGetUniqueTransactionsReversed (splits)); +} + /*################## Added for Reg2 #################*/ diff --git a/libgnucash/engine/Split.h b/libgnucash/engine/Split.h index 7f2e2025f8..fffc8409cc 100644 --- a/libgnucash/engine/Split.h +++ b/libgnucash/engine/Split.h @@ -359,6 +359,7 @@ Split * xaccSplitLookup (const GncGUID *guid, QofBook *book); /*################## Added for Reg2 #################*/ /* Get a GList of unique transactions containing the given list of Splits. */ +GList *xaccSplitListGetUniqueTransactionsReversed (const GList *splits); GList *xaccSplitListGetUniqueTransactions(const GList *splits); /*################## Added for Reg2 #################*/ /** Add a peer split to this split's lot-split list.