Upgrade to latest harfbuzz
[framework/uifw/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-set.h"
32 #include "hb-object-private.hh"
33
34
35 /* TODO Make this faster and memmory efficient. */
36
37 struct _hb_set_t
38 {
39   hb_object_header_t header;
40   ASSERT_POD ();
41
42   inline void init (void) {
43     header.init ();
44     clear ();
45   }
46   inline void fini (void) {
47   }
48   inline void clear (void) {
49     memset (elts, 0, sizeof elts);
50   }
51   inline bool empty (void) const {
52     for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++)
53       if (elts[i])
54         return false;
55     return true;
56   }
57   inline void add (hb_codepoint_t g)
58   {
59     if (unlikely (g == SENTINEL)) return;
60     if (unlikely (g > MAX_G)) return;
61     elt (g) |= mask (g);
62   }
63   inline void del (hb_codepoint_t g)
64   {
65     if (unlikely (g > MAX_G)) return;
66     elt (g) &= ~mask (g);
67   }
68   inline bool has (hb_codepoint_t g) const
69   {
70     if (unlikely (g > MAX_G)) return false;
71     return !!(elt (g) & mask (g));
72   }
73   inline bool intersects (hb_codepoint_t first,
74                           hb_codepoint_t last) const
75   {
76     if (unlikely (first > MAX_G)) return false;
77     if (unlikely (last  > MAX_G)) last = MAX_G;
78     unsigned int end = last + 1;
79     for (hb_codepoint_t i = first; i < end; i++)
80       if (has (i))
81         return true;
82     return false;
83   }
84   inline bool equal (const hb_set_t *other) const
85   {
86     for (unsigned int i = 0; i < ELTS; i++)
87       if (elts[i] != other->elts[i])
88         return false;
89     return true;
90   }
91   inline void set (const hb_set_t *other)
92   {
93     for (unsigned int i = 0; i < ELTS; i++)
94       elts[i] = other->elts[i];
95   }
96   inline void union_ (const hb_set_t *other)
97   {
98     for (unsigned int i = 0; i < ELTS; i++)
99       elts[i] |= other->elts[i];
100   }
101   inline void intersect (const hb_set_t *other)
102   {
103     for (unsigned int i = 0; i < ELTS; i++)
104       elts[i] &= other->elts[i];
105   }
106   inline void subtract (const hb_set_t *other)
107   {
108     for (unsigned int i = 0; i < ELTS; i++)
109       elts[i] &= ~other->elts[i];
110   }
111   inline void symmetric_difference (const hb_set_t *other)
112   {
113     for (unsigned int i = 0; i < ELTS; i++)
114       elts[i] ^= other->elts[i];
115   }
116   inline bool next (hb_codepoint_t *codepoint)
117   {
118     if (unlikely (*codepoint == SENTINEL)) {
119       hb_codepoint_t i = get_min ();
120       if (i != SENTINEL) {
121         *codepoint = i;
122         return true;
123       } else
124         return false;
125     }
126     for (hb_codepoint_t i = *codepoint + 1; i < MAX_G + 1; i++)
127       if (has (i)) {
128         *codepoint = i;
129         return true;
130       }
131     return false;
132   }
133   inline hb_codepoint_t get_min (void) const
134   {
135     for (unsigned int i = 0; i < ELTS; i++)
136       if (elts[i])
137         for (unsigned int j = 0; i < BITS; j++)
138           if (elts[i] & (1 << j))
139             return i * BITS + j;
140     return SENTINEL;
141   }
142   inline hb_codepoint_t get_max (void) const
143   {
144     for (unsigned int i = ELTS; i; i--)
145       if (elts[i - 1])
146         for (unsigned int j = BITS; j; j--)
147           if (elts[i - 1] & (1 << (j - 1)))
148             return (i - 1) * BITS + (j - 1);
149     return SENTINEL;
150   }
151
152   typedef uint32_t elt_t;
153   static const unsigned int MAX_G = 65536 - 1; /* XXX Fix this... */
154   static const unsigned int SHIFT = 5;
155   static const unsigned int BITS = (1 << SHIFT);
156   static const unsigned int MASK = BITS - 1;
157   static const unsigned int ELTS = (MAX_G + 1 + (BITS - 1)) / BITS;
158   static  const hb_codepoint_t SENTINEL = (hb_codepoint_t) -1;
159
160   elt_t &elt (hb_codepoint_t g) { return elts[g >> SHIFT]; }
161   elt_t elt (hb_codepoint_t g) const { return elts[g >> SHIFT]; }
162   elt_t mask (hb_codepoint_t g) const { return elt_t (1) << (g & MASK); }
163
164   elt_t elts[ELTS]; /* XXX 8kb */
165
166   ASSERT_STATIC (sizeof (elt_t) * 8 == BITS);
167   ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G);
168 };
169
170
171
172 #endif /* HB_SET_PRIVATE_HH */