Commit d32da2da authored by Thomas Haller's avatar Thomas Haller

shared: move nm_utils_array_find_binary_search() to shared utils

parent b5bdfdc7
......@@ -262,7 +262,6 @@ gssize _nm_utils_ptrarray_find_binary_search (gconstpointer *list,
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, gconstpointer needle, GCompareDataFunc cmpfcn, gpointer user_data);
GSList * _nm_utils_strv_to_slist (char **strv, gboolean deep_copy);
char ** _nm_utils_slist_to_strv (GSList *slist, gboolean deep_copy);
......
......@@ -2462,12 +2462,12 @@ _parse_info_find (const char *setting_name, const char *property_name)
#endif
G_STATIC_ASSERT_EXPR (G_STRUCT_OFFSET (ParseInfoSetting, setting_name) == 0);
idx = _nm_utils_array_find_binary_search (parse_infos,
sizeof (ParseInfoSetting),
G_N_ELEMENTS (parse_infos),
&setting_name,
nm_strcmp_p_with_data,
NULL);
idx = nm_utils_array_find_binary_search (parse_infos,
sizeof (ParseInfoSetting),
G_N_ELEMENTS (parse_infos),
&setting_name,
nm_strcmp_p_with_data,
NULL);
if (idx >= 0) {
const ParseInfoSetting *pis = &parse_infos[idx];
......
......@@ -726,40 +726,6 @@ _nm_utils_ptrarray_find_binary_search (gconstpointer *list,
return imin;
}
gssize
_nm_utils_array_find_binary_search (gconstpointer list, gsize elem_size, gsize len, gconstpointer needle, GCompareDataFunc cmpfcn, gpointer user_data)
{
gssize imin, imax, imid;
int cmp;
g_return_val_if_fail (list || !len, ~((gssize) 0));
g_return_val_if_fail (cmpfcn, ~((gssize) 0));
g_return_val_if_fail (elem_size > 0, ~((gssize) 0));
imin = 0;
if (len == 0)
return ~imin;
imax = len - 1;
while (imin <= imax) {
imid = imin + (imax - imin) / 2;
cmp = cmpfcn (&((const char *) list)[elem_size * imid], needle, user_data);
if (cmp == 0)
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. */
return ~imin;
}
GVariant *
_nm_utils_bytes_to_dbus (const GValue *prop_value)
{
......
......@@ -6188,12 +6188,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_utils_array_find_binary_search (array,
sizeof (guint32),
len,
&NEEDLE,
nm_cmp_uint32_p_with_data,
NULL);
if (expected_result >= 0)
g_assert_cmpint (expected_result, ==, idx);
else {
......@@ -6297,7 +6297,7 @@ test_nm_utils_ptrarray_find_binary_search_with_duplicates (void)
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_utils_array_find_binary_search (arr, sizeof (gpointer), i_len, &p, _test_bin_search2_cmp_p, NULL);
g_assert_cmpint (idx, ==, idx2);
if (idx_first2 < 0) {
......
......@@ -1350,6 +1350,71 @@ nm_utils_strv_make_deep_copied (const char **strv)
/*****************************************************************************/
/**
* nm_utils_array_find_binary_search:
* @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
* @needle: the value that is searched
* @cmpfcn: the compare function. The elements @list are passed as first
* argument to @cmpfcn, while @needle is passed as second. Usually, the
* needle is the same data type as inside the list, however, that is
* not necessary, as long as @cmpfcn takes care to cast the two arguments
* accordingly.
* @user_data: optional argument passed to @cmpfcn
*
* Performs binary search for @needle in @list. On success, returns the
* (non-negative) index where the compare function found the searched element.
* On success, it returns a negative value. Note that the return negative value
* is the bitwise inverse of the position where the element should be inserted.
*
* If the list contains multiple matching elements, an arbitrary index is
* returned.
*
* Returns: the index to the element in the list, or the (negative, bitwise inverted)
* 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)
{
gssize imin, imax, imid;
int cmp;
g_return_val_if_fail (list || !len, ~((gssize) 0));
g_return_val_if_fail (cmpfcn, ~((gssize) 0));
g_return_val_if_fail (elem_size > 0, ~((gssize) 0));
imin = 0;
if (len == 0)
return ~imin;
imax = len - 1;
while (imin <= imax) {
imid = imin + (imax - imin) / 2;
cmp = cmpfcn (&((const char *) list)[elem_size * imid], needle, user_data);
if (cmp == 0)
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. */
return ~imin;
}
/*****************************************************************************/
/**
* nm_utils_hash_table_equal:
* @a: one #GHashTable
......
......@@ -557,6 +557,15 @@ nm_utils_strv_make_deep_copied_nonnull (const char **strv)
/*****************************************************************************/
gssize nm_utils_array_find_binary_search (gconstpointer list,
gsize elem_size,
gsize len,
gconstpointer needle,
GCompareDataFunc cmpfcn,
gpointer user_data);
/*****************************************************************************/
typedef gboolean (*NMUtilsHashTableEqualFunc) (gconstpointer a,
gconstpointer b);
......
......@@ -655,12 +655,12 @@ nm_config_data_log (const NMConfigData *self,
const char *group = default_values[g].group;
gssize idx;
idx = _nm_utils_array_find_binary_search ((gconstpointer *) groups_full->pdata,
sizeof (char *),
groups_full->len,
&group,
(GCompareDataFunc) _nm_config_data_log_sort,
NULL);
idx = nm_utils_array_find_binary_search ((gconstpointer *) groups_full->pdata,
sizeof (char *),
groups_full->len,
&group,
(GCompareDataFunc) _nm_config_data_log_sort,
NULL);
if (idx < 0)
g_ptr_array_insert (groups_full, (~idx), (gpointer) group);
}
......
......@@ -6796,12 +6796,12 @@ nm_manager_set_capability (NMManager *self,
priv = NM_MANAGER_GET_PRIVATE (self);
idx = _nm_utils_array_find_binary_search (&g_array_index (priv->capabilities, guint32, 0),
sizeof (guint32),
priv->capabilities->len,
&cap_i,
nm_cmp_uint32_p_with_data,
NULL);
idx = nm_utils_array_find_binary_search (&g_array_index (priv->capabilities, guint32, 0),
sizeof (guint32),
priv->capabilities->len,
&cap_i,
nm_cmp_uint32_p_with_data,
NULL);
if (idx >= 0)
return;
......
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