diff options
Diffstat (limited to 'thirdparty/thorvg/src/lib/tvgArray.h')
-rw-r--r-- | thirdparty/thorvg/src/lib/tvgArray.h | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/thirdparty/thorvg/src/lib/tvgArray.h b/thirdparty/thorvg/src/lib/tvgArray.h index 8d67753ae3..0e8aef3071 100644 --- a/thirdparty/thorvg/src/lib/tvgArray.h +++ b/thirdparty/thorvg/src/lib/tvgArray.h @@ -35,30 +35,35 @@ struct Array uint32_t count = 0; uint32_t reserved = 0; + Array(){} + + Array(const Array& rhs) + { + reset(); + *this = rhs; + } + void push(T element) { if (count + 1 > reserved) { - reserved = (count + 1) * 2; - auto p = data; + reserved = count + (count + 2) / 2; data = static_cast<T*>(realloc(data, sizeof(T) * reserved)); - if (!data) { - data = p; - return; - } } data[count++] = element; } + void push(Array<T>& rhs) + { + grow(rhs.count); + memcpy(data + count, rhs.data, rhs.count * sizeof(T)); + count += rhs.count; + } + bool reserve(uint32_t size) { if (size > reserved) { reserved = size; - auto p = data; data = static_cast<T*>(realloc(data, sizeof(T) * reserved)); - if (!data) { - data = p; - return false; - } } return true; } @@ -68,11 +73,21 @@ struct Array return reserve(count + size); } - T* ptr() + T* end() const { return data + count; } + T& last() + { + return data[count - 1]; + } + + T& first() + { + return data[0]; + } + void pop() { if (count > 0) --count; @@ -80,10 +95,8 @@ struct Array void reset() { - if (data) { - free(data); - data = nullptr; - } + free(data); + data = nullptr; count = reserved = 0; } @@ -92,16 +105,21 @@ struct Array count = 0; } + bool empty() const + { + return count == 0; + } + void operator=(const Array& rhs) { reserve(rhs.count); - if (rhs.count > 0) memcpy(data, rhs.data, sizeof(T) * reserved); + if (rhs.count > 0) memcpy(data, rhs.data, sizeof(T) * rhs.count); count = rhs.count; } ~Array() { - if (data) free(data); + free(data); } }; |