gfx/harfbuzz/src/hb-set-private.hh

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright © 2012 Google, Inc.
michael@0 3 *
michael@0 4 * This is part of HarfBuzz, a text shaping library.
michael@0 5 *
michael@0 6 * Permission is hereby granted, without written agreement and without
michael@0 7 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 8 * software and its documentation for any purpose, provided that the
michael@0 9 * above copyright notice and the following two paragraphs appear in
michael@0 10 * all copies of this software.
michael@0 11 *
michael@0 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 16 * DAMAGE.
michael@0 17 *
michael@0 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 23 *
michael@0 24 * Google Author(s): Behdad Esfahbod
michael@0 25 */
michael@0 26
michael@0 27 #ifndef HB_SET_PRIVATE_HH
michael@0 28 #define HB_SET_PRIVATE_HH
michael@0 29
michael@0 30 #include "hb-private.hh"
michael@0 31 #include "hb-object-private.hh"
michael@0 32
michael@0 33
michael@0 34 /*
michael@0 35 * The set digests here implement various "filters" that support
michael@0 36 * "approximate member query". Conceptually these are like Bloom
michael@0 37 * Filter and Quotient Filter, however, much smaller, faster, and
michael@0 38 * designed to fit the requirements of our uses for glyph coverage
michael@0 39 * queries. As a result, our filters have much higher.
michael@0 40 */
michael@0 41
michael@0 42 template <typename mask_t, unsigned int shift>
michael@0 43 struct hb_set_digest_lowest_bits_t
michael@0 44 {
michael@0 45 ASSERT_POD ();
michael@0 46
michael@0 47 static const unsigned int mask_bytes = sizeof (mask_t);
michael@0 48 static const unsigned int mask_bits = sizeof (mask_t) * 8;
michael@0 49 static const unsigned int num_bits = 0
michael@0 50 + (mask_bytes >= 1 ? 3 : 0)
michael@0 51 + (mask_bytes >= 2 ? 1 : 0)
michael@0 52 + (mask_bytes >= 4 ? 1 : 0)
michael@0 53 + (mask_bytes >= 8 ? 1 : 0)
michael@0 54 + (mask_bytes >= 16? 1 : 0)
michael@0 55 + 0;
michael@0 56
michael@0 57 ASSERT_STATIC (shift < sizeof (hb_codepoint_t) * 8);
michael@0 58 ASSERT_STATIC (shift + num_bits <= sizeof (hb_codepoint_t) * 8);
michael@0 59
michael@0 60 inline void init (void) {
michael@0 61 mask = 0;
michael@0 62 }
michael@0 63
michael@0 64 inline void add (hb_codepoint_t g) {
michael@0 65 mask |= mask_for (g);
michael@0 66 }
michael@0 67
michael@0 68 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) {
michael@0 69 if ((b >> shift) - (a >> shift) >= mask_bits - 1)
michael@0 70 mask = (mask_t) -1;
michael@0 71 else {
michael@0 72 mask_t ma = mask_for (a);
michael@0 73 mask_t mb = mask_for (b);
michael@0 74 mask |= mb + (mb - ma) - (mb < ma);
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 inline bool may_have (hb_codepoint_t g) const {
michael@0 79 return !!(mask & mask_for (g));
michael@0 80 }
michael@0 81
michael@0 82 private:
michael@0 83
michael@0 84 static inline mask_t mask_for (hb_codepoint_t g) {
michael@0 85 return ((mask_t) 1) << ((g >> shift) & (mask_bits - 1));
michael@0 86 }
michael@0 87 mask_t mask;
michael@0 88 };
michael@0 89
michael@0 90 template <typename head_t, typename tail_t>
michael@0 91 struct hb_set_digest_combiner_t
michael@0 92 {
michael@0 93 ASSERT_POD ();
michael@0 94
michael@0 95 inline void init (void) {
michael@0 96 head.init ();
michael@0 97 tail.init ();
michael@0 98 }
michael@0 99
michael@0 100 inline void add (hb_codepoint_t g) {
michael@0 101 head.add (g);
michael@0 102 tail.add (g);
michael@0 103 }
michael@0 104
michael@0 105 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) {
michael@0 106 head.add_range (a, b);
michael@0 107 tail.add_range (a, b);
michael@0 108 }
michael@0 109
michael@0 110 inline bool may_have (hb_codepoint_t g) const {
michael@0 111 return head.may_have (g) && tail.may_have (g);
michael@0 112 }
michael@0 113
michael@0 114 private:
michael@0 115 head_t head;
michael@0 116 tail_t tail;
michael@0 117 };
michael@0 118
michael@0 119
michael@0 120 /*
michael@0 121 * hb_set_digest_t
michael@0 122 *
michael@0 123 * This is a combination of digests that performs "best".
michael@0 124 * There is not much science to this: it's a result of intuition
michael@0 125 * and testing.
michael@0 126 */
michael@0 127 typedef hb_set_digest_combiner_t
michael@0 128 <
michael@0 129 hb_set_digest_lowest_bits_t<unsigned long, 4>,
michael@0 130 hb_set_digest_combiner_t
michael@0 131 <
michael@0 132 hb_set_digest_lowest_bits_t<unsigned long, 0>,
michael@0 133 hb_set_digest_lowest_bits_t<unsigned long, 9>
michael@0 134 >
michael@0 135 > hb_set_digest_t;
michael@0 136
michael@0 137
michael@0 138
michael@0 139 /*
michael@0 140 * hb_set_t
michael@0 141 */
michael@0 142
michael@0 143
michael@0 144 /* TODO Make this faster and memmory efficient. */
michael@0 145
michael@0 146 struct hb_set_t
michael@0 147 {
michael@0 148 hb_object_header_t header;
michael@0 149 ASSERT_POD ();
michael@0 150 bool in_error;
michael@0 151
michael@0 152 inline void init (void) {
michael@0 153 header.init ();
michael@0 154 clear ();
michael@0 155 }
michael@0 156 inline void fini (void) {
michael@0 157 }
michael@0 158 inline void clear (void) {
michael@0 159 if (unlikely (hb_object_is_inert (this)))
michael@0 160 return;
michael@0 161 in_error = false;
michael@0 162 memset (elts, 0, sizeof elts);
michael@0 163 }
michael@0 164 inline bool is_empty (void) const {
michael@0 165 for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++)
michael@0 166 if (elts[i])
michael@0 167 return false;
michael@0 168 return true;
michael@0 169 }
michael@0 170 inline void add (hb_codepoint_t g)
michael@0 171 {
michael@0 172 if (unlikely (in_error)) return;
michael@0 173 if (unlikely (g == INVALID)) return;
michael@0 174 if (unlikely (g > MAX_G)) return;
michael@0 175 elt (g) |= mask (g);
michael@0 176 }
michael@0 177 inline void add_range (hb_codepoint_t a, hb_codepoint_t b)
michael@0 178 {
michael@0 179 if (unlikely (in_error)) return;
michael@0 180 /* TODO Speedup */
michael@0 181 for (unsigned int i = a; i < b + 1; i++)
michael@0 182 add (i);
michael@0 183 }
michael@0 184 inline void del (hb_codepoint_t g)
michael@0 185 {
michael@0 186 if (unlikely (in_error)) return;
michael@0 187 if (unlikely (g > MAX_G)) return;
michael@0 188 elt (g) &= ~mask (g);
michael@0 189 }
michael@0 190 inline void del_range (hb_codepoint_t a, hb_codepoint_t b)
michael@0 191 {
michael@0 192 if (unlikely (in_error)) return;
michael@0 193 /* TODO Speedup */
michael@0 194 for (unsigned int i = a; i < b + 1; i++)
michael@0 195 del (i);
michael@0 196 }
michael@0 197 inline bool has (hb_codepoint_t g) const
michael@0 198 {
michael@0 199 if (unlikely (g > MAX_G)) return false;
michael@0 200 return !!(elt (g) & mask (g));
michael@0 201 }
michael@0 202 inline bool intersects (hb_codepoint_t first,
michael@0 203 hb_codepoint_t last) const
michael@0 204 {
michael@0 205 if (unlikely (first > MAX_G)) return false;
michael@0 206 if (unlikely (last > MAX_G)) last = MAX_G;
michael@0 207 unsigned int end = last + 1;
michael@0 208 for (hb_codepoint_t i = first; i < end; i++)
michael@0 209 if (has (i))
michael@0 210 return true;
michael@0 211 return false;
michael@0 212 }
michael@0 213 inline bool is_equal (const hb_set_t *other) const
michael@0 214 {
michael@0 215 for (unsigned int i = 0; i < ELTS; i++)
michael@0 216 if (elts[i] != other->elts[i])
michael@0 217 return false;
michael@0 218 return true;
michael@0 219 }
michael@0 220 inline void set (const hb_set_t *other)
michael@0 221 {
michael@0 222 if (unlikely (in_error)) return;
michael@0 223 for (unsigned int i = 0; i < ELTS; i++)
michael@0 224 elts[i] = other->elts[i];
michael@0 225 }
michael@0 226 inline void union_ (const hb_set_t *other)
michael@0 227 {
michael@0 228 if (unlikely (in_error)) return;
michael@0 229 for (unsigned int i = 0; i < ELTS; i++)
michael@0 230 elts[i] |= other->elts[i];
michael@0 231 }
michael@0 232 inline void intersect (const hb_set_t *other)
michael@0 233 {
michael@0 234 if (unlikely (in_error)) return;
michael@0 235 for (unsigned int i = 0; i < ELTS; i++)
michael@0 236 elts[i] &= other->elts[i];
michael@0 237 }
michael@0 238 inline void subtract (const hb_set_t *other)
michael@0 239 {
michael@0 240 if (unlikely (in_error)) return;
michael@0 241 for (unsigned int i = 0; i < ELTS; i++)
michael@0 242 elts[i] &= ~other->elts[i];
michael@0 243 }
michael@0 244 inline void symmetric_difference (const hb_set_t *other)
michael@0 245 {
michael@0 246 if (unlikely (in_error)) return;
michael@0 247 for (unsigned int i = 0; i < ELTS; i++)
michael@0 248 elts[i] ^= other->elts[i];
michael@0 249 }
michael@0 250 inline void invert (void)
michael@0 251 {
michael@0 252 if (unlikely (in_error)) return;
michael@0 253 for (unsigned int i = 0; i < ELTS; i++)
michael@0 254 elts[i] = ~elts[i];
michael@0 255 }
michael@0 256 inline bool next (hb_codepoint_t *codepoint) const
michael@0 257 {
michael@0 258 if (unlikely (*codepoint == INVALID)) {
michael@0 259 hb_codepoint_t i = get_min ();
michael@0 260 if (i != INVALID) {
michael@0 261 *codepoint = i;
michael@0 262 return true;
michael@0 263 } else {
michael@0 264 *codepoint = INVALID;
michael@0 265 return false;
michael@0 266 }
michael@0 267 }
michael@0 268 for (hb_codepoint_t i = *codepoint + 1; i < MAX_G + 1; i++)
michael@0 269 if (has (i)) {
michael@0 270 *codepoint = i;
michael@0 271 return true;
michael@0 272 }
michael@0 273 *codepoint = INVALID;
michael@0 274 return false;
michael@0 275 }
michael@0 276 inline bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
michael@0 277 {
michael@0 278 hb_codepoint_t i;
michael@0 279
michael@0 280 i = *last;
michael@0 281 if (!next (&i))
michael@0 282 {
michael@0 283 *last = *first = INVALID;
michael@0 284 return false;
michael@0 285 }
michael@0 286
michael@0 287 *last = *first = i;
michael@0 288 while (next (&i) && i == *last + 1)
michael@0 289 (*last)++;
michael@0 290
michael@0 291 return true;
michael@0 292 }
michael@0 293
michael@0 294 inline unsigned int get_population (void) const
michael@0 295 {
michael@0 296 unsigned int count = 0;
michael@0 297 for (unsigned int i = 0; i < ELTS; i++)
michael@0 298 count += _hb_popcount32 (elts[i]);
michael@0 299 return count;
michael@0 300 }
michael@0 301 inline hb_codepoint_t get_min (void) const
michael@0 302 {
michael@0 303 for (unsigned int i = 0; i < ELTS; i++)
michael@0 304 if (elts[i])
michael@0 305 for (unsigned int j = 0; j < BITS; j++)
michael@0 306 if (elts[i] & (1 << j))
michael@0 307 return i * BITS + j;
michael@0 308 return INVALID;
michael@0 309 }
michael@0 310 inline hb_codepoint_t get_max (void) const
michael@0 311 {
michael@0 312 for (unsigned int i = ELTS; i; i--)
michael@0 313 if (elts[i - 1])
michael@0 314 for (unsigned int j = BITS; j; j--)
michael@0 315 if (elts[i - 1] & (1 << (j - 1)))
michael@0 316 return (i - 1) * BITS + (j - 1);
michael@0 317 return INVALID;
michael@0 318 }
michael@0 319
michael@0 320 typedef uint32_t elt_t;
michael@0 321 static const unsigned int MAX_G = 65536 - 1; /* XXX Fix this... */
michael@0 322 static const unsigned int SHIFT = 5;
michael@0 323 static const unsigned int BITS = (1 << SHIFT);
michael@0 324 static const unsigned int MASK = BITS - 1;
michael@0 325 static const unsigned int ELTS = (MAX_G + 1 + (BITS - 1)) / BITS;
michael@0 326 static const hb_codepoint_t INVALID = HB_SET_VALUE_INVALID;
michael@0 327
michael@0 328 elt_t &elt (hb_codepoint_t g) { return elts[g >> SHIFT]; }
michael@0 329 elt_t elt (hb_codepoint_t g) const { return elts[g >> SHIFT]; }
michael@0 330 elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & MASK); }
michael@0 331
michael@0 332 elt_t elts[ELTS]; /* XXX 8kb */
michael@0 333
michael@0 334 ASSERT_STATIC (sizeof (elt_t) * 8 == BITS);
michael@0 335 ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G);
michael@0 336 };
michael@0 337
michael@0 338
michael@0 339
michael@0 340 #endif /* HB_SET_PRIVATE_HH */

mercurial