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 749371 - Use a GHashTable as a set when possible
Use a GHashTable as a set when possible
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2015-05-14 11:40 UTC by Garrett Regier
Modified: 2017-10-26 11:27 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Use a GHashTable as a set when possible (1.87 KB, patch)
2015-05-14 11:41 UTC, Garrett Regier
none Details | Review
Use a GHashTable as a set when possible (4.23 KB, patch)
2015-05-14 15:01 UTC, Garrett Regier
needs-work Details | Review
Use g_hash_table_add() where possible (5.09 KB, patch)
2015-05-14 15:02 UTC, Garrett Regier
needs-work Details | Review
Use hash tables as sets in various places (8.65 KB, patch)
2017-10-25 10:01 UTC, Philip Withnall
committed Details | Review

Description Garrett Regier 2015-05-14 11:40:21 UTC
There are two uses of it (excluding the tests) that could be using it as a set.
Comment 1 Garrett Regier 2015-05-14 11:41:14 UTC
Created attachment 303361 [details] [review]
Use a GHashTable as a set when possible
Comment 2 Emmanuele Bassi (:ebassi) 2015-05-14 13:01:56 UTC
Review of attachment 303361 [details] [review]:

You should probably use g_hash_table_add() instead of insert() with key == value.
Comment 3 Garrett Regier 2015-05-14 15:01:41 UTC
Created attachment 303376 [details] [review]
Use a GHashTable as a set when possible

(In reply to Emmanuele Bassi (:ebassi) from comment #2)
> Review of attachment 303361 [details] [review] [review]:
> 
> You should probably use g_hash_table_add() instead of insert() with key ==
> value.

Fixed.
Comment 4 Garrett Regier 2015-05-14 15:02:28 UTC
Created attachment 303377 [details] [review]
Use g_hash_table_add() where possible
Comment 5 Philip Withnall 2017-03-16 13:05:16 UTC
Review of attachment 303376 [details] [review]:

You should also convert g_hash_table_lookup() calls into g_hash_table_contains() calls, to make it really obvious the hash table is being used as a set.

::: tests/testglib.c
@@ -1054,3 @@
     {
       array[i] = i;
-      g_hash_table_insert (hash_table, &array[i], &array[i]);

Since this is a test suite, does it make sense to convert the calls here? I’d leave this alone, and rely on the tests in glib/tests/hash.c to cover the set-like functions.
Comment 6 Philip Withnall 2017-03-16 13:15:17 UTC
Review of attachment 303377 [details] [review]:

Why not merge this into the first patch?

Similarly, I think the changes here should also include use of g_hash_table_contains().

::: gio/gresource.c
@@ +809,2 @@
           for (i = 0; children[i] != NULL; i++)
+            g_hash_table_add (hash, children[i]);

(Looks like this one’s already been changed in master.)

::: glib/deprecated/grel.c
@@ +307,3 @@
   va_end (args);
   
+  g_hash_table_add (relation->all_tuples, tuple);

The way that GRelation uses hash tables is a little more complex than the other calling sites (use of keys and values in g_hash_table_foreach() callback functions). Since it’s deprecated, it might make sense to omit the changes in grel.c.
Comment 7 Philip Withnall 2017-10-06 11:58:19 UTC
Ping?
Comment 8 Philip Withnall 2017-10-25 10:01:59 UTC
Created attachment 362242 [details] [review]
Use hash tables as sets in various places

Where we were already treating GHashTables as sets, modify them to use
the set-specific APIs g_hash_table_add() and g_hash_table_contains(), to
make that usage more obvious and less prone to being broken.

Heavily based on patches by Garrett Regier <garrettregier@gmail.com>.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 9 Philip Withnall 2017-10-25 10:03:12 UTC
I squashed the two patches together, dropped the changes to testglib and GRelation, and fixed my other review comments. Ready for review.
Comment 10 Matthias Clasen 2017-10-26 11:14:42 UTC
Review of attachment 362242 [details] [review]:

looks fine to me
Comment 11 Philip Withnall 2017-10-26 11:27:47 UTC
Attachment 362242 [details] pushed as 3eacec1 - Use hash tables as sets in various places