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 | */ |
David Corbett | b854d4f | 2019-05-10 22:51:49 -0400 | [diff] [blame] | 45 | hb_array_t () : arrayZ (nullptr), length (0), backwards_length (0) {} |
| 46 | hb_array_t (Type *array_, unsigned int length_) : arrayZ (array_), length (length_), backwards_length (0) {} |
Behdad Esfahbod | 322627a | 2019-05-09 16:08:10 -0700 | [diff] [blame] | 47 | template <unsigned int length_> |
David Corbett | b854d4f | 2019-05-10 22:51:49 -0400 | [diff] [blame] | 48 | hb_array_t (Type (&array_)[length_]) : arrayZ (array_), length (length_), backwards_length (0) {} |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 49 | |
Behdad Esfahbod | 91d958a | 2019-04-18 10:04:10 -0400 | [diff] [blame] | 50 | template <typename U, |
Behdad Esfahbod | 726002a | 2019-05-09 14:53:02 -0700 | [diff] [blame] | 51 | hb_enable_if (hb_is_cr_convertible(U, Type))> |
Behdad Esfahbod | 91d958a | 2019-04-18 10:04:10 -0400 | [diff] [blame] | 52 | hb_array_t (const hb_array_t<U> &o) : |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 53 | hb_iter_with_fallback_t<hb_array_t, Type&> (), |
David Corbett | b854d4f | 2019-05-10 22:51:49 -0400 | [diff] [blame] | 54 | arrayZ (o.arrayZ), length (o.length), backwards_length (o.backwards_length) {} |
Behdad Esfahbod | 91d958a | 2019-04-18 10:04:10 -0400 | [diff] [blame] | 55 | template <typename U, |
Behdad Esfahbod | 726002a | 2019-05-09 14:53:02 -0700 | [diff] [blame] | 56 | hb_enable_if (hb_is_cr_convertible(U, Type))> |
Behdad Esfahbod | 91d958a | 2019-04-18 10:04:10 -0400 | [diff] [blame] | 57 | hb_array_t& operator = (const hb_array_t<U> &o) |
David Corbett | b854d4f | 2019-05-10 22:51:49 -0400 | [diff] [blame] | 58 | { arrayZ = o.arrayZ; length = o.length; backwards_length = o.backwards_length; return *this; } |
Behdad Esfahbod | 91d958a | 2019-04-18 10:04:10 -0400 | [diff] [blame] | 59 | |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 60 | /* |
| 61 | * Iterator implementation. |
| 62 | */ |
Behdad Esfahbod | 636786e | 2019-01-08 23:48:35 -0800 | [diff] [blame] | 63 | typedef Type& __item_t__; |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 64 | static constexpr bool is_random_access_iterator = true; |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 65 | Type& __item_at__ (unsigned i) const |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 66 | { |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 67 | if (unlikely (i >= length)) return CrapOrNull (Type); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 68 | return arrayZ[i]; |
| 69 | } |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 70 | void __forward__ (unsigned n) |
| 71 | { |
| 72 | if (unlikely (n > length)) |
| 73 | n = length; |
| 74 | length -= n; |
David Corbett | b854d4f | 2019-05-10 22:51:49 -0400 | [diff] [blame] | 75 | backwards_length += n; |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 76 | arrayZ += n; |
| 77 | } |
| 78 | void __rewind__ (unsigned n) |
| 79 | { |
David Corbett | b854d4f | 2019-05-10 22:51:49 -0400 | [diff] [blame] | 80 | if (unlikely (n > backwards_length)) |
| 81 | n = backwards_length; |
| 82 | length += n; |
| 83 | backwards_length -= n; |
| 84 | arrayZ -= n; |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 85 | } |
| 86 | unsigned __len__ () const { return length; } |
Behdad Esfahbod | d822e0a | 2019-05-15 16:30:08 -0700 | [diff] [blame] | 87 | /* Ouch. The operator== compares the contents of the array. For range-based for loops, |
| 88 | * it's best if we can just compare arrayZ, though comparing contents is still fast, |
| 89 | * but also would require that Type has operator==. As such, we optimize this operator |
| 90 | * for range-based for loop and just compare arrayZ. No need to compare length, as we |
| 91 | * assume we're only compared to .end(). */ |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 92 | bool operator != (const hb_array_t& o) const |
Behdad Esfahbod | d822e0a | 2019-05-15 16:30:08 -0700 | [diff] [blame] | 93 | { return arrayZ != o.arrayZ; } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 94 | |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 95 | /* Extra operators. |
| 96 | */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 97 | Type * operator & () const { return arrayZ; } |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 98 | 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] | 99 | template <typename T> operator T * () const { return arrayZ; } |
Behdad Esfahbod | 94e72cf | 2018-12-17 00:06:40 -0500 | [diff] [blame] | 100 | |
Behdad Esfahbod | caa20e4 | 2019-04-12 17:59:18 -0400 | [diff] [blame] | 101 | HB_INTERNAL bool operator == (const hb_array_t &o) const; |
| 102 | HB_INTERNAL uint32_t hash () const; |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 103 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 104 | /* |
| 105 | * Compare, Sort, and Search. |
| 106 | */ |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 107 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 108 | /* Note: our compare is NOT lexicographic; it also does NOT call Type::cmp. */ |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 109 | int cmp (const hb_array_t &a) const |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 110 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 111 | if (length != a.length) |
| 112 | return (int) a.length - (int) length; |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 113 | return hb_memcmp (a.arrayZ, arrayZ, get_size ()); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 114 | } |
Behdad Esfahbod | 95df00a | 2019-04-12 17:50:03 -0400 | [diff] [blame] | 115 | HB_INTERNAL static int cmp (const void *pa, const void *pb) |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 116 | { |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 117 | hb_array_t *a = (hb_array_t *) pa; |
| 118 | hb_array_t *b = (hb_array_t *) pb; |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 119 | return b->cmp (*a); |
| 120 | } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 121 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 122 | template <typename T> |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 123 | Type *lsearch (const T &x, Type *not_found = nullptr) |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 124 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 125 | unsigned int count = length; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 126 | for (unsigned int i = 0; i < count; i++) |
| 127 | if (!this->arrayZ[i].cmp (x)) |
| 128 | return &this->arrayZ[i]; |
| 129 | return not_found; |
| 130 | } |
| 131 | template <typename T> |
| 132 | const Type *lsearch (const T &x, const Type *not_found = nullptr) const |
| 133 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 134 | unsigned int count = length; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 135 | for (unsigned int i = 0; i < count; i++) |
| 136 | if (!this->arrayZ[i].cmp (x)) |
| 137 | return &this->arrayZ[i]; |
| 138 | return not_found; |
| 139 | } |
| 140 | |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 141 | hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*)) |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 142 | { |
Behdad Esfahbod | 89949ed | 2018-12-30 01:52:19 -0500 | [diff] [blame] | 143 | if (likely (length)) |
Ebrahim Byagowi | eff579f | 2019-06-07 12:58:09 +0430 | [diff] [blame] | 144 | hb_qsort (arrayZ, length, this->item_size, cmp_); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 145 | return hb_sorted_array_t<Type> (*this); |
| 146 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 147 | hb_sorted_array_t<Type> qsort () |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 148 | { |
Behdad Esfahbod | 89949ed | 2018-12-30 01:52:19 -0500 | [diff] [blame] | 149 | if (likely (length)) |
Ebrahim Byagowi | eff579f | 2019-06-07 12:58:09 +0430 | [diff] [blame] | 150 | hb_qsort (arrayZ, length, this->item_size, Type::cmp); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 151 | return hb_sorted_array_t<Type> (*this); |
| 152 | } |
| 153 | void qsort (unsigned int start, unsigned int end) |
| 154 | { |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 155 | end = hb_min (end, length); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 156 | assert (start <= end); |
Behdad Esfahbod | 89949ed | 2018-12-30 01:52:19 -0500 | [diff] [blame] | 157 | if (likely (start < end)) |
Ebrahim Byagowi | eff579f | 2019-06-07 12:58:09 +0430 | [diff] [blame] | 158 | hb_qsort (arrayZ + start, end - start, this->item_size, Type::cmp); |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 161 | /* |
| 162 | * Other methods. |
| 163 | */ |
| 164 | |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 165 | unsigned int get_size () const { return length * this->item_size; } |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 166 | |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 167 | hb_array_t sub_array (unsigned int start_offset = 0, unsigned int *seg_count = nullptr /* IN/OUT */) const |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 168 | { |
| 169 | if (!start_offset && !seg_count) |
| 170 | return *this; |
| 171 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 172 | unsigned int count = length; |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 173 | if (unlikely (start_offset > count)) |
| 174 | count = 0; |
| 175 | else |
| 176 | count -= start_offset; |
| 177 | if (seg_count) |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 178 | count = *seg_count = hb_min (count, *seg_count); |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 179 | return hb_array_t (arrayZ + start_offset, count); |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 180 | } |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 181 | hb_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 182 | { return sub_array (start_offset, &seg_count); } |
| 183 | |
Ebrahim Byagowi | dce42ce | 2019-08-27 14:32:05 +0430 | [diff] [blame] | 184 | template <typename T, |
| 185 | unsigned P = sizeof (Type), |
| 186 | hb_enable_if (P == 1)> |
| 187 | const T *as () const |
| 188 | { return length < hb_null_size (T) ? &Null (T) : reinterpret_cast<const T *> (arrayZ); } |
| 189 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 190 | /* Only call if you allocated the underlying array using malloc() or similar. */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 191 | void free () |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 192 | { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 193 | |
Behdad Esfahbod | e8b45c1 | 2019-05-08 16:37:38 -0700 | [diff] [blame] | 194 | template <typename hb_serialize_context_t> |
| 195 | hb_array_t copy (hb_serialize_context_t *c) const |
| 196 | { |
| 197 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | 3476445 | 2019-05-08 21:14:01 -0700 | [diff] [blame] | 198 | auto* out = c->start_embed (arrayZ); |
Behdad Esfahbod | e8b45c1 | 2019-05-08 16:37:38 -0700 | [diff] [blame] | 199 | if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ()); |
| 200 | for (unsigned i = 0; i < length; i++) |
| 201 | out[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */ |
| 202 | return_trace (hb_array_t (out, length)); |
| 203 | } |
| 204 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 205 | template <typename hb_sanitize_context_t> |
| 206 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 207 | { return c->check_array (arrayZ, length); } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 208 | |
Behdad Esfahbod | a4354d2 | 2018-12-16 23:57:27 -0500 | [diff] [blame] | 209 | /* |
| 210 | * Members |
| 211 | */ |
| 212 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 213 | public: |
| 214 | Type *arrayZ; |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 215 | unsigned int length; |
David Corbett | b854d4f | 2019-05-10 22:51:49 -0400 | [diff] [blame] | 216 | unsigned int backwards_length; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 217 | }; |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 218 | template <typename T> inline hb_array_t<T> |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 219 | hb_array (T *array, unsigned int length) |
| 220 | { return hb_array_t<T> (array, length); } |
| 221 | template <typename T, unsigned int length_> inline hb_array_t<T> |
| 222 | hb_array (T (&array_)[length_]) |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 223 | { return hb_array_t<T> (array_); } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 224 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 225 | enum hb_bfind_not_found_t |
| 226 | { |
| 227 | HB_BFIND_NOT_FOUND_DONT_STORE, |
| 228 | HB_BFIND_NOT_FOUND_STORE, |
| 229 | HB_BFIND_NOT_FOUND_STORE_CLOSEST, |
| 230 | }; |
| 231 | |
| 232 | template <typename Type> |
Behdad Esfahbod | 25786f4 | 2018-12-21 19:29:00 -0500 | [diff] [blame] | 233 | struct hb_sorted_array_t : |
Behdad Esfahbod | b5d6fe1 | 2019-01-02 16:20:40 -0500 | [diff] [blame] | 234 | hb_iter_t<hb_sorted_array_t<Type>, Type&>, |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 235 | hb_array_t<Type> |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 236 | { |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 237 | typedef hb_iter_t<hb_sorted_array_t, Type&> iter_base_t; |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 238 | HB_ITER_USING (iter_base_t); |
Behdad Esfahbod | 090fe56 | 2019-01-25 15:34:03 +0100 | [diff] [blame] | 239 | static constexpr bool is_random_access_iterator = true; |
Behdad Esfahbod | 889dc1e | 2019-05-14 22:28:07 -0700 | [diff] [blame] | 240 | static constexpr bool is_sorted_iterator = true; |
Behdad Esfahbod | 570473a | 2018-12-27 13:29:51 -0500 | [diff] [blame] | 241 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 242 | hb_sorted_array_t () : hb_array_t<Type> () {} |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 243 | hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {} |
Behdad Esfahbod | 322627a | 2019-05-09 16:08:10 -0700 | [diff] [blame] | 244 | template <unsigned int length_> |
| 245 | hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {} |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 246 | |
Behdad Esfahbod | c51f15d | 2019-04-26 13:03:41 -0700 | [diff] [blame] | 247 | template <typename U, |
Behdad Esfahbod | 726002a | 2019-05-09 14:53:02 -0700 | [diff] [blame] | 248 | hb_enable_if (hb_is_cr_convertible(U, Type))> |
Behdad Esfahbod | c51f15d | 2019-04-26 13:03:41 -0700 | [diff] [blame] | 249 | hb_sorted_array_t (const hb_array_t<U> &o) : |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 250 | hb_iter_t<hb_sorted_array_t, Type&> (), |
Behdad Esfahbod | c51f15d | 2019-04-26 13:03:41 -0700 | [diff] [blame] | 251 | hb_array_t<Type> (o) {} |
| 252 | template <typename U, |
Behdad Esfahbod | 726002a | 2019-05-09 14:53:02 -0700 | [diff] [blame] | 253 | hb_enable_if (hb_is_cr_convertible(U, Type))> |
Behdad Esfahbod | c51f15d | 2019-04-26 13:03:41 -0700 | [diff] [blame] | 254 | hb_sorted_array_t& operator = (const hb_array_t<U> &o) |
| 255 | { hb_array_t<Type> (*this) = o; return *this; } |
| 256 | |
Behdad Esfahbod | 4c2fd05 | 2019-05-06 19:57:15 -0700 | [diff] [blame] | 257 | /* Iterator implementation. */ |
| 258 | bool operator != (const hb_sorted_array_t& o) const |
| 259 | { return this->arrayZ != o.arrayZ || this->length != o.length; } |
| 260 | |
Behdad Esfahbod | a06edf1 | 2019-08-29 15:21:18 -0700 | [diff] [blame^] | 261 | hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int *seg_count /* IN/OUT */) const |
| 262 | { return hb_sorted_array_t (((const hb_array_t<Type> *) (this))->sub_array (start_offset, seg_count)); } |
| 263 | hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 264 | { return sub_array (start_offset, &seg_count); } |
| 265 | |
| 266 | template <typename T> |
| 267 | Type *bsearch (const T &x, Type *not_found = nullptr) |
| 268 | { |
| 269 | unsigned int i; |
| 270 | return bfind (x, &i) ? &this->arrayZ[i] : not_found; |
| 271 | } |
| 272 | template <typename T> |
| 273 | const Type *bsearch (const T &x, const Type *not_found = nullptr) const |
| 274 | { |
| 275 | unsigned int i; |
| 276 | return bfind (x, &i) ? &this->arrayZ[i] : not_found; |
| 277 | } |
| 278 | template <typename T> |
| 279 | bool bfind (const T &x, unsigned int *i = nullptr, |
| 280 | hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, |
| 281 | unsigned int to_store = (unsigned int) -1) const |
| 282 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 283 | int min = 0, max = (int) this->length - 1; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 284 | const Type *array = this->arrayZ; |
| 285 | while (min <= max) |
| 286 | { |
| 287 | int mid = ((unsigned int) min + (unsigned int) max) / 2; |
| 288 | int c = array[mid].cmp (x); |
| 289 | if (c < 0) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 290 | max = mid - 1; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 291 | else if (c > 0) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 292 | min = mid + 1; |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 293 | else |
| 294 | { |
| 295 | if (i) |
| 296 | *i = mid; |
| 297 | return true; |
| 298 | } |
| 299 | } |
| 300 | if (i) |
| 301 | { |
| 302 | switch (not_found) |
| 303 | { |
| 304 | case HB_BFIND_NOT_FOUND_DONT_STORE: |
| 305 | break; |
| 306 | |
| 307 | case HB_BFIND_NOT_FOUND_STORE: |
| 308 | *i = to_store; |
| 309 | break; |
| 310 | |
| 311 | case HB_BFIND_NOT_FOUND_STORE_CLOSEST: |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 312 | if (max < 0 || (max < (int) this->length && array[max].cmp (x) > 0)) |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 313 | max++; |
| 314 | *i = max; |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | }; |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 321 | template <typename T> inline hb_sorted_array_t<T> |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 322 | hb_sorted_array (T *array, unsigned int length) |
| 323 | { return hb_sorted_array_t<T> (array, length); } |
| 324 | template <typename T, unsigned int length_> inline hb_sorted_array_t<T> |
| 325 | hb_sorted_array (T (&array_)[length_]) |
Behdad Esfahbod | 7b4eea8 | 2018-12-21 16:02:16 -0500 | [diff] [blame] | 326 | { return hb_sorted_array_t<T> (array_); } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 327 | |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 328 | template <typename T> |
Behdad Esfahbod | 5b66b03 | 2019-04-02 19:27:02 -0700 | [diff] [blame] | 329 | bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const |
| 330 | { |
| 331 | return length == o.length && |
| 332 | + hb_zip (*this, o) |
Behdad Esfahbod | d0df996 | 2019-05-15 00:32:41 -0700 | [diff] [blame] | 333 | | hb_map ([] (hb_pair_t<T&, T&> &&_) { return _.first == _.second; }) |
Behdad Esfahbod | 5b66b03 | 2019-04-02 19:27:02 -0700 | [diff] [blame] | 334 | | hb_all |
| 335 | ; |
| 336 | } |
| 337 | template <typename T> |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 338 | uint32_t hb_array_t<T>::hash () const |
| 339 | { |
Behdad Esfahbod | d0da547 | 2019-04-02 18:22:39 -0700 | [diff] [blame] | 340 | return |
Behdad Esfahbod | 42ab32c | 2019-04-02 18:41:33 -0700 | [diff] [blame] | 341 | + hb_iter (*this) |
Behdad Esfahbod | d0da547 | 2019-04-02 18:22:39 -0700 | [diff] [blame] | 342 | | hb_map (hb_hash) |
Behdad Esfahbod | d0df996 | 2019-05-15 00:32:41 -0700 | [diff] [blame] | 343 | | hb_reduce ([] (uint32_t a, uint32_t b) { return a * 31 + b; }, 0) |
Behdad Esfahbod | d0da547 | 2019-04-02 18:22:39 -0700 | [diff] [blame] | 344 | ; |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 345 | } |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 346 | |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 347 | typedef hb_array_t<const char> hb_bytes_t; |
Behdad Esfahbod | 3d9d7dc | 2018-12-18 22:11:23 -0500 | [diff] [blame] | 348 | typedef hb_array_t<const unsigned char> hb_ubytes_t; |
Behdad Esfahbod | dcfa4a8 | 2018-12-16 20:40:07 -0500 | [diff] [blame] | 349 | |
Behdad Esfahbod | 5b66b03 | 2019-04-02 19:27:02 -0700 | [diff] [blame] | 350 | /* TODO Specialize opeator==/hash() for hb_bytes_t and hb_ubytes_t. */ |
Behdad Esfahbod | b189bbc | 2019-03-30 19:41:48 -0700 | [diff] [blame] | 351 | //template <> |
| 352 | //uint32_t hb_array_t<const char>::hash () const { return 0; } |
Behdad Esfahbod | 9268036 | 2018-12-16 23:38:51 -0500 | [diff] [blame] | 353 | |
Behdad Esfahbod | 5a552f7 | 2018-12-16 20:07:44 -0500 | [diff] [blame] | 354 | #endif /* HB_ARRAY_HH */ |