Add option to insert a sorted arrays of values to sets.

This commit is contained in:
Andy John 2022-03-21 13:29:22 -07:00 committed by Behdad Esfahbod
parent 7a1e79c3ba
commit ef588ea97b
4 changed files with 44 additions and 2 deletions

View File

@ -203,7 +203,7 @@ struct hb_bit_set_t
bool set_sorted_array (bool v, const T *array, unsigned int count, unsigned int stride=sizeof(T))
{
if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */
if (!count) return true;
if (unlikely (!count)) return true;
dirty ();
hb_codepoint_t g = *array;
hb_codepoint_t last_g = g;
@ -222,7 +222,7 @@ struct hb_bit_set_t
if (v || page) /* The v check is to optimize out the page check if v is true. */
page->add (g);
array = (const T *) ((const char *) array + stride);
array = &StructAtOffsetUnaligned<T> (array, stride);
count--;
}
while (count && (g = *array, g < end));

View File

@ -61,6 +61,24 @@ hb_set_create ()
return set;
}
/**
* Creates a set initially populated with num_codepoint codepoints, which must
* be in increasing order.
*
* Since: REPLACEME
*/
HB_EXTERN void
hb_set_add_sorted_array (hb_set_t *set,
const hb_codepoint_t *sorted_codepoints,
unsigned int num_codepoints)
{
if (unlikely(!set || !sorted_codepoints || !num_codepoints)) return;
set->add_sorted_array (sorted_codepoints,
num_codepoints,
sizeof(hb_codepoint_t));
}
/**
* hb_set_get_empty:
*

View File

@ -60,6 +60,11 @@ typedef struct hb_set_t hb_set_t;
HB_EXTERN hb_set_t *
hb_set_create (void);
HB_EXTERN void
hb_set_add_sorted_array (hb_set_t *set,
const hb_codepoint_t *sorted_codepoints,
unsigned int num_codepoints);
HB_EXTERN hb_set_t *
hb_set_get_empty (void);

View File

@ -1067,6 +1067,23 @@ test_set_inverted_operations (void)
g_assert (all_succeeded);
}
static void
test_hb_set_add_sorted_array (void)
{
hb_set_t *set = hb_set_create ();
hb_codepoint_t array[7] = {1, 2, 3, 1000, 2000, 2001, 2002};
hb_set_add_sorted_array (set, array, 7);
g_assert_cmpint (hb_set_get_population (set), ==, 7);
g_assert (hb_set_has (set, 1));
g_assert (hb_set_has (set, 2));
g_assert (hb_set_has (set, 3));
g_assert (hb_set_has (set, 1000));
g_assert (hb_set_has (set, 2000));
g_assert (hb_set_has (set, 2001));
g_assert (hb_set_has (set, 2002));
hb_set_destroy (set);
}
int
main (int argc, char **argv)
{
@ -1090,5 +1107,7 @@ main (int argc, char **argv)
hb_test_add (test_set_inverted_equality);
hb_test_add (test_set_inverted_operations);
hb_test_add (test_hb_set_add_sorted_array);
return hb_test_run();
}