2 * Copyright © 2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_SET_PRIVATE_HH
28 #define HB_SET_PRIVATE_HH
30 #include "hb-private.hh"
32 #include "hb-object-private.hh"
35 struct hb_set_digest_common_bits_t
39 typedef unsigned int mask_t;
41 inline void init (void) {
46 inline void add (hb_codepoint_t g) {
47 if (unlikely (value == (mask_t) -1)) {
52 mask ^= (g & mask) ^ value;
56 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) {
57 /* The negation here stands for ~(x-1). */
58 mask &= -(1 << _hb_bit_storage (a ^ b));
62 inline bool may_have (hb_codepoint_t g) const {
63 return (g & mask) == value;
71 struct hb_set_digest_lowest_bits_t
75 typedef unsigned long mask_t;
77 inline void init (void) {
81 inline void add (hb_codepoint_t g) {
85 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) {
86 if (b - a >= sizeof (mask_t) * 8 - 1)
89 mask_t ma = mask_for (a);
90 mask_t mb = mask_for (b);
91 mask |= mb + (mb - ma) - (mb < ma);
95 inline bool may_have (hb_codepoint_t g) const {
96 return !!(mask & mask_for (g));
101 static inline mask_t mask_for (hb_codepoint_t g) { return ((mask_t) 1) << (g & (sizeof (mask_t) * 8 - 1)); }
105 struct hb_set_digest_t
109 inline void init (void) {
114 inline void add (hb_codepoint_t g) {
119 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) {
120 digest1.add_range (a, b);
121 digest2.add_range (a, b);
124 inline bool may_have (hb_codepoint_t g) const {
125 return digest1.may_have (g) && digest2.may_have (g);
129 hb_set_digest_common_bits_t digest1;
130 hb_set_digest_lowest_bits_t digest2;
134 /* TODO Make this faster and memmory efficient. */
138 hb_object_header_t header;
142 inline void init (void) {
146 inline void fini (void) {
148 inline void clear (void) {
149 if (unlikely (hb_object_is_inert (this)))
152 memset (elts, 0, sizeof elts);
154 inline bool is_empty (void) const {
155 for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++)
160 inline void add (hb_codepoint_t g)
162 if (unlikely (in_error)) return;
163 if (unlikely (g == SENTINEL)) return;
164 if (unlikely (g > MAX_G)) return;
167 inline void add_range (hb_codepoint_t a, hb_codepoint_t b)
169 if (unlikely (in_error)) return;
171 for (unsigned int i = a; i < b + 1; i++)
174 inline void del (hb_codepoint_t g)
176 if (unlikely (in_error)) return;
177 if (unlikely (g > MAX_G)) return;
178 elt (g) &= ~mask (g);
180 inline void del_range (hb_codepoint_t a, hb_codepoint_t b)
182 if (unlikely (in_error)) return;
184 for (unsigned int i = a; i < b + 1; i++)
187 inline bool has (hb_codepoint_t g) const
189 if (unlikely (g > MAX_G)) return false;
190 return !!(elt (g) & mask (g));
192 inline bool intersects (hb_codepoint_t first,
193 hb_codepoint_t last) const
195 if (unlikely (first > MAX_G)) return false;
196 if (unlikely (last > MAX_G)) last = MAX_G;
197 unsigned int end = last + 1;
198 for (hb_codepoint_t i = first; i < end; i++)
203 inline bool is_equal (const hb_set_t *other) const
205 for (unsigned int i = 0; i < ELTS; i++)
206 if (elts[i] != other->elts[i])
210 inline void set (const hb_set_t *other)
212 if (unlikely (in_error)) return;
213 for (unsigned int i = 0; i < ELTS; i++)
214 elts[i] = other->elts[i];
216 inline void union_ (const hb_set_t *other)
218 if (unlikely (in_error)) return;
219 for (unsigned int i = 0; i < ELTS; i++)
220 elts[i] |= other->elts[i];
222 inline void intersect (const hb_set_t *other)
224 if (unlikely (in_error)) return;
225 for (unsigned int i = 0; i < ELTS; i++)
226 elts[i] &= other->elts[i];
228 inline void subtract (const hb_set_t *other)
230 if (unlikely (in_error)) return;
231 for (unsigned int i = 0; i < ELTS; i++)
232 elts[i] &= ~other->elts[i];
234 inline void symmetric_difference (const hb_set_t *other)
236 if (unlikely (in_error)) return;
237 for (unsigned int i = 0; i < ELTS; i++)
238 elts[i] ^= other->elts[i];
240 inline void invert (void)
242 if (unlikely (in_error)) return;
243 for (unsigned int i = 0; i < ELTS; i++)
246 inline bool next (hb_codepoint_t *codepoint) const
248 if (unlikely (*codepoint == SENTINEL)) {
249 hb_codepoint_t i = get_min ();
256 for (hb_codepoint_t i = *codepoint + 1; i < MAX_G + 1; i++)
263 inline bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
272 while (next (&i) && i == *last + 1)
278 inline unsigned int get_population (void) const
280 unsigned int count = 0;
281 for (unsigned int i = 0; i < ELTS; i++)
282 count += _hb_popcount32 (elts[i]);
285 inline hb_codepoint_t get_min (void) const
287 for (unsigned int i = 0; i < ELTS; i++)
289 for (unsigned int j = 0; i < BITS; j++)
290 if (elts[i] & (1 << j))
294 inline hb_codepoint_t get_max (void) const
296 for (unsigned int i = ELTS; i; i--)
298 for (unsigned int j = BITS; j; j--)
299 if (elts[i - 1] & (1 << (j - 1)))
300 return (i - 1) * BITS + (j - 1);
304 typedef uint32_t elt_t;
305 static const unsigned int MAX_G = 65536 - 1; /* XXX Fix this... */
306 static const unsigned int SHIFT = 5;
307 static const unsigned int BITS = (1 << SHIFT);
308 static const unsigned int MASK = BITS - 1;
309 static const unsigned int ELTS = (MAX_G + 1 + (BITS - 1)) / BITS;
310 static const hb_codepoint_t SENTINEL = (hb_codepoint_t) -1;
312 elt_t &elt (hb_codepoint_t g) { return elts[g >> SHIFT]; }
313 elt_t elt (hb_codepoint_t g) const { return elts[g >> SHIFT]; }
314 elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & MASK); }
316 elt_t elts[ELTS]; /* XXX 8kb */
318 ASSERT_STATIC (sizeof (elt_t) * 8 == BITS);
319 ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G);
324 #endif /* HB_SET_PRIVATE_HH */