[vector] Simplify arrayZ

Was turned into function when we had static ones and wanted to be
move-safe...  Not the case anymore.
This commit is contained in:
Behdad Esfahbod 2019-05-10 18:40:29 -07:00
parent 4d67743ffd
commit 2fb3a8327a
4 changed files with 39 additions and 46 deletions

View File

@ -598,7 +598,7 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan,
} else { } else {
active_feature_t *feature = active_features.find (&event->feature); active_feature_t *feature = active_features.find (&event->feature);
if (feature) if (feature)
active_features.remove (feature - active_features.arrayZ ()); active_features.remove (feature - active_features.arrayZ);
} }
} }
} }

View File

@ -167,7 +167,7 @@ struct CFFIndex
byteArray.resize (buffArray.length); byteArray.resize (buffArray.length);
for (unsigned int i = 0; i < byteArray.length; i++) for (unsigned int i = 0; i < byteArray.length; i++)
{ {
byteArray[i] = byte_str_t (buffArray[i].arrayZ (), buffArray[i].length); byteArray[i] = byte_str_t (buffArray[i].arrayZ, buffArray[i].length);
} }
bool result = this->serialize (c, offSize_, byteArray); bool result = this->serialize (c, offSize_, byteArray);
byteArray.fini (); byteArray.fini ();

View File

@ -698,7 +698,7 @@ _hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
{ {
active_feature_t *feature = active_features.find (&event->feature); active_feature_t *feature = active_features.find (&event->feature);
if (feature) if (feature)
active_features.remove (feature - active_features.arrayZ ()); active_features.remove (feature - active_features.arrayZ);
} }
} }
@ -889,8 +889,8 @@ retry:
&items[i].a, &items[i].a,
script_tags[i], script_tags[i],
language_tag, language_tag,
range_char_counts.arrayZ (), range_char_counts.arrayZ,
range_properties.arrayZ (), range_properties.arrayZ,
range_properties.length, range_properties.length,
pchars + chars_offset, pchars + chars_offset,
item_chars_len, item_chars_len,
@ -930,8 +930,8 @@ retry:
&items[i].a, &items[i].a,
script_tags[i], script_tags[i],
language_tag, language_tag,
range_char_counts.arrayZ (), range_char_counts.arrayZ,
range_properties.arrayZ (), range_properties.arrayZ,
range_properties.length, range_properties.length,
pchars + chars_offset, pchars + chars_offset,
log_clusters + chars_offset, log_clusters + chars_offset,

View File

