Skip to content

keyboard: Rewrite layout intersection algorithm

The current layout intersection algorithm compares pointers to strings. It assumes that gnome_xkb_info_get_layouts_for_country() and gnome_xkb_info_get_layouts_for_language() will return two GList containing pointers to the same strings. Looking at libgnome-desktop[1] it seems like this is the case: given the same layout, the same pointer to the layout id will be stored in both lists. However, this is an implementation detail and we should not rely on that.

Rewrite the intersection algorithm in order to compare the strings. While at it, use a more efficient algorithm: add all the items of the first list to a set, then iterate over the second list and check if the item is in the set. If this happens, remove the item from the set and add it to the intersection list.

Probably the layout lists are not big enough to notice the difference, but the complexity goes from O(N*M) to O(N+M).

[1] https://gitlab.gnome.org/GNOME/gnome-desktop/-/blob/3c8834af09ff66c456662a08ef92e6087e755e38/libgnome-desktop/gnome-xkb-info.c#L281

Ping @felipeborges and @sundeep

Merge request reports