Replacing g_hash_table with d_store
So I was working on something today, maemo-todo, which uses d_store as main storage. And I'm working on this tag button toolbar, which can also do drag and drop. Anyway, to keep a long story short. I needed a quick way to go from a string to a particular button. And I started using a g_hash_table for that.
But that got me thinking, why didn't I use d_store for that, it does the same job, and should perform the same. And I'm already using it. So what does g_hash_table have that d_store does not? And I guess just one function call: d_row_get(). Retreiving a value from d_store will require a d_row_get() after a d_select_first().
But then I needed (stupid gtk) a gtk_tool_item and a gtk_button in my hash. So I refactored the g_hash_table_* calls with d_store. Easy as pie:
declaration:
- GHashTable * buttons;
+ d_store * buttons;
initialization:
- priv->buttons = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+ priv->buttons = d_store_create(NULL);
adding:
- g_hash_table_insert(priv->buttons, g_strdup(tag), button);
+ d_insert(priv->buttons, D_STRING(tag),
+ D_POINTER(button), D_POINTER(item), NULL);
retrieving:
- button = (GtkToolItem *) g_hash_table_lookup(priv->buttons, tag);
- if (button) {
+ row = d_select_first(priv->buttons, 1, D_STRING(tag));
+ if (row) {
+ button = (GtkWidget *) D_TOPOINTER(d_row_get(row, 2));
checking:
- int in_item = g_hash_table_lookup(priv->buttons,
- D_TOSTRING(d_row_get(row, TAG_NAME))) != NULL;
+ int in_item = d_select_first(priv->buttons,
+ 1, d_row_get(row, TAG_NAME)) != NULL;
(Only minimal edits for brevity, see commit here)
This is from real code (thanks darcs) ... not a school example. Pretty nice.
Now I didn't have to juggle around a custom struct containing both the toolbar item and the button. And I'm eating my own dogfood.
Last modified: 2007-11-19 20:17 GMT