xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-set.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 /*
2  * Copyright © 2012,2017  Google, Inc.
3  * Copyright © 2021 Behdad Esfahbod
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Google Author(s): Behdad Esfahbod
26  */
27 
28 #ifndef HB_SET_HH
29 #define HB_SET_HH
30 
31 #include "hb.hh"
32 #include "hb-bit-set-invertible.hh"
33 
34 
35 template <typename impl_t>
36 struct hb_sparseset_t
37 {
38   static constexpr bool realloc_move = true;
39 
40   hb_object_header_t header;
41   impl_t s;
42 
hb_sparseset_thb_sparseset_t43   hb_sparseset_t () { init (); }
~hb_sparseset_thb_sparseset_t44   ~hb_sparseset_t () { fini (); }
45 
hb_sparseset_thb_sparseset_t46   hb_sparseset_t (const hb_sparseset_t& other) : hb_sparseset_t () { set (other); }
hb_sparseset_thb_sparseset_t47   hb_sparseset_t (hb_sparseset_t&& other)  noexcept : hb_sparseset_t () { s = std::move (other.s); }
operator =hb_sparseset_t48   hb_sparseset_t& operator = (const hb_sparseset_t& other) { set (other); return *this; }
operator =hb_sparseset_t49   hb_sparseset_t& operator = (hb_sparseset_t&& other)  noexcept { s = std::move (other.s); return *this; }
swap(hb_sparseset_t & a,hb_sparseset_t & b)50   friend void swap (hb_sparseset_t& a, hb_sparseset_t& b)  noexcept { hb_swap (a.s, b.s); }
51 
hb_sparseset_thb_sparseset_t52   hb_sparseset_t (std::initializer_list<hb_codepoint_t> lst) : hb_sparseset_t ()
53   {
54     for (auto&& item : lst)
55       add (item);
56   }
57   template <typename Iterable,
58            hb_requires (hb_is_iterable (Iterable))>
hb_sparseset_thb_sparseset_t59   hb_sparseset_t (const Iterable &o) : hb_sparseset_t ()
60   {
61     hb_copy (o, *this);
62   }
63 
inithb_sparseset_t64   void init ()
65   {
66     hb_object_init (this);
67     s.init ();
68   }
finihb_sparseset_t69   void fini ()
70   {
71     hb_object_fini (this);
72     s.fini ();
73   }
74 
operator boolhb_sparseset_t75   explicit operator bool () const { return !is_empty (); }
76 
errhb_sparseset_t77   void err () { s.err (); }
in_errorhb_sparseset_t78   bool in_error () const { return s.in_error (); }
79 
allochb_sparseset_t80   void alloc (unsigned sz) { s.alloc (sz); }
resethb_sparseset_t81   void reset () { s.reset (); }
clearhb_sparseset_t82   void clear () { s.clear (); }
inverthb_sparseset_t83   void invert () { s.invert (); }
is_invertedhb_sparseset_t84   bool is_inverted () const { return s.is_inverted (); }
is_emptyhb_sparseset_t85   bool is_empty () const { return s.is_empty (); }
hashhb_sparseset_t86   uint32_t hash () const { return s.hash (); }
87 
addhb_sparseset_t88   void add (hb_codepoint_t g) { s.add (g); }
add_rangehb_sparseset_t89   bool add_range (hb_codepoint_t first, hb_codepoint_t last) { return s.add_range (first, last); }
90 
91   template <typename T>
add_arrayhb_sparseset_t92   void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
93   { s.add_array (array, count, stride); }
94   template <typename T>
add_arrayhb_sparseset_t95   void add_array (const hb_array_t<const T>& arr) { add_array (&arr, arr.len ()); }
96 
97   /* Might return false if array looks unsorted.
98    * Used for faster rejection of corrupt data. */
99   template <typename T>
add_sorted_arrayhb_sparseset_t100   bool add_sorted_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
101   { return s.add_sorted_array (array, count, stride); }
102   template <typename T>
add_sorted_arrayhb_sparseset_t103   bool add_sorted_array (const hb_sorted_array_t<const T>& arr) { return add_sorted_array (&arr, arr.len ()); }
104 
delhb_sparseset_t105   void del (hb_codepoint_t g) { s.del (g); }
del_rangehb_sparseset_t106   void del_range (hb_codepoint_t a, hb_codepoint_t b) { s.del_range (a, b); }
107 
gethb_sparseset_t108   bool get (hb_codepoint_t g) const { return s.get (g); }
109 
110   /* Has interface. */
operator []hb_sparseset_t111   bool operator [] (hb_codepoint_t k) const { return get (k); }
hashb_sparseset_t112   bool has (hb_codepoint_t k) const { return (*this)[k]; }
113 
114   /* Predicate. */
operator ()hb_sparseset_t115   bool operator () (hb_codepoint_t k) const { return has (k); }
116 
117   /* Sink interface. */
operator <<hb_sparseset_t118   hb_sparseset_t& operator << (hb_codepoint_t v)
119   { add (v); return *this; }
operator <<hb_sparseset_t120   hb_sparseset_t& operator << (const hb_codepoint_pair_t& range)
121   { add_range (range.first, range.second); return *this; }
122 
intersectshb_sparseset_t123   bool intersects (hb_codepoint_t first, hb_codepoint_t last) const
124   { return s.intersects (first, last); }
125 
sethb_sparseset_t126   void set (const hb_sparseset_t &other) { s.set (other.s); }
127 
is_equalhb_sparseset_t128   bool is_equal (const hb_sparseset_t &other) const { return s.is_equal (other.s); }
operator ==hb_sparseset_t129   bool operator == (const hb_set_t &other) const { return is_equal (other); }
operator !=hb_sparseset_t130   bool operator != (const hb_set_t &other) const { return !is_equal (other); }
131 
is_subsethb_sparseset_t132   bool is_subset (const hb_sparseset_t &larger_set) const { return s.is_subset (larger_set.s); }
133 
union_hb_sparseset_t134   void union_ (const hb_sparseset_t &other) { s.union_ (other.s); }
intersecthb_sparseset_t135   void intersect (const hb_sparseset_t &other) { s.intersect (other.s); }
subtracthb_sparseset_t136   void subtract (const hb_sparseset_t &other) { s.subtract (other.s); }
symmetric_differencehb_sparseset_t137   void symmetric_difference (const hb_sparseset_t &other) { s.symmetric_difference (other.s); }
138 
nexthb_sparseset_t139   bool next (hb_codepoint_t *codepoint) const { return s.next (codepoint); }
previoushb_sparseset_t140   bool previous (hb_codepoint_t *codepoint) const { return s.previous (codepoint); }
next_rangehb_sparseset_t141   bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
142   { return s.next_range (first, last); }
previous_rangehb_sparseset_t143   bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const
144   { return s.previous_range (first, last); }
next_manyhb_sparseset_t145   unsigned int next_many (hb_codepoint_t codepoint, hb_codepoint_t *out, unsigned int size) const
146   { return s.next_many (codepoint, out, size); }
147 
get_populationhb_sparseset_t148   unsigned int get_population () const { return s.get_population (); }
get_minhb_sparseset_t149   hb_codepoint_t get_min () const { return s.get_min (); }
get_maxhb_sparseset_t150   hb_codepoint_t get_max () const { return s.get_max (); }
151 
152   static constexpr hb_codepoint_t INVALID = impl_t::INVALID;
153 
154   /*
155    * Iterator implementation.
156    */
157   using iter_t = typename impl_t::iter_t;
iterhb_sparseset_t158   iter_t iter () const { return iter_t (this->s); }
operator iter_thb_sparseset_t159   operator iter_t () const { return iter (); }
160 };
161 
162 struct hb_set_t : hb_sparseset_t<hb_bit_set_invertible_t>
163 {
164   using sparseset = hb_sparseset_t<hb_bit_set_invertible_t>;
165 
166   ~hb_set_t () = default;
hb_set_thb_set_t167   hb_set_t () : sparseset () {};
hb_set_thb_set_t168   hb_set_t (const hb_set_t &o) : sparseset ((sparseset &) o) {};
hb_set_thb_set_t169   hb_set_t (hb_set_t&& o)  noexcept : sparseset (std::move ((sparseset &) o)) {}
170   hb_set_t& operator = (const hb_set_t&) = default;
171   hb_set_t& operator = (hb_set_t&&) = default;
hb_set_thb_set_t172   hb_set_t (std::initializer_list<hb_codepoint_t> lst) : sparseset (lst) {}
173   template <typename Iterable,
174 	    hb_requires (hb_is_iterable (Iterable))>
hb_set_thb_set_t175   hb_set_t (const Iterable &o) : sparseset (o) {}
176 
operator <<hb_set_t177   hb_set_t& operator << (hb_codepoint_t v)
178   { sparseset::operator<< (v); return *this; }
operator <<hb_set_t179   hb_set_t& operator << (const hb_codepoint_pair_t& range)
180   { sparseset::operator<< (range); return *this; }
181 };
182 
183 static_assert (hb_set_t::INVALID == HB_SET_VALUE_INVALID, "");
184 
185 
186 #endif /* HB_SET_HH */
187