@@ -233,12 +233,80 @@ modulemd_hash_table_equals (GHashTable *a,
233233 return TRUE;
234234}
235235
236+
237+ gint
238+ modulemd_hash_table_compare (GHashTable * a ,
239+ GHashTable * b ,
240+ GCompareFunc value_compare_func )
241+ {
242+ g_autoptr (GPtrArray ) set_a = NULL ;
243+ g_autoptr (GPtrArray ) set_b = NULL ;
244+ int i = 0 ;
245+ gchar * key = NULL ;
246+ gchar * value_a = NULL ;
247+ gchar * value_b = NULL ;
248+ gint cmp ;
249+
250+ /* Get the ordered list of keys from each hashtable. */
251+ set_a = modulemd_ordered_str_keys (a , modulemd_strcmp_sort );
252+ set_b = modulemd_ordered_str_keys (b , modulemd_strcmp_sort );
253+
254+ for (i = 0 ; i < set_a -> len ; i ++ )
255+ {
256+ /* If we ran out of elements in the second set, it is shorter and thus the
257+ * lesser one.
258+ */
259+ if (i >= set_b -> len )
260+ {
261+ return 1 ;
262+ }
263+
264+ /* Compare the keys. */
265+ cmp =
266+ g_strcmp0 (g_ptr_array_index (set_a , i ), g_ptr_array_index (set_b , i ));
267+ if (cmp != 0 )
268+ {
269+ return cmp ;
270+ }
271+
272+ /* If the keys match, compare the values if needed. */
273+ if (value_compare_func )
274+ {
275+ key = g_ptr_array_index (set_a , i );
276+ value_a = g_hash_table_lookup (a , key );
277+ value_b = g_hash_table_lookup (b , key );
278+ cmp = value_compare_func (value_a , value_b );
279+ if (cmp != 0 )
280+ {
281+ return cmp ;
282+ }
283+ }
284+ }
285+
286+ /* If everything has been the same so far but there are more elements in the
287+ * second set, it is longer and thus the greater one.
288+ */
289+ if (set_a -> len < set_b -> len )
290+ {
291+ return -1 ;
292+ }
293+
294+ /* If we made it this far, they are equal. */
295+ return 0 ;
296+ }
297+
236298gint
237299modulemd_strcmp_sort (gconstpointer a , gconstpointer b )
238300{
239301 return g_strcmp0 (* (const gchar * * )a , * (const gchar * * )b );
240302}
241303
304+ gint
305+ modulemd_strcmp_wrapper (gconstpointer a , gconstpointer b )
306+ {
307+ return g_strcmp0 ((gchar * )a , (gchar * )b );
308+ }
309+
242310GPtrArray *
243311modulemd_ordered_str_keys (GHashTable * htable , GCompareFunc compare_func )
244312{
0 commit comments