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
31 #include "hb-machinery.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? */
43 HB_DELETE_COPY_ASSIGN (hb_set_t);
44 hb_set_t () { init (); }
45 ~hb_set_t () { fini (); }
49 int cmp (const page_map_t &o) const { return (int) o.major - (int) major; }
57 void init0 () { v.clear (); }
58 void init1 () { v.clear (0xFF); }
60 unsigned int len () const
61 { return ARRAY_LENGTH_CONST (v); }
63 bool is_empty () const
65 for (unsigned int i = 0; i < len (); i++)
71 void add (hb_codepoint_t g) { elt (g) |= mask (g); }
72 void del (hb_codepoint_t g) { elt (g) &= ~mask (g); }
73 bool get (hb_codepoint_t g) const { return elt (g) & mask (g); }
75 void add_range (hb_codepoint_t a, hb_codepoint_t b)
80 *la |= (mask (b) << 1) - mask(a);
83 *la |= ~(mask (a) - 1);
86 memset (la, 0xff, (char *) lb - (char *) la);
88 *lb |= ((mask (b) << 1) - 1);
92 void del_range (hb_codepoint_t a, hb_codepoint_t b)
97 *la &= ~((mask (b) << 1) - mask(a));
103 memset (la, 0, (char *) lb - (char *) la);
105 *lb &= ~((mask (b) << 1) - 1);
109 bool is_equal (const page_t *other) const
111 return 0 == hb_memcmp (&v, &other->v, sizeof (v));
114 unsigned int get_population () const
116 unsigned int pop = 0;
117 for (unsigned int i = 0; i < len (); i++)
118 pop += hb_popcount (v[i]);
122 bool next (hb_codepoint_t *codepoint) const
124 unsigned int m = (*codepoint + 1) & MASK;
127 *codepoint = INVALID;
130 unsigned int i = m / ELT_BITS;
131 unsigned int j = m & ELT_MASK;
133 const elt_t vv = v[i] & ~((elt_t (1) << j) - 1);
134 for (const elt_t *p = &vv; i < len (); p = &v[++i])
137 *codepoint = i * ELT_BITS + elt_get_min (*p);
141 *codepoint = INVALID;
144 bool previous (hb_codepoint_t *codepoint) const
146 unsigned int m = (*codepoint - 1) & MASK;
149 *codepoint = INVALID;
152 unsigned int i = m / ELT_BITS;
153 unsigned int j = m & ELT_MASK;
155 /* Fancy mask to avoid shifting by elt_t bitsize, which is undefined. */
156 const elt_t mask = j < 8 * sizeof (elt_t) - 1 ?
157 ((elt_t (1) << (j + 1)) - 1) :
159 const elt_t vv = v[i] & mask;
160 const elt_t *p = &vv;
165 *codepoint = i * ELT_BITS + elt_get_max (*p);
168 if ((int) i <= 0) break;
172 *codepoint = INVALID;
175 hb_codepoint_t get_min () const
177 for (unsigned int i = 0; i < len (); i++)
179 return i * ELT_BITS + elt_get_min (v[i]);
182 hb_codepoint_t get_max () const
184 for (int i = len () - 1; i >= 0; i--)
186 return i * ELT_BITS + elt_get_max (v[i]);
190 typedef unsigned long long elt_t;
191 static constexpr unsigned PAGE_BITS = 512;
192 static_assert ((PAGE_BITS & ((PAGE_BITS) - 1)) == 0, "");
194 static unsigned int elt_get_min (const elt_t &elt) { return hb_ctz (elt); }
195 static unsigned int elt_get_max (const elt_t &elt) { return hb_bit_storage (elt) - 1; }
197 typedef hb_vector_size_t<elt_t, PAGE_BITS / 8> vector_t;
199 static constexpr unsigned ELT_BITS = sizeof (elt_t) * 8;
200 static constexpr unsigned ELT_MASK = ELT_BITS - 1;
201 static constexpr unsigned BITS = sizeof (vector_t) * 8;
202 static constexpr unsigned MASK = BITS - 1;
203 static_assert ((unsigned) PAGE_BITS == (unsigned) BITS, "");
205 elt_t &elt (hb_codepoint_t g) { return v[(g & MASK) / ELT_BITS]; }
206 elt_t const &elt (hb_codepoint_t g) const { return v[(g & MASK) / ELT_BITS]; }
207 elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & ELT_MASK); }
211 static_assert (page_t::PAGE_BITS == sizeof (page_t) * 8, "");
213 hb_object_header_t header;
214 bool successful; /* Allocations successful */
215 mutable unsigned int population;
216 hb_sorted_vector_t<page_map_t> page_map;
217 hb_vector_t<page_t> pages;
228 hb_object_init (this);
239 hb_object_fini (this);
243 bool in_error () const { return !successful; }
245 bool resize (unsigned int count)
247 if (unlikely (!successful)) return false;
248 if (!pages.resize (count) || !page_map.resize (count))
250 pages.resize (page_map.length);
259 if (unlikely (hb_object_is_immutable (this)))
267 if (unlikely (hb_object_is_immutable (this)))
273 bool is_empty () const
275 unsigned int count = pages.length;
276 for (unsigned int i = 0; i < count; i++)
277 if (!pages[i].is_empty ())
282 void dirty () { population = UINT_MAX; }
284 void add (hb_codepoint_t g)
286 if (unlikely (!successful)) return;
287 if (unlikely (g == INVALID)) return;
289 page_t *page = page_for_insert (g); if (unlikely (!page)) return;
292 bool add_range (hb_codepoint_t a, hb_codepoint_t b)
294 if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */
295 if (unlikely (a > b || a == INVALID || b == INVALID)) return false;
297 unsigned int ma = get_major (a);
298 unsigned int mb = get_major (b);
301 page_t *page = page_for_insert (a); if (unlikely (!page)) return false;
302 page->add_range (a, b);
306 page_t *page = page_for_insert (a); if (unlikely (!page)) return false;
307 page->add_range (a, major_start (ma + 1) - 1);
309 for (unsigned int m = ma + 1; m < mb; m++)
311 page = page_for_insert (major_start (m)); if (unlikely (!page)) return false;
315 page = page_for_insert (b); if (unlikely (!page)) return false;
316 page->add_range (major_start (mb), b);
321 template <typename T>
322 void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
324 if (unlikely (!successful)) return;
327 hb_codepoint_t g = *array;
330 unsigned int m = get_major (g);
331 page_t *page = page_for_insert (g); if (unlikely (!page)) return;
332 unsigned int start = major_start (m);
333 unsigned int end = major_start (m + 1);
338 array = &StructAtOffsetUnaligned<T> (array, stride);
341 while (count && (g = *array, start <= g && g < end));
345 /* Might return false if array looks unsorted.
346 * Used for faster rejection of corrupt data. */
347 template <typename T>
348 bool add_sorted_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
350 if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */
351 if (!count) return true;
353 hb_codepoint_t g = *array;
354 hb_codepoint_t last_g = g;
357 unsigned int m = get_major (g);
358 page_t *page = page_for_insert (g); if (unlikely (!page)) return false;
359 unsigned int end = major_start (m + 1);
362 /* If we try harder we can change the following comparison to <=;
363 * Not sure if it's worth it. */
364 if (g < last_g) return false;
368 array = (const T *) ((const char *) array + stride);
371 while (count && (g = *array, g < end));
376 void del (hb_codepoint_t g)
378 /* TODO perform op even if !successful. */
379 if (unlikely (!successful)) return;
380 page_t *page = page_for (g);
388 void del_pages (int ds, int de)
392 unsigned int write_index = 0;
393 for (unsigned int i = 0; i < page_map.length; i++)
395 int m = (int) page_map[i].major;
396 if (m < ds || de < m)
397 page_map[write_index++] = page_map[i];
399 compact (write_index);
400 resize (write_index);
405 void del_range (hb_codepoint_t a, hb_codepoint_t b)
407 /* TODO perform op even if !successful. */
408 if (unlikely (!successful)) return;
409 if (unlikely (a > b || a == INVALID || b == INVALID)) return;
411 unsigned int ma = get_major (a);
412 unsigned int mb = get_major (b);
413 /* Delete pages from ds through de if ds <= de. */
414 int ds = (a == major_start (ma))? (int) ma: (int) (ma + 1);
415 int de = (b + 1 == major_start (mb + 1))? (int) mb: ((int) mb - 1);
416 if (ds > de || (int) ma < ds)
418 page_t *page = page_for (a);
422 page->del_range (a, b);
424 page->del_range (a, major_start (ma + 1) - 1);
427 if (de < (int) mb && ma != mb)
429 page_t *page = page_for (b);
431 page->del_range (major_start (mb), b);
436 bool get (hb_codepoint_t g) const
438 const page_t *page = page_for (g);
441 return page->get (g);
445 static constexpr bool SENTINEL = false;
446 typedef bool value_t;
447 value_t operator [] (hb_codepoint_t k) const { return get (k); }
448 bool has (hb_codepoint_t k) const { return (*this)[k] != SENTINEL; }
450 bool operator () (hb_codepoint_t k) const { return has (k); }
452 /* Sink interface. */
453 hb_set_t& operator << (hb_codepoint_t v)
454 { add (v); return *this; }
455 hb_set_t& operator << (const hb_pair_t<hb_codepoint_t, hb_codepoint_t>& range)
456 { add_range (range.first, range.second); return *this; }
458 bool intersects (hb_codepoint_t first, hb_codepoint_t last) const
460 hb_codepoint_t c = first - 1;
461 return next (&c) && c <= last;
463 void set (const hb_set_t *other)
465 if (unlikely (!successful)) return;
466 unsigned int count = other->pages.length;
469 population = other->population;
470 memcpy ((void *) pages, (const void *) other->pages, count * pages.item_size);
471 memcpy ((void *) page_map, (const void *) other->page_map, count * page_map.item_size);
474 bool is_equal (const hb_set_t *other) const
476 if (get_population () != other->get_population ())
479 unsigned int na = pages.length;
480 unsigned int nb = other->pages.length;
482 unsigned int a = 0, b = 0;
483 for (; a < na && b < nb; )
485 if (page_at (a).is_empty ()) { a++; continue; }
486 if (other->page_at (b).is_empty ()) { b++; continue; }
487 if (page_map[a].major != other->page_map[b].major ||
488 !page_at (a).is_equal (&other->page_at (b)))
494 if (!page_at (a).is_empty ()) { return false; }
496 if (!other->page_at (b).is_empty ()) { return false; }
501 bool is_subset (const hb_set_t *larger_set) const
503 if (get_population () > larger_set->get_population ())
506 /* TODO Optimize to use pages. */
507 hb_codepoint_t c = INVALID;
509 if (!larger_set->has (c))
515 void compact (unsigned int length)
517 hb_vector_t<uint32_t> old_index_to_page_map_index;
518 old_index_to_page_map_index.resize(pages.length);
519 for (uint32_t i = 0; i < old_index_to_page_map_index.length; i++)
520 old_index_to_page_map_index[i] = 0xFFFFFFFF;
522 for (uint32_t i = 0; i < length; i++)
523 old_index_to_page_map_index[page_map[i].index] = i;
525 compact_pages (old_index_to_page_map_index);
528 void compact_pages (const hb_vector_t<uint32_t>& old_index_to_page_map_index)
530 unsigned int write_index = 0;
531 for (unsigned int i = 0; i < pages.length; i++)
533 if (old_index_to_page_map_index[i] == 0xFFFFFFFF) continue;
536 pages[write_index] = pages[i];
538 page_map[old_index_to_page_map_index[i]].index = write_index;
543 template <typename Op>
544 void process (const Op& op, const hb_set_t *other)
546 if (unlikely (!successful)) return;
550 unsigned int na = pages.length;
551 unsigned int nb = other->pages.length;
552 unsigned int next_page = na;
554 unsigned int count = 0, newCount = 0;
555 unsigned int a = 0, b = 0;
556 unsigned int write_index = 0;
557 for (; a < na && b < nb; )
559 if (page_map[a].major == other->page_map[b].major)
561 if (!Op::passthru_left)
563 // Move page_map entries that we're keeping from the left side set
564 // to the front of the page_map vector. This isn't necessary if
565 // passthru_left is set since no left side pages will be removed
568 page_map[write_index] = page_map[a];
576 else if (page_map[a].major < other->page_map[b].major)
578 if (Op::passthru_left)
584 if (Op::passthru_right)
589 if (Op::passthru_left)
591 if (Op::passthru_right)
594 if (!Op::passthru_left)
597 next_page = write_index;
598 compact (write_index);
606 /* Process in-place backward. */
611 if (page_map[a - 1].major == other->page_map[b - 1].major)
616 page_map[count] = page_map[a];
617 page_at (count).v = op (page_at (a).v, other->page_at (b).v);
619 else if (page_map[a - 1].major > other->page_map[b - 1].major)
622 if (Op::passthru_left)
625 page_map[count] = page_map[a];
631 if (Op::passthru_right)
634 page_map[count].major = other->page_map[b].major;
635 page_map[count].index = next_page++;
636 page_at (count).v = other->page_at (b).v;
640 if (Op::passthru_left)
645 page_map[count] = page_map [a];
647 if (Op::passthru_right)
652 page_map[count].major = other->page_map[b].major;
653 page_map[count].index = next_page++;
654 page_at (count).v = other->page_at (b).v;
657 if (pages.length > newCount)
661 void union_ (const hb_set_t *other)
663 process (hb_bitwise_or, other);
665 void intersect (const hb_set_t *other)
667 process (hb_bitwise_and, other);
669 void subtract (const hb_set_t *other)
671 process (hb_bitwise_sub, other);
673 void symmetric_difference (const hb_set_t *other)
675 process (hb_bitwise_xor, other);
677 bool next (hb_codepoint_t *codepoint) const
679 if (unlikely (*codepoint == INVALID)) {
680 *codepoint = get_min ();
681 return *codepoint != INVALID;
684 page_map_t map = {get_major (*codepoint), 0};
686 page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST);
687 if (i < page_map.length && page_map[i].major == map.major)
689 if (pages[page_map[i].index].next (codepoint))
691 *codepoint += page_map[i].major * page_t::PAGE_BITS;
696 for (; i < page_map.length; i++)
698 hb_codepoint_t m = pages[page_map[i].index].get_min ();
701 *codepoint = page_map[i].major * page_t::PAGE_BITS + m;
705 *codepoint = INVALID;
708 bool previous (hb_codepoint_t *codepoint) const
710 if (unlikely (*codepoint == INVALID)) {
711 *codepoint = get_max ();
712 return *codepoint != INVALID;
715 page_map_t map = {get_major (*codepoint), 0};
717 page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST);
718 if (i < page_map.length && page_map[i].major == map.major)
720 if (pages[page_map[i].index].previous (codepoint))
722 *codepoint += page_map[i].major * page_t::PAGE_BITS;
727 for (; (int) i >= 0; i--)
729 hb_codepoint_t m = pages[page_map[i].index].get_max ();
732 *codepoint = page_map[i].major * page_t::PAGE_BITS + m;
736 *codepoint = INVALID;
739 bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
746 *last = *first = INVALID;
752 while (next (&i) && i == *last + 1)
757 bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const
764 *last = *first = INVALID;
770 while (previous (&i) && i == *first - 1)
776 unsigned int get_population () const
778 if (population != UINT_MAX)
781 unsigned int pop = 0;
782 unsigned int count = pages.length;
783 for (unsigned int i = 0; i < count; i++)
784 pop += pages[i].get_population ();
789 hb_codepoint_t get_min () const
791 unsigned int count = pages.length;
792 for (unsigned int i = 0; i < count; i++)
793 if (!page_at (i).is_empty ())
794 return page_map[i].major * page_t::PAGE_BITS + page_at (i).get_min ();
797 hb_codepoint_t get_max () const
799 unsigned int count = pages.length;
800 for (int i = count - 1; i >= 0; i++)
801 if (!page_at (i).is_empty ())
802 return page_map[(unsigned) i].major * page_t::PAGE_BITS + page_at (i).get_max ();
806 static constexpr hb_codepoint_t INVALID = HB_SET_VALUE_INVALID;
809 * Iterator implementation.
811 struct iter_t : hb_iter_with_fallback_t<iter_t, hb_codepoint_t>
813 static constexpr bool is_sorted_iterator = true;
814 iter_t (const hb_set_t &s_ = Null (hb_set_t),
815 bool init = true) : s (&s_), v (INVALID), l(0)
819 l = s->get_population () + 1;
824 typedef hb_codepoint_t __item_t__;
825 hb_codepoint_t __item__ () const { return v; }
826 bool __more__ () const { return v != INVALID; }
827 void __next__ () { s->next (&v); if (l) l--; }
828 void __prev__ () { s->previous (&v); }
829 unsigned __len__ () const { return l; }
830 iter_t end () const { return iter_t (*s, false); }
831 bool operator != (const iter_t& o) const
832 { return s != o.s || v != o.v; }
839 iter_t iter () const { return iter_t (*this); }
840 operator iter_t () const { return iter (); }
844 page_t *page_for_insert (hb_codepoint_t g)
846 page_map_t map = {get_major (g), pages.length};
848 if (!page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST))
850 if (!resize (pages.length + 1))
853 pages[map.index].init0 ();
854 memmove (page_map + i + 1,
856 (page_map.length - 1 - i) * page_map.item_size);
859 return &pages[page_map[i].index];
861 page_t *page_for (hb_codepoint_t g)
863 page_map_t key = {get_major (g)};
864 const page_map_t *found = page_map.bsearch (key);
866 return &pages[found->index];
869 const page_t *page_for (hb_codepoint_t g) const
871 page_map_t key = {get_major (g)};
872 const page_map_t *found = page_map.bsearch (key);
874 return &pages[found->index];
877 page_t &page_at (unsigned int i) { return pages[page_map[i].index]; }
878 const page_t &page_at (unsigned int i) const { return pages[page_map[i].index]; }
879 unsigned int get_major (hb_codepoint_t g) const { return g / page_t::PAGE_BITS; }
880 hb_codepoint_t major_start (unsigned int major) const { return major * page_t::PAGE_BITS; }
884 #endif /* HB_SET_HH */