After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 697828 - g_hash_table_add() should return a boolean
g_hash_table_add() should return a boolean
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
: 668659 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2013-04-11 18:38 UTC by Xavier Claessens
Modified: 2013-11-24 15:38 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GHashTable: return boolean in _add(), _insert() and _replace() (5.73 KB, patch)
2013-04-15 15:13 UTC, Xavier Claessens
none Details | Review

Description Xavier Claessens 2013-04-11 18:38:48 UTC
g_hash_table_add() should return TRUE if the element was not yet in the table, and FALSE otherwise. _insert() and _replace() could do the same. That would allow doing things like:

if (g_hash_table_add (table, foo))
  g_signal_emit (self, signals[ADDED], 0, foo);
Comment 1 Xavier Claessens 2013-04-11 18:40:08 UTC
Currently I have to do 2 lookups:

if (g_hash_table_contains (table, foo))
  return;
g_hash_table_add (table, foo);
g_signal_emit (self, signals[ADDED], 0, foo);
Comment 2 Matthias Clasen 2013-04-12 09:54:42 UTC
I'm not opposed to this idea.

But it is a little more tricky for replace - what should true mean here ? that is was replaced ? in that case, it kinda means the opposite than it does for insert, where true means 'was not in the table yet'.
Comment 3 Xavier Claessens 2013-04-12 10:34:36 UTC
Note that I can already do

if (g_hash_table_remove (table, foo))
  g_signal_emit();

so doing the same on _add() would make it more consistent.

About _replace() VS _insert() that's actually something I've never understood. In which case would I want to use _insert() instead of _replace()?

Indeed the meaning of the returned boolean needs to be carefully defined and could be counter-intuitive... I would say TRUE means "a value was already there" so _insert() and _replace() would always return the same.
Comment 4 Xavier Claessens 2013-04-15 15:13:20 UTC
Created attachment 241571 [details] [review]
GHashTable: return boolean in _add(), _insert() and _replace()
Comment 5 Allison Karlitskaya (desrt) 2013-04-16 12:41:13 UTC
I prefer to handle this by improving the GHashTableIter API, which would allow quite a lot of really interesting uses and would also avoid the double-lookup you are annoyed by here.  mclasen muses about some possibilities in bug 630336.
Comment 6 Matthias Clasen 2013-08-17 21:47:18 UTC
*** Bug 668659 has been marked as a duplicate of this bug. ***
Comment 7 Xavier Claessens 2013-11-24 15:38:57 UTC
In my patch the doc is more complete, I think it is important to at least tell since which version there is a return value.