2 * Copyright © 2018 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_MAP_PRIVATE_HH
28 #define HB_MAP_PRIVATE_HH
30 #include "hb-private.hh"
31 #include "hb-object-private.hh"
35 inline uint32_t Hash (const T &v)
37 /* Knuth's multiplicative method: */
38 return (uint32_t) v * 2654435761u;
53 inline bool is_unused (void) const { return key == INVALID; }
54 inline bool is_tombstone (void) const { return key != INVALID && value == INVALID; }
57 hb_object_header_t header;
58 bool successful; /* Allocations successful */
59 unsigned int population; /* Not including tombstones. */
60 unsigned int occupancy; /* Including tombstones. */
65 inline void init_shallow (void)
68 population = occupancy = 0;
73 inline void init (void)
75 hb_object_init (this);
78 inline void fini_shallow (void)
82 inline void fini (void)
84 hb_object_fini (this);
88 inline bool resize (void)
90 if (unlikely (!successful)) return false;
92 unsigned int power = _hb_bit_storage (population * 2 + 8);
93 unsigned int new_size = 1u << power;
94 item_t *new_items = (item_t *) malloc ((size_t) new_size * sizeof (item_t));
95 if (unlikely (!new_items))
100 memset (new_items, 0xFF, (size_t) new_size * sizeof (item_t));
102 unsigned int old_size = mask + 1;
103 item_t *old_items = items;
105 /* Switch to new, empty, array. */
106 population = occupancy = 0;
108 prime = prime_for (power);
111 /* Insert back old items. */
113 for (unsigned int i = 0; i < old_size; i++)
114 if (old_items[i].key != INVALID && old_items[i].value != INVALID)
115 set (old_items[i].key, old_items[i].value);
122 inline void set (hb_codepoint_t key, hb_codepoint_t value)
124 if (unlikely (!successful)) return;
125 if (unlikely (key == INVALID)) return;
126 if ((occupancy + occupancy / 2) >= mask && !resize ()) return;
127 unsigned int i = bucket_for (key);
129 if (value == INVALID && items[i].key != key)
130 return; /* Trying to delete non-existent key. */
132 if (!items[i].is_unused ())
135 if (items[i].is_tombstone ())
140 items[i].value = value;
143 if (!items[i].is_tombstone ())
147 inline hb_codepoint_t get (hb_codepoint_t key) const
149 if (unlikely (!items)) return INVALID;
150 unsigned int i = bucket_for (key);
151 return items[i].key == key ? items[i].value : INVALID;
154 inline void del (hb_codepoint_t key)
158 inline bool has (hb_codepoint_t key) const
160 return get (key) != INVALID;
163 inline hb_codepoint_t operator [] (unsigned int key) const
164 { return get (key); }
166 static const hb_codepoint_t INVALID = HB_MAP_VALUE_INVALID;
168 inline void clear (void)
170 memset (items, 0xFF, ((size_t) mask + 1) * sizeof (item_t));
171 population = occupancy = 0;
174 inline bool is_empty (void) const
176 return population != 0;
179 inline unsigned int get_population () const
186 inline unsigned int bucket_for (hb_codepoint_t key) const
188 unsigned int i = Hash (key) % prime;
189 unsigned int step = 0;
190 unsigned int tombstone = INVALID;
191 while (!items[i].is_unused ())
193 if (items[i].key == key)
195 if (tombstone == INVALID && items[i].is_tombstone ())
197 i = (i + ++step) & mask;
199 return tombstone == INVALID ? i : tombstone;
202 static inline unsigned int prime_for (unsigned int shift)
204 /* Following comment and table copied from glib. */
205 /* Each table size has an associated prime modulo (the first prime
206 * lower than the table size) used to find the initial bucket. Probing
207 * then works modulo 2^n. The prime modulo is necessary to get a
208 * good distribution with poor hash functions.
210 /* Not declaring static to make all kinds of compilers happy... */
211 /*static*/ const unsigned int prime_mod [32] =
229 65521, /* For 1 << 16 */
244 2147483647 /* For 1 << 31 */
247 if (unlikely (shift >= ARRAY_LENGTH (prime_mod)))
248 return prime_mod[ARRAY_LENGTH (prime_mod) - 1];
250 return prime_mod[shift];
255 #endif /* HB_MAP_PRIVATE_HH */