blob: 604802381b9128ce6fceedfc9743072f409e68d2 [file] [log] [blame]
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -04001/*
Behdad Esfahboddeed4a42017-10-15 16:53:09 +02002 * Copyright © 2012,2017 Google, Inc.
Behdad Esfahbod9b390f82021-08-15 12:34:55 -06003 * Copyright © 2021 Behdad Esfahbod
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -04004 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Google Author(s): Behdad Esfahbod
26 */
27
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070028#ifndef HB_SET_HH
29#define HB_SET_HH
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -040030
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070031#include "hb.hh"
Behdad Esfahbodf0c38042021-08-17 15:37:19 -060032#include "hb-bit-set-invertible.hh"
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -040033
34
Behdad Esfahbodb5cdbdc2021-08-17 10:53:08 -060035template <typename impl_t>
Behdad Esfahbodf0c38042021-08-17 15:37:19 -060036struct hb_sparseset_t
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -040037{
Behdad Esfahbod6220e5f2012-06-06 03:30:09 -040038 hb_object_header_t header;
Behdad Esfahbodb5cdbdc2021-08-17 10:53:08 -060039 impl_t s;
Behdad Esfahboddeed4a42017-10-15 16:53:09 +020040
Behdad Esfahbodf0c38042021-08-17 15:37:19 -060041 hb_sparseset_t () { init (); }
42 ~hb_sparseset_t () { fini (); }
43
Behdad Esfahbod93ac7002021-08-29 10:32:40 -060044 hb_sparseset_t (const hb_sparseset_t& other) : hb_sparseset_t () { set (other); }
Behdad Esfahbod811f80a2021-11-02 00:14:34 -060045 hb_sparseset_t (hb_sparseset_t&& other) : hb_sparseset_t () { s = std::move (other.s); }
Behdad Esfahbod0623aa52022-05-19 14:12:42 -060046 hb_sparseset_t& operator = (const hb_sparseset_t& other) { set (other); return *this; }
47 hb_sparseset_t& operator = (hb_sparseset_t&& other) { s = std::move (other.s); return *this; }
Behdad Esfahboddcd18da2021-11-01 20:55:30 -060048 friend void swap (hb_sparseset_t& a, hb_sparseset_t& b) { hb_swap (a.s, b.s); }
49
Behdad Esfahbode97e7992021-11-01 21:34:46 -060050 hb_sparseset_t (std::initializer_list<hb_codepoint_t> lst) : hb_sparseset_t ()
Behdad Esfahboddcd18da2021-11-01 20:55:30 -060051 {
Behdad Esfahbode97e7992021-11-01 21:34:46 -060052 for (auto&& item : lst)
53 add (item);
Behdad Esfahboddcd18da2021-11-01 20:55:30 -060054 }
55 template <typename Iterable,
Behdad Esfahbodc81198b2022-05-12 11:58:37 -060056 hb_requires (hb_is_iterable (Iterable))>
Behdad Esfahboddcd18da2021-11-01 20:55:30 -060057 hb_sparseset_t (const Iterable &o) : hb_sparseset_t ()
58 {
59 hb_copy (o, *this);
60 }
Behdad Esfahbod93ac7002021-08-29 10:32:40 -060061
Ebrahim Byagowie4120082018-12-17 21:31:01 +033062 void init ()
Behdad Esfahbodf1f6bc02018-05-02 12:56:21 -040063 {
64 hb_object_init (this);
Behdad Esfahbodbcd59b52022-07-20 15:57:09 -060065 s.init ();
Behdad Esfahbodf1f6bc02018-05-02 12:56:21 -040066 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +033067 void fini ()
Behdad Esfahbodf1f6bc02018-05-02 12:56:21 -040068 {
69 hb_object_fini (this);
Behdad Esfahbodbcd59b52022-07-20 15:57:09 -060070 s.fini ();
Behdad Esfahbodf1f6bc02018-05-02 12:56:21 -040071 }
Behdad Esfahbod9daa88c2017-12-14 13:37:48 -080072
Behdad Esfahbod2fbd34f2020-06-28 22:41:09 -070073 explicit operator bool () const { return !is_empty (); }
Behdad Esfahboddeed4a42017-10-15 16:53:09 +020074
Behdad Esfahbodfad452b2021-08-16 20:48:24 -060075 void err () { s.err (); }
76 bool in_error () const { return s.in_error (); }
Behdad Esfahbodbd5f9182018-05-01 18:27:41 -040077
Behdad Esfahbod1f578b52022-05-18 15:24:40 -060078 void alloc (unsigned sz) { s.alloc (sz); }
Behdad Esfahbodfad452b2021-08-16 20:48:24 -060079 void reset () { s.reset (); }
Behdad Esfahbodfad452b2021-08-16 20:48:24 -060080 void clear () { s.clear (); }
Behdad Esfahbodf0c38042021-08-17 15:37:19 -060081 void invert () { s.invert (); }
Behdad Esfahbod51532182023-01-05 16:26:41 -070082 bool is_inverted () const { return s.is_inverted (); }
Behdad Esfahbodfad452b2021-08-16 20:48:24 -060083 bool is_empty () const { return s.is_empty (); }
Behdad Esfahbod58f848d2022-05-19 15:42:54 -060084 uint32_t hash () const { return s.hash (); }
Behdad Esfahbodfad452b2021-08-16 20:48:24 -060085
86 void add (hb_codepoint_t g) { s.add (g); }
87 bool add_range (hb_codepoint_t a, hb_codepoint_t b) { return s.add_range (a, b); }
Behdad Esfahbod5d025722017-12-14 19:33:55 -080088
Behdad Esfahbod0fe62c12017-12-13 13:12:20 -080089 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033090 void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
Behdad Esfahbodfad452b2021-08-16 20:48:24 -060091 { s.add_array (array, count, stride); }
Behdad Esfahbod092094f2021-04-01 15:47:21 -060092 template <typename T>
93 void add_array (const hb_array_t<const T>& arr) { add_array (&arr, arr.len ()); }
Behdad Esfahbod1ce7d6e2017-12-16 11:36:16 -050094
Behdad Esfahbod5d025722017-12-14 19:33:55 -080095 /* Might return false if array looks unsorted.
96 * Used for faster rejection of corrupt data. */
97 template <typename T>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033098 bool add_sorted_array (const T *array, unsigned int count, unsigned int stride=sizeof(T))
Behdad Esfahbodfad452b2021-08-16 20:48:24 -060099 { return s.add_sorted_array (array, count, stride); }
Behdad Esfahbod092094f2021-04-01 15:47:21 -0600100 template <typename T>
101 bool add_sorted_array (const hb_sorted_array_t<const T>& arr) { return add_sorted_array (&arr, arr.len ()); }
Behdad Esfahbod5d025722017-12-14 19:33:55 -0800102
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600103 void del (hb_codepoint_t g) { s.del (g); }
104 void del_range (hb_codepoint_t a, hb_codepoint_t b) { s.del_range (a, b); }
arizaa5012e92020-02-24 17:09:48 -0800105
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600106 bool get (hb_codepoint_t g) const { return s.get (g); }
Behdad Esfahboda7de1442019-01-09 08:39:25 -0800107
Behdad Esfahbodd438e612019-01-28 16:34:04 -0500108 /* Has interface. */
Behdad Esfahbodd2a26702022-11-28 19:42:27 -0700109 bool operator [] (hb_codepoint_t k) const { return get (k); }
110 bool has (hb_codepoint_t k) const { return (*this)[k]; }
Andrew John01829882022-03-25 08:36:44 -0700111
Behdad Esfahbodd438e612019-01-28 16:34:04 -0500112 /* Predicate. */
Behdad Esfahboda7de1442019-01-09 08:39:25 -0800113 bool operator () (hb_codepoint_t k) const { return has (k); }
114
Behdad Esfahboda30e1342019-01-28 16:39:01 -0500115 /* Sink interface. */
Behdad Esfahbodf0c38042021-08-17 15:37:19 -0600116 hb_sparseset_t& operator << (hb_codepoint_t v)
Behdad Esfahbod689f3f52020-04-23 10:51:12 -0700117 { add (v); return *this; }
Behdad Esfahbodf0c38042021-08-17 15:37:19 -0600118 hb_sparseset_t& operator << (const hb_pair_t<hb_codepoint_t, hb_codepoint_t>& range)
Behdad Esfahbod689f3f52020-04-23 10:51:12 -0700119 { add_range (range.first, range.second); return *this; }
Behdad Esfahboda30e1342019-01-28 16:39:01 -0500120
Behdad Esfahboda7de1442019-01-09 08:39:25 -0800121 bool intersects (hb_codepoint_t first, hb_codepoint_t last) const
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600122 { return s.intersects (first, last); }
Garret Riegerc581d112021-07-22 14:04:39 -0700123
Behdad Esfahbodf0c38042021-08-17 15:37:19 -0600124 void set (const hb_sparseset_t &other) { s.set (other.s); }
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200125
Behdad Esfahbodf0c38042021-08-17 15:37:19 -0600126 bool is_equal (const hb_sparseset_t &other) const { return s.is_equal (other.s); }
Behdad Esfahbod124f9ae2022-05-19 12:58:02 -0600127 bool operator == (const hb_set_t &other) const { return is_equal (other); }
128 bool operator != (const hb_set_t &other) const { return !is_equal (other); }
Behdad Esfahbodbd5f9182018-05-01 18:27:41 -0400129
Behdad Esfahbodf0c38042021-08-17 15:37:19 -0600130 bool is_subset (const hb_sparseset_t &larger_set) const { return s.is_subset (larger_set.s); }
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200131
Behdad Esfahbodf0c38042021-08-17 15:37:19 -0600132 void union_ (const hb_sparseset_t &other) { s.union_ (other.s); }
133 void intersect (const hb_sparseset_t &other) { s.intersect (other.s); }
134 void subtract (const hb_sparseset_t &other) { s.subtract (other.s); }
135 void symmetric_difference (const hb_sparseset_t &other) { s.symmetric_difference (other.s); }
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200136
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600137 bool next (hb_codepoint_t *codepoint) const { return s.next (codepoint); }
138 bool previous (hb_codepoint_t *codepoint) const { return s.previous (codepoint); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330139 bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600140 { return s.next_range (first, last); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330141 bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600142 { return s.previous_range (first, last); }
Behdad Esfahbod0a388782022-03-25 09:42:36 -0600143 unsigned int next_many (hb_codepoint_t codepoint, hb_codepoint_t *out, unsigned int size) const
144 { return s.next_many (codepoint, out, size); }
Behdad Esfahbod694eaf62018-02-14 01:00:10 -0800145
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600146 unsigned int get_population () const { return s.get_population (); }
147 hb_codepoint_t get_min () const { return s.get_min (); }
148 hb_codepoint_t get_max () const { return s.get_max (); }
Behdad Esfahbod694eaf62018-02-14 01:00:10 -0800149
Behdad Esfahbodb5cdbdc2021-08-17 10:53:08 -0600150 static constexpr hb_codepoint_t INVALID = impl_t::INVALID;
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -0400151
Behdad Esfahbodfc359192018-12-21 20:06:17 -0500152 /*
153 * Iterator implementation.
154 */
Behdad Esfahbodb5cdbdc2021-08-17 10:53:08 -0600155 using iter_t = typename impl_t::iter_t;
Behdad Esfahbodfad452b2021-08-16 20:48:24 -0600156 iter_t iter () const { return iter_t (this->s); }
Behdad Esfahbod6dc4a1c2018-12-26 19:49:13 -0500157 operator iter_t () const { return iter (); }
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -0400158};
159
Behdad Esfahboddcd18da2021-11-01 20:55:30 -0600160struct hb_set_t : hb_sparseset_t<hb_bit_set_invertible_t>
161{
Behdad Esfahboda09dd872022-05-12 12:58:07 -0600162 using sparseset = hb_sparseset_t<hb_bit_set_invertible_t>;
163
Behdad Esfahbod34fa5e22021-11-01 21:25:03 -0600164 ~hb_set_t () = default;
Behdad Esfahboda09dd872022-05-12 12:58:07 -0600165 hb_set_t () : sparseset () {};
166 hb_set_t (const hb_set_t &o) : sparseset ((sparseset &) o) {};
167 hb_set_t (hb_set_t&& o) : sparseset (std::move ((sparseset &) o)) {}
Behdad Esfahbod0623aa52022-05-19 14:12:42 -0600168 hb_set_t& operator = (const hb_set_t&) = default;
169 hb_set_t& operator = (hb_set_t&&) = default;
Behdad Esfahboda09dd872022-05-12 12:58:07 -0600170 hb_set_t (std::initializer_list<hb_codepoint_t> lst) : sparseset (lst) {}
Behdad Esfahboddcd18da2021-11-01 20:55:30 -0600171 template <typename Iterable,
Behdad Esfahbod7fa580b2022-05-12 13:05:32 -0600172 hb_requires (hb_is_iterable (Iterable))>
Behdad Esfahboda09dd872022-05-12 12:58:07 -0600173 hb_set_t (const Iterable &o) : sparseset (o) {}
Behdad Esfahbod35f46e72023-01-04 17:12:08 -0700174
175 hb_set_t& operator << (hb_codepoint_t v)
176 { sparseset::operator<< (v); return *this; }
177 hb_set_t& operator << (const hb_pair_t<hb_codepoint_t, hb_codepoint_t>& range)
178 { sparseset::operator<< (range); return *this; }
Behdad Esfahboddcd18da2021-11-01 20:55:30 -0600179};
Behdad Esfahbodb5cdbdc2021-08-17 10:53:08 -0600180
181static_assert (hb_set_t::INVALID == HB_SET_VALUE_INVALID, "");
182
Behdad Esfahbod0b08adb2012-04-23 22:41:09 -0400183
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700184#endif /* HB_SET_HH */