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
34 inline uint32_t Hash (const T &v)
36 /* Knuth's multiplicative method: */
37 return (uint32_t) v * 2654435761u;
47 HB_NO_COPY_ASSIGN (hb_map_t);
48 hb_map_t () { init (); }
49 ~hb_map_t () { fini (); }
56 bool is_unused () const { return key == INVALID; }
57 bool is_tombstone () const { return key != INVALID && value == INVALID; }
60 hb_object_header_t header;
61 bool successful; /* Allocations successful */
62 unsigned int population; /* Not including tombstones. */
63 unsigned int occupancy; /* Including tombstones. */
71 population = occupancy = 0;
78 hb_object_init (this);
88 population = occupancy = 0;
89 hb_object_fini (this);
93 bool in_error () const { return !successful; }
97 if (unlikely (!successful)) return false;
99 unsigned int power = hb_bit_storage (population * 2 + 8);
100 unsigned int new_size = 1u << power;
101 item_t *new_items = (item_t *) malloc ((size_t) new_size * sizeof (item_t));
102 if (unlikely (!new_items))
107 memset (new_items, 0xFF, (size_t) new_size * sizeof (item_t));
109 unsigned int old_size = mask + 1;
110 item_t *old_items = items;
112 /* Switch to new, empty, array. */
113 population = occupancy = 0;
115 prime = prime_for (power);
118 /* Insert back old items. */
120 for (unsigned int i = 0; i < old_size; i++)
121 if (old_items[i].key != INVALID && old_items[i].value != INVALID)
122 set (old_items[i].key, old_items[i].value);
129 void set (hb_codepoint_t key, hb_codepoint_t value)
131 if (unlikely (!successful)) return;
132 if (unlikely (key == INVALID)) return;
133 if ((occupancy + occupancy / 2) >= mask && !resize ()) return;
134 unsigned int i = bucket_for (key);
136 if (value == INVALID && items[i].key != key)
137 return; /* Trying to delete non-existent key. */
139 if (!items[i].is_unused ())
142 if (items[i].is_tombstone ())
147 items[i].value = value;
150 if (!items[i].is_tombstone ())
154 hb_codepoint_t get (hb_codepoint_t key) const
156 if (unlikely (!items)) return INVALID;
157 unsigned int i = bucket_for (key);
158 return items[i].key == key ? items[i].value : INVALID;
161 void del (hb_codepoint_t key) { set (key, INVALID); }
163 bool has (hb_codepoint_t key) const
164 { return get (key) != INVALID; }
166 hb_codepoint_t operator [] (unsigned int key) const
167 { return get (key); }
169 static constexpr hb_codepoint_t INVALID = HB_MAP_VALUE_INVALID;
173 memset (items, 0xFF, ((size_t) mask + 1) * sizeof (item_t));
174 population = occupancy = 0;
177 bool is_empty () const { return population == 0; }
179 unsigned int get_population () const { return population; }
183 unsigned int bucket_for (hb_codepoint_t key) const
185 unsigned int i = Hash (key) % prime;
186 unsigned int step = 0;
187 unsigned int tombstone = INVALID;
188 while (!items[i].is_unused ())
190 if (items[i].key == key)
192 if (tombstone == INVALID && items[i].is_tombstone ())
194 i = (i + ++step) & mask;
196 return tombstone == INVALID ? i : tombstone;
199 static unsigned int prime_for (unsigned int shift)
201 /* Following comment and table copied from glib. */
202 /* Each table size has an associated prime modulo (the first prime
203 * lower than the table size) used to find the initial bucket. Probing
204 * then works modulo 2^n. The prime modulo is necessary to get a
205 * good distribution with poor hash functions.
207 /* Not declaring static to make all kinds of compilers happy... */
208 /*static*/ const unsigned int prime_mod [32] =
226 65521, /* For 1 << 16 */
241 2147483647 /* For 1 << 31 */
244 if (unlikely (shift >= ARRAY_LENGTH (prime_mod)))
245 return prime_mod[ARRAY_LENGTH (prime_mod) - 1];
247 return prime_mod[shift];
252 #endif /* HB_MAP_HH */