11#ifndef COMMON_INT_SET
22#define COMMON_INT_SET
33
4+ #include < optional>
5+
46#include " common/hb_set_unique_ptr.h"
57namespace common {
68
@@ -78,14 +80,14 @@ class IntSet {
7880 }
7981
8082 IntSet (const hb_set_t * set) : set_(make_hb_set()) {
81- // We always keep exclusive ownership of the internal set, so copy the contents
82- // of the input set instead of referencing it.
83+ // We always keep exclusive ownership of the internal set, so copy the
84+ // contents of the input set instead of referencing it.
8385 hb_set_union (set_.get (), set);
8486 }
8587
8688 IntSet (const hb_set_unique_ptr& set) : set_(make_hb_set()) {
87- // We always keep exclusive ownership of the internal set, so copy the contents
88- // of the input set instead of referencing it.
89+ // We always keep exclusive ownership of the internal set, so copy the
90+ // contents of the input set instead of referencing it.
8991 hb_set_union (set_.get (), set.get ());
9092 }
9193
@@ -147,8 +149,6 @@ class IntSet {
147149 return H::combine (std::move (h), harfbuzz_hash);
148150 }
149151
150- // TODO(garretrieger): add union, intersection etc.
151-
152152 iterator begin () { return iterator (set_.get ()); }
153153
154154 iterator end () { return iterator (); }
@@ -169,25 +169,72 @@ class IntSet {
169169
170170 void add (hb_codepoint_t codepoint) { hb_set_add (set_.get (), codepoint); }
171171
172+ void add_range (hb_codepoint_t start, hb_codepoint_t end) {
173+ hb_set_add_range (set_.get (), start, end);
174+ }
175+
172176 bool contains (hb_codepoint_t codepoint) const {
173177 return hb_set_has (set_.get (), codepoint);
174178 }
175179
180+ bool is_subset_of (const IntSet& other) const {
181+ return hb_set_is_subset (set_.get (), other.set_ .get ());
182+ }
183+
184+ std::optional<hb_codepoint_t > min () const {
185+ hb_codepoint_t value = hb_set_get_min (set_.get ());
186+ if (value == HB_SET_VALUE_INVALID) {
187+ return std::nullopt ;
188+ }
189+ return value;
190+ }
191+
192+ std::optional<hb_codepoint_t > max () const {
193+ hb_codepoint_t value = hb_set_get_max (set_.get ());
194+ if (value == HB_SET_VALUE_INVALID) {
195+ return std::nullopt ;
196+ }
197+ return value;
198+ }
199+
176200 void erase (hb_codepoint_t codepoint) { hb_set_del (set_.get (), codepoint); }
177201
178202 size_t size () const { return hb_set_get_population (set_.get ()); }
179203
180204 bool empty () const { return hb_set_is_empty (set_.get ()); }
181205
182- // Remove all elements
206+ // Removes all elements
183207 void clear () { hb_set_clear (set_.get ()); }
184208
209+ // Compute the union of this and other, store the result in this set.
210+ void union_set (const IntSet& other) {
211+ hb_set_union (set_.get (), other.set_ .get ());
212+ }
213+
214+ // Compute the intersection of this and other, store the result in this set.
215+ void intersect (const IntSet& other) {
216+ hb_set_intersect (set_.get (), other.set_ .get ());
217+ }
218+
219+ // Subtract other from this set.
220+ void subtract (const IntSet& other) {
221+ hb_set_subtract (set_.get (), other.set_ .get ());
222+ }
223+
224+ // Compute the symmetric difference of this and other, store the result in
225+ // this set.
226+ void symmetric_difference (const IntSet& other) {
227+ hb_set_symmetric_difference (set_.get (), other.set_ .get ());
228+ }
229+
185230 private:
186231 // Note: set_ must always point to a valid set object. nullptr is not allowed.
187- // Note: we always retain exclusive ownership over set_. Normally hb_set_t* can be
188- // shared (via hb_set_reference()), but for this container we don't ever expose
189- // the underlying hb_set_t* pointer so that we know we're always the only owner.
190- // This prevents the sets contents from being changed outside of this class.
232+ // Note: we always retain exclusive ownership over set_. Normally hb_set_t*
233+ // can be
234+ // shared (via hb_set_reference()), but for this container we don't ever
235+ // expose the underlying hb_set_t* pointer so that we know we're always
236+ // the only owner. This prevents the sets contents from being changed
237+ // outside of this class.
191238 hb_set_unique_ptr set_;
192239};
193240
0 commit comments