[arrays] Port SortedArrayOf.bsearch/bfind to hb_sorted_array_t's

This commit is contained in:
Behdad Esfahbod 2018-11-24 01:31:00 -05:00
parent e604306f28
commit e700392f5c
3 changed files with 23 additions and 71 deletions

View File

@ -635,22 +635,19 @@ struct hb_sorted_array_t : hb_array_t<Type>
inline hb_sorted_array_t (Type *array_, unsigned int len_) : hb_array_t<Type> (array_, len_) {} inline hb_sorted_array_t (Type *array_, unsigned int len_) : hb_array_t<Type> (array_, len_) {}
template <typename T> template <typename T>
inline Type *bsearch (const T &x, inline Type *bsearch (const T &x, Type *not_found = nullptr)
Type *not_found = nullptr)
{ {
unsigned int i; unsigned int i;
return bfind (x, &i) ? &this->arrayZ[i] : not_found; return bfind (x, &i) ? &this->arrayZ[i] : not_found;
} }
template <typename T> template <typename T>
inline const Type *bsearch (const T &x, inline const Type *bsearch (const T &x, const Type *not_found = nullptr) const
const Type *not_found = nullptr) const
{ {
unsigned int i; unsigned int i;
return bfind (x, &i) ? &this->arrayZ[i] : not_found; return bfind (x, &i) ? &this->arrayZ[i] : not_found;
} }
template <typename T> template <typename T>
inline bool bfind (const T &x, inline bool bfind (const T &x, unsigned int *i = nullptr) const
unsigned int *i = nullptr) const
{ {
int min = 0, max = (int) this->len - 1; int min = 0, max = (int) this->len - 1;
const Type *array = this->arrayZ; const Type *array = this->arrayZ;

View File

@ -551,14 +551,10 @@ struct ArrayOf
template <typename T> template <typename T>
inline Type &lsearch (const T &x) inline Type &lsearch (const T &x)
{ { return *as_array ().lsearch (x, &Crap (T)); }
return *as_array ().lsearch (x, &Crap (T));
}
template <typename T> template <typename T>
inline const Type &lsearch (const T &x) const inline const Type &lsearch (const T &x) const
{ { return *as_array ().lsearch (x, &Null (T)); }
return *as_array ().lsearch (x, &Null (T));
}
inline void qsort (void) inline void qsort (void)
{ {
@ -745,46 +741,20 @@ struct ArrayOfM1
template <typename Type, typename LenType=HBUINT16> template <typename Type, typename LenType=HBUINT16>
struct SortedArrayOf : ArrayOf<Type, LenType> struct SortedArrayOf : ArrayOf<Type, LenType>
{ {
inline hb_sorted_array_t<Type> as_array (void)
{ return hb_sorted_array (this->arrayZ, this->len); }
inline hb_sorted_array_t<const Type> as_array (void) const
{ return hb_sorted_array (this->arrayZ, this->len); }
template <typename T> template <typename T>
inline Type &bsearch (const T &x) inline Type &bsearch (const T &x)
{ { return *as_array ().bsearch (x, &Crap (Type)); }
unsigned int i;
return bfind (x, &i) ? this->arrayZ[i] : Crap(Type);
}
template <typename T> template <typename T>
inline const Type &bsearch (const T &x) const inline const Type &bsearch (const T &x) const
{ { return *as_array ().bsearch (x, &Null (Type)); }
unsigned int i;
return bfind (x, &i) ? this->arrayZ[i] : Null(Type);
}
template <typename T> template <typename T>
inline bool bfind (const T &x, unsigned int *i = nullptr) const inline bool bfind (const T &x, unsigned int *i = nullptr) const
{ { return as_array ().bfind (x, i); }
int min = 0, max = (int) this->len - 1;
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)
{
if (max < 0 || (max < (int) this->len && array[max].cmp (x) > 0))
max++;
*i = max;
}
return false;
}
}; };
/* /*

View File

@ -236,36 +236,21 @@ struct hb_vector_t
} }
template <typename T> template <typename T>
inline Type *lsearch (const T &x, inline Type *lsearch (const T &x, Type *not_found = nullptr)
Type *not_found = nullptr) { return as_array ().lsearch (x, not_found); }
{
return as_array ().lsearch (x, not_found);
}
template <typename T> template <typename T>
inline const Type *lsearch (const T &x, inline const Type *lsearch (const T &x, const Type *not_found = nullptr) const
const Type *not_found = nullptr) const { return as_array ().lsearch (x, not_found); }
{
return as_array ().lsearch (x, not_found);
}
template <typename T> template <typename T>
inline Type *bsearch (const T &x, inline Type *bsearch (const T &x, Type *not_found = nullptr)
Type *not_found = nullptr) { return as_sorted_array ().bsearch (x, not_found); }
{
return as_sorted_array ().bsearch (x, not_found);
}
template <typename T> template <typename T>
inline const Type *bsearch (const T &x, inline const Type *bsearch (const T &x, const Type *not_found = nullptr) const
const Type *not_found = nullptr) const { return as_sorted_array ().bsearch (x, not_found); }
{
return as_sorted_array ().bsearch (x, not_found);
}
template <typename T> template <typename T>
inline bool bfind (const T &x, inline bool bfind (const T &x, unsigned int *i = nullptr) const
unsigned int *i = nullptr) const { return as_sorted_array ().bfind (x, i); }
{
return as_sorted_array ().bfind (x, i);
}
}; };