• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2018  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 #include "hb-map.hh"
28 
29 
30 /**
31  * SECTION:hb-map
32  * @title: hb-map
33  * @short_description: Object representing integer to integer mapping
34  * @include: hb.h
35  *
36  * Map objects are integer-to-integer hash-maps.  Currently they are
37  * not used in the HarfBuzz public API, but are provided for client's
38  * use if desired.
39  **/
40 
41 
42 /**
43  * hb_map_create:
44  *
45  * Creates a new, initially empty map.
46  *
47  * Return value: (transfer full): The new #hb_map_t
48  *
49  * Since: 1.7.7
50  **/
51 hb_map_t *
hb_map_create()52 hb_map_create ()
53 {
54   hb_map_t *map;
55 
56   if (!(map = hb_object_create<hb_map_t> ()))
57     return hb_map_get_empty ();
58 
59   return map;
60 }
61 
62 /**
63  * hb_map_get_empty:
64  *
65  * Fetches the singleton empty #hb_map_t.
66  *
67  * Return value: (transfer full): The empty #hb_map_t
68  *
69  * Since: 1.7.7
70  **/
71 hb_map_t *
hb_map_get_empty()72 hb_map_get_empty ()
73 {
74   return const_cast<hb_map_t *> (&Null (hb_map_t));
75 }
76 
77 /**
78  * hb_map_reference: (skip)
79  * @map: A map
80  *
81  * Increases the reference count on a map.
82  *
83  * Return value: (transfer full): The map
84  *
85  * Since: 1.7.7
86  **/
87 hb_map_t *
hb_map_reference(hb_map_t * map)88 hb_map_reference (hb_map_t *map)
89 {
90   return hb_object_reference (map);
91 }
92 
93 /**
94  * hb_map_destroy: (skip)
95  * @map: A map
96  *
97  * Decreases the reference count on a map. When
98  * the reference count reaches zero, the map is
99  * destroyed, freeing all memory.
100  *
101  * Since: 1.7.7
102  **/
103 void
hb_map_destroy(hb_map_t * map)104 hb_map_destroy (hb_map_t *map)
105 {
106   if (!hb_object_destroy (map)) return;
107 
108   hb_free (map);
109 }
110 
111 /**
112  * hb_map_set_user_data: (skip)
113  * @map: A map
114  * @key: The user-data key to set
115  * @data: A pointer to the user data to set
116  * @destroy: (nullable): A callback to call when @data is not needed anymore
117  * @replace: Whether to replace an existing data with the same key
118  *
119  * Attaches a user-data key/data pair to the specified map.
120  *
121  * Return value: `true` if success, `false` otherwise
122  *
123  * Since: 1.7.7
124  **/
125 hb_bool_t
hb_map_set_user_data(hb_map_t * map,hb_user_data_key_t * key,void * data,hb_destroy_func_t destroy,hb_bool_t replace)126 hb_map_set_user_data (hb_map_t           *map,
127 		      hb_user_data_key_t *key,
128 		      void *              data,
129 		      hb_destroy_func_t   destroy,
130 		      hb_bool_t           replace)
131 {
132   return hb_object_set_user_data (map, key, data, destroy, replace);
133 }
134 
135 /**
136  * hb_map_get_user_data: (skip)
137  * @map: A map
138  * @key: The user-data key to query
139  *
140  * Fetches the user data associated with the specified key,
141  * attached to the specified map.
142  *
143  * Return value: (transfer none): A pointer to the user data
144  *
145  * Since: 1.7.7
146  **/
147 void *
hb_map_get_user_data(const hb_map_t * map,hb_user_data_key_t * key)148 hb_map_get_user_data (const hb_map_t     *map,
149 		      hb_user_data_key_t *key)
150 {
151   return hb_object_get_user_data (map, key);
152 }
153 
154 
155 /**
156  * hb_map_allocation_successful:
157  * @map: A map
158  *
159  * Tests whether memory allocation for a set was successful.
160  *
161  * Return value: `true` if allocation succeeded, `false` otherwise
162  *
163  * Since: 1.7.7
164  **/
165 hb_bool_t
hb_map_allocation_successful(const hb_map_t * map)166 hb_map_allocation_successful (const hb_map_t  *map)
167 {
168   return map->successful;
169 }
170 
171 /**
172  * hb_map_copy:
173  * @map: A map
174  *
175  * Allocate a copy of @map.
176  *
177  * Return value: Newly-allocated map.
178  *
179  * Since: 4.4.0
180  **/
181 hb_map_t *
hb_map_copy(const hb_map_t * map)182 hb_map_copy (const hb_map_t *map)
183 {
184   hb_map_t *copy = hb_map_create ();
185   if (unlikely (!copy)) return nullptr;
186   copy->resize (map->population);
187   hb_copy (*map, *copy);
188   return copy;
189 }
190 
191 /**
192  * hb_map_set:
193  * @map: A map
194  * @key: The key to store in the map
195  * @value: The value to store for @key
196  *
197  * Stores @key:@value in the map.
198  *
199  * Since: 1.7.7
200  **/
201 void
hb_map_set(hb_map_t * map,hb_codepoint_t key,hb_codepoint_t value)202 hb_map_set (hb_map_t       *map,
203 	    hb_codepoint_t  key,
204 	    hb_codepoint_t  value)
205 {
206   /* Immutable-safe. */
207   map->set (key, value);
208 }
209 
210 /**
211  * hb_map_get:
212  * @map: A map
213  * @key: The key to query
214  *
215  * Fetches the value stored for @key in @map.
216  *
217  * Since: 1.7.7
218  **/
219 hb_codepoint_t
hb_map_get(const hb_map_t * map,hb_codepoint_t key)220 hb_map_get (const hb_map_t *map,
221 	    hb_codepoint_t  key)
222 {
223   return map->get (key);
224 }
225 
226 /**
227  * hb_map_del:
228  * @map: A map
229  * @key: The key to delete
230  *
231  * Removes @key and its stored value from @map.
232  *
233  * Since: 1.7.7
234  **/
235 void
hb_map_del(hb_map_t * map,hb_codepoint_t key)236 hb_map_del (hb_map_t       *map,
237 	    hb_codepoint_t  key)
238 {
239   /* Immutable-safe. */
240   map->del (key);
241 }
242 
243 /**
244  * hb_map_has:
245  * @map: A map
246  * @key: The key to query
247  *
248  * Tests whether @key is an element of @map.
249  *
250  * Return value: `true` if @key is found in @map, `false` otherwise
251  *
252  * Since: 1.7.7
253  **/
254 hb_bool_t
hb_map_has(const hb_map_t * map,hb_codepoint_t key)255 hb_map_has (const hb_map_t *map,
256 	    hb_codepoint_t  key)
257 {
258   return map->has (key);
259 }
260 
261 
262 /**
263  * hb_map_clear:
264  * @map: A map
265  *
266  * Clears out the contents of @map.
267  *
268  * Since: 1.7.7
269  **/
270 void
hb_map_clear(hb_map_t * map)271 hb_map_clear (hb_map_t *map)
272 {
273   return map->clear ();
274 }
275 
276 /**
277  * hb_map_is_empty:
278  * @map: A map
279  *
280  * Tests whether @map is empty (contains no elements).
281  *
282  * Return value: `true` if @map is empty
283  *
284  * Since: 1.7.7
285  **/
286 hb_bool_t
hb_map_is_empty(const hb_map_t * map)287 hb_map_is_empty (const hb_map_t *map)
288 {
289   return map->is_empty ();
290 }
291 
292 /**
293  * hb_map_get_population:
294  * @map: A map
295  *
296  * Returns the number of key-value pairs in the map.
297  *
298  * Return value: The population of @map
299  *
300  * Since: 1.7.7
301  **/
302 unsigned int
hb_map_get_population(const hb_map_t * map)303 hb_map_get_population (const hb_map_t *map)
304 {
305   return map->get_population ();
306 }
307 
308 /**
309  * hb_map_is_equal:
310  * @map: A map
311  * @other: Another map
312  *
313  * Tests whether @map and @other are equal (contain the same
314  * elements).
315  *
316  * Return value: `true` if the two maps are equal, `false` otherwise.
317  *
318  * Since: 4.3.0
319  **/
320 hb_bool_t
hb_map_is_equal(const hb_map_t * map,const hb_map_t * other)321 hb_map_is_equal (const hb_map_t *map,
322 		 const hb_map_t *other)
323 {
324   return map->is_equal (*other);
325 }
326 
327 /**
328  * hb_map_hash:
329  * @map: A map
330  *
331  * Creates a hash representing @map.
332  *
333  * Return value:
334  * A hash of @map.
335  *
336  * Since: 4.4.0
337  **/
338 HB_EXTERN unsigned int
hb_map_hash(const hb_map_t * map)339 hb_map_hash (const hb_map_t *map)
340 {
341   return map->hash ();
342 }
343 
344