[set] Add hb_bit_set_invertible_t and resurrect hb_set_invert()

Implementation is NOT complete yet.
This commit is contained in:
Behdad Esfahbod 2021-08-17 15:37:19 -06:00
parent b5cdbdc030
commit f0c3804fa2
8 changed files with 201 additions and 40 deletions

View File

@ -195,7 +195,6 @@ HB_OT_VAR_NO_AXIS_INDEX
hb_ot_var_axis_t hb_ot_var_axis_t
hb_ot_var_find_axis hb_ot_var_find_axis
hb_ot_var_get_axes hb_ot_var_get_axes
hb_set_invert
hb_unicode_eastasian_width_func_t hb_unicode_eastasian_width_func_t
hb_unicode_eastasian_width hb_unicode_eastasian_width
hb_unicode_funcs_set_eastasian_width_func hb_unicode_funcs_set_eastasian_width_func
@ -614,6 +613,7 @@ hb_set_get_population
hb_set_get_user_data hb_set_get_user_data
hb_set_has hb_set_has
hb_set_intersect hb_set_intersect
hb_set_invert
hb_set_is_empty hb_set_is_empty
hb_set_is_equal hb_set_is_equal
hb_set_is_subset hb_set_is_subset

View File

@ -21,6 +21,7 @@ HB_BASE_sources = \
hb-bimap.hh \ hb-bimap.hh \
hb-bit-page.hh \ hb-bit-page.hh \
hb-bit-set.hh \ hb-bit-set.hh \
hb-bit-set-invertible.hh \
hb-blob.cc \ hb-blob.cc \
hb-blob.hh \ hb-blob.hh \
hb-buffer-serialize.cc \ hb-buffer-serialize.cc \

View File

@ -0,0 +1,168 @@
/*
* Copyright © 2012,2017 Google, Inc.
* Copyright © 2021 Behdad Esfahbod
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Behdad Esfahbod
*/
#ifndef HB_BIT_SET_INVERTIBLE_HH
#define HB_BIT_SET_INVERTIBLE_HH
#include "hb.hh"
#include "hb-bit-set.hh"
struct hb_bit_set_invertible_t
{
hb_bit_set_t s;
bool inverted;
hb_bit_set_invertible_t () { init (); }
~hb_bit_set_invertible_t () { fini (); }
void init () { s.init (); inverted = false; }
void fini () { s.fini (); }
void err () { s.err (); }
bool in_error () const { return s.in_error (); }
explicit operator bool () const { return !is_empty (); }
void reset () { s.reset (); inverted = false; }
void clear () { s.clear (); inverted = false; }
void invert () { if (!s.in_error ()) inverted = !inverted; }
bool is_empty () const { return inverted ? /*XXX*/false : s.is_empty (); }
void add (hb_codepoint_t g) { inverted ? s.del (g) : s.add (g); }
bool add_range (hb_codepoint_t a, hb_codepoint_t b) { return inverted ? (s.del_range (a, b), true) : s.add_range (a, b); }
template <typename T>
void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
{ /*XXX(inverted)*/s.add_array (array, count, stride); }
template <typename T>
void add_array (const hb_array_t<const T>& arr) { add_array (&arr, arr.len ()); }
/* Might return false if array looks unsorted.
* Used for faster rejection of corrupt data. */
template <typename T>
bool add_sorted_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
{ return /*XXX(inverted)*/s.add_sorted_array (array, count, stride); }
template <typename T>
bool add_sorted_array (const hb_sorted_array_t<const T>& arr) { return add_sorted_array (&arr, arr.len ()); }
void del (hb_codepoint_t g) { inverted ? s.add (g) : s.del (g); }
void del_range (hb_codepoint_t a, hb_codepoint_t b) { inverted ? (void) s.add_range (a, b) : s.del_range (a, b); }
bool get (hb_codepoint_t g) const { return s.get (g) ^ inverted; }
/* Has interface. */
static constexpr bool SENTINEL = false;
typedef bool value_t;
value_t operator [] (hb_codepoint_t k) const { return get (k); }
bool has (hb_codepoint_t k) const { return (*this)[k] != SENTINEL; }
/* Predicate. */
bool operator () (hb_codepoint_t k) const { return has (k); }
/* Sink interface. */
hb_bit_set_invertible_t& operator << (hb_codepoint_t v)
{ add (v); return *this; }
hb_bit_set_invertible_t& operator << (const hb_pair_t<hb_codepoint_t, hb_codepoint_t>& range)
{ add_range (range.first, range.second); return *this; }
bool intersects (hb_codepoint_t first, hb_codepoint_t last) const
{ return /*XXX(inverted)*/s.intersects (first, last); }
void set (const hb_bit_set_invertible_t &other) { s.set (other.s); inverted = other.inverted; }
bool is_equal (const hb_bit_set_invertible_t &other) const
{ return inverted == other.inverted /*XXX*/ && s.is_equal (other.s); }
bool is_subset (const hb_bit_set_invertible_t &larger_set) const
{
if (inverted && !larger_set.inverted) return false; /*XXX*/
if (!inverted && larger_set.inverted)
{
/* TODO(iter) Write as hb_all dagger. */
for (auto c: s)
if (larger_set.s.has (c))
return false;
return true;
}
/* inverted == larger_set.inverted */
return inverted ? larger_set.s.is_subset (s) : s.is_subset (larger_set.s);
}
template <typename Op>
void process (const Op& op, const hb_bit_set_invertible_t &other)
{ s.process (op, other.s); }
/*XXX(inverted)*/
void union_ (const hb_bit_set_invertible_t &other) { process (hb_bitwise_or, other); }
/*XXX(inverted)*/
void intersect (const hb_bit_set_invertible_t &other) { process (hb_bitwise_and, other); }
/*XXX(inverted)*/
void subtract (const hb_bit_set_invertible_t &other) { process (hb_bitwise_sub, other); }
/*XXX(inverted)*/
void symmetric_difference (const hb_bit_set_invertible_t &other) { process (hb_bitwise_xor, other); }
bool next (hb_codepoint_t *codepoint) const
{
/*XXX(inverted)*/
return s.next (codepoint);
}
bool previous (hb_codepoint_t *codepoint) const
{
/*XXX(inverted)*/
return s.previous (codepoint);
}
bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
{
/*XXX(inverted)*/
return s.next_range (first, last);
}
bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const
{
/*XXX(inverted)*/
return s.previous_range (first, last);
}
unsigned int get_population () const
{ return inverted ? INVALID - s.get_population () : s.get_population (); }
hb_codepoint_t get_min () const
{ return /*XXX(inverted)*/s.get_min (); }
hb_codepoint_t get_max () const
{ return /*XXX(inverted)*/s.get_max (); }
static constexpr hb_codepoint_t INVALID = hb_bit_set_t::INVALID;
/*
* Iterator implementation.
*/
/*XXX(inverted)*/
using iter_t = hb_bit_set_t::iter_t;
iter_t iter () const { return iter_t (this->s); }
operator iter_t () const { return iter (); }
};
#endif /* HB_BIT_SET_INVERTIBLE_HH */

