Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 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 | #ifndef HB_ARRAY_HH |
| 28 | #define HB_ARRAY_HH |
| 29 | |
| 30 | #include "hb.hh" |
Behdad Esfahbod | 6e3ad65 | 2019-01-09 09:05:01 -0800 | [diff] [blame] | 31 | #include "hb-algs.hh" |
Behdad Esfahbod | 865deeb | 2018-12-21 17:35:58 -0500 | [diff] [blame] | 32 | #include "hb-iter.hh" |
| 33 | #include "hb-null.hh" |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 34 | |
| 35 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 36 | template <typename Type> |
| 37 | struct hb_sorted_array_t; |
| 38 | |
| 39 | template <typename Type> |
Behdad Esfahbod | 849a0f1 | 2019-01-29 17:10:19 -0800 | [diff] [blame] | 40 | struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&> |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 41 | { |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 42 | /* |
| 43 | * Constructors. |
| 44 | */ |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 45 | hb_array_t () : arrayZ (nullptr), length (0) {} |
Behdad Esfahbod | 20a73da | 2019-04-03 14:32:15 -0700 | [diff] [blame] | 46 | hb_array_t (const hb_array_t<Type> &o) : arrayZ (o.arrayZ), length (o.length) {} |
Behdad Esfahbod | 177a8af | 2019-01-07 20:20:44 -0500 | [diff] [blame] | 47 | template <typename U = Type, hb_enable_if (hb_is_const (U))> |
Behdad Esfahbod | 815cde9 | 2019-01-07 18:33:04 -0500 | [diff] [blame] | 48 | hb_array_t (const hb_array_t<hb_remove_const (Type)> &o) : arrayZ (o.arrayZ), length (o.length) {} |
Behdad Esfahbod | f02ebc8 | 2019-04-03 15:23:06 -0700 | [diff] [blame] | 49 | |
Behdad Esfahbod | 0363ce6 | 2019-01-27 01:03:56 +0100 | [diff] [blame] | 50 | hb_array_t (Type *array_, unsigned int length_) : arrayZ (array_), length (length_) {} |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 51 | template <unsigned int length_> hb_array_t (Type (&array_)[length_]) : arrayZ (array_), length (length_) {} |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 52 | |
Behdad Esfahbod | f02ebc8 | 2019-04-03 15:23:06 -0700 | [diff] [blame] | 53 | template <typename U = Type, hb_enable_if (hb_is_const (U))> |
| 54 | hb_array_t& operator = (const hb_array_t<hb_remove_const (Type)> &o) |
| 55 | { arrayZ = o.arrayZ; length = o.length; return *this; } |
| 56 | hb_array_t& operator = (const hb_array_t &o) |
| 57 | { arrayZ = o.arrayZ; length = o.length; return *this; } |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 58 | /* |
| 59 | * Iterator implementation. |
| 60 | */ |
Behdad Esfahbod | 636786e | 2019-01-08 23:48:35 -0800 | [diff] [blame] | 61 | typedef Type& __item_t__; |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 62 | static constexpr bool is_random_access_iterator = true; |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 63 | Type& __item_at__ (unsigned i) const |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 64 | { |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 65 | if (unlikely (i >= length)) return CrapOrNull (Type); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 66 | return arrayZ[i]; |
| 67 | } |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 68 | void __forward__ (unsigned n) |
| 69 | { |
| 70 | if (unlikely (n > length)) |
| 71 | n = length; |
| 72 | length -= n; |
| 73 | arrayZ += n; |
| 74 | } |
| 75 | void __rewind__ (unsigned n) |
| 76 | { |
| 77 | if (unlikely (n > length)) |
| 78 | n = length; |
| 79 | length -= n; |
| 80 | } |
| 81 | unsigned __len__ () const { return length; } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 82 | |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 83 | /* Extra operators. |
| 84 | */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 85 | Type * operator & () const { return arrayZ; } |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 86 | operator hb_array_t<const Type> () { return hb_array_t<const Type> (arrayZ, length); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 87 | template <typename T> operator T * () const { return arrayZ; } |
Behdad Esfahbod | 94e72cf | 2018-12-17 00:06:40 -0500 | [diff] [blame] | 88 | |
Behdad Esfahbod | 5b66b03 | 2019-04-02 19:27:02 -0700 | [diff] [blame] | 89 | bool operator == (const hb_array_t &o) const; |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 90 | uint32_t hash () const; |
| 91 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 92 | /* |
| 93 | * Compare, Sort, and Search. |
| 94 | */ |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 95 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 96 | /* Note: our compare is NOT lexicographic; it also does NOT call Type::cmp. */ |
| 97 | int cmp (const hb_array_t<Type> &a) const |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 98 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 99 | if (length != a.length) |
| 100 | return (int) a.length - (int) length; |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 101 | return hb_memcmp (a.arrayZ, arrayZ, get_size ()); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 102 | } |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 103 | static int cmp (const void *pa, const void *pb) |
| 104 | { |
| 105 | hb_array_t<Type> *a = (hb_array_t<Type> *) pa; |
| 106 | hb_array_t<Type> *b = (hb_array_t<Type> *) pb; |
| 107 | return b->cmp (*a); |
| 108 | } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 109 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 110 | template <typename T> |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 111 | Type *lsearch (const T &x, Type *not_found = nullptr) |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 112 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 113 | unsigned int count = length; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 114 | for (unsigned int i = 0; i < count; i++) |
| 115 | if (!this->arrayZ[i].cmp (x)) |
| 116 | return &this->arrayZ[i]; |
| 117 | return not_found; |
| 118 | } |
| 119 | template <typename T> |
| 120 | const Type *lsearch (const T &x, const Type *not_found = nullptr) const |
| 121 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 122 | unsigned int count = length; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 123 | for (unsigned int i = 0; i < count; i++) |
| 124 | if (!this->arrayZ[i].cmp (x)) |
| 125 | return &this->arrayZ[i]; |
| 126 | return not_found; |
| 127 | } |
| 128 | |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 129 | hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*)) |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 130 | { |
Behdad Esfahbod | 89949ed | 2018-12-30 01:52:19 -0500 | [diff] [blame] | 131 | if (likely (length)) |
| 132 | ::qsort (arrayZ, length, this->item_size, cmp_); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 133 | return hb_sorted_array_t<Type> (*this); |
| 134 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 135 | hb_sorted_array_t<Type> qsort () |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 136 | { |
Behdad Esfahbod | 89949ed | 2018-12-30 01:52:19 -0500 | [diff] [blame] | 137 | if (likely (length)) |
| 138 | ::qsort (arrayZ, length, this->item_size, Type::cmp); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 139 | return hb_sorted_array_t<Type> (*this); |
| 140 | } |
| 141 | void qsort (unsigned int start, unsigned int end) |
| 142 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 143 | end = MIN (end, length); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 144 | assert (start <= end); |
Behdad Esfahbod | 89949ed | 2018-12-30 01:52:19 -0500 | [diff] [blame] | 145 | if (likely (start < end)) |
| 146 | ::qsort (arrayZ + start, end - start, this->item_size, Type::cmp); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 149 | /* |
| 150 | * Other methods. |
| 151 | */ |
| 152 | |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 153 | unsigned int get_size () const { return length * this->item_size; } |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 154 | |
| 155 | hb_array_t<Type> sub_array (unsigned int start_offset = 0, unsigned int *seg_count = nullptr /* IN/OUT */) const |
| 156 | { |
| 157 | if (!start_offset && !seg_count) |
| 158 | return *this; |
| 159 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 160 | unsigned int count = length; |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 161 | if (unlikely (start_offset > count)) |
| 162 | count = 0; |
| 163 | else |
| 164 | count -= start_offset; |
| 165 | if (seg_count) |
| 166 | count = *seg_count = MIN (count, *seg_count); |
| 167 | return hb_array_t<Type> (arrayZ + start_offset, count); |
| 168 | } |
| 169 | hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const |
| 170 | { return sub_array (start_offset, &seg_count); } |
| 171 | |
| 172 | /* Only call if you allocated the underlying array using malloc() or similar. */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 173 | void free () |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 174 | { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 175 | |
| 176 | template <typename hb_sanitize_context_t> |
| 177 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 178 | { return c->check_array (arrayZ, length); } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 179 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 180 | /* |
| 181 | * Members |
| 182 | */ |
| 183 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 184 | public: |
| 185 | Type *arrayZ; |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 186 | unsigned int length; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 187 | }; |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 188 | template <typename T> inline hb_array_t<T> |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 189 | hb_array (T *array, unsigned int length) |
| 190 | { return hb_array_t<T> (array, length); } |
| 191 | template <typename T, unsigned int length_> inline hb_array_t<T> |
| 192 | hb_array (T (&array_)[length_]) |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 193 | { return hb_array_t<T> (array_); } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 194 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 195 | enum hb_bfind_not_found_t |
| 196 | { |
| 197 | HB_BFIND_NOT_FOUND_DONT_STORE, |
| 198 | HB_BFIND_NOT_FOUND_STORE, |
| 199 | HB_BFIND_NOT_FOUND_STORE_CLOSEST, |
| 200 | }; |
| 201 | |
| 202 | template <typename Type> |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 203 | struct hb_sorted_array_t : |
Behdad Esfahbod | b5d6fe1 | 2019-01-02 16:20:40 -0500 | [diff] [blame] | 204 | hb_iter_t<hb_sorted_array_t<Type>, Type&>, |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 205 | hb_array_t<Type> |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 206 | { |
Behdad Esfahbod | b5d6fe1 | 2019-01-02 16:20:40 -0500 | [diff] [blame] | 207 | typedef hb_iter_t<hb_sorted_array_t<Type>, Type&> iter_base_t; |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 208 | HB_ITER_USING (iter_base_t); |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 209 | static constexpr bool is_random_access_iterator = true; |
| 210 | static constexpr bool is_sorted_iterator = true; |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 211 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 212 | hb_sorted_array_t () : hb_array_t<Type> () {} |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 213 | hb_sorted_array_t (const hb_array_t<Type> &o) : hb_array_t<Type> (o) {} |
Behdad Esfahbod | 177a8af | 2019-01-07 20:20:44 -0500 | [diff] [blame] | 214 | template <typename U = Type, hb_enable_if (hb_is_const (U))> |
Behdad Esfahbod | 815cde9 | 2019-01-07 18:33:04 -0500 | [diff] [blame] | 215 | hb_sorted_array_t (const hb_sorted_array_t<hb_remove_const (Type)> &o) : hb_array_t<Type> (o) {} |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 216 | hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {} |
| 217 | template <unsigned int length_> hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {} |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 218 | |
| 219 | hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int *seg_count /* IN/OUT */) const |
| 220 | { return hb_sorted_array_t<Type> (((const hb_array_t<Type> *) (this))->sub_array (start_offset, seg_count)); } |
| 221 | hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const |
| 222 | { return sub_array (start_offset, &seg_count); } |
| 223 | |
| 224 | template <typename T> |
| 225 | Type *bsearch (const T &x, Type *not_found = nullptr) |
| 226 | { |
| 227 | unsigned int i; |
| 228 | return bfind (x, &i) ? &this->arrayZ[i] : not_found; |
| 229 | } |
| 230 | template <typename T> |
| 231 | const Type *bsearch (const T &x, const Type *not_found = nullptr) const |
| 232 | { |
| 233 | unsigned int i; |
| 234 | return bfind (x, &i) ? &this->arrayZ[i] : not_found; |
| 235 | } |
| 236 | template <typename T> |
| 237 | bool bfind (const T &x, unsigned int *i = nullptr, |
| 238 | hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, |
| 239 | unsigned int to_store = (unsigned int) -1) const |
| 240 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 241 | int min = 0, max = (int) this->length - 1; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 242 | const Type *array = this->arrayZ; |
| 243 | while (min <= max) |
| 244 | { |
| 245 | int mid = ((unsigned int) min + (unsigned int) max) / 2; |
| 246 | int c = array[mid].cmp (x); |
| 247 | if (c < 0) |
| 248 | max = mid - 1; |
| 249 | else if (c > 0) |
| 250 | min = mid + 1; |
| 251 | else |
| 252 | { |
| 253 | if (i) |
| 254 | *i = mid; |
| 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | if (i) |
| 259 | { |
| 260 | switch (not_found) |
| 261 | { |
| 262 | case HB_BFIND_NOT_FOUND_DONT_STORE: |
| 263 | break; |
| 264 | |
| 265 | case HB_BFIND_NOT_FOUND_STORE: |
| 266 | *i = to_store; |
| 267 | break; |
| 268 | |
| 269 | case HB_BFIND_NOT_FOUND_STORE_CLOSEST: |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 270 | if (max < 0 || (max < (int) this->length && array[max].cmp (x) > 0)) |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 271 | max++; |
| 272 | *i = max; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | return false; |
| 277 | } |
| 278 | }; |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 279 | template <typename T> inline hb_sorted_array_t<T> |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 280 | hb_sorted_array (T *array, unsigned int length) |
| 281 | { return hb_sorted_array_t<T> (array, length); } |
| 282 | template <typename T, unsigned int length_> inline hb_sorted_array_t<T> |
| 283 | hb_sorted_array (T (&array_)[length_]) |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 284 | { return hb_sorted_array_t<T> (array_); } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 285 | |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 286 | template <typename T> |
Behdad Esfahbod | 5b66b03 | 2019-04-02 19:27:02 -0700 | [diff] [blame] | 287 | bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const |
| 288 | { |
| 289 | return length == o.length && |
| 290 | + hb_zip (*this, o) |
| 291 | | hb_map ([] (hb_pair_t<T&, T&> &&_) -> bool { return _.first == _.second; }) |
| 292 | | hb_all |
| 293 | ; |
| 294 | } |
| 295 | template <typename T> |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 296 | uint32_t hb_array_t<T>::hash () const |
| 297 | { |
Behdad Esfahbod | d0da547 | 2019-04-02 18:22:39 -0700 | [diff] [blame] | 298 | return |
Behdad Esfahbod | 42ab32c | 2019-04-02 18:41:33 -0700 | [diff] [blame] | 299 | + hb_iter (*this) |
Behdad Esfahbod | d0da547 | 2019-04-02 18:22:39 -0700 | [diff] [blame] | 300 | | hb_map (hb_hash) |
| 301 | | hb_reduce ([] (uint32_t a, uint32_t b) -> uint32_t { return a * 31 + b; }, 0) |
| 302 | ; |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 303 | } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 304 | |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 305 | typedef hb_array_t<const char> hb_bytes_t; |
Behdad Esfahbod | 3d9d7dc | 2018-12-18 22:11:23 -0500 | [diff] [blame] | 306 | typedef hb_array_t<const unsigned char> hb_ubytes_t; |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 307 | |
Behdad Esfahbod | 5b66b03 | 2019-04-02 19:27:02 -0700 | [diff] [blame] | 308 | /* TODO Specialize opeator==/hash() for hb_bytes_t and hb_ubytes_t. */ |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 309 | //template <> |
| 310 | //uint32_t hb_array_t<const char>::hash () const { return 0; } |
Behdad Esfahbod | 9268036 | 2018-12-16 23:38:51 -0500 | [diff] [blame] | 311 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 312 | #endif /* HB_ARRAY_HH */ |