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