10#ifndef RUNTIME_PLATFORM_GROWABLE_ARRAY_H_
11#define RUNTIME_PLATFORM_GROWABLE_ARRAY_H_
18template <
typename T,
typename B,
typename Allocator>
22 : length_(0), capacity_(0), data_(nullptr), allocator_(allocator) {}
25 : length_(0), capacity_(0), data_(nullptr), allocator_(allocator) {
26 if (initial_capacity > 0) {
28 data_ = allocator_->template Alloc<T>(capacity_);
33 : length_(other.length_),
34 capacity_(other.capacity_),
36 allocator_(other.allocator_) {
39 other.data_ =
nullptr;
45 intptr_t temp = other.length_;
46 other.length_ = length_;
48 temp = other.capacity_;
49 other.capacity_ = capacity_;
51 T* temp_data = other.data_;
54 Allocator* temp_allocator = other.allocator_;
55 other.allocator_ = allocator_;
56 allocator_ = temp_allocator;
60 intptr_t
length()
const {
return length_; }
61 T*
data()
const {
return data_; }
70 bool isEqual(
const T&,
const T&) =
nullptr)
const {
71 for (
const auto&
value : *
this) {
74 ASSERT(isEqual ==
nullptr || isEqual(
value, other));
77 if (isEqual !=
nullptr && isEqual(
value, other)) {
99 ASSERT(length_ <= capacity_);
109 for (intptr_t i = 0; i <
length; ++i) {
115 const intptr_t old_length = length_;
116 if (old_length < new_length) {
118 for (intptr_t i = old_length; i < new_length; ++i) {
119 (*this)[i] = default_value;
132 for (intptr_t i = 0; i < src.length(); i++) {
141 for (intptr_t i = length_ - 2; i >= idx; i--) {
142 data_[i + 1] = data_[i];
148 for (intptr_t i = 0; i < length_ / 2; i++) {
149 const intptr_t j = length_ - 1 - i;
157 void Swap(intptr_t i, intptr_t j) {
171 intptr_t last = length_ - 1;
182 for (intptr_t i = idx; i < length_ - 1; i++) {
183 data_[i] = data_[i + 1];
207 const T*
begin()
const {
return &data_[0]; }
209 T*
end() {
return &data_[length_]; }
210 const T*
end()
const {
return &data_[length_]; }
221template <
typename T,
typename B,
typename Allocator>
225 if (length_ == 0)
return;
227 typedef int (*CompareFunction)(
const void*,
const void*);
228 qsort(data_, length_,
sizeof(
T),
reinterpret_cast<CompareFunction
>(
compare));
231template <
typename T,
typename B,
typename Allocator>
233 if (new_length > capacity_) {
236 allocator_->template Realloc<T>(data_, capacity_, new_capacity);
237 ASSERT(new_data !=
nullptr);
239 capacity_ = new_capacity;
241 length_ = new_length;
244template <
typename T,
typename B,
typename Allocator>
246 if (new_length > capacity_) {
247 T* new_data = allocator_->template Alloc<T>(new_length);
248 ASSERT(new_data !=
nullptr);
250 capacity_ = new_length;
252 length_ = new_length;
263 static inline T*
Realloc(
T* old_array, intptr_t old_len, intptr_t new_len) {
264 return reinterpret_cast<T*
>(
dart::realloc(old_array, new_len *
sizeof(
T)));
268 static inline void Free(
T* old_array, intptr_t old_len) {
static bool compare(const SkBitmap &ref, const SkIRect &iref, const SkBitmap &test, const SkIRect &itest)
Type::kYUV Type::kRGBA() int(0.7 *637)
bool Contains(const T &other, bool isEqual(const T &, const T &)=nullptr) const
void Swap(intptr_t i, intptr_t j)
void TruncateTo(intptr_t length)
void EraseAt(intptr_t idx)
void FillWith(const T &value, intptr_t start, intptr_t length)
void AddArray(const BaseGrowableArray< T, B, Allocator > &src)
T & operator[](intptr_t index) const
void InsertAt(intptr_t idx, const T &value)
void Resize(intptr_t new_length)
void StealBuffer(T **buffer, intptr_t *length)
BaseGrowableArray & operator=(BaseGrowableArray &&other)
const T & At(intptr_t index) const
void SetLength(intptr_t new_length)
void EnsureLength(intptr_t new_length, const T &default_value)
void RemoveAt(intptr_t i)
BaseGrowableArray(intptr_t initial_capacity, Allocator *allocator)
BaseGrowableArray(BaseGrowableArray &&other)
void Sort(int compare(const T *, const T *))
BaseGrowableArray(Allocator *allocator)
MallocGrowableArray(intptr_t initial_capacity)
static void Free(T *old_array, intptr_t old_len)
static T * Realloc(T *old_array, intptr_t old_len, intptr_t new_len)
static T * Alloc(intptr_t len)
static constexpr uintptr_t RoundUpToPowerOfTwo(uintptr_t x)
static const uint8_t buffer[]
void * malloc(size_t size)
void * realloc(void *ptr, size_t size)