2021-08-17 23:37:19 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
2021-11-02 04:07:09 +01:00
|
|
|
bool inverted = false;
|
2021-08-17 23:37:19 +02:00
|
|
|
|
2021-11-02 04:07:09 +01:00
|
|
|
hb_bit_set_invertible_t () = default;
|
2022-05-12 19:58:37 +02:00
|
|
|
hb_bit_set_invertible_t (const hb_bit_set_invertible_t& o) = default;
|
|
|
|
hb_bit_set_invertible_t (hb_bit_set_invertible_t&& other) : hb_bit_set_invertible_t () { hb_swap (*this, other); }
|
2021-11-02 04:07:09 +01:00
|
|
|
hb_bit_set_invertible_t& operator= (const hb_bit_set_invertible_t& o) = default;
|
2022-05-12 19:58:37 +02:00
|
|
|
hb_bit_set_invertible_t& operator= (hb_bit_set_invertible_t&& other) { hb_swap (*this, other); return *this; }
|
2021-11-02 04:07:09 +01:00
|
|
|
friend void swap (hb_bit_set_invertible_t &a, hb_bit_set_invertible_t &b)
|
|
|
|
{
|
2021-11-02 05:17:35 +01:00
|
|
|
if (likely (!a.s.successful || !b.s.successful))
|
|
|
|
return;
|
2021-11-02 04:07:09 +01:00
|
|
|
hb_swap (a.inverted, b.inverted);
|
|
|
|
hb_swap (a.s, b.s);
|
|
|
|
}
|
2021-08-17 23:37:19 +02:00
|
|
|
|
|
|
|
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 (); }
|
|
|
|
|
2022-05-18 23:24:40 +02:00
|
|
|
void alloc (unsigned sz) { s.alloc (sz); }
|
2021-08-24 18:31:49 +02:00
|
|
|
void reset ()
|
|
|
|
{
|
|
|
|
s.reset ();
|
|
|
|
inverted = false;
|
|
|
|
}
|
|
|
|
void clear ()
|
|
|
|
{
|
|
|
|
s.clear ();
|
|
|
|
if (likely (s.successful))
|
|
|
|
inverted = false;
|
|
|
|
}
|
|
|
|
void invert ()
|
|
|
|
{
|
|
|
|
if (likely (s.successful))
|
|
|
|
inverted = !inverted;
|
|
|
|
}
|
2021-08-17 23:37:19 +02:00
|
|
|
|
2023-01-06 00:26:41 +01:00
|
|
|
bool is_inverted () const
|
|
|
|
{
|
|
|
|
return inverted;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:47:33 +02:00
|
|
|
bool is_empty () const
|
|
|
|
{
|
|
|
|
hb_codepoint_t v = INVALID;
|
|
|
|
next (&v);
|
|
|
|
return v == INVALID;
|
|
|
|
}
|
2022-06-18 22:00:27 +02:00
|
|
|
uint32_t hash () const { return s.hash () ^ (uint32_t) inverted; }
|
2022-05-19 20:58:02 +02:00
|
|
|
|
2021-08-19 22:48:27 +02:00
|
|
|
hb_codepoint_t get_min () const
|
|
|
|
{
|
|
|
|
hb_codepoint_t v = INVALID;
|
|
|
|
next (&v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
hb_codepoint_t get_max () const
|
|
|
|
{
|
|
|
|
hb_codepoint_t v = INVALID;
|
|
|
|
previous (&v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
unsigned int get_population () const
|
|
|
|
{ return inverted ? INVALID - s.get_population () : s.get_population (); }
|
|
|
|
|
2021-08-17 23:37:19 +02:00
|
|
|
|
2021-08-19 08:51:24 +02:00
|
|
|
void add (hb_codepoint_t g) { unlikely (inverted) ? s.del (g) : s.add (g); }
|
|
|
|
bool add_range (hb_codepoint_t a, hb_codepoint_t b)
|
2022-06-16 00:57:16 +02:00
|
|
|
{ return unlikely (inverted) ? ((void) s.del_range (a, b), true) : s.add_range (a, b); }
|
2021-08-17 23:37:19 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
|
2021-08-19 05:20:54 +02:00
|
|
|
{ inverted ? s.del_array (array, count, stride) : s.add_array (array, count, stride); }
|
2021-08-17 23:37:19 +02:00
|
|
|
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))
|
2021-08-19 05:20:54 +02:00
|
|
|
{ return inverted ? s.del_sorted_array (array, count, stride) : s.add_sorted_array (array, count, stride); }
|
2021-08-17 23:37:19 +02:00
|
|
|
template <typename T>
|
|
|
|
bool add_sorted_array (const hb_sorted_array_t<const T>& arr) { return add_sorted_array (&arr, arr.len ()); }
|
|
|
|
|
2021-08-19 08:51:24 +02:00
|
|
|
void del (hb_codepoint_t g) { unlikely (inverted) ? s.add (g) : s.del (g); }
|
|
|
|
void del_range (hb_codepoint_t a, hb_codepoint_t b)
|
|
|
|
{ unlikely (inverted) ? (void) s.add_range (a, b) : s.del_range (a, b); }
|
2021-08-17 23:37:19 +02:00
|
|
|
|
|
|
|
bool get (hb_codepoint_t g) const { return s.get (g) ^ inverted; }
|
|
|
|
|
|
|
|
/* Has interface. */
|
2022-11-29 03:42:27 +01:00
|
|
|
bool operator [] (hb_codepoint_t k) const { return get (k); }
|
|
|
|
bool has (hb_codepoint_t k) const { return (*this)[k]; }
|
2021-08-17 23:37:19 +02:00
|
|
|
/* 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
|
2021-08-19 05:20:54 +02:00
|
|
|
{
|
|
|
|
hb_codepoint_t c = first - 1;
|
|
|
|
return next (&c) && c <= last;
|
|
|
|
}
|
2021-08-17 23:37:19 +02:00
|
|
|
|
2021-08-24 18:31:49 +02:00
|
|
|
void set (const hb_bit_set_invertible_t &other)
|
|
|
|
{
|
|
|
|
s.set (other.s);
|
|
|
|
if (likely (s.successful))
|
|
|
|
inverted = other.inverted;
|
|
|
|
}
|
2021-08-17 23:37:19 +02:00
|
|
|
|
|
|
|
bool is_equal (const hb_bit_set_invertible_t &other) const
|
2021-08-19 23:18:32 +02:00
|
|
|
{
|
|
|
|
if (likely (inverted == other.inverted))
|
|
|
|
return s.is_equal (other.s);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* TODO Add iter_ranges() and use here. */
|
|
|
|
auto it1 = iter ();
|
|
|
|
auto it2 = other.iter ();
|
|
|
|
return hb_all (+ hb_zip (it1, it2)
|
|
|
|
| hb_map ([](hb_pair_t<hb_codepoint_t, hb_codepoint_t> _) { return _.first == _.second; }));
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 23:37:19 +02:00
|
|
|
|
|
|
|
bool is_subset (const hb_bit_set_invertible_t &larger_set) const
|
|
|
|
{
|
2021-08-19 22:59:57 +02:00
|
|
|
if (unlikely (inverted != larger_set.inverted))
|
2021-08-19 05:24:29 +02:00
|
|
|
return hb_all (hb_iter (s) | hb_map (larger_set.s));
|
2021-08-19 22:59:57 +02:00
|
|
|
else
|
|
|
|
return unlikely (inverted) ? larger_set.s.is_subset (s) : s.is_subset (larger_set.s);
|
2021-08-17 23:37:19 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 08:48:00 +02:00
|
|
|
protected:
|
2021-08-17 23:37:19 +02:00
|
|
|
template <typename Op>
|
|
|
|
void process (const Op& op, const hb_bit_set_invertible_t &other)
|
2021-08-19 09:12:25 +02:00
|
|
|
{ s.process (op, other.s); }
|
2021-08-19 08:48:00 +02:00
|
|
|
public:
|
|
|
|
void union_ (const hb_bit_set_invertible_t &other)
|
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (likely (inverted == other.inverted))
|
2021-08-19 08:48:00 +02:00
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (unlikely (inverted))
|
2021-08-19 08:48:00 +02:00
|
|
|
process (hb_bitwise_and, other);
|
|
|
|
else
|
|
|
|
process (hb_bitwise_or, other); /* Main branch. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (unlikely (inverted))
|
2021-08-19 08:48:00 +02:00
|
|
|
process (hb_bitwise_gt, other);
|
|
|
|
else
|
|
|
|
process (hb_bitwise_lt, other);
|
|
|
|
}
|
2021-08-24 18:31:49 +02:00
|
|
|
if (likely (s.successful))
|
|
|
|
inverted = inverted || other.inverted;
|
2021-08-19 08:48:00 +02:00
|
|
|
}
|
|
|
|
void intersect (const hb_bit_set_invertible_t &other)
|
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (likely (inverted == other.inverted))
|
2021-08-19 08:48:00 +02:00
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (unlikely (inverted))
|
2021-08-19 08:48:00 +02:00
|
|
|
process (hb_bitwise_or, other);
|
|
|
|
else
|
|
|
|
process (hb_bitwise_and, other); /* Main branch. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (unlikely (inverted))
|
2021-08-19 21:01:07 +02:00
|
|
|
process (hb_bitwise_lt, other);
|
2021-08-19 08:48:00 +02:00
|
|
|
else
|
2021-08-19 21:01:07 +02:00
|
|
|
process (hb_bitwise_gt, other);
|
2021-08-19 08:48:00 +02:00
|
|
|
}
|
2021-08-24 18:31:49 +02:00
|
|
|
if (likely (s.successful))
|
|
|
|
inverted = inverted && other.inverted;
|
2021-08-19 08:48:00 +02:00
|
|
|
}
|
|
|
|
void subtract (const hb_bit_set_invertible_t &other)
|
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (likely (inverted == other.inverted))
|
2021-08-19 08:48:00 +02:00
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (unlikely (inverted))
|
2021-08-19 08:48:00 +02:00
|
|
|
process (hb_bitwise_lt, other);
|
|
|
|
else
|
|
|
|
process (hb_bitwise_gt, other); /* Main branch. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-19 08:51:24 +02:00
|
|
|
if (unlikely (inverted))
|
2021-08-19 21:05:12 +02:00
|
|
|
process (hb_bitwise_or, other);
|
2021-08-19 08:48:00 +02:00
|
|
|
else
|
|
|
|
process (hb_bitwise_and, other);
|
|
|
|
}
|
2021-08-24 18:31:49 +02:00
|
|
|
if (likely (s.successful))
|
|
|
|
inverted = inverted && !other.inverted;
|
2021-08-19 08:48:00 +02:00
|
|
|
}
|
|
|
|
void symmetric_difference (const hb_bit_set_invertible_t &other)
|
|
|
|
{
|
|
|
|
process (hb_bitwise_xor, other);
|
2021-08-24 18:31:49 +02:00
|
|
|
if (likely (s.successful))
|
|
|
|
inverted = inverted ^ other.inverted;
|
2021-08-19 08:48:00 +02:00
|
|
|
}
|
2021-08-17 23:37:19 +02:00
|
|
|
|
|
|
|
bool next (hb_codepoint_t *codepoint) const
|
|
|
|
{
|
2021-08-19 18:17:51 +02:00
|
|
|
if (likely (!inverted))
|
|
|
|
return s.next (codepoint);
|
|
|
|
|
|
|
|
auto old = *codepoint;
|
2021-08-20 00:33:48 +02:00
|
|
|
if (unlikely (old + 1 == INVALID))
|
|
|
|
{
|
|
|
|
*codepoint = INVALID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-19 19:19:24 +02:00
|
|
|
auto v = old;
|
|
|
|
s.next (&v);
|
|
|
|
if (old + 1 < v)
|
2021-08-19 18:17:51 +02:00
|
|
|
{
|
2021-08-19 19:19:24 +02:00
|
|
|
*codepoint = old + 1;
|
|
|
|
return true;
|
2021-08-19 18:17:51 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:26:25 +02:00
|
|
|
v = old;
|
2021-08-19 19:19:24 +02:00
|
|
|
s.next_range (&old, &v);
|
|
|
|
|
|
|
|
*codepoint = v + 1;
|
|
|
|
return *codepoint != INVALID;
|
2021-08-17 23:37:19 +02:00
|
|
|
}
|
|
|
|
bool previous (hb_codepoint_t *codepoint) const
|
|
|
|
{
|
2021-08-19 18:17:51 +02:00
|
|
|
if (likely (!inverted))
|
|
|
|
return s.previous (codepoint);
|
|
|
|
|
|
|
|
auto old = *codepoint;
|
2021-08-20 00:33:48 +02:00
|
|
|
if (unlikely (old - 1 == INVALID))
|
|
|
|
{
|
|
|
|
*codepoint = INVALID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-19 19:19:24 +02:00
|
|
|
auto v = old;
|
|
|
|
s.previous (&v);
|
2021-08-20 00:33:48 +02:00
|
|
|
|
2021-08-20 00:30:48 +02:00
|
|
|
if (old - 1 > v || v == INVALID)
|
2021-08-19 18:17:51 +02:00
|
|
|
{
|
2021-08-19 19:19:24 +02:00
|
|
|
*codepoint = old - 1;
|
|
|
|
return true;
|
2021-08-19 18:17:51 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:26:25 +02:00
|
|
|
v = old;
|
2021-08-19 19:19:24 +02:00
|
|
|
s.previous_range (&v, &old);
|
|
|
|
|
|
|
|
*codepoint = v - 1;
|
|
|
|
return *codepoint != INVALID;
|
2021-08-17 23:37:19 +02:00
|
|
|
}
|
|
|
|
bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
|
|
|
|
{
|
2021-08-19 18:17:51 +02:00
|
|
|
if (likely (!inverted))
|
|
|
|
return s.next_range (first, last);
|
|
|
|
|
2021-08-19 19:24:17 +02:00
|
|
|
if (!next (last))
|
2021-08-19 18:01:24 +02:00
|
|
|
{
|
|
|
|
*last = *first = INVALID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-19 19:24:17 +02:00
|
|
|
*first = *last;
|
|
|
|
s.next (last);
|
|
|
|
--*last;
|
2021-08-19 18:01:24 +02:00
|
|
|
return true;
|
2021-08-17 23:37:19 +02:00
|
|
|
}
|
|
|
|
bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const
|
|
|
|
{
|
2021-08-19 18:17:51 +02:00
|
|
|
if (likely (!inverted))
|
|
|
|
return s.previous_range (first, last);
|
|
|
|
|
2021-08-19 19:24:17 +02:00
|
|
|
if (!previous (first))
|
2021-08-19 18:01:24 +02:00
|
|
|
{
|
|
|
|
*last = *first = INVALID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-19 19:24:17 +02:00
|
|
|
*last = *first;
|
|
|
|
s.previous (first);
|
|
|
|
++*first;
|
2021-08-19 18:01:24 +02:00
|
|
|
return true;
|
2021-08-17 23:37:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 16:42:36 +01:00
|
|
|
unsigned int next_many (hb_codepoint_t codepoint,
|
|
|
|
hb_codepoint_t *out,
|
|
|
|
unsigned int size) const
|
2022-03-25 16:36:44 +01:00
|
|
|
{
|
2022-03-25 16:42:36 +01:00
|
|
|
return inverted ? s.next_many_inverted (codepoint, out, size)
|
|
|
|
: s.next_many (codepoint, out, size);
|
2022-03-25 16:36:44 +01:00
|
|
|
}
|
|
|
|
|
2021-08-17 23:37:19 +02:00
|
|
|
static constexpr hb_codepoint_t INVALID = hb_bit_set_t::INVALID;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterator implementation.
|
|
|
|
*/
|
2021-08-19 05:39:04 +02:00
|
|
|
struct iter_t : hb_iter_with_fallback_t<iter_t, hb_codepoint_t>
|
|
|
|
{
|
|
|
|
static constexpr bool is_sorted_iterator = true;
|
|
|
|
iter_t (const hb_bit_set_invertible_t &s_ = Null (hb_bit_set_invertible_t),
|
|
|
|
bool init = true) : s (&s_), v (INVALID), l(0)
|
|
|
|
{
|
|
|
|
if (init)
|
|
|
|
{
|
|
|
|
l = s->get_population () + 1;
|
|
|
|
__next__ ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef hb_codepoint_t __item_t__;
|
|
|
|
hb_codepoint_t __item__ () const { return v; }
|
|
|
|
bool __more__ () const { return v != INVALID; }
|
|
|
|
void __next__ () { s->next (&v); if (l) l--; }
|
|
|
|
void __prev__ () { s->previous (&v); }
|
|
|
|
unsigned __len__ () const { return l; }
|
|
|
|
iter_t end () const { return iter_t (*s, false); }
|
|
|
|
bool operator != (const iter_t& o) const
|
|
|
|
{ return s != o.s || v != o.v; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const hb_bit_set_invertible_t *s;
|
|
|
|
hb_codepoint_t v;
|
|
|
|
unsigned l;
|
|
|
|
};
|
|
|
|
iter_t iter () const { return iter_t (*this); }
|
2021-08-17 23:37:19 +02:00
|
|
|
operator iter_t () const { return iter (); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_BIT_SET_INVERTIBLE_HH */
|