View File

@ -35,14 +35,8 @@
struct hb_bit_set_t struct hb_bit_set_t
{ {
hb_bit_set_t () hb_bit_set_t () { init (); }
{ ~hb_bit_set_t () { fini (); }
init ();
}
~hb_bit_set_t ()
{
fini ();
}
void init () void init ()
{ {
@ -558,6 +552,11 @@ struct hb_bit_set_t
resize (newCount); resize (newCount);
} }
void union_ (const hb_bit_set_t &other) { process (hb_bitwise_or, other); }
void intersect (const hb_bit_set_t &other) { process (hb_bitwise_and, other); }
void subtract (const hb_bit_set_t &other) { process (hb_bitwise_sub, other); }
void symmetric_difference (const hb_bit_set_t &other) { process (hb_bitwise_xor, other); }
bool next (hb_codepoint_t *codepoint) const bool next (hb_codepoint_t *codepoint) const
{ {
// TODO: this should be merged with prev() as both implementations // TODO: this should be merged with prev() as both implementations

View File

@ -107,9 +107,6 @@ hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
hb_font_get_glyph_func_t func, hb_font_get_glyph_func_t func,
void *user_data, hb_destroy_func_t destroy); void *user_data, hb_destroy_func_t destroy);
HB_EXTERN HB_DEPRECATED void
hb_set_invert (hb_set_t *set);
/** /**
* hb_unicode_eastasian_width_func_t: * hb_unicode_eastasian_width_func_t:
* @ufuncs: A Unicode-functions structure * @ufuncs: A Unicode-functions structure

View File

@ -432,22 +432,19 @@ hb_set_symmetric_difference (hb_set_t *set,
set->symmetric_difference (*other); set->symmetric_difference (*other);
} }
#ifndef HB_DISABLE_DEPRECATED
/** /**
* hb_set_invert: * hb_set_invert:
* @set: A set * @set: A set
* *
* Inverts the contents of @set. * Inverts the contents of @set.
* *
* Since: 0.9.10 * Since: 3.0.0
*
* Deprecated: 1.6.1
**/ **/
void void
hb_set_invert (hb_set_t *set HB_UNUSED) hb_set_invert (hb_set_t *set)
{ {
set->invert ();
} }
#endif
/** /**
* hb_set_get_population: * hb_set_get_population:

View File

@ -94,6 +94,9 @@ hb_set_clear (hb_set_t *set);
HB_EXTERN hb_bool_t HB_EXTERN hb_bool_t
hb_set_is_empty (const hb_set_t *set); hb_set_is_empty (const hb_set_t *set);
HB_EXTERN void
hb_set_invert (hb_set_t *set);
HB_EXTERN hb_bool_t HB_EXTERN hb_bool_t
hb_set_has (const hb_set_t *set, hb_set_has (const hb_set_t *set,
hb_codepoint_t codepoint); hb_codepoint_t codepoint);

View File

@ -29,28 +29,25 @@
#define HB_SET_HH #define HB_SET_HH
#include "hb.hh" #include "hb.hh"
#include "hb-bit-set.hh" #include "hb-bit-set-invertible.hh"
template <typename impl_t> template <typename impl_t>
struct hb_intset_t struct hb_sparseset_t
{ {
hb_object_header_t header; hb_object_header_t header;
impl_t s; impl_t s;
void init_shallow () hb_sparseset_t () { init (); }
{ ~hb_sparseset_t () { fini (); }
s.init ();
} void init_shallow () { s.init (); }
void init () void init ()
{ {
hb_object_init (this); hb_object_init (this);
init_shallow (); init_shallow ();
} }
void fini_shallow () void fini_shallow () { s.fini (); }
{
s.fini ();
}
void fini () void fini ()
{ {
hb_object_fini (this); hb_object_fini (this);
@ -63,9 +60,8 @@ struct hb_intset_t
bool in_error () const { return s.in_error (); } bool in_error () const { return s.in_error (); }
void reset () { s.reset (); } void reset () { s.reset (); }
void clear () { s.clear (); } void clear () { s.clear (); }
void invert () { s.invert (); }
bool is_empty () const { return s.is_empty (); } bool is_empty () const { return s.is_empty (); }
void add (hb_codepoint_t g) { s.add (g); } void add (hb_codepoint_t g) { s.add (g); }
@ -99,24 +95,24 @@ struct hb_intset_t
bool operator () (hb_codepoint_t k) const { return has (k); } bool operator () (hb_codepoint_t k) const { return has (k); }
/* Sink interface. */ /* Sink interface. */
hb_intset_t& operator << (hb_codepoint_t v) hb_sparseset_t& operator << (hb_codepoint_t v)
{ add (v); return *this; } { add (v); return *this; }
hb_intset_t& operator << (const hb_pair_t<hb_codepoint_t, hb_codepoint_t>& range) hb_sparseset_t& operator << (const hb_pair_t<hb_codepoint_t, hb_codepoint_t>& range)
{ add_range (range.first, range.second); return *this; } { add_range (range.first, range.second); return *this; }
bool intersects (hb_codepoint_t first, hb_codepoint_t last) const bool intersects (hb_codepoint_t first, hb_codepoint_t last) const
{ return s.intersects (first, last); } { return s.intersects (first, last); }
void set (const hb_intset_t &other) { s.set (other.s); } void set (const hb_sparseset_t &other) { s.set (other.s); }
bool is_equal (const hb_intset_t &other) const { return s.is_equal (other.s); } bool is_equal (const hb_sparseset_t &other) const { return s.is_equal (other.s); }
bool is_subset (const hb_intset_t &larger_set) const { return s.is_subset (larger_set.s); } bool is_subset (const hb_sparseset_t &larger_set) const { return s.is_subset (larger_set.s); }
void union_ (const hb_intset_t &other) { s.process (hb_bitwise_or, other.s); } void union_ (const hb_sparseset_t &other) { s.union_ (other.s); }
void intersect (const hb_intset_t &other) { s.process (hb_bitwise_and, other.s); } void intersect (const hb_sparseset_t &other) { s.intersect (other.s); }
void subtract (const hb_intset_t &other) { s.process (hb_bitwise_sub, other.s); } void subtract (const hb_sparseset_t &other) { s.subtract (other.s); }
void symmetric_difference (const hb_intset_t &other) { s.process (hb_bitwise_xor, other.s); } void symmetric_difference (const hb_sparseset_t &other) { s.symmetric_difference (other.s); }
bool next (hb_codepoint_t *codepoint) const { return s.next (codepoint); } bool next (hb_codepoint_t *codepoint) const { return s.next (codepoint); }
bool previous (hb_codepoint_t *codepoint) const { return s.previous (codepoint); } bool previous (hb_codepoint_t *codepoint) const { return s.previous (codepoint); }
@ -139,7 +135,7 @@ struct hb_intset_t
operator iter_t () const { return iter (); } operator iter_t () const { return iter (); }
}; };
struct hb_set_t : hb_intset_t<hb_bit_set_t> {}; struct hb_set_t : hb_sparseset_t<hb_bit_set_invertible_t> {};
static_assert (hb_set_t::INVALID == HB_SET_VALUE_INVALID, ""); static_assert (hb_set_t::INVALID == HB_SET_VALUE_INVALID, "");