• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hb-set.hh"
28 
29 
30 /**
31  * SECTION:hb-set
32  * @title: hb-set
33  * @short_description: Objects representing a set of integers
34  * @include: hb.h
35  *
36  * Set objects represent a mathematical set of integer values.  They are
37  * used in non-shaping APIs to query certain sets of characters or glyphs,
38  * or other integer values.
39  **/
40 
41 
42 /**
43  * hb_set_create:
44  *
45  * Creates a new, initially empty set.
46  *
47  * Return value: (transfer full): The new #hb_set_t
48  *
49  * Since: 0.9.2
50  **/
51 hb_set_t *
hb_set_create()52 hb_set_create ()
53 {
54   hb_set_t *set;
55 
56   if (!(set = hb_object_create<hb_set_t> ()))
57     return hb_set_get_empty ();
58 
59   return set;
60 }
61 
62 /**
63  * hb_set_get_empty:
64  *
65  * Fetches the singleton empty #hb_set_t.
66  *
67  * Return value: (transfer full): The empty #hb_set_t
68  *
69  * Since: 0.9.2
70  **/
71 hb_set_t *
hb_set_get_empty()72 hb_set_get_empty ()
73 {
74   return const_cast<hb_set_t *> (&Null (hb_set_t));
75 }
76 
77 /**
78  * hb_set_reference: (skip)
79  * @set: A set
80  *
81  * Increases the reference count on a set.
82  *
83  * Return value: (transfer full): The set
84  *
85  * Since: 0.9.2
86  **/
87 hb_set_t *
hb_set_reference(hb_set_t * set)88 hb_set_reference (hb_set_t *set)
89 {
90   return hb_object_reference (set);
91 }
92 
93 /**
94  * hb_set_destroy: (skip)
95  * @set: A set
96  *
97  * Decreases the reference count on a set. When
98  * the reference count reaches zero, the set is
99  * destroyed, freeing all memory.
100  *
101  * Since: 0.9.2
102  **/
103 void
hb_set_destroy(hb_set_t * set)104 hb_set_destroy (hb_set_t *set)
105 {
106   if (!hb_object_destroy (set)) return;
107 
108   hb_free (set);
109 }
110 
111 /**
112  * hb_set_set_user_data: (skip)
113  * @set: A set
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 set.
120  *
121  * Return value: `true` if success, `false` otherwise
122  *
123  * Since: 0.9.2
124  **/
125 hb_bool_t
hb_set_set_user_data(hb_set_t * set,hb_user_data_key_t * key,void * data,hb_destroy_func_t destroy,hb_bool_t replace)126 hb_set_set_user_data (hb_set_t           *set,
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 (set, key, data, destroy, replace);
133 }
134 
135 /**
136  * hb_set_get_user_data: (skip)
137  * @set: A set
138  * @key: The user-data key to query
139  *
140  * Fetches the user data associated with the specified key,
141  * attached to the specified set.
142  *
143  * Return value: (transfer none): A pointer to the user data
144  *
145  * Since: 0.9.2
146  **/
147 void *
hb_set_get_user_data(const hb_set_t * set,hb_user_data_key_t * key)148 hb_set_get_user_data (const hb_set_t     *set,
149 		      hb_user_data_key_t *key)
150 {
151   return hb_object_get_user_data (set, key);
152 }
153 
154 
155 /**
156  * hb_set_allocation_successful:
157  * @set: A set
158  *
159  * Tests whether memory allocation for a set was successful.
160  *
161  * Return value: `true` if allocation succeeded, `false` otherwise
162  *
163  * Since: 0.9.2
164  **/
165 hb_bool_t
hb_set_allocation_successful(const hb_set_t * set)166 hb_set_allocation_successful (const hb_set_t  *set)
167 {
168   return !set->in_error ();
169 }
170 
171 /**
172  * hb_set_copy:
173  * @set: A set
174  *
175  * Allocate a copy of @set.
176  *
177  * Return value: Newly-allocated set.
178  *
179  * Since: 2.8.2
180  **/
181 hb_set_t *
hb_set_copy(const hb_set_t * set)182 hb_set_copy (const hb_set_t *set)
183 {
184   hb_set_t *copy = hb_set_create ();
185   if (unlikely (!copy)) return nullptr;
186   copy->set (*set);
187   return copy;
188 }
189 
190 /**
191  * hb_set_clear:
192  * @set: A set
193  *
194  * Clears out the contents of a set.
195  *
196  * Since: 0.9.2
197  **/
198 void
hb_set_clear(hb_set_t * set)199 hb_set_clear (hb_set_t *set)
200 {
201   /* Immutible-safe. */
202   set->clear ();
203 }
204 
205 /**
206  * hb_set_is_empty:
207  * @set: a set.
208  *
209  * Tests whether a set is empty (contains no elements).
210  *
211  * Return value: `true` if @set is empty
212  *
213  * Since: 0.9.7
214  **/
215 hb_bool_t
hb_set_is_empty(const hb_set_t * set)216 hb_set_is_empty (const hb_set_t *set)
217 {
218   return set->is_empty ();
219 }
220 
221 /**
222  * hb_set_has:
223  * @set: A set
224  * @codepoint: The element to query
225  *
226  * Tests whether @codepoint belongs to @set.
227  *
228  * Return value: `true` if @codepoint is in @set, `false` otherwise
229  *
230  * Since: 0.9.2
231  **/
232 hb_bool_t
hb_set_has(const hb_set_t * set,hb_codepoint_t codepoint)233 hb_set_has (const hb_set_t *set,
234 	    hb_codepoint_t  codepoint)
235 {
236   return set->has (codepoint);
237 }
238 
239 /**
240  * hb_set_add:
241  * @set: A set
242  * @codepoint: The element to add to @set
243  *
244  * Adds @codepoint to @set.
245  *
246  * Since: 0.9.2
247  **/
248 void
hb_set_add(hb_set_t * set,hb_codepoint_t codepoint)249 hb_set_add (hb_set_t       *set,
250 	    hb_codepoint_t  codepoint)
251 {
252   /* Immutible-safe. */
253   set->add (codepoint);
254 }
255 
256 /**
257  * hb_set_add_sorted_array:
258  * @set: A set
259  * @sorted_codepoints: (array length=num_codepoints): Array of codepoints to add
260  * @num_codepoints: Length of @sorted_codepoints
261  *
262  * Adds @num_codepoints codepoints to a set at once.
263  * The codepoints array must be in increasing order,
264  * with size at least @num_codepoints.
265  *
266  * Since: 4.1.0
267  */
268 HB_EXTERN void
hb_set_add_sorted_array(hb_set_t * set,const hb_codepoint_t * sorted_codepoints,unsigned int num_codepoints)269 hb_set_add_sorted_array (hb_set_t             *set,
270 		         const hb_codepoint_t *sorted_codepoints,
271 		         unsigned int          num_codepoints)
272 {
273   /* Immutible-safe. */
274   set->add_sorted_array (sorted_codepoints,
275 		         num_codepoints,
276 		         sizeof(hb_codepoint_t));
277 }
278 
279 /**
280  * hb_set_add_range:
281  * @set: A set
282  * @first: The first element to add to @set
283  * @last: The final element to add to @set
284  *
285  * Adds all of the elements from @first to @last
286  * (inclusive) to @set.
287  *
288  * Since: 0.9.7
289  **/
290 void
hb_set_add_range(hb_set_t * set,hb_codepoint_t first,hb_codepoint_t last)291 hb_set_add_range (hb_set_t       *set,
292 		  hb_codepoint_t  first,
293 		  hb_codepoint_t  last)
294 {
295   /* Immutible-safe. */
296   set->add_range (first, last);
297 }
298 
299 /**
300  * hb_set_del:
301  * @set: A set
302  * @codepoint: Removes @codepoint from @set
303  *
304  * Removes @codepoint from @set.
305  *
306  * Since: 0.9.2
307  **/
308 void
hb_set_del(hb_set_t * set,hb_codepoint_t codepoint)309 hb_set_del (hb_set_t       *set,
310 	    hb_codepoint_t  codepoint)
311 {
312   /* Immutible-safe. */
313   set->del (codepoint);
314 }
315 
316 /**
317  * hb_set_del_range:
318  * @set: A set
319  * @first: The first element to remove from @set
320  * @last: The final element to remove from @set
321  *
322  * Removes all of the elements from @first to @last
323  * (inclusive) from @set.
324  *
325  * If @last is #HB_SET_VALUE_INVALID, then all values
326  * greater than or equal to @first are removed.
327  *
328  * Since: 0.9.7
329  **/
330 void
hb_set_del_range(hb_set_t * set,hb_codepoint_t first,hb_codepoint_t last)331 hb_set_del_range (hb_set_t       *set,
332 		  hb_codepoint_t  first,
333 		  hb_codepoint_t  last)
334 {
335   /* Immutible-safe. */
336   set->del_range (first, last);
337 }
338 
339 /**
340  * hb_set_is_equal:
341  * @set: A set
342  * @other: Another set
343  *
344  * Tests whether @set and @other are equal (contain the same
345  * elements).
346  *
347  * Return value: `true` if the two sets are equal, `false` otherwise.
348  *
349  * Since: 0.9.7
350  **/
351 hb_bool_t
hb_set_is_equal(const hb_set_t * set,const hb_set_t * other)352 hb_set_is_equal (const hb_set_t *set,
353 		 const hb_set_t *other)
354 {
355   return set->is_equal (*other);
356 }
357 
358 /**
359  * hb_set_hash:
360  * @set: A set
361  *
362  * Creates a hash representing @set.
363  *
364  * Return value:
365  * A hash of @set.
366  *
367  * Since: 4.4.0
368  **/
369 HB_EXTERN unsigned int
hb_set_hash(const hb_set_t * set)370 hb_set_hash (const hb_set_t *set)
371 {
372   return set->hash ();
373 }
374 
375 /**
376  * hb_set_is_subset:
377  * @set: A set
378  * @larger_set: Another set
379  *
380  * Tests whether @set is a subset of @larger_set.
381  *
382  * Return value: `true` if the @set is a subset of (or equal to) @larger_set, `false` otherwise.
383  *
384  * Since: 1.8.1
385  **/
386 hb_bool_t
hb_set_is_subset(const hb_set_t * set,const hb_set_t * larger_set)387 hb_set_is_subset (const hb_set_t *set,
388 		  const hb_set_t *larger_set)
389 {
390   return set->is_subset (*larger_set);
391 }
392 
393 /**
394  * hb_set_set:
395  * @set: A set
396  * @other: Another set
397  *
398  * Makes the contents of @set equal to the contents of @other.
399  *
400  * Since: 0.9.2
401  **/
402 void
hb_set_set(hb_set_t * set,const hb_set_t * other)403 hb_set_set (hb_set_t       *set,
404 	    const hb_set_t *other)
405 {
406   /* Immutible-safe. */
407   set->set (*other);
408 }
409 
410 /**
411  * hb_set_union:
412  * @set: A set
413  * @other: Another set
414  *
415  * Makes @set the union of @set and @other.
416  *
417  * Since: 0.9.2
418  **/
419 void
hb_set_union(hb_set_t * set,const hb_set_t * other)420 hb_set_union (hb_set_t       *set,
421 	      const hb_set_t *other)
422 {
423   /* Immutible-safe. */
424   set->union_ (*other);
425 }
426 
427 /**
428  * hb_set_intersect:
429  * @set: A set
430  * @other: Another set
431  *
432  * Makes @set the intersection of @set and @other.
433  *
434  * Since: 0.9.2
435  **/
436 void
hb_set_intersect(hb_set_t * set,const hb_set_t * other)437 hb_set_intersect (hb_set_t       *set,
438 		  const hb_set_t *other)
439 {
440   /* Immutible-safe. */
441   set->intersect (*other);
442 }
443 
444 /**
445  * hb_set_subtract:
446  * @set: A set
447  * @other: Another set
448  *
449  * Subtracts the contents of @other from @set.
450  *
451  * Since: 0.9.2
452  **/
453 void
hb_set_subtract(hb_set_t * set,const hb_set_t * other)454 hb_set_subtract (hb_set_t       *set,
455 		 const hb_set_t *other)
456 {
457   /* Immutible-safe. */
458   set->subtract (*other);
459 }
460 
461 /**
462  * hb_set_symmetric_difference:
463  * @set: A set
464  * @other: Another set
465  *
466  * Makes @set the symmetric difference of @set
467  * and @other.
468  *
469  * Since: 0.9.2
470  **/
471 void
hb_set_symmetric_difference(hb_set_t * set,const hb_set_t * other)472 hb_set_symmetric_difference (hb_set_t       *set,
473 			     const hb_set_t *other)
474 {
475   /* Immutible-safe. */
476   set->symmetric_difference (*other);
477 }
478 
479 /**
480  * hb_set_invert:
481  * @set: A set
482  *
483  * Inverts the contents of @set.
484  *
485  * Since: 3.0.0
486  **/
487 void
hb_set_invert(hb_set_t * set)488 hb_set_invert (hb_set_t *set)
489 {
490   /* Immutible-safe. */
491   set->invert ();
492 }
493 
494 /**
495  * hb_set_get_population:
496  * @set: A set
497  *
498  * Returns the number of elements in the set.
499  *
500  * Return value: The population of @set
501  *
502  * Since: 0.9.7
503  **/
504 unsigned int
hb_set_get_population(const hb_set_t * set)505 hb_set_get_population (const hb_set_t *set)
506 {
507   return set->get_population ();
508 }
509 
510 /**
511  * hb_set_get_min:
512  * @set: A set
513  *
514  * Finds the smallest element in the set.
515  *
516  * Return value: minimum of @set, or #HB_SET_VALUE_INVALID if @set is empty.
517  *
518  * Since: 0.9.7
519  **/
520 hb_codepoint_t
hb_set_get_min(const hb_set_t * set)521 hb_set_get_min (const hb_set_t *set)
522 {
523   return set->get_min ();
524 }
525 
526 /**
527  * hb_set_get_max:
528  * @set: A set
529  *
530  * Finds the largest element in the set.
531  *
532  * Return value: maximum of @set, or #HB_SET_VALUE_INVALID if @set is empty.
533  *
534  * Since: 0.9.7
535  **/
536 hb_codepoint_t
hb_set_get_max(const hb_set_t * set)537 hb_set_get_max (const hb_set_t *set)
538 {
539   return set->get_max ();
540 }
541 
542 /**
543  * hb_set_next:
544  * @set: A set
545  * @codepoint: (inout): Input = Code point to query
546  *             Output = Code point retrieved
547  *
548  * Fetches the next element in @set that is greater than current value of @codepoint.
549  *
550  * Set @codepoint to #HB_SET_VALUE_INVALID to get started.
551  *
552  * Return value: `true` if there was a next value, `false` otherwise
553  *
554  * Since: 0.9.2
555  **/
556 hb_bool_t
hb_set_next(const hb_set_t * set,hb_codepoint_t * codepoint)557 hb_set_next (const hb_set_t *set,
558 	     hb_codepoint_t *codepoint)
559 {
560   return set->next (codepoint);
561 }
562 
563 /**
564  * hb_set_previous:
565  * @set: A set
566  * @codepoint: (inout): Input = Code point to query
567  *             Output = Code point retrieved
568  *
569  * Fetches the previous element in @set that is lower than current value of @codepoint.
570  *
571  * Set @codepoint to #HB_SET_VALUE_INVALID to get started.
572  *
573  * Return value: `true` if there was a previous value, `false` otherwise
574  *
575  * Since: 1.8.0
576  **/
577 hb_bool_t
hb_set_previous(const hb_set_t * set,hb_codepoint_t * codepoint)578 hb_set_previous (const hb_set_t *set,
579 		 hb_codepoint_t *codepoint)
580 {
581   return set->previous (codepoint);
582 }
583 
584 /**
585  * hb_set_next_range:
586  * @set: A set
587  * @first: (out): The first code point in the range
588  * @last: (inout): Input = The current last code point in the range
589  *         Output = The last code point in the range
590  *
591  * Fetches the next consecutive range of elements in @set that
592  * are greater than current value of @last.
593  *
594  * Set @last to #HB_SET_VALUE_INVALID to get started.
595  *
596  * Return value: `true` if there was a next range, `false` otherwise
597  *
598  * Since: 0.9.7
599  **/
600 hb_bool_t
hb_set_next_range(const hb_set_t * set,hb_codepoint_t * first,hb_codepoint_t * last)601 hb_set_next_range (const hb_set_t *set,
602 		   hb_codepoint_t *first,
603 		   hb_codepoint_t *last)
604 {
605   return set->next_range (first, last);
606 }
607 
608 /**
609  * hb_set_previous_range:
610  * @set: A set
611  * @first: (inout): Input = The current first code point in the range
612  *         Output = The first code point in the range
613  * @last: (out): The last code point in the range
614  *
615  * Fetches the previous consecutive range of elements in @set that
616  * are greater than current value of @last.
617  *
618  * Set @first to #HB_SET_VALUE_INVALID to get started.
619  *
620  * Return value: `true` if there was a previous range, `false` otherwise
621  *
622  * Since: 1.8.0
623  **/
624 hb_bool_t
hb_set_previous_range(const hb_set_t * set,hb_codepoint_t * first,hb_codepoint_t * last)625 hb_set_previous_range (const hb_set_t *set,
626 		       hb_codepoint_t *first,
627 		       hb_codepoint_t *last)
628 {
629   return set->previous_range (first, last);
630 }
631 
632 /**
633  * hb_set_next_many:
634  * @set: A set
635  * @codepoint: Outputting codepoints starting after this one.
636  *             Use #HB_SET_VALUE_INVALID to get started.
637  * @out: (array length=size): An array of codepoints to write to.
638  * @size: The maximum number of codepoints to write out.
639  *
640  * Finds the next element in @set that is greater than @codepoint. Writes out
641  * codepoints to @out, until either the set runs out of elements, or @size
642  * codepoints are written, whichever comes first.
643  *
644  * Return value: the number of values written.
645  *
646  * Since: 4.2.0
647  **/
648 unsigned int
hb_set_next_many(const hb_set_t * set,hb_codepoint_t codepoint,hb_codepoint_t * out,unsigned int size)649 hb_set_next_many (const hb_set_t *set,
650 		  hb_codepoint_t  codepoint,
651 		  hb_codepoint_t *out,
652 		  unsigned int    size)
653 {
654   return set->next_many (codepoint, out, size);
655 }
656