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"
|
2019-01-09 18:05:01 +01:00
|
|
|
#include "hb-algs.hh"
|
2018-12-21 23:35:58 +01:00
|
|
|
#include "hb-iter.hh"
|
|
|
|
#include "hb-null.hh"
|
2018-12-17 02:07:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
struct hb_sorted_array_t;
|
|
|
|
|
2021-07-22 20:27:33 +02:00
|
|
|
enum hb_not_found_t
|
|
|
|
{
|
|
|
|
HB_NOT_FOUND_DONT_STORE,
|
|
|
|
HB_NOT_FOUND_STORE,
|
|
|
|
HB_NOT_FOUND_STORE_CLOSEST,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-12-17 02:07:44 +01:00
|
|
|
template <typename Type>
|
2019-01-30 02:10:19 +01:00
|
|
|
struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2018-12-17 05:57:27 +01:00
|
|
|
/*
|
|
|
|
* Constructors.
|
|
|
|
*/
|
2021-11-02 04:55:14 +01:00
|
|
|
hb_array_t () = default;
|
2021-11-19 19:49:03 +01:00
|
|
|
hb_array_t (const hb_array_t&) = default;
|
|
|
|
~hb_array_t () = default;
|
|
|
|
hb_array_t& operator= (const hb_array_t&) = default;
|
|
|
|
hb_array_t& operator= (hb_array_t&&) = default;
|
|
|
|
|
2021-11-19 20:33:37 +01:00
|
|
|
constexpr hb_array_t (Type *array_, unsigned int length_) : arrayZ (array_), length (length_) {}
|
2019-05-10 01:08:10 +02:00
|
|
|
template <unsigned int length_>
|
2021-11-19 20:33:37 +01:00
|
|
|
constexpr hb_array_t (Type (&array_)[length_]) : hb_array_t (array_, length_) {}
|
2018-12-17 02:07:44 +01:00
|
|
|
|
2019-04-18 16:04:10 +02:00
|
|
|
template <typename U,
|
2019-05-09 23:53:02 +02:00
|
|
|
hb_enable_if (hb_is_cr_convertible(U, Type))>
|
2021-11-19 20:33:37 +01:00
|
|
|
constexpr hb_array_t (const hb_array_t<U> &o) :
|
2019-08-30 00:21:18 +02:00
|
|
|
hb_iter_with_fallback_t<hb_array_t, Type&> (),
|
2019-05-11 04:51:49 +02:00
|
|
|
arrayZ (o.arrayZ), length (o.length), backwards_length (o.backwards_length) {}
|
2019-04-18 16:04:10 +02:00
|
|
|
template <typename U,
|
2019-05-09 23:53:02 +02:00
|
|
|
hb_enable_if (hb_is_cr_convertible(U, Type))>
|
2021-11-20 01:19:09 +01:00
|
|
|
hb_array_t& operator = (const hb_array_t<U> &o)
|
2019-05-11 04:51:49 +02:00
|
|
|
{ arrayZ = o.arrayZ; length = o.length; backwards_length = o.backwards_length; return *this; }
|
2019-04-18 16:04:10 +02: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
|
|
|
*/
|
2019-01-09 08:48:35 +01:00
|
|
|
typedef Type& __item_t__;
|
2019-01-25 15:34:03 +01:00
|
|
|
static constexpr bool 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;
|
2019-05-11 04:51:49 +02:00
|
|
|
backwards_length += n;
|
2018-12-22 01:29:00 +01:00
|
|
|
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
|
|
|
{
|
2019-05-11 04:51:49 +02:00
|
|
|
if (unlikely (n > backwards_length))
|
|
|
|
n = backwards_length;
|
|
|
|
length += n;
|
|
|
|
backwards_length -= n;
|
|
|
|
arrayZ -= n;
|
2018-12-17 06:20:19 +01:00
|
|
|
}
|
2018-12-22 01:29:00 +01:00
|
|
|
unsigned __len__ () const { return length; }
|
2019-05-16 01:30:08 +02:00
|
|
|
/* Ouch. The operator== compares the contents of the array. For range-based for loops,
|
|
|
|
* it's best if we can just compare arrayZ, though comparing contents is still fast,
|
|
|
|
* but also would require that Type has operator==. As such, we optimize this operator
|
2022-11-30 05:35:54 +01:00
|
|
|
* for range-based for loop and just compare arrayZ and length.
|
|
|
|
*
|
|
|
|
* The above comment is outdated now because we implemented separate begin/end to
|
|
|
|
* objects that were using hb_array_t for range-based loop before. */
|
2019-05-07 04:57:15 +02:00
|
|
|
bool operator != (const hb_array_t& o) const
|
2022-07-18 02:44:11 +02:00
|
|
|
{ return this->arrayZ != o.arrayZ || this->length != o.length; }
|
2018-12-22 01:29:00 +01:00
|
|
|
|
2022-11-21 20:00:10 +01:00
|
|
|
/* Faster range-based for loop without bounds-check. */
|
|
|
|
Type *begin () const { return arrayZ; }
|
|
|
|
Type *end () const { return arrayZ + length; }
|
|
|
|
|
|
|
|
|
2018-12-22 01:29:00 +01:00
|
|
|
/* 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
|
|
|
|
2019-04-12 23:59:18 +02:00
|
|
|
HB_INTERNAL bool operator == (const hb_array_t &o) const;
|
2019-10-22 01:10:06 +02:00
|
|
|
|
2022-11-19 01:26:58 +01:00
|
|
|
uint32_t hash () const
|
|
|
|
{
|
2022-11-22 20:23:17 +01:00
|
|
|
uint32_t current = 0;
|
|
|
|
for (auto &v : *this)
|
|
|
|
current = current * 31 + hb_hash (v);
|
|
|
|
return current;
|
2019-10-22 01:10:06 +02:00
|
|
|
}
|
2019-03-31 04:41:48 +02: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. */
|
2019-08-30 00:21:18 +02:00
|
|
|
int cmp (const hb_array_t &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 ());
|
|
|
|
}
|
2019-04-12 23:50:03 +02:00
|
|
|
HB_INTERNAL static int cmp (const void *pa, const void *pb)
|
2018-12-17 05:57:27 +01:00
|
|
|
{
|
2019-08-30 00:21:18 +02:00
|
|
|
hb_array_t *a = (hb_array_t *) pa;
|
|
|
|
hb_array_t *b = (hb_array_t *) pb;
|
2018-12-17 05:57:27 +01:00
|
|
|
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
|
|
|
{
|
2020-06-28 10:43:25 +02:00
|
|
|
unsigned i;
|
|
|
|
return lfind (x, &i) ? &this->arrayZ[i] : not_found;
|
2018-12-17 02:07:44 +01:00
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
const Type *lsearch (const T &x, const Type *not_found = nullptr) const
|
|
|
|
{
|
2020-06-28 10:43:25 +02:00
|
|
|
unsigned i;
|
|
|
|
return lfind (x, &i) ? &this->arrayZ[i] : not_found;
|
|
|
|
}
|
|
|
|
template <typename T>
|
2021-07-22 20:27:33 +02:00
|
|
|
bool lfind (const T &x, unsigned *pos = nullptr,
|
|
|
|
hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
|
|
|
|
unsigned int to_store = (unsigned int) -1) const
|
2020-06-28 10:43:25 +02:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < length; ++i)
|
2021-02-05 19:41:19 +01:00
|
|
|
if (hb_equal (x, this->arrayZ[i]))
|
2020-06-28 10:43:25 +02:00
|
|
|
{
|
|
|
|
if (pos)
|
|
|
|
*pos = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:27:33 +02:00
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
switch (not_found)
|
|
|
|
{
|
|
|
|
case HB_NOT_FOUND_DONT_STORE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HB_NOT_FOUND_STORE:
|
|
|
|
*pos = to_store;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HB_NOT_FOUND_STORE_CLOSEST:
|
|
|
|
*pos = length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 10:43:25 +02:00
|
|
|
return false;
|
2018-12-17 02:07:44 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-11-24 02:34:54 +01:00
|
|
|
//static_assert (hb_enable_if (hb_is_trivially_copy_assignable(Type)), "");
|
2018-12-30 07:52:19 +01:00
|
|
|
if (likely (length))
|
2019-08-30 00:45:21 +02:00
|
|
|
hb_qsort (arrayZ, length, this->get_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
|
|
|
{
|
2022-11-24 02:34:54 +01:00
|
|
|
//static_assert (hb_enable_if (hb_is_trivially_copy_assignable(Type)), "");
|
2018-12-30 07:52:19 +01:00
|
|
|
if (likely (length))
|
2019-08-30 00:45:21 +02:00
|
|
|
hb_qsort (arrayZ, length, this->get_item_size (), Type::cmp);
|
2018-12-17 02:07:44 +01:00
|
|
|
return hb_sorted_array_t<Type> (*this);
|
|
|
|
}
|
|
|
|
|
2018-12-17 05:57:27 +01:00
|
|
|
/*
|
|
|
|
* Other methods.
|
|
|
|
*/
|
2018-12-17 02:07:44 +01:00
|
|
|
|
2019-08-30 00:45:21 +02:00
|
|
|
unsigned int get_size () const { return length * this->get_item_size (); }
|
2018-12-17 05:57:27 +01:00
|
|
|
|
2020-02-27 00:09:04 +01:00
|
|
|
/*
|
|
|
|
* Reverse the order of items in this array in the range [start, end).
|
|
|
|
*/
|
|
|
|
void reverse (unsigned start = 0, unsigned end = -1)
|
2020-02-26 02:39:59 +01:00
|
|
|
{
|
2020-02-27 00:09:04 +01:00
|
|
|
start = hb_min (start, length);
|
|
|
|
end = hb_min (end, length);
|
|
|
|
|
|
|
|
if (end < start + 2)
|
|
|
|
return;
|
|
|
|
|
2022-07-13 23:13:07 +02:00
|
|
|
for (unsigned lhs = start, rhs = end - 1; lhs < rhs; lhs++, rhs--)
|
|
|
|
hb_swap (arrayZ[rhs], arrayZ[lhs]);
|
2020-02-26 02:39:59 +01:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:21:18 +02:00
|
|
|
hb_array_t 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)
|
2019-05-08 05:54:31 +02:00
|
|
|
count = *seg_count = hb_min (count, *seg_count);
|
2019-08-30 00:21:18 +02:00
|
|
|
return hb_array_t (arrayZ + start_offset, count);
|
2018-12-17 02:40:07 +01:00
|
|
|
}
|
2019-08-30 00:21:18 +02:00
|
|
|
hb_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const
|
2018-12-17 05:57:27 +01:00
|
|
|
{ return sub_array (start_offset, &seg_count); }
|
|
|
|
|
2019-08-30 00:23:48 +02:00
|
|
|
hb_array_t truncate (unsigned length) const { return sub_array (0, length); }
|
|
|
|
|
2019-08-27 12:02:05 +02:00
|
|
|
template <typename T,
|
|
|
|
unsigned P = sizeof (Type),
|
|
|
|
hb_enable_if (P == 1)>
|
|
|
|
const T *as () const
|
2021-04-16 21:14:48 +02:00
|
|
|
{ return length < hb_min_size (T) ? &Null (T) : reinterpret_cast<const T *> (arrayZ); }
|
2019-08-27 12:02:05 +02:00
|
|
|
|
2019-10-29 20:06:50 +01:00
|
|
|
template <typename T,
|
|
|
|
unsigned P = sizeof (Type),
|
|
|
|
hb_enable_if (P == 1)>
|
2019-12-05 10:45:21 +01:00
|
|
|
bool check_range (const T *p, unsigned int size = T::static_size) const
|
2019-10-29 20:06:50 +01:00
|
|
|
{
|
2019-12-05 10:39:48 +01:00
|
|
|
return arrayZ <= ((const char *) p)
|
|
|
|
&& ((const char *) p) <= arrayZ + length
|
|
|
|
&& (unsigned int) (arrayZ + length - (const char *) p) >= size;
|
2019-10-29 20:06:50 +01:00
|
|
|
}
|
|
|
|
|
2021-07-08 18:58:50 +02:00
|
|
|
/* Only call if you allocated the underlying array using hb_malloc() or similar. */
|
2021-07-08 18:53:45 +02:00
|
|
|
void fini ()
|
2021-07-08 18:58:50 +02:00
|
|
|
{ hb_free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
|
2018-12-17 02:40:07 +01:00
|
|
|
|
2022-11-25 22:23:57 +01:00
|
|
|
template <typename hb_serialize_context_t,
|
|
|
|
typename U = Type,
|
2022-12-02 04:19:04 +01:00
|
|
|
hb_enable_if (!(sizeof (U) < sizeof (long long) && hb_is_trivially_copy_assignable(hb_decay<Type>)))>
|
2019-05-09 01:37:38 +02:00
|
|
|
hb_array_t copy (hb_serialize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2019-05-09 06:14:01 +02:00
|
|
|
auto* out = c->start_embed (arrayZ);
|
2022-12-01 22:19:27 +01:00
|
|
|
if (unlikely (!c->extend_size (out, get_size (), false))) return_trace (hb_array_t ());
|
2019-05-09 01:37:38 +02:00
|
|
|
for (unsigned i = 0; i < length; i++)
|
|
|
|
out[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */
|
|
|
|
return_trace (hb_array_t (out, length));
|
|
|
|
}
|
|
|
|
|
2022-11-25 22:23:57 +01:00
|
|
|
template <typename hb_serialize_context_t,
|
|
|
|
typename U = Type,
|
2022-12-02 04:19:04 +01:00
|
|
|
hb_enable_if (sizeof (U) < sizeof (long long) && hb_is_trivially_copy_assignable(hb_decay<Type>))>
|
2022-11-25 22:23:57 +01:00
|
|
|
hb_array_t copy (hb_serialize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
auto* out = c->start_embed (arrayZ);
|
2022-12-01 22:19:27 +01:00
|
|
|
if (unlikely (!c->extend_size (out, get_size (), false))) return_trace (hb_array_t ());
|
2022-12-02 04:27:56 +01:00
|
|
|
hb_memcpy (out, arrayZ, get_size ());
|
2022-11-25 22:23:57 +01:00
|
|
|
return_trace (hb_array_t (out, length));
|
|
|
|
}
|
|
|
|
|
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:
|
2021-11-02 04:55:14 +01:00
|
|
|
Type *arrayZ = nullptr;
|
|
|
|
unsigned int length = 0;
|
|
|
|
unsigned int backwards_length = 0;
|
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
|
|
|
|
|
|
|
template <typename Type>
|
2018-12-22 01:29:00 +01:00
|
|
|
struct hb_sorted_array_t :
|
2022-11-14 21:43:58 +01:00
|
|
|
hb_array_t<Type>,
|
|
|
|
hb_iter_t<hb_sorted_array_t<Type>, Type&>
|
2018-12-17 02:07:44 +01:00
|
|
|
{
|
2019-08-30 00:21:18 +02:00
|
|
|
typedef hb_iter_t<hb_sorted_array_t, Type&> iter_base_t;
|
2018-12-27 19:29:51 +01:00
|
|
|
HB_ITER_USING (iter_base_t);
|
2019-01-25 15:34:03 +01:00
|
|
|
static constexpr bool is_random_access_iterator = true;
|
2019-05-15 07:28:07 +02:00
|
|
|
static constexpr bool is_sorted_iterator = true;
|
2018-12-27 19:29:51 +01:00
|
|
|
|
2021-11-02 04:55:14 +01:00
|
|
|
hb_sorted_array_t () = default;
|
2021-11-19 19:49:03 +01:00
|
|
|
hb_sorted_array_t (const hb_sorted_array_t&) = default;
|
|
|
|
~hb_sorted_array_t () = default;
|
|
|
|
hb_sorted_array_t& operator= (const hb_sorted_array_t&) = default;
|
|
|
|
hb_sorted_array_t& operator= (hb_sorted_array_t&&) = default;
|
|
|
|
|
2021-11-19 20:33:37 +01:00
|
|
|
constexpr hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {}
|
2019-05-10 01:08:10 +02:00
|
|
|
template <unsigned int length_>
|
2021-11-19 20:33:37 +01:00
|
|
|
constexpr hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {}
|
2018-12-17 02:07:44 +01:00
|
|
|
|
2019-04-26 22:03:41 +02:00
|
|
|
template <typename U,
|
2019-05-09 23:53:02 +02:00
|
|
|
hb_enable_if (hb_is_cr_convertible(U, Type))>
|
2021-11-19 20:33:37 +01:00
|
|
|
constexpr hb_sorted_array_t (const hb_array_t<U> &o) :
|
2022-11-14 21:43:58 +01:00
|
|
|
hb_array_t<Type> (o),
|
|
|
|
hb_iter_t<hb_sorted_array_t, Type&> () {}
|
2019-04-26 22:03:41 +02:00
|
|
|
template <typename U,
|
2019-05-09 23:53:02 +02:00
|
|
|
hb_enable_if (hb_is_cr_convertible(U, Type))>
|
2021-11-20 01:19:09 +01:00
|
|
|
hb_sorted_array_t& operator = (const hb_array_t<U> &o)
|
2019-04-26 22:03:41 +02:00
|
|
|
{ hb_array_t<Type> (*this) = o; return *this; }
|
|
|
|
|
2019-05-07 04:57:15 +02:00
|
|
|
/* Iterator implementation. */
|
2022-07-18 02:44:11 +02:00
|
|
|
|
|
|
|
/* See comment in hb_array_of::operator != */
|
2019-05-07 04:57:15 +02:00
|
|
|
bool operator != (const hb_sorted_array_t& o) const
|
|
|
|
{ return this->arrayZ != o.arrayZ || this->length != o.length; }
|
|
|
|
|
2022-11-21 20:11:30 +01:00
|
|
|
/* Faster range-based for loop without bounds-check. */
|
|
|
|
Type *begin () const { return this->arrayZ; }
|
|
|
|
Type *end () const { return this->arrayZ + this->length; }
|
|
|
|
|
|
|
|
|
2019-08-30 00:21:18 +02:00
|
|
|
hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int *seg_count /* IN/OUT */) const
|
|
|
|
{ return hb_sorted_array_t (((const hb_array_t<Type> *) (this))->sub_array (start_offset, seg_count)); }
|
|
|
|
hb_sorted_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const
|
2018-12-17 02:07:44 +01:00
|
|
|
{ return sub_array (start_offset, &seg_count); }
|
|
|
|
|
2019-08-30 00:23:48 +02:00
|
|
|
hb_sorted_array_t truncate (unsigned length) const { return sub_array (0, length); }
|
|
|
|
|
2018-12-17 02:07:44 +01:00
|
|
|
template <typename T>
|
|
|
|
Type *bsearch (const T &x, Type *not_found = nullptr)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
return bfind (x, &i) ? &this->arrayZ[i] : not_found;
|
|
|
|
}
|
2022-08-19 00:13:56 +02:00
|
|
|
template <typename T>
|
2018-12-17 02:07:44 +01:00
|
|
|
const Type *bsearch (const T &x, const Type *not_found = nullptr) const
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
return bfind (x, &i) ? &this->arrayZ[i] : not_found;
|
|
|
|
}
|
2022-05-17 23:28:50 +02:00
|
|
|
template <typename T>
|
2019-12-06 04:00:23 +01:00
|
|
|
bool bfind (const T &x, unsigned int *i = nullptr,
|
2021-07-22 20:27:33 +02:00
|
|
|
hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
|
2022-05-17 23:28:50 +02:00
|
|
|
unsigned int to_store = (unsigned int) -1) const
|
2019-12-06 04:00:23 +01:00
|
|
|
{
|
2019-12-06 06:04:11 +01:00
|
|
|
unsigned pos;
|
2019-12-06 04:00:23 +01:00
|
|
|
|
2022-05-17 23:28:50 +02:00
|
|
|
if (bsearch_impl (x, &pos))
|
2019-12-06 04:00:23 +01:00
|
|
|
{
|
|
|
|
if (i)
|
|
|
|
*i = pos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:07:44 +01:00
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
switch (not_found)
|
|
|
|
{
|
2021-07-22 20:27:33 +02:00
|
|
|
case HB_NOT_FOUND_DONT_STORE:
|
2018-12-17 02:07:44 +01:00
|
|
|
break;
|
|
|
|
|
2021-07-22 20:27:33 +02:00
|
|
|
case HB_NOT_FOUND_STORE:
|
2018-12-17 02:07:44 +01:00
|
|
|
*i = to_store;
|
|
|
|
break;
|
|
|
|
|
2021-07-22 20:27:33 +02:00
|
|
|
case HB_NOT_FOUND_STORE_CLOSEST:
|
2019-12-06 04:00:23 +01:00
|
|
|
*i = pos;
|
2018-12-17 02:07:44 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-17 23:19:40 +02:00
|
|
|
template <typename T, typename ...Ts>
|
|
|
|
bool bsearch_impl (const T &x, unsigned *pos, Ts... ds) const
|
2019-12-08 05:01:13 +01:00
|
|
|
{
|
|
|
|
return hb_bsearch_impl (pos,
|
|
|
|
x,
|
|
|
|
this->arrayZ,
|
|
|
|
this->length,
|
|
|
|
sizeof (Type),
|
2022-05-17 23:19:40 +02:00
|
|
|
_hb_cmp_method<T, Type, Ts...>,
|
2022-08-19 00:15:00 +02:00
|
|
|
std::forward<Ts> (ds)...);
|
2019-12-08 05:01:13 +01:00
|
|
|
}
|
2018-12-17 02:07:44 +01:00
|
|
|
};
|
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
|
|
|
|
2019-04-03 04:27:02 +02:00
|
|
|
template <typename T>
|
2022-05-20 02:15:46 +02:00
|
|
|
inline bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const
|
2019-04-03 04:27:02 +02:00
|
|
|
{
|
2019-10-02 23:28:53 +02:00
|
|
|
if (o.length != this->length) return false;
|
|
|
|
for (unsigned int i = 0; i < this->length; i++) {
|
|
|
|
if (this->arrayZ[i] != o.arrayZ[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-04-03 04:27:02 +02:00
|
|
|
}
|
2022-05-20 02:15:46 +02:00
|
|
|
template <>
|
|
|
|
inline bool hb_array_t<const char>::operator == (const hb_array_t<const char> &o) const
|
|
|
|
{
|
|
|
|
if (o.length != this->length) return false;
|
2022-05-20 20:34:49 +02:00
|
|
|
return 0 == hb_memcmp (arrayZ, o.arrayZ, length);
|
2022-05-20 02:15:46 +02:00
|
|
|
}
|
|
|
|
template <>
|
|
|
|
inline bool hb_array_t<const unsigned char>::operator == (const hb_array_t<const unsigned char> &o) const
|
|
|
|
{
|
|
|
|
if (o.length != this->length) return false;
|
2022-05-20 20:34:49 +02:00
|
|
|
return 0 == hb_memcmp (arrayZ, o.arrayZ, length);
|
2022-05-20 02:15:46 +02:00
|
|
|
}
|
2019-10-22 01:10:06 +02:00
|
|
|
|
2022-11-22 07:19:42 +01:00
|
|
|
|
|
|
|
/* Specialize hash() for byte arrays. */
|
|
|
|
|
2022-11-22 07:08:51 +01:00
|
|
|
template <>
|
2022-11-22 07:19:42 +01:00
|
|
|
inline uint32_t hb_array_t<const char>::hash () const
|
|
|
|
{
|
2022-11-22 07:08:51 +01:00
|
|
|
uint32_t current = 0;
|
2022-11-22 07:19:42 +01:00
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
#if defined(__OPTIMIZE__) && !defined(HB_NO_PACKED) && \
|
|
|
|
((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__))
|
|
|
|
struct __attribute__((packed)) packed_uint32_t { uint32_t v; };
|
|
|
|
for (; i + 4 <= this->length; i += 4)
|
2022-11-28 22:12:55 +01:00
|
|
|
current = current * 31 + hb_hash ((uint32_t) ((packed_uint32_t *) &this->arrayZ[i])->v);
|
2022-11-22 07:19:42 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (; i < this->length; i++)
|
2022-11-26 23:38:21 +01:00
|
|
|
current = current * 31 + hb_hash (this->arrayZ[i]);
|
2022-11-22 07:08:51 +01:00
|
|
|
return current;
|
|
|
|
}
|
2022-11-22 07:19:42 +01:00
|
|
|
|
2022-11-22 07:08:51 +01:00
|
|
|
template <>
|
2022-11-22 07:19:42 +01:00
|
|
|
inline uint32_t hb_array_t<const unsigned char>::hash () const
|
|
|
|
{
|
2022-11-22 07:08:51 +01:00
|
|
|
uint32_t current = 0;
|
2022-11-22 07:19:42 +01:00
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
#if defined(__OPTIMIZE__) && !defined(HB_NO_PACKED) && \
|
|
|
|
((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__))
|
|
|
|
struct __attribute__((packed)) packed_uint32_t { uint32_t v; };
|
|
|
|
for (; i + 4 <= this->length; i += 4)
|
2022-11-28 22:12:55 +01:00
|
|
|
current = current * 31 + hb_hash ((uint32_t) ((packed_uint32_t *) &this->arrayZ[i])->v);
|
2022-11-22 07:19:42 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (; i < this->length; i++)
|
2022-11-26 23:38:21 +01:00
|
|
|
current = current * 31 + hb_hash (this->arrayZ[i]);
|
2022-11-22 07:08:51 +01:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:10:06 +02: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
|
|
|
|
2019-10-22 01:10:06 +02:00
|
|
|
|
2018-12-17 05:38:51 +01:00
|
|
|
|
2018-12-17 02:07:44 +01:00
|
|
|
#endif /* HB_ARRAY_HH */
|