diff options
Diffstat (limited to 'Build/source/libs/harfbuzz/harfbuzz-src/src/hb-vector.hh')
-rw-r--r-- | Build/source/libs/harfbuzz/harfbuzz-src/src/hb-vector.hh | 97 |
1 files changed, 45 insertions, 52 deletions
diff --git a/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-vector.hh b/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-vector.hh index d8e3f4ef109..2fd739b5c1f 100644 --- a/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-vector.hh +++ b/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-vector.hh @@ -29,29 +29,28 @@ #include "hb.hh" #include "hb-array.hh" +#include "hb-null.hh" -template <typename Type, unsigned int PreallocedCount=8> +template <typename Type> struct hb_vector_t { - typedef Type ItemType; - enum { item_size = hb_static_size (Type) }; + typedef Type item_t; + static constexpr unsigned item_size = hb_static_size (Type); - HB_NO_COPY_ASSIGN_TEMPLATE2 (hb_vector_t, Type, PreallocedCount); + HB_NO_COPY_ASSIGN_TEMPLATE (hb_vector_t, Type); hb_vector_t () { init (); } ~hb_vector_t () { fini (); } - unsigned int len; + unsigned int length; private: - unsigned int allocated; /* == 0 means allocation failed. */ + int allocated; /* == -1 means allocation failed. */ Type *arrayZ_; - Type static_array[PreallocedCount]; public: void init () { - len = 0; - allocated = ARRAY_LENGTH (static_array); + allocated = length = 0; arrayZ_ = nullptr; } @@ -59,40 +58,41 @@ struct hb_vector_t { if (arrayZ_) free (arrayZ_); - arrayZ_ = nullptr; - allocated = len = 0; + init (); } void fini_deep () { Type *array = arrayZ(); - unsigned int count = len; + unsigned int count = length; for (unsigned int i = 0; i < count; i++) array[i].fini (); fini (); } - Type * arrayZ () { return arrayZ_ ? arrayZ_ : static_array; } - const Type * arrayZ () const { return arrayZ_ ? arrayZ_ : static_array; } + const Type * arrayZ () const { return arrayZ_; } + Type * arrayZ () { return arrayZ_; } Type& operator [] (int i_) { unsigned int i = (unsigned int) i_; - if (unlikely (i >= len)) + if (unlikely (i >= length)) return Crap (Type); return arrayZ()[i]; } const Type& operator [] (int i_) const { unsigned int i = (unsigned int) i_; - if (unlikely (i >= len)) + if (unlikely (i >= length)) return Null(Type); return arrayZ()[i]; } + explicit_operator bool () const { return length; } + hb_array_t<Type> as_array () - { return hb_array (arrayZ(), len); } + { return hb_array (arrayZ(), length); } hb_array_t<const Type> as_array () const - { return hb_array (arrayZ(), len); } + { return hb_array (arrayZ(), length); } hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const { return as_array ().sub_array (start_offset, count);} @@ -104,9 +104,9 @@ struct hb_vector_t { return as_array ().sub_array (start_offset, count);} hb_sorted_array_t<Type> as_sorted_array () - { return hb_sorted_array (arrayZ(), len); } + { return hb_sorted_array (arrayZ(), length); } hb_sorted_array_t<const Type> as_sorted_array () const - { return hb_sorted_array (arrayZ(), len); } + { return hb_sorted_array (arrayZ(), length); } hb_array_t<const Type> sorted_sub_array (unsigned int start_offset, unsigned int count) const { return as_sorted_array ().sorted_sub_array (start_offset, count);} @@ -127,9 +127,9 @@ struct hb_vector_t Type *push () { - if (unlikely (!resize (len + 1))) + if (unlikely (!resize (length + 1))) return &Crap(Type); - return &arrayZ()[len - 1]; + return &arrayZ()[length - 1]; } Type *push (const Type& v) { @@ -138,15 +138,15 @@ struct hb_vector_t return p; } - bool in_error () const { return allocated == 0; } + bool in_error () const { return allocated < 0; } - /* Allocate for size but don't adjust len. */ + /* Allocate for size but don't adjust length. */ bool alloc (unsigned int size) { - if (unlikely (!allocated)) + if (unlikely (allocated < 0)) return false; - if (likely (size <= allocated)) + if (likely (size <= (unsigned) allocated)) return true; /* Reallocate */ @@ -156,23 +156,16 @@ struct hb_vector_t new_allocated += (new_allocated >> 1) + 8; Type *new_array = nullptr; - - if (!arrayZ_) - { - new_array = (Type *) calloc (new_allocated, sizeof (Type)); - if (new_array) - memcpy (new_array, static_array, len * sizeof (Type)); - } - else - { - bool overflows = (new_allocated < allocated) || hb_unsigned_mul_overflows (new_allocated, sizeof (Type)); - if (likely (!overflows)) - new_array = (Type *) realloc (arrayZ_, new_allocated * sizeof (Type)); - } + bool overflows = + (int) new_allocated < 0 || + (new_allocated < (unsigned) allocated) || + hb_unsigned_mul_overflows (new_allocated, sizeof (Type)); + if (likely (!overflows)) + new_array = (Type *) realloc (arrayZ_, new_allocated * sizeof (Type)); if (unlikely (!new_array)) { - allocated = 0; + allocated = -1; return false; } @@ -188,42 +181,42 @@ struct hb_vector_t if (!alloc (size)) return false; - if (size > len) - memset (arrayZ() + len, 0, (size - len) * sizeof (*arrayZ())); + if (size > length) + memset (arrayZ() + length, 0, (size - length) * sizeof (*arrayZ())); - len = size; + length = size; return true; } void pop () { - if (!len) return; - len--; + if (!length) return; + length--; } void remove (unsigned int i) { - if (unlikely (i >= len)) + if (unlikely (i >= length)) return; Type *array = arrayZ(); memmove (static_cast<void *> (&array[i]), static_cast<void *> (&array[i + 1]), - (len - i - 1) * sizeof (Type)); - len--; + (length - i - 1) * sizeof (Type)); + length--; } void shrink (int size_) { unsigned int size = size_ < 0 ? 0u : (unsigned int) size_; - if (size < len) - len = size; + if (size < length) + length = size; } template <typename T> Type *find (T v) { Type *array = arrayZ(); - for (unsigned int i = 0; i < len; i++) + for (unsigned int i = 0; i < length; i++) if (array[i] == v) return &array[i]; return nullptr; @@ -232,7 +225,7 @@ struct hb_vector_t const Type *find (T v) const { const Type *array = arrayZ(); - for (unsigned int i = 0; i < len; i++) + for (unsigned int i = 0; i < length; i++) if (array[i] == v) return &array[i]; return nullptr; |