[arrays] Port hb_vector_t.lsearch() to hb_array_t's

This commit is contained in:
Behdad Esfahbod 2018-11-24 01:09:28 -05:00
parent 96cf088980
commit 830856ba6b
2 changed files with 27 additions and 15 deletions

View File

@ -575,9 +575,24 @@ struct hb_array_t
inline unsigned int get_size (void) const { return len * sizeof (Type); }
template <typename hb_sanitize_context_t>
inline bool sanitize (hb_sanitize_context_t *c) const
{ return c->check_array (arrayZ, len); }
template <typename T>
inline Type *lsearch (const T &x, Type *not_found = nullptr)
{
unsigned int count = len;
for (unsigned int i = 0; i < count; i++)
if (!this->arrayZ[i].cmp (x))
return &this->arrayZ[i];
return not_found;
}
template <typename T>
inline const Type *lsearch (const T &x, const Type *not_found = nullptr) const
{
unsigned int count = len;
for (unsigned int i = 0; i < count; i++)
if (!this->arrayZ[i].cmp (x))
return &this->arrayZ[i];
return not_found;
}
template <typename T> inline operator T * (void) const { return arrayZ; }
@ -601,6 +616,11 @@ struct hb_array_t
inline void free (void) { ::free ((void *) arrayZ); arrayZ = nullptr; len = 0; }
template <typename hb_sanitize_context_t>
inline bool sanitize (hb_sanitize_context_t *c) const
{ return c->check_array (arrayZ, len); }
public:
Type *arrayZ;
unsigned int len;
};

View File

@ -229,22 +229,14 @@ struct hb_vector_t
}
template <typename T>
inline Type *lsearch (const T &x)
inline Type *lsearch (const T &x, Type *not_found = nullptr)
{
Type *array = arrayZ();
for (unsigned int i = 0; i < len; i++)
if (0 == array[i].cmp (x))
return &array[i];
return nullptr;
return as_array ().lsearch (x, not_found);
}
template <typename T>
inline const Type *lsearch (const T &x) const
inline const Type *lsearch (const T &x, const Type *not_found = nullptr) const
{
const Type *array = arrayZ();
for (unsigned int i = 0; i < len; i++)
if (0 == array[i].cmp (x))
return &array[i];
return nullptr;
return as_array ().lsearch (x, not_found);
}
template <typename T>