Commit a587d324 authored by Thomas Haller's avatar Thomas Haller

shared: move nm_utils_ptrarray_find_binary_search() to shared utils

parent d32da2da
......@@ -255,14 +255,6 @@ GPtrArray *_nm_utils_copy_object_array (const GPtrArray *array);
gssize _nm_utils_ptrarray_find_first (gconstpointer *list, gssize len, gconstpointer needle);
gssize _nm_utils_ptrarray_find_binary_search (gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data,
gssize *out_idx_first,
gssize *out_idx_last);
GSList * _nm_utils_strv_to_slist (char **strv, gboolean deep_copy);
char ** _nm_utils_slist_to_strv (GSList *slist, gboolean deep_copy);
......
......@@ -2473,13 +2473,13 @@ _parse_info_find (const char *setting_name, const char *property_name)
G_STATIC_ASSERT_EXPR (G_STRUCT_OFFSET (ParseInfoProperty, property_name) == 0);
nm_assert (nm_streq (pis->setting_name, setting_name));
idx = _nm_utils_ptrarray_find_binary_search ((gconstpointer *) pis->properties,
NM_PTRARRAY_LEN (pis->properties),
&property_name,
nm_strcmp_p_with_data,
NULL,
NULL,
NULL);
idx = nm_utils_ptrarray_find_binary_search ((gconstpointer *) pis->properties,
NM_PTRARRAY_LEN (pis->properties),
&property_name,
nm_strcmp_p_with_data,
NULL,
NULL,
NULL);
if (idx >= 0)
return pis->properties[idx];
}
......
......@@ -647,85 +647,6 @@ _nm_utils_ptrarray_find_first (gconstpointer *list, gssize len, gconstpointer ne
return -1;
}
gssize
_nm_utils_ptrarray_find_binary_search (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;
g_return_val_if_fail (list || !len, ~((gssize) 0));
g_return_val_if_fail (cmpfcn, ~((gssize) 0));
imin = 0;
if (len > 0) {
imax = len - 1;
while (imin <= imax) {
imid = imin + (imax - imin) / 2;
cmp = cmpfcn (list[imid], needle, user_data);
if (cmp == 0) {
/* we found a matching entry at index imid.
*
* Does the caller request the first/last index as well (in case that
* there are multiple entries which compare equal). */
if (out_idx_first) {
i2min = imin;
i2max = imid + 1;
while (i2min <= i2max) {
i2mid = i2min + (i2max - i2min) / 2;
cmp = cmpfcn (list[i2mid], needle, user_data);
if (cmp == 0)
i2max = i2mid -1;
else {
nm_assert (cmp < 0);
i2min = i2mid + 1;
}
}
*out_idx_first = i2min;
}
if (out_idx_last) {
i2min = imid + 1;
i2max = imax;
while (i2min <= i2max) {
i2mid = i2min + (i2max - i2min) / 2;
cmp = cmpfcn (list[i2mid], needle, user_data);
if (cmp == 0)
i2min = i2mid + 1;
else {
nm_assert (cmp > 0);
i2max = i2mid - 1;
}
}
*out_idx_last = i2min - 1;
}
return imid;
}
if (cmp < 0)
imin = imid + 1;
else
imax = imid - 1;
}
}
/* return the inverse of @imin. This is a negative number, but
* also is ~imin the position where the value should be inserted. */
imin = ~imin;
NM_SET_OUT (out_idx_first, imin);
NM_SET_OUT (out_idx_last, imin);
return imin;
}
GVariant *
_nm_utils_bytes_to_dbus (const GValue *prop_value)
{
......
......@@ -6126,7 +6126,7 @@ _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 (parray, len, pneedle, _test_find_binary_search_cmp, NULL, &idx_first, &idx_last);
idx = nm_utils_ptrarray_find_binary_search (parray, len, pneedle, _test_find_binary_search_cmp, NULL, &idx_first, &idx_last);
if (expected_result >= 0) {
g_assert_cmpint (expected_result, ==, idx);
} else {
......@@ -6293,7 +6293,7 @@ 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 (arr, i_len, p, _test_bin_search2_cmp, NULL, &idx_first, &idx_last);
idx = nm_utils_ptrarray_find_binary_search (arr, i_len, p, _test_bin_search2_cmp, NULL, &idx_first, &idx_last);
idx_first2 = _nm_utils_ptrarray_find_first (arr, i_len, p);
......@@ -7197,8 +7197,8 @@ int 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", 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);
g_test_add_func ("/core/general/nm_utils_ptrarray_find_binary_search", 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);
g_test_add_func ("/core/general/_nm_utils_strstrdictkey", test_nm_utils_strstrdictkey);
g_test_add_func ("/core/general/nm_ptrarray_len", test_nm_ptrarray_len);
......
......@@ -1350,6 +1350,87 @@ nm_utils_strv_make_deep_copied (const char **strv)
/*****************************************************************************/
gssize
nm_utils_ptrarray_find_binary_search (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;
g_return_val_if_fail (list || !len, ~((gssize) 0));
g_return_val_if_fail (cmpfcn, ~((gssize) 0));
imin = 0;
if (len > 0) {
imax = len - 1;
while (imin <= imax) {
imid = imin + (imax - imin) / 2;
cmp = cmpfcn (list[imid], needle, user_data);
if (cmp == 0) {
/* we found a matching entry at index imid.
*
* Does the caller request the first/last index as well (in case that
* there are multiple entries which compare equal). */
if (out_idx_first) {
i2min = imin;
i2max = imid + 1;
while (i2min <= i2max) {
i2mid = i2min + (i2max - i2min) / 2;
cmp = cmpfcn (list[i2mid], needle, user_data);
if (cmp == 0)
i2max = i2mid -1;
else {
nm_assert (cmp < 0);
i2min = i2mid + 1;
}
}
*out_idx_first = i2min;
}
if (out_idx_last) {
i2min = imid + 1;
i2max = imax;
while (i2min <= i2max) {
i2mid = i2min + (i2max - i2min) / 2;
cmp = cmpfcn (list[i2mid], needle, user_data);
if (cmp == 0)
i2min = i2mid + 1;
else {
nm_assert (cmp > 0);
i2max = i2mid - 1;
}
}
*out_idx_last = i2min - 1;
}
return imid;
}
if (cmp < 0)
imin = imid + 1;
else
imax = imid - 1;
}
}
/* return the inverse of @imin. This is a negative number, but
* also is ~imin the position where the value should be inserted. */
imin = ~imin;
NM_SET_OUT (out_idx_first, imin);
NM_SET_OUT (out_idx_last, imin);
return imin;
}
/*****************************************************************************/
/**
* nm_utils_array_find_binary_search:
* @list: the list to search. It must be sorted according to @cmpfcn ordering.
......
......@@ -557,6 +557,14 @@ nm_utils_strv_make_deep_copied_nonnull (const char **strv)
/*****************************************************************************/
gssize nm_utils_ptrarray_find_binary_search (gconstpointer *list,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data,
gssize *out_idx_first,
gssize *out_idx_last);
gssize nm_utils_array_find_binary_search (gconstpointer list,
gsize elem_size,
gsize len,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment