[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.
This commit is contained in:
Christopher Lam 2021-02-22 18:17:02 +08:00
parent 0a22f688a6
commit e79db92d8d
2 changed files with 18 additions and 6 deletions

View File

@ -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 #################*/

View File

@ -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.