gnc_commodity_namespace->cm_table is an unordered_map

This commit is contained in:
Christopher Lam 2024-05-01 07:39:12 +08:00
parent 2f4ab3c026
commit 2ddb191953

View File

@ -107,13 +107,15 @@ struct _GncCommodityClass
static void commodity_free(gnc_commodity * cm); static void commodity_free(gnc_commodity * cm);
static void gnc_commodity_set_default_symbol(gnc_commodity *, const char *); static void gnc_commodity_set_default_symbol(gnc_commodity *, const char *);
using StrCommodityMap = std::unordered_map<std::string,gnc_commodity*>;
struct gnc_commodity_namespace_s struct gnc_commodity_namespace_s
{ {
QofInstance inst; QofInstance inst;
const gchar *name; const gchar *name;
gboolean iso4217; gboolean iso4217;
GHashTable * cm_table; StrCommodityMap cm_table;
GList * cm_list; GList * cm_list;
}; };
@ -1659,7 +1661,7 @@ gnc_commodity_obtain_twin (const gnc_commodity *from, QofBook *book)
static void static void
count_coms(gpointer key, gpointer value, gpointer user_data) count_coms(gpointer key, gpointer value, gpointer user_data)
{ {
GHashTable *tbl = ((gnc_commodity_namespace*)value)->cm_table; auto tbl = ((gnc_commodity_namespace*)value)->cm_table;
guint *count = (guint*)user_data; guint *count = (guint*)user_data;
if (g_strcmp0((char*)key, GNC_COMMODITY_NS_CURRENCY) == 0) if (g_strcmp0((char*)key, GNC_COMMODITY_NS_CURRENCY) == 0)
@ -1670,7 +1672,7 @@ count_coms(gpointer key, gpointer value, gpointer user_data)
if (!value) return; if (!value) return;
*count += g_hash_table_size(tbl); *count += tbl.size();
} }
guint guint
@ -1712,7 +1714,7 @@ gnc_commodity_table_lookup(const gnc_commodity_table * table,
if (it != gnc_new_iso_codes.end()) if (it != gnc_new_iso_codes.end())
mnemonic = it->second.c_str(); mnemonic = it->second.c_str();
} }
return GNC_COMMODITY(g_hash_table_lookup(nsp->cm_table, (gpointer)mnemonic)); return GNC_COMMODITY(nsp->cm_table[mnemonic]);
} }
else else
{ {
@ -1838,10 +1840,8 @@ gnc_commodity_table_insert(gnc_commodity_table * table,
nsp = gnc_commodity_table_add_namespace(table, ns_name, book); nsp = gnc_commodity_table_add_namespace(table, ns_name, book);
PINFO ("insert %p %s into nsp=%p %s", priv->mnemonic, priv->mnemonic, PINFO ("insert %p %s into nsp=%p %s", priv->mnemonic, priv->mnemonic,
nsp->cm_table, nsp->name); &nsp->cm_table, nsp->name);
g_hash_table_insert(nsp->cm_table, nsp->cm_table [priv->mnemonic] = comm;
(gpointer)CACHE_INSERT(priv->mnemonic),
(gpointer)comm);
nsp->cm_list = g_list_append(nsp->cm_list, comm); nsp->cm_list = g_list_append(nsp->cm_list, comm);
qof_event_gen (&comm->inst, QOF_EVENT_ADD, nullptr); qof_event_gen (&comm->inst, QOF_EVENT_ADD, nullptr);
@ -1877,7 +1877,7 @@ gnc_commodity_table_remove(gnc_commodity_table * table,
if (!nsp) return; if (!nsp) return;
nsp->cm_list = g_list_remove(nsp->cm_list, comm); nsp->cm_list = g_list_remove(nsp->cm_list, comm);
g_hash_table_remove (nsp->cm_table, priv->mnemonic); nsp->cm_table.erase (priv->mnemonic);
/* XXX minor mem leak, should remove the key as well */ /* XXX minor mem leak, should remove the key as well */
} }
@ -1914,21 +1914,6 @@ hash_keys_helper (const char* key, gnc_commodity* value, std::vector<std::string
l->push_back (key); l->push_back (key);
} }
static void
hash_values_helper(gpointer key, gpointer value, gpointer data)
{
auto l = (GList**)data;
*l = g_list_prepend(*l, value);
}
static GList *
g_hash_table_values(GHashTable * table)
{
GList * l = nullptr;
g_hash_table_foreach(table, &hash_values_helper, (gpointer) &l);
return l;
}
/******************************************************************** /********************************************************************
* gnc_commodity_table_get_namespaces * gnc_commodity_table_get_namespaces
* see if any commodities in the namespace exist * see if any commodities in the namespace exist
@ -2000,10 +1985,8 @@ commodity_table_get_all_noncurrency_commodities(const gnc_commodity_table* table
ns = gnc_commodity_table_find_namespace(table, name_space.c_str()); ns = gnc_commodity_table_find_namespace(table, name_space.c_str());
if (!ns) if (!ns)
continue; continue;
auto val = g_hash_table_values(ns->cm_table); std::for_each (ns->cm_table.begin(), ns->cm_table.end(),
for (auto n = val; n; n = n->next) [&retval](auto it){ retval.push_back (it.second); });
retval.push_back (GNC_COMMODITY(n->data));
g_list_free (val);
} }
return retval; return retval;
} }
@ -2022,10 +2005,8 @@ gnc_commodity_table_get_commodities(const gnc_commodity_table * table,
if (!ns) if (!ns)
return retval; return retval;
auto val = g_hash_table_values(ns->cm_table); std::for_each (ns->cm_table.begin(), ns->cm_table.end(),
for (auto n = val; n; n = n->next) [&retval](auto it){ retval.push_back (it.second); });
retval.push_back (GNC_COMMODITY(n->data));
g_list_free (val);
return retval; return retval;
} }
@ -2087,7 +2068,9 @@ gnc_commodity_table_get_quotable_commodities(const gnc_commodity_table * table)
ns = gnc_commodity_table_find_namespace(table, name_space); ns = gnc_commodity_table_find_namespace(table, name_space);
if (ns) if (ns)
{ {
g_hash_table_foreach(ns->cm_table, get_quotables_helper1, &rv); std::for_each (ns->cm_table.begin(), ns->cm_table.end(),
[&rv](auto it)
{ get_quotables_helper1 (nullptr, it.second, &rv); });
} }
} }
} }
@ -2138,7 +2121,7 @@ gnc_commodity_table_add_namespace(gnc_commodity_table * table,
if (!ns) if (!ns)
{ {
ns = static_cast<gnc_commodity_namespace*>(g_object_new(GNC_TYPE_COMMODITY_NAMESPACE, nullptr)); ns = static_cast<gnc_commodity_namespace*>(g_object_new(GNC_TYPE_COMMODITY_NAMESPACE, nullptr));
ns->cm_table = g_hash_table_new(g_str_hash, g_str_equal); new (&ns->cm_table) StrCommodityMap ();
ns->name = CACHE_INSERT(static_cast<const char*>(name_space)); ns->name = CACHE_INSERT(static_cast<const char*>(name_space));
ns->iso4217 = gnc_commodity_namespace_is_iso(name_space); ns->iso4217 = gnc_commodity_namespace_is_iso(name_space);
qof_instance_init_data (&ns->inst, GNC_ID_COMMODITY_NAMESPACE, book); qof_instance_init_data (&ns->inst, GNC_ID_COMMODITY_NAMESPACE, book);
@ -2180,15 +2163,6 @@ gnc_commodity_find_commodity_by_guid(const GncGUID *guid, QofBook *book)
* delete a namespace * delete a namespace
********************************************************************/ ********************************************************************/
static int
ns_helper(gpointer key, gpointer value, gpointer user_data)
{
auto c = GNC_COMMODITY(value);
gnc_commodity_destroy(c);
CACHE_REMOVE(static_cast<char*>(key)); /* key is commodity mnemonic */
return TRUE;
}
void void
gnc_commodity_table_delete_namespace(gnc_commodity_table * table, gnc_commodity_table_delete_namespace(gnc_commodity_table * table,
const char * name_space) const char * name_space)
@ -2208,8 +2182,9 @@ gnc_commodity_table_delete_namespace(gnc_commodity_table * table,
g_list_free(ns->cm_list); g_list_free(ns->cm_list);
ns->cm_list = nullptr; ns->cm_list = nullptr;
g_hash_table_foreach_remove(ns->cm_table, ns_helper, nullptr); std::for_each (ns->cm_table.begin(), ns->cm_table.end(),
g_hash_table_destroy(ns->cm_table); [](auto it){ gnc_commodity_destroy (it.second); });
ns->cm_table.~StrCommodityMap ();
CACHE_REMOVE(ns->name); CACHE_REMOVE(ns->name);
qof_event_gen (&ns->inst, QOF_EVENT_DESTROY, nullptr); qof_event_gen (&ns->inst, QOF_EVENT_DESTROY, nullptr);
@ -2245,8 +2220,9 @@ iter_commodity (gpointer key, gpointer value, gpointer user_data)
static void static void
iter_namespace (gpointer key, gpointer value, gpointer user_data) iter_namespace (gpointer key, gpointer value, gpointer user_data)
{ {
GHashTable *namespace_hash = ((gnc_commodity_namespace *) value)->cm_table; auto namespace_hash = ((gnc_commodity_namespace *) value)->cm_table;
g_hash_table_foreach (namespace_hash, iter_commodity, user_data); std::for_each (namespace_hash.begin(), namespace_hash.end(),
[user_data](auto it){ iter_commodity (nullptr, it.second, user_data); });
} }
gboolean gboolean