2 * Copyright © 2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_SET_PRIVATE_HH
28 #define HB_SET_PRIVATE_HH
30 #include "hb-private.hh"
32 #include "hb-object-private.hh"
35 /* TODO Make this faster and memmory efficient. */
39 hb_object_header_t header;
42 inline void init (void) {
46 inline void fini (void) {
48 inline void clear (void) {
49 memset (elts, 0, sizeof elts);
51 inline bool empty (void) const {
52 for (unsigned int i = 0; i < ARRAY_LENGTH (elts); i++)
57 inline void add (hb_codepoint_t g)
59 if (unlikely (g == SENTINEL)) return;
60 if (unlikely (g > MAX_G)) return;
63 inline void del (hb_codepoint_t g)
65 if (unlikely (g > MAX_G)) return;
68 inline bool has (hb_codepoint_t g) const
70 if (unlikely (g > MAX_G)) return false;
71 return !!(elt (g) & mask (g));
73 inline bool intersects (hb_codepoint_t first,
74 hb_codepoint_t last) const
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++)
84 inline bool equal (const hb_set_t *other) const
86 for (unsigned int i = 0; i < ELTS; i++)
87 if (elts[i] != other->elts[i])
91 inline void set (const hb_set_t *other)
93 for (unsigned int i = 0; i < ELTS; i++)
94 elts[i] = other->elts[i];
96 inline void union_ (const hb_set_t *other)
98 for (unsigned int i = 0; i < ELTS; i++)
99 elts[i] |= other->elts[i];
101 inline void intersect (const hb_set_t *other)
103 for (unsigned int i = 0; i < ELTS; i++)
104 elts[i] &= other->elts[i];
106 inline void subtract (const hb_set_t *other)
108 for (unsigned int i = 0; i < ELTS; i++)
109 elts[i] &= ~other->elts[i];
111 inline void symmetric_difference (const hb_set_t *other)
113 for (unsigned int i = 0; i < ELTS; i++)
114 elts[i] ^= other->elts[i];
116 inline bool next (hb_codepoint_t *codepoint)
118 if (unlikely (*codepoint == SENTINEL)) {
119 hb_codepoint_t i = get_min ();
126 for (hb_codepoint_t i = *codepoint + 1; i < MAX_G + 1; i++)
133 inline hb_codepoint_t get_min (void) const
135 for (unsigned int i = 0; i < ELTS; i++)
137 for (unsigned int j = 0; i < BITS; j++)
138 if (elts[i] & (1 << j))
142 inline hb_codepoint_t get_max (void) const
144 for (unsigned int i = ELTS; i; i--)
146 for (unsigned int j = BITS; j; j--)
147 if (elts[i - 1] & (1 << (j - 1)))
148 return (i - 1) * BITS + (j - 1);
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;
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); }
164 elt_t elts[ELTS]; /* XXX 8kb */
166 ASSERT_STATIC (sizeof (elt_t) * 8 == BITS);
167 ASSERT_STATIC (sizeof (elt_t) * 8 * ELTS > MAX_G);
172 #endif /* HB_SET_PRIVATE_HH */