OLD | NEW |
1 /* | 1 /* |
2 * Copyright © 2012 Google, Inc. | 2 * Copyright © 2012 Google, Inc. |
3 * | 3 * |
4 * This is part of HarfBuzz, a text shaping library. | 4 * This is part of HarfBuzz, a text shaping library. |
5 * | 5 * |
6 * Permission is hereby granted, without written agreement and without | 6 * Permission is hereby granted, without written agreement and without |
7 * license or royalty fees, to use, copy, modify, and distribute this | 7 * license or royalty fees, to use, copy, modify, and distribute this |
8 * software and its documentation for any purpose, provided that the | 8 * software and its documentation for any purpose, provided that the |
9 * above copyright notice and the following two paragraphs appear in | 9 * above copyright notice and the following two paragraphs appear in |
10 * all copies of this software. | 10 * all copies of this software. |
(...skipping 14 matching lines...) Expand all Loading... |
25 */ | 25 */ |
26 | 26 |
27 #ifndef HB_SET_PRIVATE_HH | 27 #ifndef HB_SET_PRIVATE_HH |
28 #define HB_SET_PRIVATE_HH | 28 #define HB_SET_PRIVATE_HH |
29 | 29 |
30 #include "hb-private.hh" | 30 #include "hb-private.hh" |
31 #include "hb-set.h" | 31 #include "hb-set.h" |
32 #include "hb-object-private.hh" | 32 #include "hb-object-private.hh" |
33 | 33 |
34 | 34 |
| 35 struct hb_set_digest_common_bits_t |
| 36 { |
| 37 ASSERT_POD (); |
| 38 |
| 39 typedef unsigned int mask_t; |
| 40 |
| 41 inline void init (void) { |
| 42 mask = ~0; |
| 43 value = (mask_t) -1; |
| 44 } |
| 45 |
| 46 inline void add (hb_codepoint_t g) { |
| 47 if (unlikely (value == (mask_t) -1)) { |
| 48 value = g; |
| 49 return; |
| 50 } |
| 51 |
| 52 mask ^= (g & mask) ^ value; |
| 53 value &= mask; |
| 54 } |
| 55 |
| 56 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) { |
| 57 /* The negation here stands for ~(x-1). */ |
| 58 mask &= -(1 << _hb_bit_storage (a ^ b)); |
| 59 value &= mask; |
| 60 } |
| 61 |
| 62 inline bool may_have (hb_codepoint_t g) const { |
| 63 return (g & mask) == value; |
| 64 } |
| 65 |
| 66 private: |
| 67 mask_t mask; |
| 68 mask_t value; |
| 69 }; |
| 70 |
| 71 struct hb_set_digest_lowest_bits_t |
| 72 { |
| 73 ASSERT_POD (); |
| 74 |
| 75 typedef unsigned long mask_t; |
| 76 |
| 77 inline void init (void) { |
| 78 mask = 0; |
| 79 } |
| 80 |
| 81 inline void add (hb_codepoint_t g) { |
| 82 mask |= mask_for (g); |
| 83 } |
| 84 |
| 85 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) { |
| 86 if (b - a >= sizeof (mask_t) * 8 - 1) |
| 87 mask = (mask_t) -1; |
| 88 else { |
| 89 mask_t ma = mask_for (a); |
| 90 mask_t mb = mask_for (b); |
| 91 mask |= mb + (mb - ma) - (mb < ma); |
| 92 } |
| 93 } |
| 94 |
| 95 inline bool may_have (hb_codepoint_t g) const { |
| 96 return !!(mask & mask_for (g)); |
| 97 } |
| 98 |
| 99 private: |
| 100 |
| 101 mask_t mask_for (hb_codepoint_t g) const { return ((mask_t) 1) << (g & (sizeof
(mask_t) * 8 - 1)); } |
| 102 mask_t mask; |
| 103 }; |
| 104 |
| 105 struct hb_set_digest_t |
| 106 { |
| 107 ASSERT_POD (); |
| 108 |
| 109 inline void init (void) { |
| 110 digest1.init (); |
| 111 digest2.init (); |
| 112 } |
| 113 |
| 114 inline void add (hb_codepoint_t g) { |
| 115 digest1.add (g); |
| 116 digest2.add (g); |
| 117 } |
| 118 |
| 119 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) { |
| 120 digest1.add_range (a, b); |
| 121 digest2.add_range (a, b); |
| 122 } |
| 123 |
| 124 inline bool may_have (hb_codepoint_t g) const { |
| 125 return digest1.may_have (g) && digest2.may_have (g); |
| 126 } |
| 127 |
| 128 private: |
| 129 hb_set_digest_common_bits_t digest1; |
| 130 hb_set_digest_lowest_bits_t digest2; |
| 131 }; |
| 132 |
| 133 |
35 /* TODO Make this faster and memmory efficient. */ | 134 /* TODO Make this faster and memmory efficient. */ |
36 | 135 |
37 struct _hb_set_t | 136 struct hb_set_t |
38 { | 137 { |
39 hb_object_header_t header; | 138 hb_object_header_t header; |
40 ASSERT_POD (); | 139 ASSERT_POD (); |
41 | 140 |
42 inline void init (void) { | 141 inline void init (void) { |
43 header.init (); | 142 header.init (); |
44 clear (); | 143 clear (); |
45 } | 144 } |
46 inline void fini (void) { | 145 inline void fini (void) { |
47 } | 146 } |
48 inline void clear (void) { | 147 inline void clear (void) { |
49 memset (elts, 0, sizeof elts); | 148 memset (elts, 0, sizeof elts); |
50 } | 149 } |
51 inline bool empty (void) const { | 150 inline bool empty (void) const { |
52 for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++) | 151 for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++) |
53 if (elts[i]) | 152 if (elts[i]) |
54 return false; | 153 return false; |
55 return true; | 154 return true; |
56 } | 155 } |
57 inline void add (hb_codepoint_t g) | 156 inline void add (hb_codepoint_t g) |
58 { | 157 { |
59 if (unlikely (g == SENTINEL)) return; | 158 if (unlikely (g == SENTINEL)) return; |
60 if (unlikely (g > MAX_G)) return; | 159 if (unlikely (g > MAX_G)) return; |
61 elt (g) |= mask (g); | 160 elt (g) |= mask (g); |
62 } | 161 } |
| 162 inline void add_range (hb_codepoint_t a, hb_codepoint_t b) |
| 163 { |
| 164 for (unsigned int i = a; i < b + 1; i++) |
| 165 add (i); |
| 166 } |
63 inline void del (hb_codepoint_t g) | 167 inline void del (hb_codepoint_t g) |
64 { | 168 { |
65 if (unlikely (g > MAX_G)) return; | 169 if (unlikely (g > MAX_G)) return; |
66 elt (g) &= ~mask (g); | 170 elt (g) &= ~mask (g); |
67 } | 171 } |
68 inline bool has (hb_codepoint_t g) const | 172 inline bool has (hb_codepoint_t g) const |
69 { | 173 { |
70 if (unlikely (g > MAX_G)) return false; | 174 if (unlikely (g > MAX_G)) return false; |
71 return !!(elt (g) & mask (g)); | 175 return !!(elt (g) & mask (g)); |
72 } | 176 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 267 |
164 elt_t elts[ELTS]; /* XXX 8kb */ | 268 elt_t elts[ELTS]; /* XXX 8kb */ |
165 | 269 |
166 ASSERT_STATIC (sizeof (elt_t) * 8 == BITS); | 270 ASSERT_STATIC (sizeof (elt_t) * 8 == BITS); |
167 ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G); | 271 ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G); |
168 }; | 272 }; |
169 | 273 |
170 | 274 |
171 | 275 |
172 #endif /* HB_SET_PRIVATE_HH */ | 276 #endif /* HB_SET_PRIVATE_HH */ |
OLD | NEW |