glib-aux: rename nm_utils_array_find_binary_search() to nm_array_bsearch()

The "nm_utils_" prefix is just too verbose. Drop it.
Also, Posix has a bsearch function. As this function
is similar, rename it.

Note that currently the arguments are provided in differnt
order from bsearch(). That will be partly addressed next.
That is the main reason for the rename. The next commit
will swap the arguments, so do a rename first to get a compilation
error when backporting a patch that uses the changed API.
This commit is contained in:
Thomas Haller 2022-09-27 09:08:35 +02:00
parent 1c067fe4c6
commit 2953ebccba
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
15 changed files with 158 additions and 169 deletions

View file

@ -7904,12 +7904,12 @@ nm_manager_set_capability(NMManager *self, NMCapability cap)
priv = NM_MANAGER_GET_PRIVATE(self);
idx = nm_utils_array_find_binary_search(nm_g_array_index_p(priv->capabilities, guint32, 0),
sizeof(guint32),
priv->capabilities->len,
&cap_i,
nm_cmp_uint32_p_with_data,
NULL);
idx = nm_array_find_bsearch(nm_g_array_index_p(priv->capabilities, guint32, 0),
sizeof(guint32),
priv->capabilities->len,
&cap_i,
nm_cmp_uint32_p_with_data,
NULL);
if (idx >= 0)
return;

View file

@ -1080,12 +1080,12 @@ nms_ifcfg_well_known_key_find_info(const char *key, gssize *out_idx)
G_STATIC_ASSERT(G_STRUCT_OFFSET(NMSIfcfgKeyTypeInfo, key_name) == 0);
idx = nm_utils_array_find_binary_search(nms_ifcfg_well_known_keys,
sizeof(nms_ifcfg_well_known_keys[0]),
G_N_ELEMENTS(nms_ifcfg_well_known_keys),
&key,
nm_strcmp_p_with_data,
NULL);
idx = nm_array_find_bsearch(nms_ifcfg_well_known_keys,
sizeof(nms_ifcfg_well_known_keys[0]),
G_N_ELEMENTS(nms_ifcfg_well_known_keys),
&key,
nm_strcmp_p_with_data,
NULL);
NM_SET_OUT(out_idx, idx);
if (idx < 0)
return NULL;

View file

