2018-12-17 02:07:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Google, Inc.
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Google Author(s): Behdad Esfahbod
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HB_ARRAY_HH
|
|
|
|
#define HB_ARRAY_HH
|
|
|
|
|
|
|
|
#include "hb.hh"
|
2018-12-21 23:35:58 +01:00
|
|
|
#include "hb-dsalgs.hh"
|
|
|
|
#include "hb-iter.hh"
|
|
|
|
#include "hb-null.hh"
|
2018-12-17 02:07:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
struct hb_sorted_array_t;
|
|
|
|
|
|
|
|
template <typename Type>
|
2018-12-22 01:29:00 +01:00
|
|
|
struct hb_array_t :
|
2018-12-27 23:17:28 +01:00
|
|
|
hb_iter_t<hb_array_t<Type>, Type&>,
|
|
|
|
hb_iter_mixin_t<hb_array_t<Type>, Type&>
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-17 05:57:27 +01:00
|
|
|
/*
|
|
|
|
* Constructors.
|
|
|
|
*/
|
2018-12-22 00:46:51 +01:00
|
|
|
hb_array_t () : arrayZ (nullptr), length (0) {}
|
|
|
|
hb_array_t (Type *array_, unsigned int length_) : arrayZ (array_), length (length_) {}
|
|
|
|
template <unsigned int length_> hb_array_t (Type (&array_)[length_]) : arrayZ (array_), length (length_) {}
|
2018-12-17 02:07:44 +01:00
|
|
|
|
2018-12-22 01:29:00 +01:00
|
|
|
|
2018-12-17 05:57:27 +01:00
|
|
|
/*
|
2018-12-22 01:29:00 +01:00
|
|
|
* Iterator implementation.
|
2018-12-17 05:57:27 +01:00
|
|
|
*/
|
2018-12-27 23:17:28 +01:00
|
|
|
typedef Type& __item_type__;
|
2018-12-31 02:51:31 +01:00
|
|
|
enum { is_random_access_iterator = true };
|
2018-12-22 01:29:00 +01:00
|
|
|
Type& __item_at__ (unsigned i) const
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-22 01:29:00 +01:00
|
|
|
if (unlikely (i >= length)) return CrapOrNull (Type);
|
2018-12-17 02:07:44 +01:00
|
|
|
return arrayZ[i];
|
|
|
|
}
|
2018-12-22 01:29:00 +01:00
|
|
|
void __forward__ (unsigned n)
|
2018-12-17 05:45:07 +01:00
|
|
|
{
|
2018-12-22 01:29:00 +01:00
|
|
|
if (unlikely (n > length))
|
|
|
|
n = length;
|
|
|
|
length -= n;
|
|
|
|
arrayZ += n;
|
2018-12-17 05:45:07 +01:00
|
|
|
}
|
2018-12-22 01:29:00 +01:00
|
|
|
void __rewind__ (unsigned n)
|
2018-12-17 06:20:19 +01:00
|
|
|
{
|
2018-12-22 01:29:00 +01:00
|
|
|
if (unlikely (n > length))
|
|
|
|
n = length;
|
|
|
|
length -= n;
|
2018-12-17 06:20:19 +01:00
|
|
|
}
|
2018-12-22 01:29:00 +01:00
|
|
|
unsigned __len__ () const { return length; }
|
|
|
|
|
|
|
|
/* Extra operators.
|
|
|
|
*/
|
|
|
|
Type * operator & () const { return arrayZ; }
|
|
|
|
operator hb_array_t<const Type> () { return hb_array_t<const Type> (arrayZ, length); }
|
|
|
|
template <typename T> operator T * () const { return arrayZ; }
|
2018-12-17 05:45:07 +01:00
|
|
|
|
2018-12-17 05:57:27 +01:00
|
|
|
/*
|
|
|
|
* Compare, Sort, and Search.
|
|
|
|
*/
|
2018-12-17 02:07:44 +01:00
|
|
|
|
2018-12-17 05:57:27 +01:00
|
|
|
/* Note: our compare is NOT lexicographic; it also does NOT call Type::cmp. */
|
|
|
|
int cmp (const hb_array_t<Type> &a) const
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-22 00:46:51 +01:00
|
|
|
if (length != a.length)
|
|
|
|
return (int) a.length - (int) length;
|
2018-12-17 05:57:27 +01:00
|
|
|
return hb_memcmp (a.arrayZ, arrayZ, get_size ());
|
|
|
|
}
|
|
|
|
static int cmp (const void *pa, const void *pb)
|
|
|
|
{
|
|
|
|
hb_array_t<Type> *a = (hb_array_t<Type> *) pa;
|
|
|
|
hb_array_t<Type> *b = (hb_array_t<Type> *) pb;
|
|
|
|
return b->cmp (*a);
|
2018-12-17 02:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-17 02:40:07 +01:00
|
|
|
Type *lsearch (const T &x, Type *not_found = nullptr)
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-22 00:46:51 +01:00
|
|
|
unsigned int count = length;
|
2018-12-17 02:07:44 +01:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
if (!this->arrayZ[i].cmp (x))
|
|
|
|
return &this->arrayZ[i];
|
|
|
|
return not_found;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
const Type *lsearch (const T &x, const Type *not_found = nullptr) const
|
|
|
|
{
|
2018-12-22 00:46:51 +01:00
|
|
|
unsigned int count = length;
|
2018-12-17 02:07:44 +01:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
if (!this->arrayZ[i].cmp (x))
|
|
|
|
return &this->arrayZ[i];
|
|
|
|
return not_found;
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:40:07 +01:00
|
|
|
hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*))
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-30 07:52:19 +01:00
|
|
|
if (likely (length))
|
|
|
|
::qsort (arrayZ, length, this->item_size, cmp_);
|
2018-12-17 02:07:44 +01:00
|
|
|
return hb_sorted_array_t<Type> (*this);
|
|
|
|
}
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_sorted_array_t<Type> qsort ()
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-30 07:52:19 +01:00
|
|
|
if (likely (length))
|
|
|
|
::qsort (arrayZ, length, this->item_size, Type::cmp);
|
2018-12-17 02:07:44 +01:00
|
|
|
return hb_sorted_array_t<Type> (*this);
|
|
|
|
}
|
|
|
|
void qsort (unsigned int start, unsigned int end)
|
|
|
|
{
|
2018-12-22 00:46:51 +01:00
|
|
|
end = MIN (end, length);
|
2018-12-17 02:07:44 +01:00
|
|
|
assert (start <= end);
|
2018-12-30 07:52:19 +01:00
|
|
|
if (likely (start < end))
|
|
|
|
::qsort (arrayZ + start, end - start, this->item_size, Type::cmp);
|
2018-12-17 02:07:44 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 05:57:27 +01:00
|
|
|
/*
|
|
|
|
* Other methods.
|
|
|
|
*/
|
2018-12-17 02:07:44 +01:00
|
|
|
|
2018-12-22 01:29:00 +01:00
|
|
|
unsigned int get_size () const { return length * this->item_size; }
|
2018-12-17 05:57:27 +01:00
|
|
|
|
|
|
|
hb_array_t<Type> sub_array (unsigned int start_offset = 0, unsigned int *seg_count = nullptr /* IN/OUT */) const
|
2018-12-17 02:40:07 +01:00
|
|
|
{
|
2018-12-17 05:57:27 +01:00
|
|
|
if (!start_offset && !seg_count)
|
|
|
|
return *this;
|
|
|
|
|
2018-12-22 00:46:51 +01:00
|
|
|
unsigned int count = length;
|
2018-12-17 05:57:27 +01:00
|
|
|
if (unlikely (start_offset > count))
|
|
|
|
count = 0;
|
|
|
|
else
|
|
|
|
count -= start_offset;
|
|
|
|
if (seg_count)
|
|
|
|
count = *seg_count = MIN (count, *seg_count);
|
|
|
|
return hb_array_t<Type> (arrayZ + start_offset, count);
|
2018-12-17 02:40:07 +01:00
|
|
|
}
|
2018-12-17 05:57:27 +01:00
|
|
|
hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const
|
|
|
|
{ return sub_array (start_offset, &seg_count); }
|
|
|
|
|
|
|
|
/* Only call if you allocated the underlying array using malloc() or similar. */
|
2018-12-17 19:01:01 +01:00
|
|
|
void free ()
|
2018-12-22 00:46:51 +01:00
|
|
|
{ ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
|
2018-12-17 02:40:07 +01:00
|
|
|
|
2018-12-17 02:07:44 +01:00
|
|
|
template <typename hb_sanitize_context_t>
|
|
|
|
bool sanitize (hb_sanitize_context_t *c) const
|
2018-12-22 00:46:51 +01:00
|
|
|
{ return c->check_array (arrayZ, length); }
|
2018-12-17 02:07:44 +01:00
|
|
|
|
2018-12-17 05:57:27 +01:00
|
|
|
/*
|
|
|
|
* Members
|
|
|
|
*/
|
|
|
|
|
2018-12-17 02:07:44 +01:00
|
|
|
public:
|
|
|
|
Type *arrayZ;
|
2018-12-22 00:46:51 +01:00
|
|
|
unsigned int length;
|
2018-12-17 02:07:44 +01:00
|
|
|
};
|
2018-12-21 22:02:16 +01:00
|
|
|
template <typename T> inline hb_array_t<T>
|
2018-12-22 00:46:51 +01:00
|
|
|
hb_array (T *array, unsigned int length)
|
|
|
|
{ return hb_array_t<T> (array, length); }
|
|
|
|
template <typename T, unsigned int length_> inline hb_array_t<T>
|
|
|
|
hb_array (T (&array_)[length_])
|
2018-12-21 22:02:16 +01:00
|
|
|
{ return hb_array_t<T> (array_); }
|
2018-12-17 02:07:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
enum hb_bfind_not_found_t
|
|
|
|
{
|
|
|
|
HB_BFIND_NOT_FOUND_DONT_STORE,
|
|
|
|
HB_BFIND_NOT_FOUND_STORE,
|
|
|
|
HB_BFIND_NOT_FOUND_STORE_CLOSEST,
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Type>
|
2018-12-22 01:29:00 +01:00
|
|
|
struct hb_sorted_array_t :
|
2018-12-31 02:06:12 +01:00
|
|
|
hb_sorted_iter_t<hb_sorted_array_t<Type>, Type&>,
|
2018-12-27 19:29:51 +01:00
|
|
|
hb_array_t<Type>
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-31 02:06:12 +01:00
|
|
|
typedef hb_sorted_iter_t<hb_sorted_array_t<Type>, Type&> iter_base_t;
|
2018-12-27 19:29:51 +01:00
|
|
|
HB_ITER_USING (iter_base_t);
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_sorted_array_t () : hb_array_t<Type> () {}
|
2018-12-17 02:07:44 +01:00
|
|
|
hb_sorted_array_t (const hb_array_t<Type> &o) : hb_array_t<Type> (o) {}
|
2018-12-22 00:46:51 +01:00
|
|
|
hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {}
|
|
|
|
template <unsigned int length_> hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {}
|
2018-12-17 02:07:44 +01:00
|
|
|
|
|
|
|
hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int *seg_count /* IN/OUT */) const
|
|
|
|
{ return hb_sorted_array_t<Type> (((const hb_array_t<Type> *) (this))->sub_array (start_offset, seg_count)); }
|
|
|
|
hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const
|
|
|
|
{ return sub_array (start_offset, &seg_count); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Type *bsearch (const T &x, Type *not_found = nullptr)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
return bfind (x, &i) ? &this->arrayZ[i] : not_found;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
const Type *bsearch (const T &x, const Type *not_found = nullptr) const
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
return bfind (x, &i) ? &this->arrayZ[i] : not_found;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
bool bfind (const T &x, unsigned int *i = nullptr,
|
|
|
|
hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
|
|
|
|
unsigned int to_store = (unsigned int) -1) const
|
|
|
|
{
|
2018-12-22 00:46:51 +01:00
|
|
|
int min = 0, max = (int) this->length - 1;
|
2018-12-17 02:07:44 +01:00
|
|
|
const Type *array = this->arrayZ;
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
int mid = ((unsigned int) min + (unsigned int) max) / 2;
|
|
|
|
int c = array[mid].cmp (x);
|
|
|
|
if (c < 0)
|
|
|
|
max = mid - 1;
|
|
|
|
else if (c > 0)
|
|
|
|
min = mid + 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i)
|
|
|
|
*i = mid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
switch (not_found)
|
|
|
|
{
|
|
|
|
case HB_BFIND_NOT_FOUND_DONT_STORE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HB_BFIND_NOT_FOUND_STORE:
|
|
|
|
*i = to_store;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HB_BFIND_NOT_FOUND_STORE_CLOSEST:
|
2018-12-22 00:46:51 +01:00
|
|
|
if (max < 0 || (max < (int) this->length && array[max].cmp (x) > 0))
|
2018-12-17 02:07:44 +01:00
|
|
|
max++;
|
|
|
|
*i = max;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2018-12-21 22:02:16 +01:00
|
|
|
template <typename T> inline hb_sorted_array_t<T>
|
2018-12-22 00:46:51 +01:00
|
|
|
hb_sorted_array (T *array, unsigned int length)
|
|
|
|
{ return hb_sorted_array_t<T> (array, length); }
|
|
|
|
template <typename T, unsigned int length_> inline hb_sorted_array_t<T>
|
|
|
|
hb_sorted_array (T (&array_)[length_])
|
2018-12-21 22:02:16 +01:00
|
|
|
{ return hb_sorted_array_t<T> (array_); }
|
2018-12-17 02:07:44 +01:00
|
|
|
|
|
|
|
|
2018-12-17 02:40:07 +01:00
|
|
|
typedef hb_array_t<const char> hb_bytes_t;
|
2018-12-19 04:11:23 +01:00
|
|
|
typedef hb_array_t<const unsigned char> hb_ubytes_t;
|
2018-12-17 02:40:07 +01:00
|
|
|
|
2018-12-17 05:38:51 +01:00
|
|
|
|
2018-12-17 02:07:44 +01:00
|
|
|
#endif /* HB_ARRAY_HH */
|