@ -49,35 +49,34 @@ struct hb_vector_t
{ {
allocated = o.allocated; allocated = o.allocated;
length = o.length; length = o.length;
arrayZ_ = o.arrayZ_; arrayZ = o.arrayZ;
o.init (); o.init ();
} }
~hb_vector_t () { fini (); } ~hb_vector_t () { fini (); }
unsigned int length;
private: private:
int allocated; /* == -1 means allocation failed. */ int allocated; /* == -1 means allocation failed. */
Type *arrayZ_;
public: public:
unsigned int length;
public:
Type *arrayZ;
void init () void init ()
{ {
allocated = length = 0; allocated = length = 0;
arrayZ_ = nullptr; arrayZ = nullptr;
} }
void fini () void fini ()
{ {
if (arrayZ_) free (arrayZ);
free (arrayZ_);
init (); init ();
} }
void fini_deep () void fini_deep ()
{ {
Type *array = arrayZ();
unsigned int count = length; unsigned int count = length;
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
array[i].fini (); arrayZ[i].fini ();
fini (); fini ();
} }
@ -95,33 +94,30 @@ struct hb_vector_t
fini (); fini ();
allocated = o.allocated; allocated = o.allocated;
length = o.length; length = o.length;
arrayZ_ = o.arrayZ_; arrayZ = o.arrayZ;
o.init (); o.init ();
return *this; return *this;
} }
hb_bytes_t as_bytes () const hb_bytes_t as_bytes () const
{ return hb_bytes_t ((const char *) arrayZ(), length * item_size); } { return hb_bytes_t ((const char *) arrayZ, length * item_size); }
bool operator == (const hb_vector_t &o) const { return as_array () == o.as_array (); } bool operator == (const hb_vector_t &o) const { return as_array () == o.as_array (); }
uint32_t hash () const { return as_array ().hash (); } uint32_t hash () const { return as_array ().hash (); }
const Type * arrayZ () const { return arrayZ_; }
Type * arrayZ () { return arrayZ_; }
Type& operator [] (int i_) Type& operator [] (int i_)
{ {
unsigned int i = (unsigned int) i_; unsigned int i = (unsigned int) i_;
if (unlikely (i >= length)) if (unlikely (i >= length))
return Crap (Type); return Crap (Type);
return arrayZ()[i]; return arrayZ[i];
} }
const Type& operator [] (int i_) const const Type& operator [] (int i_) const
{ {
unsigned int i = (unsigned int) i_; unsigned int i = (unsigned int) i_;
if (unlikely (i >= length)) if (unlikely (i >= length))
return Null(Type); return Null(Type);
return arrayZ()[i]; return arrayZ[i];
} }
Type& tail () { return (*this)[length - 1]; } Type& tail () { return (*this)[length - 1]; }
@ -134,8 +130,8 @@ struct hb_vector_t
template <typename T> template <typename T>
hb_vector_t& operator << (T&& v) { push (hb_forward<T> (v)); return *this; } hb_vector_t& operator << (T&& v) { push (hb_forward<T> (v)); return *this; }
hb_array_t< Type> as_array () { return hb_array (arrayZ(), length); } hb_array_t< Type> as_array () { return hb_array (arrayZ, length); }
hb_array_t<const Type> as_array () const { return hb_array (arrayZ(), length); } hb_array_t<const Type> as_array () const { return hb_array (arrayZ, length); }
/* Iterator. */ /* Iterator. */
typedef hb_array_t<const Type> iter_t; typedef hb_array_t<const Type> iter_t;
@ -155,21 +151,21 @@ struct hb_vector_t
{ return as_array ().sub_array (start_offset, count);} { return as_array ().sub_array (start_offset, count);}
hb_sorted_array_t<Type> as_sorted_array () hb_sorted_array_t<Type> as_sorted_array ()
{ return hb_sorted_array (arrayZ(), length); } { return hb_sorted_array (arrayZ, length); }
hb_sorted_array_t<const Type> as_sorted_array () const hb_sorted_array_t<const Type> as_sorted_array () const
{ return hb_sorted_array (arrayZ(), length); } { return hb_sorted_array (arrayZ, length); }
template <typename T> explicit operator T * () { return arrayZ(); } template <typename T> explicit operator T * () { return arrayZ; }
template <typename T> explicit operator const T * () const { return arrayZ(); } template <typename T> explicit operator const T * () const { return arrayZ; }
Type * operator + (unsigned int i) { return arrayZ() + i; } Type * operator + (unsigned int i) { return arrayZ + i; }
const Type * operator + (unsigned int i) const { return arrayZ() + i; } const Type * operator + (unsigned int i) const { return arrayZ + i; }
Type *push () Type *push ()
{ {
if (unlikely (!resize (length + 1))) if (unlikely (!resize (length + 1)))
return &Crap(Type); return &Crap(Type);
return &arrayZ()[length - 1]; return &arrayZ[length - 1];
} }
template <typename T> template <typename T>
Type *push (T&& v) Type *push (T&& v)
@ -202,7 +198,7 @@ struct hb_vector_t
(new_allocated < (unsigned) allocated) || (new_allocated < (unsigned) allocated) ||
hb_unsigned_mul_overflows (new_allocated, sizeof (Type)); hb_unsigned_mul_overflows (new_allocated, sizeof (Type));
if (likely (!overflows)) if (likely (!overflows))
new_array = (Type *) realloc (arrayZ_, new_allocated * sizeof (Type)); new_array = (Type *) realloc (arrayZ, new_allocated * sizeof (Type));
if (unlikely (!new_array)) if (unlikely (!new_array))
{ {
@ -210,7 +206,7 @@ struct hb_vector_t
return false; return false;
} }
arrayZ_ = new_array; arrayZ = new_array;
allocated = new_allocated; allocated = new_allocated;
return true; return true;
@ -223,7 +219,7 @@ struct hb_vector_t
return false; return false;
if (size > length) if (size > length)
memset (arrayZ() + length, 0, (size - length) * sizeof (*arrayZ())); memset (arrayZ + length, 0, (size - length) * sizeof (*arrayZ));
length = size; length = size;
return true; return true;
@ -232,16 +228,15 @@ struct hb_vector_t
Type pop () Type pop ()
{ {
if (!length) return Null(Type); if (!length) return Null(Type);
return hb_move (arrayZ()[--length]); /* Does this move actually work? */ return hb_move (arrayZ[--length]); /* Does this move actually work? */
} }
void remove (unsigned int i) void remove (unsigned int i)
{ {
if (unlikely (i >= length)) if (unlikely (i >= length))
return; return;
Type *array = arrayZ(); memmove (static_cast<void *> (&arrayZ[i]),
memmove (static_cast<void *> (&array[i]), static_cast<void *> (&arrayZ[i + 1]),
static_cast<void *> (&array[i + 1]),
(length - i - 1) * sizeof (Type)); (length - i - 1) * sizeof (Type));
length--; length--;
} }
@ -256,19 +251,17 @@ struct hb_vector_t
template <typename T> template <typename T>
Type *find (T v) Type *find (T v)
{ {
Type *array = arrayZ();
for (unsigned int i = 0; i < length; i++) for (unsigned int i = 0; i < length; i++)
if (array[i] == v) if (arrayZ[i] == v)
return &array[i]; return &arrayZ[i];
return nullptr; return nullptr;
} }
template <typename T> template <typename T>
const Type *find (T v) const const Type *find (T v) const
{ {
const Type *array = arrayZ();
for (unsigned int i = 0; i < length; i++) for (unsigned int i = 0; i < length; i++)
if (array[i] == v) if (arrayZ[i] == v)
return &array[i]; return &arrayZ[i];
return nullptr; return nullptr;
} }
@ -288,8 +281,8 @@ struct hb_vector_t
template <typename Type> template <typename Type>
struct hb_sorted_vector_t : hb_vector_t<Type> struct hb_sorted_vector_t : hb_vector_t<Type>
{ {
hb_sorted_array_t< Type> as_array () { return hb_sorted_array (this->arrayZ(), this->length); } hb_sorted_array_t< Type> as_array () { return hb_sorted_array (this->arrayZ, this->length); }
hb_sorted_array_t<const Type> as_array () const { return hb_sorted_array (this->arrayZ(), this->length); } hb_sorted_array_t<const Type> as_array () const { return hb_sorted_array (this->arrayZ, this->length); }
/* Iterator. */ /* Iterator. */
typedef hb_sorted_array_t<const Type> const_iter_t; typedef hb_sorted_array_t<const Type> const_iter_t;