@ -279,12 +279,12 @@ nm_supplicant_settings_verify_setting(const char *key, const char *value, const
}
}
opt_idx = nm_utils_array_find_binary_search(opt_table,
sizeof(opt_table[0]),
G_N_ELEMENTS(opt_table),
&key,
nm_strcmp_p_with_data,
NULL);
opt_idx = nm_array_find_bsearch(opt_table,
sizeof(opt_table[0]),
G_N_ELEMENTS(opt_table),
&key,
nm_strcmp_p_with_data,
NULL);
if (opt_idx < 0) {
if (nm_streq(key, "mode")) {
if (len != 1)

View file

@ -271,12 +271,12 @@ nm_ethtool_data_get_by_optname(const char *optname)
_ASSERT_data();
idx = nm_utils_array_find_binary_search((gconstpointer *) _by_name,
sizeof(_by_name[0]),
_NM_ETHTOOL_ID_NUM,
optname,
_by_name_cmp,
NULL);
idx = nm_array_find_bsearch((gconstpointer *) _by_name,
sizeof(_by_name[0]),
_NM_ETHTOOL_ID_NUM,
optname,
_by_name_cmp,
NULL);
return (idx < 0) ? NULL : nm_ethtool_data[_by_name[idx]];
}

View file

@ -752,11 +752,11 @@ nml_dbus_meta_iface_get(const char *dbus_iface_name)
if (NM_STR_HAS_PREFIX(dbus_iface_name, COMMON_PREFIX)) {
/* optimize, that in fact all our interfaces have the same prefix. */
idx = nm_utils_ptrarray_find_binary_search((gconstpointer *) _nml_dbus_meta_ifaces,
G_N_ELEMENTS(_nml_dbus_meta_ifaces),
&dbus_iface_name[NM_STRLEN(COMMON_PREFIX)],
_strcmp_common_prefix,
NULL);
idx = nm_ptrarray_find_bsearch((gconstpointer *) _nml_dbus_meta_ifaces,
G_N_ELEMENTS(_nml_dbus_meta_ifaces),
&dbus_iface_name[NM_STRLEN(COMMON_PREFIX)],
_strcmp_common_prefix,
NULL);
} else
return NULL;
@ -775,12 +775,12 @@ nml_dbus_meta_property_get(const NMLDBusMetaIface *meta_iface,
nm_assert(meta_iface);
nm_assert(dbus_property_name);
idx = nm_utils_array_find_binary_search(meta_iface->dbus_properties,
sizeof(meta_iface->dbus_properties[0]),
meta_iface->n_dbus_properties,
&dbus_property_name,
nm_strcmp_p_with_data,
NULL);
idx = nm_array_find_bsearch(meta_iface->dbus_properties,
sizeof(meta_iface->dbus_properties[0]),
meta_iface->n_dbus_properties,
&dbus_property_name,
nm_strcmp_p_with_data,
NULL);
if (idx < 0) {
NM_SET_OUT(out_idx, meta_iface->n_dbus_properties);
return NULL;

View file

@ -349,12 +349,12 @@ nm_auth_permission_from_string(const char *str)
if (!NM_STR_HAS_PREFIX(str, AUTH_PERMISSION_PREFIX))
return NM_CLIENT_PERMISSION_NONE;
idx = nm_utils_array_find_binary_search(nm_auth_permission_sorted,
sizeof(nm_auth_permission_sorted[0]),
G_N_ELEMENTS(nm_auth_permission_sorted),
&str[NM_STRLEN(AUTH_PERMISSION_PREFIX)],
_nm_auth_permission_from_string_cmp,
NULL);
idx = nm_array_find_bsearch(nm_auth_permission_sorted,
sizeof(nm_auth_permission_sorted[0]),
G_N_ELEMENTS(nm_auth_permission_sorted),
&str[NM_STRLEN(AUTH_PERMISSION_PREFIX)],
_nm_auth_permission_from_string_cmp,
NULL);
if (idx < 0)
return NM_CLIENT_PERMISSION_NONE;
return nm_auth_permission_sorted[idx];

View file

@ -3188,11 +3188,11 @@ _parse_info_find(NMSetting *setting,
G_STATIC_ASSERT_EXPR(G_STRUCT_OFFSET(ParseInfoProperty, property_name) == 0);
idx = nm_utils_ptrarray_find_binary_search((gconstpointer *) pis->properties,
NM_PTRARRAY_LEN(pis->properties),
&property_name,
nm_strcmp_p_with_data,
NULL);
idx = nm_ptrarray_find_bsearch((gconstpointer *) pis->properties,
NM_PTRARRAY_LEN(pis->properties),
&property_name,
nm_strcmp_p_with_data,
NULL);
if (idx >= 0)
pip = pis->properties[idx];
}

View file

@ -693,12 +693,12 @@ nm_meta_setting_infos_by_name(const char *name)
}
G_STATIC_ASSERT_EXPR(G_STRUCT_OFFSET(NMMetaSettingInfo, setting_name) == 0);
idx = nm_utils_array_find_binary_search(nm_meta_setting_infos,
sizeof(NMMetaSettingInfo),
_NM_META_SETTING_TYPE_NUM,
&name,
nm_strcmp_p_with_data,
NULL);
idx = nm_array_find_bsearch(nm_meta_setting_infos,
sizeof(NMMetaSettingInfo),
_NM_META_SETTING_TYPE_NUM,
&name,
nm_strcmp_p_with_data,
NULL);
return idx >= 0 ? &nm_meta_setting_infos[idx] : NULL;
}

View file

@ -2934,12 +2934,12 @@ _rr_dbus_attr_from_name(const char *name)
}
}
idx = nm_utils_array_find_binary_search(rr_dbus_data,
sizeof(rr_dbus_data[0]),
_RR_DBUS_ATTR_NUM,
&name,
nm_strcmp_p_with_data,
NULL);
idx = nm_array_find_bsearch(rr_dbus_data,
sizeof(rr_dbus_data[0]),
_RR_DBUS_ATTR_NUM,
&name,
nm_strcmp_p_with_data,
NULL);
if (idx < 0)
return _RR_DBUS_ATTR_NUM;
return idx;

View file

