summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libnm-core/nm-core-internal.h8
-rw-r--r--libnm-core/nm-keyfile.c14
-rw-r--r--libnm-core/nm-utils.c79
-rw-r--r--libnm-core/tests/test-general.c8
-rw-r--r--shared/nm-utils/nm-shared-utils.c81
-rw-r--r--shared/nm-utils/nm-shared-utils.h8
6 files changed, 100 insertions, 98 deletions
diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index e826a9fba2..966566481b 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -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);
diff --git a/libnm-core/nm-keyfile.c b/libnm-core/nm-keyfile.c
index c87c68e3bf..a9f15455f4 100644
--- a/libnm-core/nm-keyfile.c
+++ b/libnm-core/nm-keyfile.c
@@ -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];
}
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index 5665743c03..f481f11fe5 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -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)
{
diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c
index adf42cca1f..39e2cf90e4 100644
--- a/libnm-core/tests/test-general.c
+++ b/libnm-core/tests/test-general.c
@@ -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);
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index a9c7ab7e9d..34c93c8108 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -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.
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index feb93f2c5a..6ee77b989f 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -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,