Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 2 | * Copyright © 2012,2017 Google, Inc. |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 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. |
| 11 | * |
| 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 |
| 16 | * DAMAGE. |
| 17 | * |
| 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. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 27 | #ifndef HB_SET_HH |
| 28 | #define HB_SET_HH |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 29 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb.hh" |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 31 | |
| 32 | |
Behdad Esfahbod | 0edd0fd | 2013-04-17 17:26:56 -0400 | [diff] [blame] | 33 | /* |
Behdad Esfahbod | 0d5798a | 2013-04-17 18:19:21 -0400 | [diff] [blame] | 34 | * hb_set_t |
| 35 | */ |
Behdad Esfahbod | b40f2c0 | 2013-04-16 23:21:38 -0400 | [diff] [blame] | 36 | |
Behdad Esfahbod | eeb26d2 | 2017-12-02 15:22:04 -0800 | [diff] [blame] | 37 | /* TODO Keep a free-list so we can free pages that are completely zeroed. At that |
| 38 | * point maybe also use a sentinel value for "all-1" pages? */ |
| 39 | |
Behdad Esfahbod | 1bc1cb3 | 2012-06-16 15:21:55 -0400 | [diff] [blame] | 40 | struct hb_set_t |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 41 | { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 42 | struct page_map_t |
| 43 | { |
| 44 | inline int cmp (const page_map_t *o) const { return (int) o->major - (int) major; } |
| 45 | |
| 46 | uint32_t major; |
| 47 | uint32_t index; |
| 48 | }; |
| 49 | |
| 50 | struct page_t |
| 51 | { |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 52 | inline void init0 (void) { memset (&v, 0, sizeof (v)); } |
| 53 | inline void init1 (void) { memset (&v, 0xff, sizeof (v)); } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 54 | |
| 55 | inline unsigned int len (void) const |
| 56 | { return ARRAY_LENGTH_CONST (v); } |
| 57 | |
| 58 | inline bool is_empty (void) const |
| 59 | { |
| 60 | for (unsigned int i = 0; i < len (); i++) |
| 61 | if (v[i]) |
| 62 | return false; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | inline void add (hb_codepoint_t g) { elt (g) |= mask (g); } |
| 67 | inline void del (hb_codepoint_t g) { elt (g) &= ~mask (g); } |
| 68 | inline bool has (hb_codepoint_t g) const { return !!(elt (g) & mask (g)); } |
| 69 | |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 70 | inline void add_range (hb_codepoint_t a, hb_codepoint_t b) |
| 71 | { |
Behdad Esfahbod | 81f27df | 2017-12-16 06:12:06 -0800 | [diff] [blame] | 72 | elt_t *la = &elt (a); |
| 73 | elt_t *lb = &elt (b); |
| 74 | if (la == lb) |
| 75 | *la |= (mask (b) << 1) - mask(a); |
| 76 | else |
| 77 | { |
| 78 | *la |= ~(mask (a) - 1); |
| 79 | la++; |
Behdad Esfahbod | 9d0194b | 2017-12-01 13:56:06 -0800 | [diff] [blame] | 80 | |
Behdad Esfahbod | 81f27df | 2017-12-16 06:12:06 -0800 | [diff] [blame] | 81 | memset (la, 0xff, (char *) lb - (char *) la); |
Behdad Esfahbod | 9d0194b | 2017-12-01 13:56:06 -0800 | [diff] [blame] | 82 | |
Behdad Esfahbod | 81f27df | 2017-12-16 06:12:06 -0800 | [diff] [blame] | 83 | *lb |= ((mask (b) << 1) - 1); |
| 84 | } |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 87 | inline bool is_equal (const page_t *other) const |
| 88 | { |
| 89 | return 0 == memcmp (&v, &other->v, sizeof (v)); |
| 90 | } |
| 91 | |
| 92 | inline unsigned int get_population (void) const |
| 93 | { |
| 94 | unsigned int pop = 0; |
| 95 | for (unsigned int i = 0; i < len (); i++) |
Behdad Esfahbod | bddeb2b | 2018-07-10 14:12:37 +0200 | [diff] [blame] | 96 | pop += hb_popcount (v[i]); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 97 | return pop; |
| 98 | } |
| 99 | |
| 100 | inline bool next (hb_codepoint_t *codepoint) const |
| 101 | { |
| 102 | unsigned int m = (*codepoint + 1) & MASK; |
| 103 | if (!m) |
| 104 | { |
| 105 | *codepoint = INVALID; |
| 106 | return false; |
| 107 | } |
| 108 | unsigned int i = m / ELT_BITS; |
| 109 | unsigned int j = m & ELT_MASK; |
| 110 | |
Behdad Esfahbod | f18b9fb | 2018-02-16 18:14:41 -0800 | [diff] [blame] | 111 | const elt_t vv = v[i] & ~((elt_t (1) << j) - 1); |
| 112 | for (const elt_t *p = &vv; i < len (); p = &v[++i]) |
| 113 | if (*p) |
| 114 | { |
| 115 | *codepoint = i * ELT_BITS + elt_get_min (*p); |
| 116 | return true; |
| 117 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 118 | |
| 119 | *codepoint = INVALID; |
| 120 | return false; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 121 | } |
Behdad Esfahbod | 694eaf6 | 2018-02-14 01:00:10 -0800 | [diff] [blame] | 122 | inline bool previous (hb_codepoint_t *codepoint) const |
| 123 | { |
| 124 | unsigned int m = (*codepoint - 1) & MASK; |
| 125 | if (m == MASK) |
| 126 | { |
| 127 | *codepoint = INVALID; |
| 128 | return false; |
| 129 | } |
| 130 | unsigned int i = m / ELT_BITS; |
| 131 | unsigned int j = m & ELT_MASK; |
| 132 | |
Behdad Esfahbod | f18b9fb | 2018-02-16 18:14:41 -0800 | [diff] [blame] | 133 | const elt_t vv = v[i] & ((elt_t (1) << (j + 1)) - 1); |
| 134 | for (const elt_t *p = &vv; (int) i >= 0; p = &v[--i]) |
| 135 | if (*p) |
| 136 | { |
| 137 | *codepoint = i * ELT_BITS + elt_get_max (*p); |
| 138 | return true; |
| 139 | } |
Behdad Esfahbod | 694eaf6 | 2018-02-14 01:00:10 -0800 | [diff] [blame] | 140 | |
| 141 | *codepoint = INVALID; |
| 142 | return false; |
Behdad Esfahbod | 694eaf6 | 2018-02-14 01:00:10 -0800 | [diff] [blame] | 143 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 144 | inline hb_codepoint_t get_min (void) const |
| 145 | { |
| 146 | for (unsigned int i = 0; i < len (); i++) |
| 147 | if (v[i]) |
Behdad Esfahbod | f18b9fb | 2018-02-16 18:14:41 -0800 | [diff] [blame] | 148 | return i * ELT_BITS + elt_get_min (v[i]); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 149 | return INVALID; |
| 150 | } |
| 151 | inline hb_codepoint_t get_max (void) const |
| 152 | { |
| 153 | for (int i = len () - 1; i >= 0; i--) |
| 154 | if (v[i]) |
Behdad Esfahbod | f18b9fb | 2018-02-16 18:14:41 -0800 | [diff] [blame] | 155 | return i * ELT_BITS + elt_get_max (v[i]); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Behdad Esfahbod | d25c3e6 | 2018-02-16 17:45:09 -0800 | [diff] [blame] | 159 | typedef unsigned long long elt_t; |
Behdad Esfahbod | fda994e | 2018-09-07 15:02:57 -0400 | [diff] [blame^] | 160 | enum { PAGE_BITS = 512 }; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 161 | static_assert ((PAGE_BITS & ((PAGE_BITS) - 1)) == 0, ""); |
| 162 | |
Behdad Esfahbod | bddeb2b | 2018-07-10 14:12:37 +0200 | [diff] [blame] | 163 | static inline unsigned int elt_get_min (const elt_t &elt) { return hb_ctz (elt); } |
| 164 | static inline unsigned int elt_get_max (const elt_t &elt) { return hb_bit_storage (elt) - 1; } |
Behdad Esfahbod | f18b9fb | 2018-02-16 18:14:41 -0800 | [diff] [blame] | 165 | |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 166 | typedef hb_vector_size_t<elt_t, PAGE_BITS / 8> vector_t; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 167 | |
Behdad Esfahbod | fda994e | 2018-09-07 15:02:57 -0400 | [diff] [blame^] | 168 | enum { ELT_BITS = sizeof (elt_t) * 8 }; |
| 169 | enum { ELT_MASK = ELT_BITS - 1 }; |
| 170 | enum { BITS = sizeof (vector_t) * 8 }; |
| 171 | enum { MASK = BITS - 1 }; |
| 172 | static_assert ((unsigned) PAGE_BITS == (unsigned) BITS, ""); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 173 | |
| 174 | elt_t &elt (hb_codepoint_t g) { return v[(g & MASK) / ELT_BITS]; } |
| 175 | elt_t const &elt (hb_codepoint_t g) const { return v[(g & MASK) / ELT_BITS]; } |
| 176 | elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & ELT_MASK); } |
Behdad Esfahbod | 4ca211b | 2018-06-01 17:18:57 -0700 | [diff] [blame] | 177 | |
| 178 | vector_t v; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 179 | }; |
Behdad Esfahbod | d0f0ff8 | 2017-10-23 08:34:30 -0400 | [diff] [blame] | 180 | static_assert (page_t::PAGE_BITS == sizeof (page_t) * 8, ""); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 181 | |
Behdad Esfahbod | 6220e5f | 2012-06-06 03:30:09 -0400 | [diff] [blame] | 182 | hb_object_header_t header; |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 183 | bool successful; /* Allocations successful */ |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 184 | mutable unsigned int population; |
Behdad Esfahbod | 33d6f46 | 2018-06-01 17:25:35 -0700 | [diff] [blame] | 185 | hb_vector_t<page_map_t, 1> page_map; |
Behdad Esfahbod | 5c3112a | 2018-05-01 19:07:04 -0400 | [diff] [blame] | 186 | hb_vector_t<page_t, 1> pages; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 187 | |
Behdad Esfahbod | f1f6bc0 | 2018-05-02 12:56:21 -0400 | [diff] [blame] | 188 | inline void init_shallow (void) |
Behdad Esfahbod | 9daa88c | 2017-12-14 13:37:48 -0800 | [diff] [blame] | 189 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 190 | successful = true; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 191 | population = 0; |
Behdad Esfahbod | 9daa88c | 2017-12-14 13:37:48 -0800 | [diff] [blame] | 192 | page_map.init (); |
| 193 | pages.init (); |
| 194 | } |
Behdad Esfahbod | f1f6bc0 | 2018-05-02 12:56:21 -0400 | [diff] [blame] | 195 | inline void init (void) |
| 196 | { |
| 197 | hb_object_init (this); |
| 198 | init_shallow (); |
| 199 | } |
| 200 | inline void fini_shallow (void) |
Behdad Esfahbod | 9daa88c | 2017-12-14 13:37:48 -0800 | [diff] [blame] | 201 | { |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 202 | page_map.fini (); |
| 203 | pages.fini (); |
Behdad Esfahbod | 9daa88c | 2017-12-14 13:37:48 -0800 | [diff] [blame] | 204 | } |
Behdad Esfahbod | f1f6bc0 | 2018-05-02 12:56:21 -0400 | [diff] [blame] | 205 | inline void fini (void) |
| 206 | { |
| 207 | hb_object_fini (this); |
| 208 | fini_shallow (); |
| 209 | } |
Behdad Esfahbod | 9daa88c | 2017-12-14 13:37:48 -0800 | [diff] [blame] | 210 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 211 | inline bool resize (unsigned int count) |
| 212 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 213 | if (unlikely (!successful)) return false; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 214 | if (!pages.resize (count) || !page_map.resize (count)) |
| 215 | { |
| 216 | pages.resize (page_map.len); |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 217 | successful = false; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | return true; |
| 221 | } |
Behdad Esfahbod | 6220e5f | 2012-06-06 03:30:09 -0400 | [diff] [blame] | 222 | |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 223 | inline void clear (void) { |
Behdad Esfahbod | 7b1b720 | 2013-01-02 23:02:59 -0600 | [diff] [blame] | 224 | if (unlikely (hb_object_is_inert (this))) |
| 225 | return; |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 226 | successful = true; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 227 | population = 0; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 228 | page_map.resize (0); |
| 229 | pages.resize (0); |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 230 | } |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 231 | inline bool is_empty (void) const { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 232 | unsigned int count = pages.len; |
| 233 | for (unsigned int i = 0; i < count; i++) |
| 234 | if (!pages[i].is_empty ()) |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 235 | return false; |
| 236 | return true; |
| 237 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 238 | |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 239 | inline void dirty (void) { population = (unsigned int) -1; } |
| 240 | |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 241 | inline void add (hb_codepoint_t g) |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 242 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 243 | if (unlikely (!successful)) return; |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 244 | if (unlikely (g == INVALID)) return; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 245 | dirty (); |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 246 | page_t *page = page_for_insert (g); if (unlikely (!page)) return; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 247 | page->add (g); |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 248 | } |
Behdad Esfahbod | 81f27df | 2017-12-16 06:12:06 -0800 | [diff] [blame] | 249 | inline bool add_range (hb_codepoint_t a, hb_codepoint_t b) |
Behdad Esfahbod | 67bb9e8 | 2012-06-09 02:02:46 -0400 | [diff] [blame] | 250 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 251 | if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */ |
Behdad Esfahbod | 2fe5f88 | 2017-12-19 14:48:26 -0500 | [diff] [blame] | 252 | if (unlikely (a > b || a == INVALID || b == INVALID)) return false; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 253 | dirty (); |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 254 | unsigned int ma = get_major (a); |
| 255 | unsigned int mb = get_major (b); |
| 256 | if (ma == mb) |
| 257 | { |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 258 | page_t *page = page_for_insert (a); if (unlikely (!page)) return false; |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 259 | page->add_range (a, b); |
| 260 | } |
| 261 | else |
| 262 | { |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 263 | page_t *page = page_for_insert (a); if (unlikely (!page)) return false; |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 264 | page->add_range (a, major_start (ma + 1) - 1); |
| 265 | |
| 266 | for (unsigned int m = ma + 1; m < mb; m++) |
| 267 | { |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 268 | page = page_for_insert (major_start (m)); if (unlikely (!page)) return false; |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 269 | page->init1 (); |
| 270 | } |
| 271 | |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 272 | page = page_for_insert (b); if (unlikely (!page)) return false; |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 273 | page->add_range (major_start (mb), b); |
| 274 | } |
Behdad Esfahbod | 81f27df | 2017-12-16 06:12:06 -0800 | [diff] [blame] | 275 | return true; |
Behdad Esfahbod | 67bb9e8 | 2012-06-09 02:02:46 -0400 | [diff] [blame] | 276 | } |
Behdad Esfahbod | 5d02572 | 2017-12-14 19:33:55 -0800 | [diff] [blame] | 277 | |
Behdad Esfahbod | 0fe62c1 | 2017-12-13 13:12:20 -0800 | [diff] [blame] | 278 | template <typename T> |
Behdad Esfahbod | 5d02572 | 2017-12-14 19:33:55 -0800 | [diff] [blame] | 279 | inline void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T)) |
Behdad Esfahbod | 0fe62c1 | 2017-12-13 13:12:20 -0800 | [diff] [blame] | 280 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 281 | if (unlikely (!successful)) return; |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 282 | if (!count) return; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 283 | dirty (); |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 284 | hb_codepoint_t g = *array; |
| 285 | while (count) |
Behdad Esfahbod | 5d02572 | 2017-12-14 19:33:55 -0800 | [diff] [blame] | 286 | { |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 287 | unsigned int m = get_major (g); |
| 288 | page_t *page = page_for_insert (g); if (unlikely (!page)) return; |
| 289 | unsigned int start = major_start (m); |
| 290 | unsigned int end = major_start (m + 1); |
| 291 | do |
| 292 | { |
| 293 | page->add (g); |
| 294 | |
Behdad Esfahbod | 34ac354 | 2018-02-07 18:07:45 -0600 | [diff] [blame] | 295 | array = (const T *) ((const char *) array + stride); |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 296 | count--; |
| 297 | } |
| 298 | while (count && (g = *array, start <= g && g < end)); |
Behdad Esfahbod | 5d02572 | 2017-12-14 19:33:55 -0800 | [diff] [blame] | 299 | } |
Behdad Esfahbod | 0fe62c1 | 2017-12-13 13:12:20 -0800 | [diff] [blame] | 300 | } |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 301 | |
Behdad Esfahbod | 5d02572 | 2017-12-14 19:33:55 -0800 | [diff] [blame] | 302 | /* Might return false if array looks unsorted. |
| 303 | * Used for faster rejection of corrupt data. */ |
| 304 | template <typename T> |
| 305 | inline bool add_sorted_array (const T *array, unsigned int count, unsigned int stride=sizeof(T)) |
| 306 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 307 | if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */ |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 308 | if (!count) return true; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 309 | dirty (); |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 310 | hb_codepoint_t g = *array; |
Behdad Esfahbod | 493a005 | 2017-12-16 11:49:39 -0500 | [diff] [blame] | 311 | hb_codepoint_t last_g = g; |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 312 | while (count) |
Behdad Esfahbod | 5d02572 | 2017-12-14 19:33:55 -0800 | [diff] [blame] | 313 | { |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 314 | unsigned int m = get_major (g); |
| 315 | page_t *page = page_for_insert (g); if (unlikely (!page)) return false; |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 316 | unsigned int end = major_start (m + 1); |
| 317 | do |
| 318 | { |
Behdad Esfahbod | 493a005 | 2017-12-16 11:49:39 -0500 | [diff] [blame] | 319 | /* If we try harder we can change the following comparison to <=; |
| 320 | * Not sure if it's worth it. */ |
| 321 | if (g < last_g) return false; |
| 322 | last_g = g; |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 323 | page->add (g); |
| 324 | |
Behdad Esfahbod | 34ac354 | 2018-02-07 18:07:45 -0600 | [diff] [blame] | 325 | array = (const T *) ((const char *) array + stride); |
Behdad Esfahbod | 1ce7d6e | 2017-12-16 11:36:16 -0500 | [diff] [blame] | 326 | count--; |
| 327 | } |
Behdad Esfahbod | 493a005 | 2017-12-16 11:49:39 -0500 | [diff] [blame] | 328 | while (count && (g = *array, g < end)); |
Behdad Esfahbod | 5d02572 | 2017-12-14 19:33:55 -0800 | [diff] [blame] | 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 333 | inline void del (hb_codepoint_t g) |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 334 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 335 | /* TODO perform op even if !successful. */ |
| 336 | if (unlikely (!successful)) return; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 337 | page_t *p = page_for (g); |
| 338 | if (!p) |
| 339 | return; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 340 | dirty (); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 341 | p->del (g); |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 342 | } |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 343 | inline void del_range (hb_codepoint_t a, hb_codepoint_t b) |
| 344 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 345 | /* TODO perform op even if !successful. */ |
Behdad Esfahbod | eeb26d2 | 2017-12-02 15:22:04 -0800 | [diff] [blame] | 346 | /* TODO Optimize, like add_range(). */ |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 347 | if (unlikely (!successful)) return; |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 348 | for (unsigned int i = a; i < b + 1; i++) |
| 349 | del (i); |
| 350 | } |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 351 | inline bool has (hb_codepoint_t g) const |
| 352 | { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 353 | const page_t *p = page_for (g); |
| 354 | if (!p) |
| 355 | return false; |
| 356 | return p->has (g); |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 357 | } |
| 358 | inline bool intersects (hb_codepoint_t first, |
| 359 | hb_codepoint_t last) const |
| 360 | { |
Behdad Esfahbod | 10d4365 | 2017-10-15 16:56:05 -0400 | [diff] [blame] | 361 | hb_codepoint_t c = first - 1; |
| 362 | return next (&c) && c <= last; |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 363 | } |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 364 | inline void set (const hb_set_t *other) |
| 365 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 366 | if (unlikely (!successful)) return; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 367 | unsigned int count = other->pages.len; |
| 368 | if (!resize (count)) |
| 369 | return; |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 370 | population = other->population; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 371 | memcpy (pages.arrayZ, other->pages.arrayZ, count * sizeof (pages.arrayZ[0])); |
| 372 | memcpy (page_map.arrayZ, other->page_map.arrayZ, count * sizeof (page_map.arrayZ[0])); |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 373 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 374 | |
| 375 | inline bool is_equal (const hb_set_t *other) const |
| 376 | { |
Behdad Esfahbod | dd22c29 | 2018-05-22 20:57:19 -0700 | [diff] [blame] | 377 | if (get_population () != other->get_population ()) |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 378 | return false; |
| 379 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 380 | unsigned int na = pages.len; |
| 381 | unsigned int nb = other->pages.len; |
| 382 | |
| 383 | unsigned int a = 0, b = 0; |
| 384 | for (; a < na && b < nb; ) |
| 385 | { |
| 386 | if (page_at (a).is_empty ()) { a++; continue; } |
| 387 | if (other->page_at (b).is_empty ()) { b++; continue; } |
| 388 | if (page_map[a].major != other->page_map[b].major || |
| 389 | !page_at (a).is_equal (&other->page_at (b))) |
| 390 | return false; |
| 391 | a++; |
| 392 | b++; |
| 393 | } |
| 394 | for (; a < na; a++) |
| 395 | if (!page_at (a).is_empty ()) { return false; } |
| 396 | for (; b < nb; b++) |
| 397 | if (!other->page_at (b).is_empty ()) { return false; } |
| 398 | |
| 399 | return true; |
| 400 | } |
| 401 | |
Behdad Esfahbod | 11f1f41 | 2018-06-06 16:46:50 -0700 | [diff] [blame] | 402 | inline bool is_subset (const hb_set_t *larger_set) const |
| 403 | { |
| 404 | if (get_population () > larger_set->get_population ()) |
| 405 | return false; |
| 406 | |
Behdad Esfahbod | 7cb47d0 | 2018-07-10 12:51:29 +0200 | [diff] [blame] | 407 | /* TODO Optimize to use pages. */ |
Behdad Esfahbod | 11f1f41 | 2018-06-06 16:46:50 -0700 | [diff] [blame] | 408 | hb_codepoint_t c = INVALID; |
| 409 | while (next (&c)) |
| 410 | if (!larger_set->has (c)) |
| 411 | return false; |
| 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 416 | template <class Op> |
| 417 | inline void process (const hb_set_t *other) |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 418 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 419 | if (unlikely (!successful)) return; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 420 | |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 421 | dirty (); |
| 422 | |
Behdad Esfahbod | 30a591e | 2017-10-23 14:28:35 -0400 | [diff] [blame] | 423 | unsigned int na = pages.len; |
| 424 | unsigned int nb = other->pages.len; |
Behdad Esfahbod | f014a12 | 2018-03-07 10:49:26 +0100 | [diff] [blame] | 425 | unsigned int next_page = na; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 426 | |
Jonathan Kew | 82484b0 | 2018-06-11 20:55:14 -0700 | [diff] [blame] | 427 | unsigned int count = 0, newCount = 0; |
Behdad Esfahbod | 30a591e | 2017-10-23 14:28:35 -0400 | [diff] [blame] | 428 | unsigned int a = 0, b = 0; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 429 | for (; a < na && b < nb; ) |
| 430 | { |
| 431 | if (page_map[a].major == other->page_map[b].major) |
| 432 | { |
| 433 | count++; |
| 434 | a++; |
| 435 | b++; |
| 436 | } |
| 437 | else if (page_map[a].major < other->page_map[b].major) |
| 438 | { |
| 439 | if (Op::passthru_left) |
| 440 | count++; |
| 441 | a++; |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | if (Op::passthru_right) |
| 446 | count++; |
| 447 | b++; |
| 448 | } |
| 449 | } |
| 450 | if (Op::passthru_left) |
| 451 | count += na - a; |
| 452 | if (Op::passthru_right) |
| 453 | count += nb - b; |
| 454 | |
Jonathan Kew | 82484b0 | 2018-06-11 20:55:14 -0700 | [diff] [blame] | 455 | if (count > pages.len) |
| 456 | if (!resize (count)) |
| 457 | return; |
| 458 | newCount = count; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 459 | |
| 460 | /* Process in-place backward. */ |
Behdad Esfahbod | 30a591e | 2017-10-23 14:28:35 -0400 | [diff] [blame] | 461 | a = na; |
| 462 | b = nb; |
| 463 | for (; a && b; ) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 464 | { |
Jonathan Kew | dfd234a | 2017-10-26 16:59:50 +0100 | [diff] [blame] | 465 | if (page_map[a - 1].major == other->page_map[b - 1].major) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 466 | { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 467 | a--; |
| 468 | b--; |
Behdad Esfahbod | 7587683 | 2018-03-07 09:55:22 +0100 | [diff] [blame] | 469 | count--; |
Behdad Esfahbod | f014a12 | 2018-03-07 10:49:26 +0100 | [diff] [blame] | 470 | page_map[count] = page_map[a]; |
Behdad Esfahbod | 7587683 | 2018-03-07 09:55:22 +0100 | [diff] [blame] | 471 | Op::process (page_at (count).v, page_at (a).v, other->page_at (b).v); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 472 | } |
Jonathan Kew | dfd234a | 2017-10-26 16:59:50 +0100 | [diff] [blame] | 473 | else if (page_map[a - 1].major > other->page_map[b - 1].major) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 474 | { |
Behdad Esfahbod | 7587683 | 2018-03-07 09:55:22 +0100 | [diff] [blame] | 475 | a--; |
| 476 | if (Op::passthru_left) |
| 477 | { |
| 478 | count--; |
Behdad Esfahbod | f014a12 | 2018-03-07 10:49:26 +0100 | [diff] [blame] | 479 | page_map[count] = page_map[a]; |
Behdad Esfahbod | 7587683 | 2018-03-07 09:55:22 +0100 | [diff] [blame] | 480 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 481 | } |
| 482 | else |
| 483 | { |
Behdad Esfahbod | 7587683 | 2018-03-07 09:55:22 +0100 | [diff] [blame] | 484 | b--; |
| 485 | if (Op::passthru_right) |
| 486 | { |
| 487 | count--; |
Behdad Esfahbod | f014a12 | 2018-03-07 10:49:26 +0100 | [diff] [blame] | 488 | page_map[count].major = other->page_map[b].major; |
| 489 | page_map[count].index = next_page++; |
Behdad Esfahbod | 7587683 | 2018-03-07 09:55:22 +0100 | [diff] [blame] | 490 | page_at (count).v = other->page_at (b).v; |
| 491 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 492 | } |
| 493 | } |
Behdad Esfahbod | 8170801 | 2017-10-23 14:26:48 -0400 | [diff] [blame] | 494 | if (Op::passthru_left) |
Behdad Esfahbod | 30a591e | 2017-10-23 14:28:35 -0400 | [diff] [blame] | 495 | while (a) |
Behdad Esfahbod | f014a12 | 2018-03-07 10:49:26 +0100 | [diff] [blame] | 496 | { |
| 497 | a--; |
| 498 | count--; |
| 499 | page_map[count] = page_map [a]; |
| 500 | } |
Behdad Esfahbod | 8170801 | 2017-10-23 14:26:48 -0400 | [diff] [blame] | 501 | if (Op::passthru_right) |
Behdad Esfahbod | 30a591e | 2017-10-23 14:28:35 -0400 | [diff] [blame] | 502 | while (b) |
Behdad Esfahbod | f014a12 | 2018-03-07 10:49:26 +0100 | [diff] [blame] | 503 | { |
| 504 | b--; |
| 505 | count--; |
| 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; |
| 509 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 510 | assert (!count); |
Jonathan Kew | 82484b0 | 2018-06-11 20:55:14 -0700 | [diff] [blame] | 511 | if (pages.len > newCount) |
| 512 | resize (newCount); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | inline void union_ (const hb_set_t *other) |
| 516 | { |
Behdad Esfahbod | f8a0ec5 | 2017-10-15 16:10:35 -0400 | [diff] [blame] | 517 | process<HbOpOr> (other); |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 518 | } |
| 519 | inline void intersect (const hb_set_t *other) |
| 520 | { |
Behdad Esfahbod | f8a0ec5 | 2017-10-15 16:10:35 -0400 | [diff] [blame] | 521 | process<HbOpAnd> (other); |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 522 | } |
| 523 | inline void subtract (const hb_set_t *other) |
| 524 | { |
Behdad Esfahbod | f8a0ec5 | 2017-10-15 16:10:35 -0400 | [diff] [blame] | 525 | process<HbOpMinus> (other); |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 526 | } |
Behdad Esfahbod | 62c3e11 | 2012-05-25 13:48:00 -0400 | [diff] [blame] | 527 | inline void symmetric_difference (const hb_set_t *other) |
| 528 | { |
Behdad Esfahbod | f8a0ec5 | 2017-10-15 16:10:35 -0400 | [diff] [blame] | 529 | process<HbOpXor> (other); |
Behdad Esfahbod | 8165f27 | 2013-01-02 22:50:36 -0600 | [diff] [blame] | 530 | } |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 531 | inline bool next (hb_codepoint_t *codepoint) const |
Behdad Esfahbod | 29ce446 | 2012-05-25 14:17:54 -0400 | [diff] [blame] | 532 | { |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 533 | if (unlikely (*codepoint == INVALID)) { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 534 | *codepoint = get_min (); |
| 535 | return *codepoint != INVALID; |
| 536 | } |
| 537 | |
Behdad Esfahbod | dd33e4e | 2017-10-23 08:36:40 -0400 | [diff] [blame] | 538 | page_map_t map = {get_major (*codepoint), 0}; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 539 | unsigned int i; |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 540 | page_map.bfind (map, &i); |
Behdad Esfahbod | fe3bc52 | 2018-02-13 23:51:45 -0800 | [diff] [blame] | 541 | if (i < page_map.len && page_map[i].major == map.major) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 542 | { |
| 543 | if (pages[page_map[i].index].next (codepoint)) |
| 544 | { |
Behdad Esfahbod | d0f0ff8 | 2017-10-23 08:34:30 -0400 | [diff] [blame] | 545 | *codepoint += page_map[i].major * page_t::PAGE_BITS; |
Behdad Esfahbod | 694eaf6 | 2018-02-14 01:00:10 -0800 | [diff] [blame] | 546 | return true; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 547 | } |
| 548 | i++; |
| 549 | } |
| 550 | for (; i < page_map.len; i++) |
| 551 | { |
| 552 | hb_codepoint_t m = pages[page_map[i].index].get_min (); |
| 553 | if (m != INVALID) |
| 554 | { |
Behdad Esfahbod | d0f0ff8 | 2017-10-23 08:34:30 -0400 | [diff] [blame] | 555 | *codepoint = page_map[i].major * page_t::PAGE_BITS + m; |
Behdad Esfahbod | 29ce446 | 2012-05-25 14:17:54 -0400 | [diff] [blame] | 556 | return true; |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 557 | } |
Behdad Esfahbod | 29ce446 | 2012-05-25 14:17:54 -0400 | [diff] [blame] | 558 | } |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 559 | *codepoint = INVALID; |
Behdad Esfahbod | 29ce446 | 2012-05-25 14:17:54 -0400 | [diff] [blame] | 560 | return false; |
| 561 | } |
Behdad Esfahbod | 694eaf6 | 2018-02-14 01:00:10 -0800 | [diff] [blame] | 562 | inline bool previous (hb_codepoint_t *codepoint) const |
| 563 | { |
| 564 | if (unlikely (*codepoint == INVALID)) { |
| 565 | *codepoint = get_max (); |
| 566 | return *codepoint != INVALID; |
| 567 | } |
| 568 | |
| 569 | page_map_t map = {get_major (*codepoint), 0}; |
| 570 | unsigned int i; |
| 571 | page_map.bfind (map, &i); |
| 572 | if (i < page_map.len && page_map[i].major == map.major) |
| 573 | { |
| 574 | if (pages[page_map[i].index].previous (codepoint)) |
| 575 | { |
| 576 | *codepoint += page_map[i].major * page_t::PAGE_BITS; |
| 577 | return true; |
| 578 | } |
| 579 | } |
| 580 | i--; |
| 581 | for (; (int) i >= 0; i--) |
| 582 | { |
| 583 | hb_codepoint_t m = pages[page_map[i].index].get_max (); |
| 584 | if (m != INVALID) |
| 585 | { |
| 586 | *codepoint = page_map[i].major * page_t::PAGE_BITS + m; |
| 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | *codepoint = INVALID; |
| 591 | return false; |
| 592 | } |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 593 | inline bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const |
| 594 | { |
| 595 | hb_codepoint_t i; |
| 596 | |
| 597 | i = *last; |
| 598 | if (!next (&i)) |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 599 | { |
| 600 | *last = *first = INVALID; |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 601 | return false; |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 602 | } |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 603 | |
Behdad Esfahbod | 694eaf6 | 2018-02-14 01:00:10 -0800 | [diff] [blame] | 604 | /* TODO Speed up. */ |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 605 | *last = *first = i; |
| 606 | while (next (&i) && i == *last + 1) |
| 607 | (*last)++; |
| 608 | |
| 609 | return true; |
| 610 | } |
Behdad Esfahbod | 694eaf6 | 2018-02-14 01:00:10 -0800 | [diff] [blame] | 611 | inline bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const |
| 612 | { |
| 613 | hb_codepoint_t i; |
| 614 | |
| 615 | i = *first; |
| 616 | if (!previous (&i)) |
| 617 | { |
| 618 | *last = *first = INVALID; |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | /* TODO Speed up. */ |
| 623 | *last = *first = i; |
| 624 | while (previous (&i) && i == *first - 1) |
| 625 | (*first)--; |
| 626 | |
| 627 | return true; |
| 628 | } |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 629 | |
| 630 | inline unsigned int get_population (void) const |
| 631 | { |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 632 | if (population != (unsigned int) -1) |
| 633 | return population; |
| 634 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 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 (); |
Behdad Esfahbod | bd5f918 | 2018-05-01 18:27:41 -0400 | [diff] [blame] | 639 | |
| 640 | population = pop; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 641 | return pop; |
Behdad Esfahbod | aec89de | 2012-11-15 16:15:42 -0800 | [diff] [blame] | 642 | } |
Behdad Esfahbod | f039e79 | 2012-05-17 20:55:12 -0400 | [diff] [blame] | 643 | inline hb_codepoint_t get_min (void) const |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 644 | { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 645 | unsigned int count = pages.len; |
| 646 | for (unsigned int i = 0; i < count; i++) |
| 647 | if (!page_at (i).is_empty ()) |
Behdad Esfahbod | d0f0ff8 | 2017-10-23 08:34:30 -0400 | [diff] [blame] | 648 | return page_map[i].major * page_t::PAGE_BITS + page_at (i).get_min (); |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 649 | return INVALID; |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 650 | } |
Behdad Esfahbod | f039e79 | 2012-05-17 20:55:12 -0400 | [diff] [blame] | 651 | inline hb_codepoint_t get_max (void) const |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 652 | { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 653 | unsigned int count = pages.len; |
| 654 | for (int i = count - 1; i >= 0; i++) |
| 655 | if (!page_at (i).is_empty ()) |
Behdad Esfahbod | d0f0ff8 | 2017-10-23 08:34:30 -0400 | [diff] [blame] | 656 | return page_map[i].major * page_t::PAGE_BITS + page_at (i).get_max (); |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 657 | return INVALID; |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 658 | } |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 659 | |
Behdad Esfahbod | 20cbc1f | 2013-09-06 15:29:22 -0400 | [diff] [blame] | 660 | static const hb_codepoint_t INVALID = HB_SET_VALUE_INVALID; |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 661 | |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 662 | inline page_t *page_for_insert (hb_codepoint_t g) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 663 | { |
Behdad Esfahbod | dd33e4e | 2017-10-23 08:36:40 -0400 | [diff] [blame] | 664 | page_map_t map = {get_major (g), pages.len}; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 665 | unsigned int i; |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 666 | if (!page_map.bfind (map, &i)) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 667 | { |
| 668 | if (!resize (pages.len + 1)) |
| 669 | return nullptr; |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 670 | |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 671 | pages[map.index].init0 (); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 672 | memmove (&page_map[i + 1], &page_map[i], (page_map.len - 1 - i) * sizeof (page_map[0])); |
| 673 | page_map[i] = map; |
| 674 | } |
| 675 | return &pages[page_map[i].index]; |
| 676 | } |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 677 | inline page_t *page_for (hb_codepoint_t g) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 678 | { |
Behdad Esfahbod | dd33e4e | 2017-10-23 08:36:40 -0400 | [diff] [blame] | 679 | page_map_t key = {get_major (g)}; |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 680 | const page_map_t *found = page_map.bsearch (key); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 681 | if (found) |
| 682 | return &pages[found->index]; |
| 683 | return nullptr; |
| 684 | } |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 685 | inline const page_t *page_for (hb_codepoint_t g) const |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 686 | { |
Behdad Esfahbod | dd33e4e | 2017-10-23 08:36:40 -0400 | [diff] [blame] | 687 | page_map_t key = {get_major (g)}; |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 688 | const page_map_t *found = page_map.bsearch (key); |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 689 | if (found) |
| 690 | return &pages[found->index]; |
| 691 | return nullptr; |
| 692 | } |
Behdad Esfahbod | 438c325 | 2017-12-01 13:34:14 -0800 | [diff] [blame] | 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; } |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 697 | }; |
| 698 | |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 699 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 700 | #endif /* HB_SET_HH */ |