mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
[gnc-pricedb.cpp] convert hash_table_to_list to hash_table_to_vector
This commit is contained in:
parent
5e09b662a1
commit
79c0fe4724
@ -56,12 +56,6 @@ enum
|
|||||||
PROP_VALUE, /* Table, 2 fields (numeric) */
|
PROP_VALUE, /* Table, 2 fields (numeric) */
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
gpointer key;
|
|
||||||
gpointer value;
|
|
||||||
} HashEntry;
|
|
||||||
|
|
||||||
/* Like strcmp, returns -1 if a < b, +1 if a > b, and 0 if they're equal. */
|
/* Like strcmp, returns -1 if a < b, +1 if a > b, and 0 if they're equal. */
|
||||||
static inline int
|
static inline int
|
||||||
time64_cmp (time64 a, time64 b)
|
time64_cmp (time64 a, time64 b)
|
||||||
@ -69,30 +63,22 @@ time64_cmp (time64 a, time64 b)
|
|||||||
return a < b ? -1 : a > b ? 1 : 0;
|
return a < b ? -1 : a > b ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
using CommodityPtrPair = std::pair<const gnc_commodity*, gpointer>;
|
||||||
hash_entry_insert(gpointer key, gpointer val, gpointer user_data)
|
using CommodityPtrPairVec = std::vector<CommodityPtrPair>;
|
||||||
{
|
|
||||||
GSList **result = (GSList **) user_data;
|
|
||||||
HashEntry *entry = g_new(HashEntry, 1);
|
|
||||||
|
|
||||||
entry->key = key;
|
|
||||||
entry->value = val;
|
|
||||||
*result = g_slist_prepend(*result, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
static GSList *
|
|
||||||
hash_table_to_list(GHashTable *table)
|
|
||||||
{
|
|
||||||
GSList *result_list = NULL;
|
|
||||||
g_hash_table_foreach(table, hash_entry_insert, &result_list);
|
|
||||||
return result_list;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
hash_entry_free_gfunc(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
hash_entry_insert(const gnc_commodity* key, const gpointer val, CommodityPtrPairVec *result)
|
||||||
{
|
{
|
||||||
HashEntry *entry = (HashEntry *) data;
|
result->emplace_back (key, val);
|
||||||
g_free(entry);
|
}
|
||||||
|
|
||||||
|
static CommodityPtrPairVec
|
||||||
|
hash_table_to_vector (GHashTable *table)
|
||||||
|
{
|
||||||
|
CommodityPtrPairVec result_vec;
|
||||||
|
result_vec.reserve (g_hash_table_size (table));
|
||||||
|
g_hash_table_foreach(table, (GHFunc)hash_entry_insert, &result_vec);
|
||||||
|
return result_vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* GObject Initialization */
|
/* GObject Initialization */
|
||||||
@ -2766,84 +2752,53 @@ pricedb_pricelist_traversal(GNCPriceDB *db,
|
|||||||
return foreach_data.ok;
|
return foreach_data.ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gint
|
static bool
|
||||||
compare_hash_entries_by_commodity_key(gconstpointer a, gconstpointer b)
|
compare_hash_entries_by_commodity_key (const CommodityPtrPair& he_a, const CommodityPtrPair& he_b)
|
||||||
{
|
{
|
||||||
HashEntry *he_a = (HashEntry *) a;
|
auto ca = he_a.first;
|
||||||
HashEntry *he_b = (HashEntry *) b;
|
auto cb = he_b.first;
|
||||||
gnc_commodity *ca;
|
|
||||||
gnc_commodity *cb;
|
|
||||||
int cmp_result;
|
|
||||||
|
|
||||||
if (a == b) return 0;
|
if (ca == cb || !cb)
|
||||||
if (!a && !b) return 0;
|
return false;
|
||||||
if (!a) return -1;
|
|
||||||
if (!b) return 1;
|
|
||||||
|
|
||||||
ca = (gnc_commodity *) he_a->key;
|
if (!ca)
|
||||||
cb = (gnc_commodity *) he_b->key;
|
return true;
|
||||||
|
|
||||||
cmp_result = g_strcmp0(gnc_commodity_get_namespace(ca),
|
auto cmp_result = g_strcmp0 (gnc_commodity_get_namespace (ca), gnc_commodity_get_namespace (cb));
|
||||||
gnc_commodity_get_namespace(cb));
|
|
||||||
|
|
||||||
if (cmp_result != 0) return cmp_result;
|
if (cmp_result)
|
||||||
|
return (cmp_result < 0);
|
||||||
|
|
||||||
return g_strcmp0(gnc_commodity_get_mnemonic(ca),
|
return g_strcmp0(gnc_commodity_get_mnemonic (ca), gnc_commodity_get_mnemonic (cb)) < 0;
|
||||||
gnc_commodity_get_mnemonic(cb));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static bool
|
||||||
stable_price_traversal(GNCPriceDB *db,
|
stable_price_traversal(GNCPriceDB *db,
|
||||||
gboolean (*f)(GNCPrice *p, gpointer user_data),
|
gboolean (*f)(GNCPrice *p, gpointer user_data),
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GSList *currency_hashes = NULL;
|
g_return_val_if_fail (db && f, false);
|
||||||
gboolean ok = TRUE;
|
|
||||||
GSList *i = NULL;
|
|
||||||
|
|
||||||
if (!db || !f) return FALSE;
|
auto currency_hashes = hash_table_to_vector (db->commodity_hash);
|
||||||
|
std::sort (currency_hashes.begin(), currency_hashes.end(), compare_hash_entries_by_commodity_key);
|
||||||
|
|
||||||
currency_hashes = hash_table_to_list(db->commodity_hash);
|
for (const auto& entry : currency_hashes)
|
||||||
currency_hashes = g_slist_sort(currency_hashes,
|
|
||||||
compare_hash_entries_by_commodity_key);
|
|
||||||
|
|
||||||
for (i = currency_hashes; i; i = i->next)
|
|
||||||
{
|
{
|
||||||
HashEntry *entry = (HashEntry *) i->data;
|
auto price_lists = hash_table_to_vector (static_cast<GHashTable*>(entry.second));
|
||||||
GHashTable *currency_hash = (GHashTable *) entry->value;
|
std::sort (price_lists.begin(), price_lists.end(), compare_hash_entries_by_commodity_key);
|
||||||
GSList *price_lists = hash_table_to_list(currency_hash);
|
|
||||||
GSList *j;
|
|
||||||
|
|
||||||
price_lists = g_slist_sort(price_lists, compare_hash_entries_by_commodity_key);
|
for (const auto& pricelist_entry : price_lists)
|
||||||
for (j = price_lists; j; j = j->next)
|
|
||||||
{
|
{
|
||||||
HashEntry *pricelist_entry = (HashEntry *) j->data;
|
for (auto node = static_cast<GList*>(pricelist_entry.second); node; node = node->next)
|
||||||
GList *price_list = (GList *) pricelist_entry->value;
|
|
||||||
GList *node;
|
|
||||||
|
|
||||||
for (node = (GList *) price_list; node; node = node->next)
|
|
||||||
{
|
{
|
||||||
GNCPrice *price = (GNCPrice *) node->data;
|
|
||||||
|
|
||||||
/* stop traversal when f returns FALSE */
|
/* stop traversal when f returns FALSE */
|
||||||
if (FALSE == ok) break;
|
if (!f(static_cast<GNCPrice *>(node->data), user_data))
|
||||||
if (!f(price, user_data)) ok = FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (price_lists)
|
|
||||||
{
|
|
||||||
g_slist_foreach(price_lists, hash_entry_free_gfunc, NULL);
|
|
||||||
g_slist_free(price_lists);
|
|
||||||
price_lists = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currency_hashes)
|
return true;
|
||||||
{
|
|
||||||
g_slist_foreach(currency_hashes, hash_entry_free_gfunc, NULL);
|
|
||||||
g_slist_free(currency_hashes);
|
|
||||||
}
|
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
|
Loading…
Reference in New Issue
Block a user