2018-08-06 13:42:46 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2017,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
|
|
|
|
*/
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#ifndef HB_VECTOR_HH
|
|
|
|
#define HB_VECTOR_HH
|
2018-08-06 13:42:46 +02:00
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb.hh"
|
2018-12-17 02:07:44 +01:00
|
|
|
#include "hb-array.hh"
|
2018-12-21 23:35:58 +01:00
|
|
|
#include "hb-null.hh"
|
2018-08-06 13:42:46 +02:00
|
|
|
|
|
|
|
|
2018-11-24 06:29:22 +01:00
|
|
|
template <typename Type, unsigned int PreallocedCount=8>
|
2018-08-06 13:42:46 +02:00
|
|
|
struct hb_vector_t
|
|
|
|
{
|
2018-12-21 07:57:40 +01:00
|
|
|
typedef Type item_t;
|
2018-12-17 02:35:11 +01:00
|
|
|
enum { item_size = hb_static_size (Type) };
|
2018-11-09 16:01:50 +01:00
|
|
|
|
2018-11-24 06:29:22 +01:00
|
|
|
HB_NO_COPY_ASSIGN_TEMPLATE2 (hb_vector_t, Type, PreallocedCount);
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_vector_t () { init (); }
|
|
|
|
~hb_vector_t () { fini (); }
|
2018-10-30 01:37:41 +01:00
|
|
|
|
2018-08-06 13:42:46 +02:00
|
|
|
unsigned int len;
|
2018-10-05 18:39:48 +02:00
|
|
|
private:
|
2018-08-06 13:42:46 +02:00
|
|
|
unsigned int allocated; /* == 0 means allocation failed. */
|
2018-10-05 18:39:48 +02:00
|
|
|
Type *arrayZ_;
|
2018-11-24 06:29:22 +01:00
|
|
|
Type static_array[PreallocedCount];
|
2018-10-05 18:39:48 +02:00
|
|
|
public:
|
2018-08-06 13:42:46 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void init ()
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
len = 0;
|
|
|
|
allocated = ARRAY_LENGTH (static_array);
|
2018-10-05 18:39:48 +02:00
|
|
|
arrayZ_ = nullptr;
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void fini ()
|
2018-10-30 01:37:41 +01:00
|
|
|
{
|
|
|
|
if (arrayZ_)
|
|
|
|
free (arrayZ_);
|
|
|
|
arrayZ_ = nullptr;
|
|
|
|
allocated = len = 0;
|
|
|
|
}
|
2018-12-17 19:01:01 +01:00
|
|
|
void fini_deep ()
|
2018-10-30 01:37:41 +01:00
|
|
|
{
|
|
|
|
Type *array = arrayZ();
|
|
|
|
unsigned int count = len;
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
array[i].fini ();
|
|
|
|
fini ();
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
Type * arrayZ () { return arrayZ_ ? arrayZ_ : static_array; }
|
|
|
|
const Type * arrayZ () const { return arrayZ_ ? arrayZ_ : static_array; }
|
2018-10-05 18:39:48 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
Type& operator [] (int i_)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
2018-12-01 01:57:12 +01:00
|
|
|
unsigned int i = (unsigned int) i_;
|
2018-08-06 13:42:46 +02:00
|
|
|
if (unlikely (i >= len))
|
|
|
|
return Crap (Type);
|
2018-10-05 18:39:48 +02:00
|
|
|
return arrayZ()[i];
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
2018-12-16 20:08:10 +01:00
|
|
|
const Type& operator [] (int i_) const
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
2018-12-01 01:57:12 +01:00
|
|
|
unsigned int i = (unsigned int) i_;
|
2018-08-06 13:42:46 +02:00
|
|
|
if (unlikely (i >= len))
|
|
|
|
return Null(Type);
|
2018-10-05 18:39:48 +02:00
|
|
|
return arrayZ()[i];
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 05:15:49 +01:00
|
|
|
explicit_operator bool () const { return len; }
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_array_t<Type> as_array ()
|
2018-11-24 07:24:48 +01:00
|
|
|
{ return hb_array (arrayZ(), len); }
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_array_t<const Type> as_array () const
|
2018-11-24 07:24:48 +01:00
|
|
|
{ return hb_array (arrayZ(), len); }
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_array ().sub_array (start_offset, count);}
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_array ().sub_array (start_offset, count);}
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_array ().sub_array (start_offset, count);}
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_array ().sub_array (start_offset, count);}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_sorted_array_t<Type> as_sorted_array ()
|
2018-11-24 07:24:48 +01:00
|
|
|
{ return hb_sorted_array (arrayZ(), len); }
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_sorted_array_t<const Type> as_sorted_array () const
|
2018-11-24 07:24:48 +01:00
|
|
|
{ return hb_sorted_array (arrayZ(), len); }
|
2018-11-23 22:07:43 +01:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<const Type> sorted_sub_array (unsigned int start_offset, unsigned int count) const
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_sorted_array ().sorted_sub_array (start_offset, count);}
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<const Type> sorted_sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_sorted_array ().sorted_sub_array (start_offset, count);}
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<Type> sorted_sub_array (unsigned int start_offset, unsigned int count)
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_sorted_array ().sorted_sub_array (start_offset, count);}
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_array_t<Type> sorted_sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
|
2018-12-06 19:21:06 +01:00
|
|
|
{ return as_sorted_array ().sorted_sub_array (start_offset, count);}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
template <typename T> explicit_operator T * () { return arrayZ(); }
|
|
|
|
template <typename T> explicit_operator const T * () const { return arrayZ(); }
|
|
|
|
operator hb_array_t<Type> () { return as_array (); }
|
|
|
|
operator hb_array_t<const Type> () const { return as_array (); }
|
2018-10-26 01:50:38 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
Type * operator + (unsigned int i) { return arrayZ() + i; }
|
|
|
|
const Type * operator + (unsigned int i) const { return arrayZ() + i; }
|
2018-10-26 01:50:38 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
Type *push ()
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
if (unlikely (!resize (len + 1)))
|
|
|
|
return &Crap(Type);
|
2018-10-05 18:39:48 +02:00
|
|
|
return &arrayZ()[len - 1];
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
2018-12-16 20:08:10 +01:00
|
|
|
Type *push (const Type& v)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
Type *p = push ();
|
|
|
|
*p = v;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
bool in_error () const { return allocated == 0; }
|
2018-09-04 05:50:11 +02:00
|
|
|
|
2018-08-06 13:42:46 +02:00
|
|
|
/* Allocate for size but don't adjust len. */
|
2018-12-16 20:08:10 +01:00
|
|
|
bool alloc (unsigned int size)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
if (unlikely (!allocated))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (likely (size <= allocated))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Reallocate */
|
|
|
|
|
|
|
|
unsigned int new_allocated = allocated;
|
|
|
|
while (size >= new_allocated)
|
|
|
|
new_allocated += (new_allocated >> 1) + 8;
|
|
|
|
|
|
|
|
Type *new_array = nullptr;
|
|
|
|
|
2018-10-05 18:39:48 +02:00
|
|
|
if (!arrayZ_)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
new_array = (Type *) calloc (new_allocated, sizeof (Type));
|
|
|
|
if (new_array)
|
2018-10-05 18:39:48 +02:00
|
|
|
memcpy (new_array, static_array, len * sizeof (Type));
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool overflows = (new_allocated < allocated) || hb_unsigned_mul_overflows (new_allocated, sizeof (Type));
|
|
|
|
if (likely (!overflows))
|
2018-10-05 18:39:48 +02:00
|
|
|
new_array = (Type *) realloc (arrayZ_, new_allocated * sizeof (Type));
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely (!new_array))
|
|
|
|
{
|
|
|
|
allocated = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-05 18:39:48 +02:00
|
|
|
arrayZ_ = new_array;
|
2018-08-06 13:42:46 +02:00
|
|
|
allocated = new_allocated;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool resize (int size_)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
unsigned int size = size_ < 0 ? 0u : (unsigned int) size_;
|
|
|
|
if (!alloc (size))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (size > len)
|
2018-10-05 18:39:48 +02:00
|
|
|
memset (arrayZ() + len, 0, (size - len) * sizeof (*arrayZ()));
|
2018-08-06 13:42:46 +02:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void pop ()
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
if (!len) return;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
void remove (unsigned int i)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
2018-10-05 18:39:48 +02:00
|
|
|
if (unlikely (i >= len))
|
|
|
|
return;
|
|
|
|
Type *array = arrayZ();
|
|
|
|
memmove (static_cast<void *> (&array[i]),
|
|
|
|
static_cast<void *> (&array[i + 1]),
|
|
|
|
(len - i - 1) * sizeof (Type));
|
|
|
|
len--;
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
void shrink (int size_)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
unsigned int size = size_ < 0 ? 0u : (unsigned int) size_;
|
|
|
|
if (size < len)
|
|
|
|
len = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
Type *find (T v)
|
2018-10-05 18:39:48 +02:00
|
|
|
{
|
|
|
|
Type *array = arrayZ();
|
2018-08-06 13:42:46 +02:00
|
|
|
for (unsigned int i = 0; i < len; i++)
|
2018-10-05 18:39:48 +02:00
|
|
|
if (array[i] == v)
|
|
|
|
return &array[i];
|
2018-08-06 13:42:46 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
const Type *find (T v) const
|
2018-10-05 18:39:48 +02:00
|
|
|
{
|
|
|
|
const Type *array = arrayZ();
|
2018-08-06 13:42:46 +02:00
|
|
|
for (unsigned int i = 0; i < len; i++)
|
2018-10-05 18:39:48 +02:00
|
|
|
if (array[i] == v)
|
|
|
|
return &array[i];
|
2018-08-06 13:42:46 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
void qsort (int (*cmp)(const void*, const void*))
|
2018-11-24 07:45:58 +01:00
|
|
|
{ as_array ().qsort (cmp); }
|
2018-12-16 20:08:10 +01:00
|
|
|
void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
|
2018-11-24 07:45:58 +01:00
|
|
|
{ as_array ().qsort (start, end); }
|
2018-08-06 13:42:46 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
Type *lsearch (const T &x, Type *not_found = nullptr)
|
2018-11-24 07:31:00 +01:00
|
|
|
{ return as_array ().lsearch (x, not_found); }
|
2018-10-05 18:39:48 +02:00
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
const Type *lsearch (const T &x, const Type *not_found = nullptr) const
|
2018-11-24 07:31:00 +01:00
|
|
|
{ return as_array ().lsearch (x, not_found); }
|
2018-08-06 13:42:46 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
Type *bsearch (const T &x, Type *not_found = nullptr)
|
2018-11-24 07:31:00 +01:00
|
|
|
{ return as_sorted_array ().bsearch (x, not_found); }
|
2018-08-06 13:42:46 +02:00
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
const Type *bsearch (const T &x, const Type *not_found = nullptr) const
|
2018-11-24 07:31:00 +01:00
|
|
|
{ return as_sorted_array ().bsearch (x, not_found); }
|
2018-08-06 13:42:46 +02:00
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
bool bfind (const T &x, unsigned int *i = nullptr,
|
2018-11-24 16:06:13 +01:00
|
|
|
hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
|
|
|
|
unsigned int to_store = (unsigned int) -1) const
|
|
|
|
{ return as_sorted_array ().bfind (x, i, not_found, to_store); }
|
2018-08-06 13:42:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#endif /* HB_VECTOR_HH */
|