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"
31#include "hb-algs.hh"
32#include "hb-iter.hh"
33#include "hb-null.hh"
34
35
36template <typename Type>
37struct hb_sorted_array_t;
38
39template <typename Type>
40struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
41{
42 /*
43 * Constructors.
44 */
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) {}
49
50 template <typename U,
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) {}
55 template <typename U,
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; }
59
60 /*
61 * Iterator implementation.
62 */
63 typedef Type& __item_t__;
64 static constexpr bool is_random_access_iterator = true;
65 Type& __item_at__ (unsigned i) const
66 {
67 if (unlikely (i >= length)) return CrapOrNull (Type);
68 return arrayZ[i];
69 }
70 void __forward__ (unsigned n)
71 {
72 if (unlikely (n > length))
73 n = length;
74 length -= n;
75 backwards_length += n;
76 arrayZ += n;
77 }
78 void __rewind__ (unsigned n)
79 {
80 if (unlikely (n > backwards_length))
81 n = backwards_length;
82 length += n;
83 backwards_length -= n;
84 arrayZ -= n;
85 }
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; }
94
95 /* Extra operators.
96 */
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; }
100
101 HB_INTERNAL bool operator == (const hb_array_t &o) const;
102
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]);
107 }
108 return current;
109 }
110
111 /*
112 * Compare, Sort, and Search.
113 */
114
115 /* Note: our compare is NOT lexicographic; it also does NOT call Type::cmp. */
116 int cmp (const hb_array_t &a) const
117 {
118 if (length != a.length)
119 return (int) a.length - (int) length;
120 return hb_memcmp (a.arrayZ, arrayZ, get_size ());
121 }
122 HB_INTERNAL static int cmp (const void *pa, const void *pb)
123 {
124 hb_array_t *a = (hb_array_t *) pa;
125 hb_array_t *b = (hb_array_t *) pb;
126 return b->cmp (*a);
127 }
128
129 template <typename T>
130 Type *lsearch (const T &x, Type *not_found = nullptr)
131 {
132 unsigned i;
133 return lfind (x, &i) ? &this->arrayZ[i] : not_found;
134 }
135 template <typename T>
136 const Type *lsearch (const T &x, const Type *not_found = nullptr) const
137 {
138 unsigned i;
139 return lfind (x, &i) ? &this->arrayZ[i] : not_found;
140 }
141 template <typename T>
142 bool lfind (const T &x, unsigned *pos = nullptr) const
143 {
144 for (unsigned i = 0; i < length; ++i)
145 if (!this->arrayZ[i].cmp (x))
146 {
147 if (pos)
148 *pos = i;
149 return true;
150 }
151
152 return false;
153 }
154
155 hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*))
156 {
157 if (likely (length))
158 hb_qsort (arrayZ, length, this->get_item_size (), cmp_);
159 return hb_sorted_array_t<Type> (*this);
160 }
161 hb_sorted_array_t<Type> qsort ()
162 {
163 if (likely (length))
164 hb_qsort (arrayZ, length, this->get_item_size (), Type::cmp);
165 return hb_sorted_array_t<Type> (*this);
166 }
167 void qsort (unsigned int start, unsigned int end)
168 {
169 end = hb_min (end, length);
170 assert (start <= end);
171 if (likely (start < end))
172 hb_qsort (arrayZ + start, end - start, this->get_item_size (), Type::cmp);
173 }
174
175 /*
176 * Other methods.
177 */
178
179 unsigned int get_size () const { return length * this->get_item_size (); }
180
181 /*
182 * Reverse the order of items in this array in the range [start, end).
183 */
184 void reverse (unsigned start = 0, unsigned end = -1)
185 {
186 start = hb_min (start, length);
187 end = hb_min (end, length);
188
189 if (end < start + 2)
190 return;
191
192 for (unsigned lhs = start, rhs = end - 1; lhs < rhs; lhs++, rhs--) {
193 Type temp = arrayZ[rhs];
194 arrayZ[rhs] = arrayZ[lhs];
195 arrayZ[lhs] = temp;
196 }
197 }
198
199 hb_array_t sub_array (unsigned int start_offset = 0, unsigned int *seg_count = nullptr /* IN/OUT */) const
200 {
201 if (!start_offset && !seg_count)
202 return *this;
203
204 unsigned int count = length;
205 if (unlikely (start_offset > count))
206 count = 0;
207 else
208 count -= start_offset;
209 if (seg_count)
210 count = *seg_count = hb_min (count, *seg_count);
211 return hb_array_t (arrayZ + start_offset, count);
212 }
213 hb_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const
214 { return sub_array (start_offset, &seg_count); }
215
216 hb_array_t truncate (unsigned length) const { return sub_array (0, length); }
217
218 template <typename T,
219 unsigned P = sizeof (Type),
220 hb_enable_if (P == 1)>
221 const T *as () const
222 { return length < hb_null_size (T) ? &Null (T) : reinterpret_cast<const T *> (arrayZ); }
223
224 template <typename T,
225 unsigned P = sizeof (Type),
226 hb_enable_if (P == 1)>
227 bool check_range (const T *p, unsigned int size = T::static_size) const
228 {
229 return arrayZ <= ((const char *) p)
230 && ((const char *) p) <= arrayZ + length
231 && (unsigned int) (arrayZ + length - (const char *) p) >= size;
232 }
233
234 /* Only call if you allocated the underlying array using malloc() or similar. */
235 void free ()
236 { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
237
238 template <typename hb_serialize_context_t>
239 hb_array_t copy (hb_serialize_context_t *c) const
240 {
241 TRACE_SERIALIZE (this);
242 auto* out = c->start_embed (arrayZ);
243 if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ());
244 for (unsigned i = 0; i < length; i++)
245 out[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */
246 return_trace (hb_array_t (out, length));
247 }
248
249 template <typename hb_sanitize_context_t>
250 bool sanitize (hb_sanitize_context_t *c) const
251 { return c->check_array (arrayZ, length); }
252
253 /*
254 * Members
255 */
256
257 public:
258 Type *arrayZ;
259 unsigned int length;
260 unsigned int backwards_length;
261};
262template <typename T> inline hb_array_t<T>
263hb_array (T *array, unsigned int length)
264{ return hb_array_t<T> (array, length); }
265template <typename T, unsigned int length_> inline hb_array_t<T>
266hb_array (T (&array_)[length_])
267{ return hb_array_t<T> (array_); }
268
269enum hb_bfind_not_found_t
270{
271 HB_BFIND_NOT_FOUND_DONT_STORE,
272 HB_BFIND_NOT_FOUND_STORE,
273 HB_BFIND_NOT_FOUND_STORE_CLOSEST,
274};
275
276template <typename Type>
277struct hb_sorted_array_t :
278 hb_iter_t<hb_sorted_array_t<Type>, Type&>,
279 hb_array_t<Type>
280{
281 typedef hb_iter_t<hb_sorted_array_t, Type&> iter_base_t;
282 HB_ITER_USING (iter_base_t);
283 static constexpr bool is_random_access_iterator = true;
284 static constexpr bool is_sorted_iterator = true;
285
286 hb_sorted_array_t () : hb_array_t<Type> () {}
287 hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {}
288 template <unsigned int length_>
289 hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {}
290
291 template <typename U,
292 hb_enable_if (hb_is_cr_convertible(U, Type))>
293 hb_sorted_array_t (const hb_array_t<U> &o) :
294 hb_iter_t<hb_sorted_array_t, Type&> (),
295 hb_array_t<Type> (o) {}
296 template <typename U,
297 hb_enable_if (hb_is_cr_convertible(U, Type))>
298 hb_sorted_array_t& operator = (const hb_array_t<U> &o)
299 { hb_array_t<Type> (*this) = o; return *this; }
300
301 /* Iterator implementation. */
302 bool operator != (const hb_sorted_array_t& o) const
303 { return this->arrayZ != o.arrayZ || this->length != o.length; }
304
305 hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int *seg_count /* IN/OUT */) const
306 { return hb_sorted_array_t (((const hb_array_t<Type> *) (this))->sub_array (start_offset, seg_count)); }
307 hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const
308 { return sub_array (start_offset, &seg_count); }
309
310 hb_sorted_array_t truncate (unsigned length) const { return sub_array (0, length); }
311
312 template <typename T>
313 Type *bsearch (const T &x, Type *not_found = nullptr)
314 {
315 unsigned int i;
316 return bfind (x, &i) ? &this->arrayZ[i] : not_found;
317 }
318 template <typename T>
319 const Type *bsearch (const T &x, const Type *not_found = nullptr) const
320 {
321 unsigned int i;
322 return bfind (x, &i) ? &this->arrayZ[i] : not_found;
323 }
324 template <typename T>
325 bool bfind (const T &x, unsigned int *i = nullptr,
326 hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
327 unsigned int to_store = (unsigned int) -1) const
328 {
329 unsigned pos;
330
331 if (bsearch_impl (x, &pos))
332 {
333 if (i)
334 *i = pos;
335 return true;
336 }
337
338 if (i)
339 {
340 switch (not_found)
341 {
342 case HB_BFIND_NOT_FOUND_DONT_STORE:
343 break;
344
345 case HB_BFIND_NOT_FOUND_STORE:
346 *i = to_store;
347 break;
348
349 case HB_BFIND_NOT_FOUND_STORE_CLOSEST:
350 *i = pos;
351 break;
352 }
353 }
354 return false;
355 }
356 template <typename T>
357 bool bsearch_impl (const T &x, unsigned *pos) const
358 {
359 return hb_bsearch_impl (pos,
360 x,
361 this->arrayZ,
362 this->length,
363 sizeof (Type),
364 _hb_cmp_method<T, Type>);
365 }
366};
367template <typename T> inline hb_sorted_array_t<T>
368hb_sorted_array (T *array, unsigned int length)
369{ return hb_sorted_array_t<T> (array, length); }
370template <typename T, unsigned int length_> inline hb_sorted_array_t<T>
371hb_sorted_array (T (&array_)[length_])
372{ return hb_sorted_array_t<T> (array_); }
373
374template <typename T>
375bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const
376{
377 if (o.length != this->length) return false;
378 for (unsigned int i = 0; i < this->length; i++) {
379 if (this->arrayZ[i] != o.arrayZ[i]) return false;
380 }
381 return true;
382}
383
384/* TODO Specialize opeator== for hb_bytes_t and hb_ubytes_t. */
385
386template <>
387inline uint32_t hb_array_t<const char>::hash () const {
388 uint32_t current = 0;
389 for (unsigned int i = 0; i < this->length; i++)
390 current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
391 return current;
392}
393
394template <>
395inline uint32_t hb_array_t<const unsigned char>::hash () const {
396 uint32_t current = 0;
397 for (unsigned int i = 0; i < this->length; i++)
398 current = current * 31 + (uint32_t) (this->arrayZ[i] * 2654435761u);
399 return current;
400}
401
402
403typedef hb_array_t<const char> hb_bytes_t;
404typedef hb_array_t<const unsigned char> hb_ubytes_t;
405
406
407
408#endif /* HB_ARRAY_HH */
409