Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Google, Inc. |
| 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" |
| 31 | #include "hb-set.h" |
| 32 | #include "hb-object-private.hh" |
| 33 | |
| 34 | |
| 35 | |
| 36 | struct _hb_set_t |
| 37 | { |
Behdad Esfahbod | 1827dc2 | 2012-04-24 16:56:37 -0400 | [diff] [blame] | 38 | inline void init (void) { |
| 39 | clear (); |
| 40 | } |
Behdad Esfahbod | a5e39fe | 2012-04-25 00:14:46 -0400 | [diff] [blame] | 41 | inline void fini (void) { |
| 42 | } |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 43 | inline void clear (void) { |
| 44 | memset (elts, 0, sizeof elts); |
| 45 | } |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 46 | inline bool empty (void) const { |
| 47 | for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++) |
| 48 | if (elts[i]) |
| 49 | return false; |
| 50 | return true; |
| 51 | } |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 52 | inline void add (hb_codepoint_t g) |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 53 | { |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 54 | if (unlikely (g > MAX_G)) return; |
| 55 | elt (g) |= mask (g); |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 56 | } |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 57 | inline void del (hb_codepoint_t g) |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 58 | { |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 59 | if (unlikely (g > MAX_G)) return; |
| 60 | elt (g) &= ~mask (g); |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 61 | } |
| 62 | inline bool has (hb_codepoint_t g) const |
| 63 | { |
| 64 | if (unlikely (g > MAX_G)) return false; |
| 65 | return !!(elt (g) & mask (g)); |
| 66 | } |
| 67 | inline bool intersects (hb_codepoint_t first, |
| 68 | hb_codepoint_t last) const |
| 69 | { |
| 70 | if (unlikely (first > MAX_G)) return false; |
| 71 | if (unlikely (last > MAX_G)) last = MAX_G; |
| 72 | unsigned int end = last + 1; |
| 73 | for (hb_codepoint_t i = first; i < end; i++) |
| 74 | if (has (i)) |
| 75 | return true; |
| 76 | return false; |
| 77 | } |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 78 | inline bool equal (const hb_set_t *other) const |
| 79 | { |
| 80 | for (unsigned int i = 0; i < ELTS; i++) |
| 81 | if (elts[i] != other->elts[i]) |
| 82 | return false; |
| 83 | return true; |
| 84 | } |
| 85 | inline void set (const hb_set_t *other) |
| 86 | { |
| 87 | for (unsigned int i = 0; i < ELTS; i++) |
| 88 | elts[i] = other->elts[i]; |
| 89 | } |
| 90 | inline void union_ (const hb_set_t *other) |
| 91 | { |
| 92 | for (unsigned int i = 0; i < ELTS; i++) |
| 93 | elts[i] |= other->elts[i]; |
| 94 | } |
| 95 | inline void intersect (const hb_set_t *other) |
| 96 | { |
| 97 | for (unsigned int i = 0; i < ELTS; i++) |
| 98 | elts[i] &= other->elts[i]; |
| 99 | } |
| 100 | inline void subtract (const hb_set_t *other) |
| 101 | { |
| 102 | for (unsigned int i = 0; i < ELTS; i++) |
| 103 | elts[i] &= ~other->elts[i]; |
| 104 | } |
| 105 | inline hb_codepoint_t min (void) const |
| 106 | { |
| 107 | for (unsigned int i = 0; i < ELTS; i++) |
| 108 | if (elts[i]) |
| 109 | for (unsigned int j = 0; i < BITS; j++) |
| 110 | if (elts[i] & (1 << j)) |
| 111 | return i * BITS + j; |
| 112 | return 0; |
| 113 | } |
| 114 | inline hb_codepoint_t max (void) const |
| 115 | { |
| 116 | for (unsigned int i = ELTS; i; i--) |
| 117 | if (elts[i - 1]) |
| 118 | for (unsigned int j = BITS; j; j--) |
| 119 | if (elts[i - 1] & (1 << (j - 1))) |
| 120 | return (i - 1) * BITS + (j - 1); |
| 121 | return 0; |
| 122 | } |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 123 | |
| 124 | typedef uint32_t elt_t; |
| 125 | static const unsigned int MAX_G = 65536 - 1; |
| 126 | static const unsigned int SHIFT = 5; |
| 127 | static const unsigned int BITS = (1 << SHIFT); |
| 128 | static const unsigned int MASK = BITS - 1; |
Behdad Esfahbod | 6c6ccaf | 2012-04-24 14:21:15 -0400 | [diff] [blame] | 129 | static const unsigned int ELTS = (MAX_G + 1 + (BITS - 1)) / BITS; |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 130 | |
| 131 | elt_t &elt (hb_codepoint_t g) { return elts[g >> SHIFT]; } |
| 132 | elt_t elt (hb_codepoint_t g) const { return elts[g >> SHIFT]; } |
| 133 | elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & MASK); } |
| 134 | |
| 135 | hb_object_header_t header; |
Behdad Esfahbod | b5fa37c | 2012-05-10 23:09:48 +0200 | [diff] [blame] | 136 | elt_t elts[ELTS]; /* XXX 8kb */ |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 137 | |
| 138 | ASSERT_STATIC (sizeof (elt_t) * 8 == BITS); |
Behdad Esfahbod | 1a2a4a0 | 2012-05-05 22:38:20 +0200 | [diff] [blame] | 139 | ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G); |
Behdad Esfahbod | 0b08adb | 2012-04-23 22:41:09 -0400 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | |
| 143 | |
| 144 | #endif /* HB_SET_PRIVATE_HH */ |