2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
36 template <typename Type>
37 struct hb_sorted_array_t;
39 template <typename Type>
40 struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
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) {}
47 template <unsigned int length_>
48 hb_array_t (Type (&array_)[length_]) : arrayZ (array_), length (length_), backwards_length (0) {}
51 hb_enable_if (hb_is_cr_convertible(U, Type))>
52 hb_array_t (const hb_array_t<U> &o) :
53 hb_iter_with_fallback_t<hb_array_t, Type&> (),
54 arrayZ (o.arrayZ), length (o.length), backwards_length (o.backwards_length) {}
56 hb_enable_if (hb_is_cr_convertible(U, Type))>
57 hb_array_t& operator = (const hb_array_t<U> &o)
58 { arrayZ = o.arrayZ; length = o.length; backwards_length = o.backwards_length; return *this; }
61 * Iterator implementation.
63 typedef Type& __item_t__;
64 static constexpr bool is_random_access_iterator = true;
65 Type& __item_at__ (unsigned i) const
67 if (unlikely (i >= length)) return CrapOrNull (Type);
70 void __forward__ (unsigned n)
72 if (unlikely (n > length))
75 backwards_length += n;
78 void __rewind__ (unsigned n)
80 if (unlikely (n > backwards_length))
83 backwards_length -= n;
86 unsigned __len__ () const { return length; }
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(). */
92 bool operator != (const hb_array_t& o) const
93 { return arrayZ != o.arrayZ; }
97 Type * operator & () const { return arrayZ; }
98 operator hb_array_t<const Type> () { return hb_array_t<const Type> (arrayZ, length); }
99 template <typename T> operator T * () const { return arrayZ; }
101 HB_INTERNAL bool operator == (const hb_array_t &o) const;
103 uint32_t hash () const {
104 uint32_t current = 0;
105 for (unsigned int i = 0; i < this->length; i++) {
106 current = current * 31 + hb_hash (this->arrayZ[i]);
112 * Compare, Sort, and Search.
115 /* Note: our compare is NOT lexicographic; it also does NOT call Type::cmp. */
116 int cmp (const hb_array_t &a) const
118 if (length != a.length)
119 return (int) a.length - (int) length;
120 return hb_memcmp (a.arrayZ, arrayZ, get_size ());
122 HB_INTERNAL static int cmp (const void *pa, const void *pb)
124 hb_array_t *a = (hb_array_t *) pa;
125 hb_array_t *b = (hb_array_t *) pb;
129 template <typename T>
130 Type *lsearch (const T &x, Type *not_found = nullptr)
132 unsigned int count = length;
133 for (unsigned int i = 0; i < count; i++)
134 if (!this->arrayZ[i].cmp (x))
135 return &this->arrayZ[i];
138 template <typename T>
139 const Type *lsearch (const T &x, const Type *not_found = nullptr) const
141 unsigned int count = length;
142 for (unsigned int i = 0; i < count; i++)
143 if (!this->arrayZ[i].cmp (x))
144 return &this->arrayZ[i];
148 hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*))
151 hb_qsort (arrayZ, length, this->get_item_size (), cmp_);
152 return hb_sorted_array_t<Type> (*this);
154 hb_sorted_array_t<Type> qsort ()
157 hb_qsort (arrayZ, length, this->get_item_size (), Type::cmp);
158 return hb_sorted_array_t<Type> (*this);
160 void qsort (unsigned int start, unsigned int end)
162 end = hb_min (end, length);
163 assert (start <= end);
164 if (likely (start < end))
165 hb_qsort (arrayZ + start, end - start, this->get_item_size (), Type::cmp);
172 unsigned int get_size () const { return length * this->get_item_size (); }
175 * Reverse the order of items in this array in the range [start, end).
177 void reverse (unsigned start = 0, unsigned end = -1)
179 start = hb_min (start, length);
180 end = hb_min (end, length);
185 for (unsigned lhs = start, rhs = end - 1; lhs < rhs; lhs++, rhs--) {
186 Type temp = arrayZ[rhs];
187 arrayZ[rhs] = arrayZ[lhs];
192 hb_array_t sub_array (unsigned int start_offset = 0, unsigned int *seg_count = nullptr /* IN/OUT */) const
194 if (!start_offset && !seg_count)
197 unsigned int count = length;
198 if (unlikely (start_offset > count))
201 count -= start_offset;
203 count = *seg_count = hb_min (count, *seg_count);
204 return hb_array_t (arrayZ + start_offset, count);
206 hb_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const
207 { return sub_array (start_offset, &seg_count); }
209 hb_array_t truncate (unsigned length) const { return sub_array (0, length); }
211 template <typename T,
212 unsigned P = sizeof (Type),
213 hb_enable_if (P == 1)>
215 { return length < hb_null_size (T) ? &Null (T) : reinterpret_cast<const T *> (arrayZ); }
217 template <typename T,
218 unsigned P = sizeof (Type),
219 hb_enable_if (P == 1)>
220 bool check_range (const T *p, unsigned int size = T::static_size) const
222 return arrayZ <= ((const char *) p)
223 && ((const char *) p) <= arrayZ + length
224 && (unsigned int) (arrayZ + length - (const char *) p) >= size;
227 /* Only call if you allocated the underlying array using malloc() or similar. */
229 { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
231 template <typename hb_serialize_context_t>
232 hb_array_t copy (hb_serialize_context_t *c) const
234 TRACE_SERIALIZE (this);
235 auto* out = c->start_embed (arrayZ);
236 if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ());
237 for (unsigned i = 0; i < length; i++)
238 out[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */
239 return_trace (hb_array_t (out, length));
242 template <typename hb_sanitize_context_t>
243 bool sanitize (hb_sanitize_context_t *c) const
244 { return c->check_array (arrayZ, length); }
253 unsigned int backwards_length;
255 template <typename T> inline hb_array_t<T>
256 hb_array (T *array, unsigned int length)
257 { return hb_array_t<T> (array, length); }
258 template <typename T, unsigned int length_> inline hb_array_t<T>
259 hb_array (T (&array_)[length_])
260 { return hb_array_t<T> (array_); }
262 enum hb_bfind_not_found_t
264 HB_BFIND_NOT_FOUND_DONT_STORE,
265 HB_BFIND_NOT_FOUND_STORE,
266 HB_BFIND_NOT_FOUND_STORE_CLOSEST,
269 template <typename Type>
270 struct hb_sorted_array_t :
271 hb_iter_t<hb_sorted_array_t<Type>, Type&>,
274 typedef hb_iter_t<hb_sorted_array_t, Type&> iter_base_t;
275 HB_ITER_USING (iter_base_t);
276 static constexpr bool is_random_access_iterator = true;
277 static constexpr bool is_sorted_iterator = true;
279 hb_sorted_array_t () : hb_array_t<Type> () {}
280 hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {}
281 template <unsigned int length_>
282 hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {}
284 template <typename U,
285 hb_enable_if (hb_is_cr_convertible(U, Type))>
286 hb_sorted_array_t (const hb_array_t<U> &o) :
287 hb_iter_t<hb_sorted_array_t, Type&> (),
288 hb_array_t<Type> (o) {}
289 template <typename U,
290 hb_enable_if (hb_is_cr_convertible(U, Type))>
291 hb_sorted_array_t& operator = (const hb_array_t<U> &o)
292 { hb_array_t<Type> (*this) = o; return *this; }
294 /* Iterator implementation. */
295 bool operator != (const hb_sorted_array_t& o) const
296 { return this->arrayZ != o.arrayZ || this->length != o.length; }
298 hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int *seg_count /* IN/OUT */) const
299 { return hb_sorted_array_t (((const hb_array_t<Type> *) (this))->sub_array (start_offset, seg_count)); }
300 hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const
301 { return sub_array (start_offset, &seg_count); }
303 hb_sorted_array_t truncate (unsigned length) const { return sub_array (0, length); }
305 template <typename T>
306 Type *bsearch (const T &x, Type *not_found = nullptr)
309 return bfind (x, &i) ? &this->arrayZ[i] : not_found;
311 template <typename T>
312 const Type *bsearch (const T &x, const Type *not_found = nullptr) const
315 return bfind (x, &i) ? &this->arrayZ[i] : not_found;
317 template <typename T>
318 bool bfind (const T &x, unsigned int *i = nullptr,
319 hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
320 unsigned int to_store = (unsigned int) -1) const
324 if (bsearch_impl (x, &pos))
335 case HB_BFIND_NOT_FOUND_DONT_STORE:
338 case HB_BFIND_NOT_FOUND_STORE:
342 case HB_BFIND_NOT_FOUND_STORE_CLOSEST:
349 template <typename T>
350 bool bsearch_impl (const T &x, unsigned *pos) const
352 return hb_bsearch_impl (pos,
357 _hb_cmp_method<T, Type>);
360 template <typename T> inline hb_sorted_array_t<T>
361 hb_sorted_array (T *array, unsigned int length)
362 { return hb_sorted_array_t<T> (array, length); }
363 template <typename T, unsigned int length_> inline hb_sorted_array_t<T>
364 hb_sorted_array (T (&array_)[length_])
365 { return hb_sorted_array_t<T> (array_); }
367 template <typename T>
368 bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const
370 if (o.length != this->length) return false;
371 for (unsigned int i = 0; i < this->length; i++) {
372 if (this->arrayZ[i] != o.arrayZ[i]) return false;
377 /* TODO Specialize opeator== for hb_bytes_t and hb_ubytes_t. */
380 inline uint32_t hb_array_t<const char>::hash () const {
381 uint32_t current = 0;
382 for (unsigned int i = 0; i < this->length; i++)
383 current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
388 inline uint32_t hb_array_t<const unsigned char>::hash () const {
389 uint32_t current = 0;
390 for (unsigned int i = 0; i < this->length; i++)
391 current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
396 typedef hb_array_t<const char> hb_bytes_t;
397 typedef hb_array_t<const unsigned char> hb_ubytes_t;
401 #endif /* HB_ARRAY_HH */