Revert "Imported Upstream version 1.2.7"
[platform/upstream/harfbuzz.git] / src / hb-set-private.hh
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-object-private.hh"
32
33
34 /*
35  * The set digests here implement various "filters" that support
36  * "approximate member query".  Conceptually these are like Bloom
37  * Filter and Quotient Filter, however, much smaller, faster, and
38  * designed to fit the requirements of our uses for glyph coverage
39  * queries.  As a result, our filters have much higher.
40  */
41
42 template <typename mask_t, unsigned int shift>
43 struct hb_set_digest_lowest_bits_t
44 {
45   ASSERT_POD ();
46
47   static const unsigned int mask_bytes = sizeof (mask_t);
48   static const unsigned int mask_bits = sizeof (mask_t) * 8;
49   static const unsigned int num_bits = 0
50                                      + (mask_bytes >= 1 ? 3 : 0)
51                                      + (mask_bytes >= 2 ? 1 : 0)
52                                      + (mask_bytes >= 4 ? 1 : 0)
53                                      + (mask_bytes >= 8 ? 1 : 0)
54                                      + (mask_bytes >= 16? 1 : 0)
55                                      + 0;
56
57   ASSERT_STATIC (shift < sizeof (hb_codepoint_t) * 8);
58   ASSERT_STATIC (shift + num_bits <= sizeof (hb_codepoint_t) * 8);
59
60   inline void init (void) {
61     mask = 0;
62   }
63
64   inline void add (hb_codepoint_t g) {
65     mask |= mask_for (g);
66   }
67
68   inline void add_range (hb_codepoint_t a, hb_codepoint_t b) {
69     if ((b >> shift) - (a >> shift) >= mask_bits - 1)
70       mask = (mask_t) -1;
71     else {
72       mask_t ma = mask_for (a);
73       mask_t mb = mask_for (b);
74       mask |= mb + (mb - ma) - (mb < ma);
75     }
76   }
77
78   inline bool may_have (hb_codepoint_t g) const {
79     return !!(mask & mask_for (g));
80   }
81
82   private:
83
84   static inline mask_t mask_for (hb_codepoint_t g) {
85     return ((mask_t) 1) << ((g >> shift) & (mask_bits - 1));
86   }
87   mask_t mask;
88 };
89
90 template <typename head_t, typename tail_t>
91 struct hb_set_digest_combiner_t
92 {
93   ASSERT_POD ();
94
95   inline void init (void) {
96     head.init ();
97     tail.init ();
98   }
99
100   inline void add (hb_codepoint_t g) {
101     head.add (g);
102     tail.add (g);
103   }
104
105   inline void add_range (hb_codepoint_t a, hb_codepoint_t b) {
106     head.add_range (a, b);
107     tail.add_range (a, b);
108   }
109
110   inline bool may_have (hb_codepoint_t g) const {
111     return head.may_have (g) && tail.may_have (g);
112   }
113
114   private:
115   head_t head;
116   tail_t tail;
117 };
118
119
120 /*
121  * hb_set_digest_t
122  *
123  * This is a combination of digests that performs "best".
124  * There is not much science to this: it's a result of intuition
125  * and testing.
126  */
127 typedef hb_set_digest_combiner_t
128 <
129   hb_set_digest_lowest_bits_t<unsigned long, 4>,
130   hb_set_digest_combiner_t
131   <
132     hb_set_digest_lowest_bits_t<unsigned long, 0>,
133     hb_set_digest_lowest_bits_t<unsigned long, 9>
134   >
135 > hb_set_digest_t;
136
137
138
139 /*
140  * hb_set_t
141  */
142
143
144 /* TODO Make this faster and memmory efficient. */
145
146 struct hb_set_t
147 {
148   friend struct hb_frozen_set_t;
149
150   hb_object_header_t header;
151   ASSERT_POD ();
152   bool in_error;
153
154   inline void init (void) {
155     hb_object_init (this);
156     clear ();
157   }
158   inline void fini (void) {
159   }
160   inline void clear (void) {
161     if (unlikely (hb_object_is_inert (this)))
162       return;
163     in_error = false;
164     memset (elts, 0, sizeof elts);
165   }
166   inline bool is_empty (void) const {
167     for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++)
168       if (elts[i])
169         return false;
170     return true;
171   }
172   inline void add (hb_codepoint_t g)
173   {
174     if (unlikely (in_error)) return;
175     if (unlikely (g == INVALID)) return;
176     if (unlikely (g > MAX_G)) return;
177     elt (g) |= mask (g);
178   }
179   inline void add_range (hb_codepoint_t a, hb_codepoint_t b)
180   {
181     if (unlikely (in_error)) return;
182     /* TODO Speedup */
183     for (unsigned int i = a; i < b + 1; i++)
184       add (i);
185   }
186   inline void del (hb_codepoint_t g)
187   {
188     if (unlikely (in_error)) return;
189     if (unlikely (g > MAX_G)) return;
190     elt (g) &= ~mask (g);
191   }
192   inline void del_range (hb_codepoint_t a, hb_codepoint_t b)
193   {
194     if (unlikely (in_error)) return;
195     /* TODO Speedup */
196     for (unsigned int i = a; i < b + 1; i++)
197       del (i);
198   }
199   inline bool has (hb_codepoint_t g) const
200   {
201     if (unlikely (g > MAX_G)) return false;
202     return !!(elt (g) & mask (g));
203   }
204   inline bool intersects (hb_codepoint_t first,
205                           hb_codepoint_t last) const
206   {
207     if (unlikely (first > MAX_G)) return false;
208     if (unlikely (last  > MAX_G)) last = MAX_G;
209     unsigned int end = last + 1;
210     for (hb_codepoint_t i = first; i < end; i++)
211       if (has (i))
212         return true;
213     return false;
214   }
215   inline bool is_equal (const hb_set_t *other) const
216   {
217     for (unsigned int i = 0; i < ELTS; i++)
218       if (elts[i] != other->elts[i])
219         return false;
220     return true;
221   }
222   inline void set (const hb_set_t *other)
223   {
224     if (unlikely (in_error)) return;
225     for (unsigned int i = 0; i < ELTS; i++)
226       elts[i] = other->elts[i];
227   }
228   inline void union_ (const hb_set_t *other)
229   {
230     if (unlikely (in_error)) return;
231     for (unsigned int i = 0; i < ELTS; i++)
232       elts[i] |= other->elts[i];
233   }
234   inline void intersect (const hb_set_t *other)
235   {
236     if (unlikely (in_error)) return;
237     for (unsigned int i = 0; i < ELTS; i++)
238       elts[i] &= other->elts[i];
239   }
240   inline void subtract (const hb_set_t *other)
241   {
242     if (unlikely (in_error)) return;
243     for (unsigned int i = 0; i < ELTS; i++)
244       elts[i] &= ~other->elts[i];
245   }
246   inline void symmetric_difference (const hb_set_t *other)
247   {
248     if (unlikely (in_error)) return;
249     for (unsigned int i = 0; i < ELTS; i++)
250       elts[i] ^= other->elts[i];
251   }
252   inline void invert (void)
253   {
254     if (unlikely (in_error)) return;
255     for (unsigned int i = 0; i < ELTS; i++)
256       elts[i] = ~elts[i];
257   }
258   inline bool next (hb_codepoint_t *codepoint) const
259   {
260     if (unlikely (*codepoint == INVALID)) {
261       hb_codepoint_t i = get_min ();
262       if (i != INVALID) {
263         *codepoint = i;
264         return true;
265       } else {
266         *codepoint = INVALID;
267         return false;
268       }
269     }
270     for (hb_codepoint_t i = *codepoint + 1; i < MAX_G + 1; i++)
271       if (has (i)) {
272         *codepoint = i;
273         return true;
274       }
275     *codepoint = INVALID;
276     return false;
277   }
278   inline bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
279   {
280     hb_codepoint_t i;
281
282     i = *last;
283     if (!next (&i))
284     {
285       *last = *first = INVALID;
286       return false;
287     }
288
289     *last = *first = i;
290     while (next (&i) && i == *last + 1)
291       (*last)++;
292
293     return true;
294   }
295
296   inline unsigned int get_population (void) const
297   {
298     unsigned int count = 0;
299     for (unsigned int i = 0; i < ELTS; i++)
300       count += _hb_popcount32 (elts[i]);
301     return count;
302   }
303   inline hb_codepoint_t get_min (void) const
304   {
305     for (unsigned int i = 0; i < ELTS; i++)
306       if (elts[i])
307         for (unsigned int j = 0; j < BITS; j++)
308           if (elts[i] & (1 << j))
309             return i * BITS + j;
310     return INVALID;
311   }
312   inline hb_codepoint_t get_max (void) const
313   {
314     for (unsigned int i = ELTS; i; i--)
315       if (elts[i - 1])
316         for (unsigned int j = BITS; j; j--)
317           if (elts[i - 1] & (1 << (j - 1)))
318             return (i - 1) * BITS + (j - 1);
319     return INVALID;
320   }
321
322   typedef uint32_t elt_t;
323   static const unsigned int MAX_G = 65536 - 1; /* XXX Fix this... */
324   static const unsigned int SHIFT = 5;
325   static const unsigned int BITS = (1 << SHIFT);
326   static const unsigned int MASK = BITS - 1;
327   static const unsigned int ELTS = (MAX_G + 1 + (BITS - 1)) / BITS;
328   static  const hb_codepoint_t INVALID = HB_SET_VALUE_INVALID;
329
330   elt_t &elt (hb_codepoint_t g) { return elts[g >> SHIFT]; }
331   elt_t const &elt (hb_codepoint_t g) const { return elts[g >> SHIFT]; }
332   elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & MASK); }
333
334   elt_t elts[ELTS]; /* XXX 8kb */
335
336   ASSERT_STATIC (sizeof (elt_t) * 8 == BITS);
337   ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G);
338 };
339
340 struct hb_frozen_set_t
341 {
342   static const unsigned int SHIFT = hb_set_t::SHIFT;
343   static const unsigned int BITS = hb_set_t::BITS;
344   static const unsigned int MASK = hb_set_t::MASK;
345   typedef hb_set_t::elt_t elt_t;
346
347   inline void init (const hb_set_t &set)
348   {
349     start = count = 0;
350     elts = NULL;
351
352     unsigned int max = set.get_max ();
353     if (max == set.INVALID)
354       return;
355     unsigned int min = set.get_min ();
356     const elt_t &min_elt = set.elt (min);
357     const elt_t &max_elt = set.elt (max);
358
359     start = min & ~MASK;
360     count = max - start + 1;
361     unsigned int num_elts = (count + BITS - 1) / BITS;
362     unsigned int elts_size = num_elts * sizeof (elt_t);
363     elts = (elt_t *) malloc (elts_size);
364     if (unlikely (!elts))
365     {
366       start = count = 0;
367       return;
368     }
369     memcpy (elts, &min_elt, elts_size);
370   }
371
372   inline void fini (void)
373   {
374     if (elts)
375       free (elts);
376   }
377
378   inline bool has (hb_codepoint_t g) const
379   {
380     /* hb_codepoint_t is unsigned. */
381     g -= start;
382     if (unlikely (g > count)) return false;
383     return !!(elt (g) & mask (g));
384   }
385
386   elt_t const &elt (hb_codepoint_t g) const { return elts[g >> SHIFT]; }
387   elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & MASK); }
388
389   private:
390   hb_codepoint_t start, count;
391   elt_t *elts;
392 };
393
394
395 #endif /* HB_SET_PRIVATE_HH */