hb-set.hh (7189B)
1 /* 2 * Copyright © 2012,2017 Google, Inc. 3 * Copyright © 2021 Behdad Esfahbod 4 * 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 28 #ifndef HB_SET_HH 29 #define HB_SET_HH 30 31 #include "hb.hh" 32 #include "hb-bit-set-invertible.hh" 33 #include "hb-bit-vector.hh" // Just to include 34 35 36 template <typename impl_t> 37 struct hb_sparseset_t 38 { 39 static constexpr bool realloc_move = true; 40 41 hb_object_header_t header; 42 impl_t s; 43 44 hb_sparseset_t () { init (); } 45 ~hb_sparseset_t () { fini (); } 46 47 hb_sparseset_t (const hb_sparseset_t& other) : hb_sparseset_t () { set (other); } 48 hb_sparseset_t (hb_sparseset_t&& other) noexcept : hb_sparseset_t () { s = std::move (other.s); } 49 hb_sparseset_t& operator = (const hb_sparseset_t& other) { set (other); return *this; } 50 hb_sparseset_t& operator = (hb_sparseset_t&& other) noexcept { s = std::move (other.s); return *this; } 51 friend void swap (hb_sparseset_t& a, hb_sparseset_t& b) noexcept { hb_swap (a.s, b.s); } 52 53 hb_sparseset_t (std::initializer_list<hb_codepoint_t> lst) : hb_sparseset_t () 54 { 55 for (auto&& item : lst) 56 add (item); 57 } 58 template <typename Iterable, 59 hb_requires (hb_is_iterable (Iterable))> 60 hb_sparseset_t (const Iterable &o) : hb_sparseset_t () 61 { 62 hb_copy (o, *this); 63 } 64 65 void init () 66 { 67 hb_object_init (this); 68 s.init (); 69 } 70 void fini () 71 { 72 hb_object_fini (this); 73 s.fini (); 74 } 75 76 explicit operator bool () const { return !is_empty (); } 77 78 void err () { s.err (); } 79 bool in_error () const { return s.in_error (); } 80 81 void alloc (unsigned sz) { s.alloc (sz); } 82 void reset () { s.reset (); } 83 void clear () { s.clear (); } 84 void invert () { s.invert (); } 85 bool is_inverted () const { return s.is_inverted (); } 86 bool is_empty () const { return s.is_empty (); } 87 uint32_t hash () const { return s.hash (); } 88 89 void add (hb_codepoint_t g) { s.add (g); } 90 bool add_range (hb_codepoint_t first, hb_codepoint_t last) { return s.add_range (first, last); } 91 92 template <typename T> 93 void add_array (const T *array, unsigned int count, unsigned int stride=sizeof(T)) 94 { s.add_array (array, count, stride); } 95 template <typename T> 96 void add_array (const hb_array_t<const T>& arr) { add_array (&arr, arr.len ()); } 97 98 /* Might return false if array looks unsorted. 99 * Used for faster rejection of corrupt data. */ 100 template <typename T> 101 bool add_sorted_array (const T *array, unsigned int count, unsigned int stride=sizeof(T)) 102 { return s.add_sorted_array (array, count, stride); } 103 template <typename T> 104 bool add_sorted_array (const hb_sorted_array_t<const T>& arr) { return add_sorted_array (&arr, arr.len ()); } 105 106 void del (hb_codepoint_t g) { s.del (g); } 107 void del_range (hb_codepoint_t a, hb_codepoint_t b) { s.del_range (a, b); } 108 109 bool get (hb_codepoint_t g) const { return s.get (g); } 110 bool may_have (hb_codepoint_t g) const { return get (g); } 111 112 /* Has interface. */ 113 bool operator [] (hb_codepoint_t k) const { return get (k); } 114 bool has (hb_codepoint_t k) const { return (*this)[k]; } 115 116 /* Predicate. */ 117 bool operator () (hb_codepoint_t k) const { return has (k); } 118 119 /* Sink interface. */ 120 hb_sparseset_t& operator << (hb_codepoint_t v) 121 { add (v); return *this; } 122 hb_sparseset_t& operator << (const hb_codepoint_pair_t& range) 123 { add_range (range.first, range.second); return *this; } 124 125 bool may_intersect (const hb_sparseset_t &other) const 126 { return s.may_intersect (other.s); } 127 128 bool intersects (hb_codepoint_t first, hb_codepoint_t last) const 129 { return s.intersects (first, last); } 130 131 void set (const hb_sparseset_t &other) { s.set (other.s); } 132 133 bool is_equal (const hb_sparseset_t &other) const { return s.is_equal (other.s); } 134 bool operator == (const hb_set_t &other) const { return is_equal (other); } 135 bool operator != (const hb_set_t &other) const { return !is_equal (other); } 136 137 bool is_subset (const hb_sparseset_t &larger_set) const { return s.is_subset (larger_set.s); } 138 139 void union_ (const hb_sparseset_t &other) { s.union_ (other.s); } 140 void intersect (const hb_sparseset_t &other) { s.intersect (other.s); } 141 void subtract (const hb_sparseset_t &other) { s.subtract (other.s); } 142 void symmetric_difference (const hb_sparseset_t &other) { s.symmetric_difference (other.s); } 143 144 bool next (hb_codepoint_t *codepoint) const { return s.next (codepoint); } 145 bool previous (hb_codepoint_t *codepoint) const { return s.previous (codepoint); } 146 bool next_range (hb_codepoint_t *first, hb_codepoint_t *last) const 147 { return s.next_range (first, last); } 148 bool previous_range (hb_codepoint_t *first, hb_codepoint_t *last) const 149 { return s.previous_range (first, last); } 150 unsigned int next_many (hb_codepoint_t codepoint, hb_codepoint_t *out, unsigned int size) const 151 { return s.next_many (codepoint, out, size); } 152 153 unsigned int get_population () const { return s.get_population (); } 154 hb_codepoint_t get_min () const { return s.get_min (); } 155 hb_codepoint_t get_max () const { return s.get_max (); } 156 157 static constexpr hb_codepoint_t INVALID = impl_t::INVALID; 158 159 /* 160 * Iterator implementation. 161 */ 162 using iter_t = typename impl_t::iter_t; 163 iter_t iter () const { return iter_t (this->s); } 164 operator iter_t () const { return iter (); } 165 }; 166 167 struct hb_set_t : hb_sparseset_t<hb_bit_set_invertible_t> 168 { 169 using sparseset = hb_sparseset_t<hb_bit_set_invertible_t>; 170 171 ~hb_set_t () = default; 172 hb_set_t () : sparseset () {}; 173 hb_set_t (const hb_set_t &o) : sparseset ((sparseset &) o) {}; 174 hb_set_t (hb_set_t&& o) noexcept : sparseset (std::move ((sparseset &) o)) {} 175 hb_set_t& operator = (const hb_set_t&) = default; 176 hb_set_t& operator = (hb_set_t&&) = default; 177 hb_set_t (std::initializer_list<hb_codepoint_t> lst) : sparseset (lst) {} 178 template <typename Iterable, 179 hb_requires (hb_is_iterable (Iterable))> 180 hb_set_t (const Iterable &o) : sparseset (o) {} 181 182 hb_set_t& operator << (hb_codepoint_t v) 183 { sparseset::operator<< (v); return *this; } 184 hb_set_t& operator << (const hb_codepoint_pair_t& range) 185 { sparseset::operator<< (range); return *this; } 186 }; 187 188 static_assert (hb_set_t::INVALID == HB_SET_VALUE_INVALID, ""); 189 190 191 #endif /* HB_SET_HH */