Skip to content

Commit 811f526

Browse files
mmathesiussgallagher
authored andcommitted
Add BuildConfig and Buildopts sorting functions
Signed-off-by: Merlin Mathesius <[email protected]>
1 parent 05afb01 commit 811f526

File tree

9 files changed

+301
-7
lines changed

9 files changed

+301
-7
lines changed

modulemd/include/private/modulemd-build-config.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ modulemd_build_config_validate (ModulemdBuildConfig *buildconfig,
347347
ModulemdBuildConfig *
348348
modulemd_build_config_copy (ModulemdBuildConfig *self);
349349

350+
350351
/**
351352
* modulemd_build_config_equals:
352353
* @self_1: A pointer to a #ModulemdBuildConfig object.
@@ -362,4 +363,19 @@ gboolean
362363
modulemd_build_config_equals (ModulemdBuildConfig *self_1,
363364
ModulemdBuildConfig *self_2);
364365

366+
367+
/**
368+
* modulemd_build_config_compare:
369+
* @self_1: (in): A pointer to a #ModulemdBuildConfig object.
370+
* @self_2: (in): A pointer to a #ModulemdBuildConfig object.
371+
*
372+
* Returns: Less than zero if @self_1 sorts less than @self_2, zero for equal,
373+
* greater than zero if @self_1 is greater than @self_2.
374+
*
375+
* Since: 2.10
376+
*/
377+
gint
378+
modulemd_build_config_compare (ModulemdBuildConfig *self_1,
379+
ModulemdBuildConfig *self_2);
380+
365381
G_END_DECLS

modulemd/include/private/modulemd-buildopts-private.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,18 @@ gboolean
6363
modulemd_buildopts_emit_yaml (ModulemdBuildopts *self,
6464
yaml_emitter_t *emitter,
6565
GError **error);
66+
67+
68+
/**
69+
* modulemd_buildopts_compare:
70+
* @self_1: (in): A #ModulemdBuildopts object.
71+
* @self_2: (in): A #ModulemdBuildopts object.
72+
*
73+
* Returns: Less than zero if @self_1 sorts less than @self_2, zero for equal,
74+
* greater than zero if @self_1 is greater than @self_2.
75+
*
76+
* Since: 2.10
77+
*/
78+
gint
79+
modulemd_buildopts_compare (ModulemdBuildopts *self_1,
80+
ModulemdBuildopts *self_2);

modulemd/include/private/modulemd-util.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,27 @@ modulemd_hash_table_equals (GHashTable *a,
205205
GHashTable *b,
206206
GEqualFunc compare_func);
207207

208+
/*
209+
* modulemd_hash_table_compare:
210+
* @a: (in): A #GHashTable object.
211+
* @b: (in): A #GHashTable object.
212+
* @value_compare_func: (in): A #GCompareFunc function that is called to compare
213+
* pairs of #GHashTable values for sorting. If NULL, only the #GHashTable keys
214+
* are compared.
215+
*
216+
* Using guidance from
217+
* https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types
218+
*
219+
* Returns: Less than zero if @a sorts less than @b, zero for equal, greater than
220+
* zero if @a is greater than @b.
221+
*
222+
* Since: 2.10
223+
*/
224+
gint
225+
modulemd_hash_table_compare (GHashTable *a,
226+
GHashTable *b,
227+
GCompareFunc value_compare_func);
228+
208229
/**
209230
* modulemd_strcmp_sort:
210231
* @a: A #gconstpointer.
@@ -218,6 +239,19 @@ modulemd_hash_table_equals (GHashTable *a,
218239
gint
219240
modulemd_strcmp_sort (gconstpointer a, gconstpointer b);
220241

242+
/**
243+
* modulemd_strcmp_wrapper:
244+
* @a: (in): A #gconstpointer.
245+
* @b: (in): A #gconstpointer.
246+
*
247+
* Returns: 0 if @a and @b are identical strings, a negative value if @a is less
248+
* than @b, and a positive value if @a is greater than @b.
249+
*
250+
* Since: 2.10
251+
*/
252+
gint
253+
modulemd_strcmp_wrapper (gconstpointer a, gconstpointer b);
254+
221255
/**
222256
* modulemd_ordered_str_keys:
223257
* @htable: A #GHashTable.

modulemd/modulemd-build-config.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,67 @@ modulemd_build_config_equals (ModulemdBuildConfig *self_1,
722722

723723
return TRUE;
724724
}
725+
726+
727+
/* return less than zero if first arg is less than second arg,
728+
* zero for equal,
729+
* greater than zero if first arg is greater than second arg.
730+
*/
731+
gint
732+
modulemd_build_config_compare (ModulemdBuildConfig *self_1,
733+
ModulemdBuildConfig *self_2)
734+
{
735+
gint cmp;
736+
737+
if (!self_1 && !self_2)
738+
{
739+
return 0;
740+
}
741+
742+
if (!self_1)
743+
{
744+
return -1;
745+
}
746+
747+
if (!self_2)
748+
{
749+
return 1;
750+
}
751+
752+
g_return_val_if_fail (MODULEMD_IS_BUILD_CONFIG (self_1), 1);
753+
g_return_val_if_fail (MODULEMD_IS_BUILD_CONFIG (self_2), -1);
754+
755+
cmp = g_strcmp0 (self_1->context, self_2->context);
756+
if (cmp != 0)
757+
{
758+
return cmp;
759+
}
760+
761+
cmp = g_strcmp0 (self_1->platform, self_2->platform);
762+
if (cmp != 0)
763+
{
764+
return cmp;
765+
}
766+
767+
cmp = modulemd_hash_table_compare (
768+
self_1->buildrequires, self_2->buildrequires, modulemd_strcmp_wrapper);
769+
if (cmp != 0)
770+
{
771+
return cmp;
772+
}
773+
774+
cmp = modulemd_hash_table_compare (
775+
self_1->requires, self_2->requires, modulemd_strcmp_wrapper);
776+
if (cmp != 0)
777+
{
778+
return cmp;
779+
}
780+
781+
cmp = modulemd_buildopts_compare (self_1->buildopts, self_2->buildopts);
782+
if (cmp != 0)
783+
{
784+
return cmp;
785+
}
786+
787+
return 0;
788+
}

modulemd/modulemd-buildopts.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,52 @@ modulemd_buildopts_equals (ModulemdBuildopts *self_1,
8282
return TRUE;
8383
}
8484

85+
gint
86+
modulemd_buildopts_compare (ModulemdBuildopts *self_1,
87+
ModulemdBuildopts *self_2)
88+
{
89+
gint cmp;
90+
91+
if (!self_1 && !self_2)
92+
{
93+
return 0;
94+
}
95+
96+
if (!self_1)
97+
{
98+
return -1;
99+
}
100+
101+
if (!self_2)
102+
{
103+
return 1;
104+
}
105+
106+
g_return_val_if_fail (MODULEMD_IS_BUILDOPTS (self_1), 1);
107+
g_return_val_if_fail (MODULEMD_IS_BUILDOPTS (self_2), -1);
108+
109+
cmp = g_strcmp0 (self_1->rpm_macros, self_2->rpm_macros);
110+
if (cmp != 0)
111+
{
112+
return cmp;
113+
}
114+
115+
cmp =
116+
modulemd_hash_table_compare (self_1->whitelist, self_2->whitelist, NULL);
117+
if (cmp != 0)
118+
{
119+
return cmp;
120+
}
121+
122+
cmp = modulemd_hash_table_compare (self_1->arches, self_2->arches, NULL);
123+
if (cmp != 0)
124+
{
125+
return cmp;
126+
}
127+
128+
return 0;
129+
}
130+
85131

86132
ModulemdBuildopts *
87133
modulemd_buildopts_new (void)

modulemd/modulemd-obsoletes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,7 @@ modulemd_obsoletes_emit_yaml (ModulemdObsoletes *self,
11661166
g_set_error (error,
11671167
MODULEMD_ERROR,
11681168
MMD_ERROR_VALIDATE,
1169-
"Cannot convert eol_date: %" PRIu64
1170-
" to iso8601 date.",
1169+
"Cannot convert eol_date: %" PRIu64 " to iso8601 date.",
11711170
eol_date);
11721171
return FALSE;
11731172
}

modulemd/modulemd-util.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
236298
gint
237299
modulemd_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+
242310
GPtrArray *
243311
modulemd_ordered_str_keys (GHashTable *htable, GCompareFunc compare_func)
244312
{

0 commit comments

Comments
 (0)