@ -477,12 +477,12 @@ _nm_sett_info_setting_get_property_info(const NMSettInfoSetting *sett_info,
return NULL;
G_STATIC_ASSERT_EXPR(G_STRUCT_OFFSET(NMSettInfoProperty, name) == 0);
idx = nm_utils_array_find_binary_search(sett_info->property_infos,
sizeof(NMSettInfoProperty),
sett_info->property_infos_len,
&property_name,
nm_strcmp_p_with_data,
NULL);
idx = nm_array_find_bsearch(sett_info->property_infos,
sizeof(NMSettInfoProperty),
sett_info->property_infos_len,
&property_name,
nm_strcmp_p_with_data,
NULL);
if (idx < 0)
return NULL;

View file

@ -5020,11 +5020,8 @@ _nm_variant_attribute_spec_find_binary_search(const NMVariantAttributeSpec *cons
G_STATIC_ASSERT_EXPR(G_STRUCT_OFFSET(NMVariantAttributeSpec, name) == 0);
idx = nm_utils_ptrarray_find_binary_search((gconstpointer *) array,
len,
&name,
nm_strcmp_p_with_data,
NULL);
idx =
nm_ptrarray_find_bsearch((gconstpointer *) array, len, &name, nm_strcmp_p_with_data, NULL);
if (idx < 0)
return NULL;
return array[idx];

View file

@ -8784,19 +8784,15 @@ _test_find_binary_search_do(const int *array, gsize len)
expected_result = nm_utils_ptrarray_find_first(parray, len, pneedle);
idx = nm_utils_ptrarray_find_binary_search_range(parray,
len,
pneedle,
_test_find_binary_search_cmp,
NULL,
&idx_first,
&idx_last);
idx = nm_ptrarray_find_bsearch_range(parray,
len,
pneedle,
_test_find_binary_search_cmp,
NULL,
&idx_first,
&idx_last);
idx2 = nm_utils_ptrarray_find_binary_search(parray,
len,
pneedle,
_test_find_binary_search_cmp,
NULL);
idx2 = nm_ptrarray_find_bsearch(parray, len, pneedle, _test_find_binary_search_cmp, NULL);
g_assert_cmpint(idx, ==, idx2);
if (expected_result >= 0) {
@ -8861,12 +8857,12 @@ _test_find_binary_search_do_uint32(const int *int_array, gsize len)
expected_result = idx;
}
idx = nm_utils_array_find_binary_search(array,
sizeof(guint32),
len,
&NEEDLE,
nm_cmp_uint32_p_with_data,
NULL);
idx = nm_array_find_bsearch(array,
sizeof(guint32),
len,
&NEEDLE,
nm_cmp_uint32_p_with_data,
NULL);
if (expected_result >= 0)
g_assert_cmpint(expected_result, ==, idx);
else {
@ -8960,29 +8956,25 @@ test_nm_utils_ptrarray_find_binary_search_with_duplicates(void)
for (i = 0; i < i_len + BIN_SEARCH_W_DUPS_JITTER; i++) {
gconstpointer p = GINT_TO_POINTER(i);
idx = nm_utils_ptrarray_find_binary_search_range(arr,
i_len,
p,
_test_bin_search2_cmp,
NULL,
&idx_first,
&idx_last);
idx = nm_ptrarray_find_bsearch_range(arr,
i_len,
p,
_test_bin_search2_cmp,
NULL,
&idx_first,
&idx_last);
idx_first2 = nm_utils_ptrarray_find_first(arr, i_len, p);
idx2 = nm_utils_array_find_binary_search(arr,
sizeof(gpointer),
i_len,
&p,
_test_bin_search2_cmp_p,
NULL);
idx2 = nm_array_find_bsearch(arr,
sizeof(gpointer),
i_len,
&p,
_test_bin_search2_cmp_p,
NULL);
g_assert_cmpint(idx, ==, idx2);
idx2 = nm_utils_ptrarray_find_binary_search(arr,
i_len,
p,
_test_bin_search2_cmp,
NULL);
idx2 = nm_ptrarray_find_bsearch(arr, i_len, p, _test_bin_search2_cmp, NULL);
g_assert_cmpint(idx, ==, idx2);
if (idx_first2 < 0) {
@ -11254,7 +11246,7 @@ main(int argc, char **argv)
g_test_add_func("/core/general/_nm_utils_ascii_str_to_int64", test_nm_utils_ascii_str_to_int64);
g_test_add_func("/core/general/nm_utils_is_power_of_two", test_nm_utils_is_power_of_two);
g_test_add_func("/core/general/nm_utils_ptrarray_find_binary_search_range",
g_test_add_func("/core/general/nm_ptrarray_find_bsearch_range",
test_nm_utils_ptrarray_find_binary_search);
g_test_add_func("/core/general/nm_utils_ptrarray_find_binary_search_with_duplicates",
test_nm_utils_ptrarray_find_binary_search_with_duplicates);

View file

@ -3093,12 +3093,12 @@ nm_utils_named_value_list_find(const NMUtilsNamedValue *arr,
#endif
if (sorted) {
return nm_utils_array_find_binary_search(arr,
sizeof(NMUtilsNamedValue),
len,
&name,
nm_strcmp_p_with_data,
NULL);
return nm_array_find_bsearch(arr,
sizeof(NMUtilsNamedValue),
len,
&name,
nm_strcmp_p_with_data,
NULL);
}
for (i = 0; i < len; i++) {
if (nm_streq(arr[i].name, name))
@ -3786,11 +3786,11 @@ nm_utils_ptrarray_is_sorted(gconstpointer *list,
}
gssize
nm_utils_ptrarray_find_binary_search(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data)
nm_ptrarray_find_bsearch(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data)
{
gssize imin, imax, imid;
int cmp;
@ -3823,13 +3823,13 @@ nm_utils_ptrarray_find_binary_search(gconstpointer *list,
}
gssize
nm_utils_ptrarray_find_binary_search_range(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data,
gssize *out_idx_first,
gssize *out_idx_last)
nm_ptrarray_find_bsearch_range(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data,
gssize *out_idx_first,
gssize *out_idx_last)
{
gssize imin, imax, imid, i2min, i2max, i2mid;
int cmp;
@ -3904,7 +3904,7 @@ nm_utils_ptrarray_find_binary_search_range(gconstpointer *list,
/*****************************************************************************/
/**
* nm_utils_array_find_binary_search:
* nm_array_find_bsearch:
* @list: the list to search. It must be sorted according to @cmpfcn ordering.
* @elem_size: the size in bytes of each element in the list
* @len: the number of elements in @list
@ -3928,12 +3928,12 @@ nm_utils_ptrarray_find_binary_search_range(gconstpointer *list,
* position where it should be.
*/
gssize
nm_utils_array_find_binary_search(gconstpointer list,
gsize elem_size,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data)
nm_array_find_bsearch(gconstpointer list,
gsize elem_size,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data)
{
gssize imin, imax, imid;
int cmp;

View file

@ -2103,42 +2103,42 @@ gboolean nm_utils_ptrarray_is_sorted(gconstpointer *list,
GCompareDataFunc cmpfcn,
gpointer user_data);
gssize nm_utils_ptrarray_find_binary_search(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data);
gssize nm_ptrarray_find_bsearch(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data);
gssize nm_utils_ptrarray_find_binary_search_range(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data,
gssize *out_idx_first,
gssize *out_idx_last);
gssize nm_ptrarray_find_bsearch_range(gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data,
gssize *out_idx_first,
gssize *out_idx_last);
#define nm_strv_find_binary_search(strv, len, needle) \
({ \
const char *const *const _strv = NM_CAST_STRV_CC(strv); \
const gsize _len = (len); \
const char *const _needle = (needle); \
\
nm_assert(_len == 0 || _strv); \
nm_assert(_needle); \
\
nm_utils_ptrarray_find_binary_search((gconstpointer *) _strv, \
_len, \
_needle, \
nm_strcmp_with_data, \
NULL); \
#define nm_strv_find_binary_search(strv, len, needle) \
({ \
const char *const *const _strv = NM_CAST_STRV_CC(strv); \
const gsize _len = (len); \
const char *const _needle = (needle); \
\
nm_assert(_len == 0 || _strv); \
nm_assert(_needle); \
\
nm_ptrarray_find_bsearch((gconstpointer *) _strv, \
_len, \
_needle, \
nm_strcmp_with_data, \
NULL); \
})
gssize nm_utils_array_find_binary_search(gconstpointer list,
gsize elem_size,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data);
gssize nm_array_find_bsearch(gconstpointer list,
gsize elem_size,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data);
gssize nm_utils_ptrarray_find_first(gconstpointer *list, gssize len, gconstpointer needle);

View file

@ -693,12 +693,12 @@ nm_meta_setting_infos_by_name(const char *name)
}
G_STATIC_ASSERT_EXPR(G_STRUCT_OFFSET(NMMetaSettingInfo, setting_name) == 0);
idx = nm_utils_array_find_binary_search(nm_meta_setting_infos,
sizeof(NMMetaSettingInfo),
_NM_META_SETTING_TYPE_NUM,
&name,
nm_strcmp_p_with_data,
NULL);
idx = nm_array_find_bsearch(nm_meta_setting_infos,
sizeof(NMMetaSettingInfo),
_NM_META_SETTING_TYPE_NUM,
&name,
nm_strcmp_p_with_data,
NULL);
return idx >= 0 ? &nm_meta_setting_infos[idx] : NULL;
}