2 * Copyright © 2012,2017 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"
31 #include "hb-object-private.hh"
38 /* TODO Keep a free-list so we can free pages that are completely zeroed. At that
39 * point maybe also use a sentinel value for "all-1" pages? */
45 inline int cmp (const page_map_t *o) const { return (int) o->major - (int) major; }
53 inline void init0 (void) { memset (&v, 0, sizeof (v)); }
54 inline void init1 (void) { memset (&v, 0xff, sizeof (v)); }
56 inline unsigned int len (void) const
57 { return ARRAY_LENGTH_CONST (v); }
59 inline bool is_empty (void) const
61 for (unsigned int i = 0; i < len (); i++)
67 inline void add (hb_codepoint_t g) { elt (g) |= mask (g); }
68 inline void del (hb_codepoint_t g) { elt (g) &= ~mask (g); }
69 inline bool has (hb_codepoint_t g) const { return !!(elt (g) & mask (g)); }
71 inline void add_range (hb_codepoint_t a, hb_codepoint_t b)
76 *la |= (mask (b) << 1) - mask(a);
79 *la |= ~(mask (a) - 1);
82 memset (la, 0xff, (char *) lb - (char *) la);
84 *lb |= ((mask (b) << 1) - 1);
88 inline bool is_equal (const page_t *other) const
90 return 0 == memcmp (&v, &other->v, sizeof (v));
93 inline unsigned int get_population (void) const
96 for (unsigned int i = 0; i < len (); i++)
97 pop += _hb_popcount (v[i]);
101 inline bool next (hb_codepoint_t *codepoint) const
103 unsigned int m = (*codepoint + 1) & MASK;
106 *codepoint = INVALID;
109 unsigned int i = m / ELT_BITS;
110 unsigned int j = m & ELT_MASK;
112 const elt_t vv = v[i] & ~((elt_t (1) << j) - 1);
113 for (const elt_t *p = &vv; i < len (); p = &v[++i])
116 *codepoint = i * ELT_BITS + elt_get_min (*p);
120 *codepoint = INVALID;
123 inline bool previous (hb_codepoint_t *codepoint) const
125 unsigned int m = (*codepoint - 1) & MASK;
128 *codepoint = INVALID;
131 unsigned int i = m / ELT_BITS;
132 unsigned int j = m & ELT_MASK;
134 const elt_t vv = v[i] & ((elt_t (1) << (j + 1)) - 1);
135 for (const elt_t *p = &vv; (int) i >= 0; p = &v[--i])
138 *codepoint = i * ELT_BITS + elt_get_max (*p);
142 *codepoint = INVALID;
145 inline hb_codepoint_t get_min (void) const
147 for (unsigned int i = 0; i < len (); i++)
149 return i * ELT_BITS + elt_get_min (v[i]);
152 inline hb_codepoint_t get_max (void) const
154 for (int i = len () - 1; i >= 0; i--)
156 return i * ELT_BITS + elt_get_max (v[i]);
160 typedef unsigned long long elt_t;
161 static const unsigned int PAGE_BITS = 512;
162 static_assert ((PAGE_BITS & ((PAGE_BITS) - 1)) == 0, "");
164 static inline unsigned int elt_get_min (const elt_t &elt) { return _hb_ctz (elt); }
165 static inline unsigned int elt_get_max (const elt_t &elt) { return _hb_bit_storage (elt) - 1; }
167 typedef hb_vector_size_t<elt_t, PAGE_BITS / 8> vector_t;
169 static const unsigned int ELT_BITS = sizeof (elt_t) * 8;
170 static const unsigned int ELT_MASK = ELT_BITS - 1;
171 static const unsigned int BITS = sizeof (vector_t) * 8;
172 static const unsigned int MASK = BITS - 1;
173 static_assert (PAGE_BITS == BITS, "");
175 elt_t &elt (hb_codepoint_t g) { return v[(g & MASK) / ELT_BITS]; }
176 elt_t const &elt (hb_codepoint_t g) const { return v[(g & MASK) / ELT_BITS]; }
177 elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & ELT_MASK); }
181 static_assert (page_t::PAGE_BITS == sizeof (page_t) * 8, "");
183 hb_object_header_t header;
184 bool successful; /* Allocations successful */
185 mutable unsigned int population;
186 hb_vector_t<page_map_t, 1> page_map;
187 hb_vector_t<page_t, 1> pages;
189 inline void init_shallow (void)
196 inline void init (void)
198 hb_object_init (this);
201 inline void fini_shallow (void)
206 inline void fini (void)
208 hb_object_fini (this);
212 inline bool resize (unsigned int count)
214 if (unlikely (!successful)) return false;
215 if (!pages.resize (count) || !page_map.resize (count))
217 pages.resize (page_map.len);
224 inline void clear (void) {
225 if (unlikely (hb_object_is_inert (this)))
232 inline bool is_empty (void) const {
233 unsigned int count = pages.len;
234 for (unsigned int i = 0; i < count; i++)
235 if (!pages[i].is_empty ())
240 inline void dirty (void) { population = (unsigned int) -1; }
242 inline void add (hb_codepoint_t g)
244 if (unlikely (!successful)) return;
245 if (unlikely (g == INVALID)) return;
247 page_t *page = page_for_insert (g); if (unlikely (!page)) return;
250 inline bool add_range (hb_codepoint_t a, hb_codepoint_t b)
252 if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */
253 if (unlikely (a > b || a == INVALID || b == INVALID)) return false;
255 unsigned int ma = get_major (a);
256 unsigned int mb = get_major (b);
259 page_t *page = page_for_insert (a); if (unlikely (!page)) return false;
260 page->add_range (a, b);
264 page_t *page = page_for_insert (a); if (unlikely (!page)) return false;
265 page->add_range (a, major_start (ma + 1) - 1);
267 for (unsigned int m = ma + 1; m < mb; m++)
269 page = page_for_insert (major_start (m)); if (unlikely (!page)) return false;
273 page = page_for_insert (b); if (unlikely (!page)) return false;
274 page->add_range (major_start (mb), b);
279 template <typename T>
280 inline void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
282 if (unlikely (!successful)) return;
285 hb_codepoint_t g = *array;
288 unsigned int m = get_major (g);
289 page_t *page = page_for_insert (g); if (unlikely (!page)) return;
290 unsigned int start = major_start (m);
291 unsigned int end = major_start (m + 1);
296 array = (const T *) ((const char *) array + stride);
299 while (count && (g = *array, start <= g && g < end));
303 /* Might return false if array looks unsorted.
304 * Used for faster rejection of corrupt data. */
305 template <typename T>
306 inline bool add_sorted_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
308 if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */
309 if (!count) return true;
311 hb_codepoint_t g = *array;
312 hb_codepoint_t last_g = g;
315 unsigned int m = get_major (g);
316 page_t *page = page_for_insert (g); if (unlikely (!page)) return false;
317 unsigned int end = major_start (m + 1);
320 /* If we try harder we can change the following comparison to <=;
321 * Not sure if it's worth it. */
322 if (g < last_g) return false;
326 array = (const T *) ((const char *) array + stride);
329 while (count && (g = *array, g < end));
334 inline void del (hb_codepoint_t g)
336 /* TODO perform op even if !successful. */
337 if (unlikely (!successful)) return;
338 page_t *p = page_for (g);
344 inline void del_range (hb_codepoint_t a, hb_codepoint_t b)
346 /* TODO perform op even if !successful. */
347 /* TODO Optimize, like add_range(). */
348 if (unlikely (!successful)) return;
349 for (unsigned int i = a; i < b + 1; i++)
352 inline bool has (hb_codepoint_t g) const
354 const page_t *p = page_for (g);
359 inline bool intersects (hb_codepoint_t first,
360 hb_codepoint_t last) const
362 hb_codepoint_t c = first - 1;
363 return next (&c) && c <= last;
365 inline void set (const hb_set_t *other)
367 if (unlikely (!successful)) return;
368 unsigned int count = other->pages.len;
371 population = other->population;
372 memcpy (pages.arrayZ, other->pages.arrayZ, count * sizeof (pages.arrayZ[0]));
373 memcpy (page_map.arrayZ, other->page_map.arrayZ, count * sizeof (page_map.arrayZ[0]));
376 inline bool is_equal (const hb_set_t *other) const
378 if (get_population () != other->get_population ())
381 unsigned int na = pages.len;
382 unsigned int nb = other->pages.len;
384 unsigned int a = 0, b = 0;
385 for (; a < na && b < nb; )
387 if (page_at (a).is_empty ()) { a++; continue; }
388 if (other->page_at (b).is_empty ()) { b++; continue; }
389 if (page_map[a].major != other->page_map[b].major ||
390 !page_at (a).is_equal (&other->page_at (b)))
396 if (!page_at (a).is_empty ()) { return false; }
398 if (!other->page_at (b).is_empty ()) { return false; }
403 inline bool is_subset (const hb_set_t *larger_set) const
405 if (get_population () > larger_set->get_population ())
408 hb_codepoint_t c = INVALID;
410 if (!larger_set->has (c))
417 inline void process (const hb_set_t *other)
419 if (unlikely (!successful)) return;
423 unsigned int na = pages.len;
424 unsigned int nb = other->pages.len;
425 unsigned int next_page = na;
427 unsigned int count = 0, newCount = 0;
428 unsigned int a = 0, b = 0;
429 for (; a < na && b < nb; )
431 if (page_map[a].major == other->page_map[b].major)
437 else if (page_map[a].major < other->page_map[b].major)
439 if (Op::passthru_left)
445 if (Op::passthru_right)
450 if (Op::passthru_left)
452 if (Op::passthru_right)
455 if (count > pages.len)
460 /* Process in-place backward. */
465 if (page_map[a - 1].major == other->page_map[b - 1].major)
470 page_map[count] = page_map[a];
471 Op::process (page_at (count).v, page_at (a).v, other->page_at (b).v);
473 else if (page_map[a - 1].major > other->page_map[b - 1].major)
476 if (Op::passthru_left)
479 page_map[count] = page_map[a];
485 if (Op::passthru_right)
488 page_map[count].major = other->page_map[b].major;
489 page_map[count].index = next_page++;
490 page_at (count).v = other->page_at (b).v;
494 if (Op::passthru_left)
499 page_map[count] = page_map [a];
501 if (Op::passthru_right)
506 page_map[count].major = other->page_map[b].major;
507 page_map[count].index = next_page++;
508 page_at (count).v = other->page_at (b).v;
511 if (pages.len > newCount)
515 inline void union_ (const hb_set_t *other)
517 process<HbOpOr> (other);
519 inline void intersect (const hb_set_t *other)
521 process<HbOpAnd> (other);
523 inline void subtract (const hb_set_t *other)
525 process<HbOpMinus> (other);
527 inline void symmetric_difference (const hb_set_t *other)
529 process<HbOpXor> (other);
531 inline bool next (hb_codepoint_t *codepoint) const
533 if (unlikely (*codepoint == INVALID)) {
534 *codepoint = get_min ();
535 return *codepoint != INVALID;
538 page_map_t map = {get_major (*codepoint), 0};
540 page_map.bfind (map, &i);
541 if (i < page_map.len && page_map[i].major == map.major)
543 if (pages[page_map[i].index].next (codepoint))
545 *codepoint += page_map[i].major * page_t::PAGE_BITS;
550 for (; i < page_map.len; i++)
552 hb_codepoint_t m = pages[page_map[i].index].get_min ();
555 *codepoint = page_map[i].major * page_t::PAGE_BITS + m;
559 *codepoint = INVALID;
562 inline bool previous (hb_codepoint_t *codepoint) const
564 if (unlikely (*codepoint == INVALID)) {
565 *codepoint = get_max ();
566 return *codepoint != INVALID;
569 page_map_t map = {get_major (*codepoint), 0};
571 page_map.bfind (map, &i);
572 if (i < page_map.len && page_map[i].major == map.major)
574 if (pages[page_map[i].index].previous (codepoint))
576 *codepoint += page_map[i].major * page_t::PAGE_BITS;
581 for (; (int) i >= 0; i--)
583 hb_codepoint_t m = pages[page_map[i].index].get_max ();
586 *codepoint = page_map[i].major * page_t::PAGE_BITS + m;
590 *codepoint = INVALID;
593 inline bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
600 *last = *first = INVALID;
606 while (next (&i) && i == *last + 1)
611 inline bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const
618 *last = *first = INVALID;
624 while (previous (&i) && i == *first - 1)
630 inline unsigned int get_population (void) const
632 if (population != (unsigned int) -1)
635 unsigned int pop = 0;
636 unsigned int count = pages.len;
637 for (unsigned int i = 0; i < count; i++)
638 pop += pages[i].get_population ();
643 inline hb_codepoint_t get_min (void) const
645 unsigned int count = pages.len;
646 for (unsigned int i = 0; i < count; i++)
647 if (!page_at (i).is_empty ())
648 return page_map[i].major * page_t::PAGE_BITS + page_at (i).get_min ();
651 inline hb_codepoint_t get_max (void) const
653 unsigned int count = pages.len;
654 for (int i = count - 1; i >= 0; i++)
655 if (!page_at (i).is_empty ())
656 return page_map[i].major * page_t::PAGE_BITS + page_at (i).get_max ();
660 static const hb_codepoint_t INVALID = HB_SET_VALUE_INVALID;
662 inline page_t *page_for_insert (hb_codepoint_t g)
664 page_map_t map = {get_major (g), pages.len};
666 if (!page_map.bfind (map, &i))
668 if (!resize (pages.len + 1))
671 pages[map.index].init0 ();
672 memmove (&page_map[i + 1], &page_map[i], (page_map.len - 1 - i) * sizeof (page_map[0]));
675 return &pages[page_map[i].index];
677 inline page_t *page_for (hb_codepoint_t g)
679 page_map_t key = {get_major (g)};
680 const page_map_t *found = page_map.bsearch (key);
682 return &pages[found->index];
685 inline const page_t *page_for (hb_codepoint_t g) const
687 page_map_t key = {get_major (g)};
688 const page_map_t *found = page_map.bsearch (key);
690 return &pages[found->index];
693 inline page_t &page_at (unsigned int i) { return pages[page_map[i].index]; }
694 inline const page_t &page_at (unsigned int i) const { return pages[page_map[i].index]; }
695 inline unsigned int get_major (hb_codepoint_t g) const { return g / page_t::PAGE_BITS; }
696 inline hb_codepoint_t major_start (unsigned int major) const { return major * page_t::PAGE_BITS; }
700 #endif /* HB_SET_PRIVATE_HH */