2011-07-28 14:26:00 +00:00
|
|
|
/*
|
2011-09-14 13:54:05 +00:00
|
|
|
* Copyright 2011 Google Inc.
|
2011-07-28 14:26:00 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
#ifndef SkTArray_DEFINED
|
|
|
|
#define SkTArray_DEFINED
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
#include <new>
|
2011-09-14 13:54:05 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
#include "SkTemplates.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-10-07 20:54:15 +00:00
|
|
|
template <typename T, bool MEM_COPY = false> class SkTArray;
|
|
|
|
|
|
|
|
namespace SkTArrayExt {
|
|
|
|
|
2014-03-21 19:39:02 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void copy(SkTArray<T, true>* self, int dst, int src) {
|
|
|
|
memcpy(&self->fItemArray[dst], &self->fItemArray[src], sizeof(T));
|
|
|
|
}
|
2011-10-07 20:54:15 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void copy(SkTArray<T, true>* self, const T* array) {
|
|
|
|
memcpy(self->fMemArray, array, self->fCount * sizeof(T));
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) {
|
|
|
|
memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T));
|
|
|
|
}
|
|
|
|
|
2014-03-21 19:39:02 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void copy(SkTArray<T, false>* self, int dst, int src) {
|
|
|
|
SkNEW_PLACEMENT_ARGS(&self->fItemArray[dst], T, (self->fItemArray[src]));
|
|
|
|
}
|
2011-10-07 20:54:15 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void copy(SkTArray<T, false>* self, const T* array) {
|
|
|
|
for (int i = 0; i < self->fCount; ++i) {
|
2013-06-05 15:40:59 +00:00
|
|
|
SkNEW_PLACEMENT_ARGS(self->fItemArray + i, T, (array[i]));
|
2011-10-07 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) {
|
|
|
|
for (int i = 0; i < self->fCount; ++i) {
|
2013-06-05 15:40:59 +00:00
|
|
|
SkNEW_PLACEMENT_ARGS(newMemArray + sizeof(T) * i, T, (self->fItemArray[i]));
|
2011-10-07 20:54:15 +00:00
|
|
|
self->fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:13:46 +00:00
|
|
|
template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_COPY>*, int);
|
|
|
|
|
2011-10-07 20:54:15 +00:00
|
|
|
/** When MEM_COPY is true T will be bit copied when moved.
|
|
|
|
When MEM_COPY is false, T will be copy constructed / destructed.
|
2014-03-24 15:55:01 +00:00
|
|
|
In all cases T will be default-initialized on allocation,
|
2011-10-07 20:54:15 +00:00
|
|
|
and its destructor will be called from this object's destructor.
|
|
|
|
*/
|
|
|
|
template <typename T, bool MEM_COPY> class SkTArray {
|
2010-12-22 21:39:39 +00:00
|
|
|
public:
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Creates an empty array with no initial storage
|
|
|
|
*/
|
|
|
|
SkTArray() {
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount = 0;
|
2011-09-14 13:54:05 +00:00
|
|
|
fReserveCount = gMIN_ALLOC_COUNT;
|
2010-12-22 21:39:39 +00:00
|
|
|
fAllocCount = 0;
|
|
|
|
fMemArray = NULL;
|
|
|
|
fPreAllocMemArray = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Creates an empty array that will preallocate space for reserveCount
|
2011-09-14 13:54:05 +00:00
|
|
|
* elements.
|
|
|
|
*/
|
|
|
|
explicit SkTArray(int reserveCount) {
|
2011-09-27 19:10:05 +00:00
|
|
|
this->init(NULL, 0, NULL, reserveCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Copies one array to another. The new array will be heap allocated.
|
|
|
|
*/
|
|
|
|
explicit SkTArray(const SkTArray& array) {
|
2011-09-27 19:10:05 +00:00
|
|
|
this->init(array.fItemArray, array.fCount, NULL, 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Creates a SkTArray by copying contents of a standard C array. The new
|
2011-09-14 13:54:05 +00:00
|
|
|
* array will be heap allocated. Be careful not to use this constructor
|
|
|
|
* when you really want the (void*, int) version.
|
|
|
|
*/
|
|
|
|
SkTArray(const T* array, int count) {
|
2011-09-27 19:10:05 +00:00
|
|
|
this->init(array, count, NULL, 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* assign copy of array to this
|
|
|
|
*/
|
|
|
|
SkTArray& operator =(const SkTArray& array) {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
fCount = 0;
|
2013-06-13 15:13:46 +00:00
|
|
|
this->checkRealloc((int)array.count());
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount = array.count();
|
2011-10-07 20:54:15 +00:00
|
|
|
SkTArrayExt::copy(this, static_cast<const T*>(array.fMemArray));
|
2010-12-22 21:39:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
virtual ~SkTArray() {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
if (fMemArray != fPreAllocMemArray) {
|
2011-09-14 13:54:05 +00:00
|
|
|
sk_free(fMemArray);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Resets to count() == 0
|
|
|
|
*/
|
2011-03-03 13:54:13 +00:00
|
|
|
void reset() { this->pop_back_n(fCount); }
|
|
|
|
|
2013-06-05 15:40:59 +00:00
|
|
|
/**
|
|
|
|
* Resets to count() = n newly constructed T objects.
|
|
|
|
*/
|
|
|
|
void reset(int n) {
|
|
|
|
SkASSERT(n >= 0);
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
// set fCount to 0 before calling checkRealloc so that no copy cons. are called.
|
|
|
|
fCount = 0;
|
|
|
|
this->checkRealloc(n);
|
|
|
|
fCount = n;
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
SkNEW_PLACEMENT(fItemArray + i, T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 20:06:51 +00:00
|
|
|
/**
|
|
|
|
* Resets to a copy of a C array.
|
|
|
|
*/
|
|
|
|
void reset(const T* array, int count) {
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
int delta = count - fCount;
|
|
|
|
this->checkRealloc(delta);
|
|
|
|
fCount = count;
|
2014-03-21 19:39:02 +00:00
|
|
|
SkTArrayExt::copy(this, array);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeShuffle(int n) {
|
|
|
|
SkASSERT(n < fCount);
|
|
|
|
int newCount = fCount - 1;
|
|
|
|
fCount = newCount;
|
|
|
|
fItemArray[n].~T();
|
|
|
|
if (n != newCount) {
|
|
|
|
SkTArrayExt::copy(this, n, newCount);
|
|
|
|
fItemArray[newCount].~T();
|
2013-04-01 20:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Number of elements in the array.
|
|
|
|
*/
|
2011-02-14 16:51:21 +00:00
|
|
|
int count() const { return fCount; }
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Is the array empty.
|
|
|
|
*/
|
2010-12-22 21:39:39 +00:00
|
|
|
bool empty() const { return !fCount; }
|
|
|
|
|
2011-09-13 18:49:13 +00:00
|
|
|
/**
|
2014-03-24 15:55:01 +00:00
|
|
|
* Adds 1 new default-initialized T value and returns it by reference. Note
|
2011-09-13 18:49:13 +00:00
|
|
|
* the reference only remains valid until the next call that adds or removes
|
|
|
|
* elements.
|
|
|
|
*/
|
2010-12-22 21:39:39 +00:00
|
|
|
T& push_back() {
|
2013-06-13 15:13:46 +00:00
|
|
|
T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
|
|
|
|
SkNEW_PLACEMENT(newT, T);
|
|
|
|
return *newT;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 19:06:12 +00:00
|
|
|
/**
|
|
|
|
* Version of above that uses a copy constructor to initialize the new item
|
|
|
|
*/
|
|
|
|
T& push_back(const T& t) {
|
2013-06-13 15:13:46 +00:00
|
|
|
T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
|
|
|
|
SkNEW_PLACEMENT_ARGS(newT, T, (t));
|
|
|
|
return *newT;
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
|
|
|
|
2011-09-13 18:49:13 +00:00
|
|
|
/**
|
2014-03-24 15:55:01 +00:00
|
|
|
* Allocates n more default-initialized T values, and returns the address of
|
|
|
|
* the start of that new range. Note: this address is only valid until the
|
|
|
|
* next API call made on the array that might add or remove elements.
|
2011-09-13 18:49:13 +00:00
|
|
|
*/
|
|
|
|
T* push_back_n(int n) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(n >= 0);
|
2013-06-13 15:13:46 +00:00
|
|
|
T* newTs = reinterpret_cast<T*>(this->push_back_raw(n));
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2013-06-13 15:13:46 +00:00
|
|
|
SkNEW_PLACEMENT(newTs + i, T);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2013-06-13 15:13:46 +00:00
|
|
|
return newTs;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 19:06:12 +00:00
|
|
|
/**
|
|
|
|
* Version of above that uses a copy constructor to initialize all n items
|
|
|
|
* to the same T.
|
|
|
|
*/
|
|
|
|
T* push_back_n(int n, const T& t) {
|
|
|
|
SkASSERT(n >= 0);
|
2013-06-13 15:13:46 +00:00
|
|
|
T* newTs = reinterpret_cast<T*>(this->push_back_raw(n));
|
2011-09-20 19:06:12 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2013-06-13 15:13:46 +00:00
|
|
|
SkNEW_PLACEMENT_ARGS(newTs[i], T, (t));
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
2013-06-13 15:13:46 +00:00
|
|
|
return newTs;
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Version of above that uses a copy constructor to initialize the n items
|
|
|
|
* to separate T values.
|
|
|
|
*/
|
|
|
|
T* push_back_n(int n, const T t[]) {
|
|
|
|
SkASSERT(n >= 0);
|
2013-06-05 15:40:59 +00:00
|
|
|
this->checkRealloc(n);
|
2011-09-20 19:06:12 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2013-06-05 15:40:59 +00:00
|
|
|
SkNEW_PLACEMENT_ARGS(fItemArray + fCount + i, T, (t[i]));
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
|
|
|
fCount += n;
|
|
|
|
return fItemArray + fCount - n;
|
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Removes the last element. Not safe to call when count() == 0.
|
|
|
|
*/
|
2010-12-22 21:39:39 +00:00
|
|
|
void pop_back() {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(fCount > 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
--fCount;
|
|
|
|
fItemArray[fCount].~T();
|
2013-06-05 15:40:59 +00:00
|
|
|
this->checkRealloc(0);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Removes the last n elements. Not safe to call when count() < n.
|
|
|
|
*/
|
2011-02-14 16:51:21 +00:00
|
|
|
void pop_back_n(int n) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(n >= 0);
|
|
|
|
SkASSERT(fCount >= n);
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount -= n;
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2013-06-13 15:13:46 +00:00
|
|
|
fItemArray[fCount + i].~T();
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2013-06-05 15:40:59 +00:00
|
|
|
this->checkRealloc(0);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Pushes or pops from the back to resize. Pushes will be default
|
|
|
|
* initialized.
|
|
|
|
*/
|
2011-02-14 16:51:21 +00:00
|
|
|
void resize_back(int newCount) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(newCount >= 0);
|
2011-02-14 16:51:21 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
if (newCount > fCount) {
|
2013-06-05 15:40:59 +00:00
|
|
|
this->push_back_n(newCount - fCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
} else if (newCount < fCount) {
|
2013-06-05 15:40:59 +00:00
|
|
|
this->pop_back_n(fCount - newCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 18:21:22 +00:00
|
|
|
T* begin() {
|
|
|
|
return fItemArray;
|
|
|
|
}
|
|
|
|
const T* begin() const {
|
|
|
|
return fItemArray;
|
|
|
|
}
|
|
|
|
T* end() {
|
|
|
|
return fItemArray ? fItemArray + fCount : NULL;
|
|
|
|
}
|
|
|
|
const T* end() const {
|
|
|
|
return fItemArray ? fItemArray + fCount : NULL;;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-14 13:54:05 +00:00
|
|
|
* Get the i^th element.
|
|
|
|
*/
|
2011-02-14 16:51:21 +00:00
|
|
|
T& operator[] (int i) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(i < fCount);
|
|
|
|
SkASSERT(i >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
return fItemArray[i];
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
const T& operator[] (int i) const {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(i < fCount);
|
|
|
|
SkASSERT(i >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
return fItemArray[i];
|
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* equivalent to operator[](0)
|
|
|
|
*/
|
|
|
|
T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* equivalent to operator[](count() - 1)
|
|
|
|
*/
|
|
|
|
T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* equivalent to operator[](count()-1-i)
|
|
|
|
*/
|
2011-02-14 16:51:21 +00:00
|
|
|
T& fromBack(int i) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(i >= 0);
|
|
|
|
SkASSERT(i < fCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
return fItemArray[fCount - i - 1];
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
const T& fromBack(int i) const {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(i >= 0);
|
|
|
|
SkASSERT(i < fCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
return fItemArray[fCount - i - 1];
|
|
|
|
}
|
|
|
|
|
2013-03-12 12:26:08 +00:00
|
|
|
bool operator==(const SkTArray<T, MEM_COPY>& right) const {
|
|
|
|
int leftCount = this->count();
|
|
|
|
if (leftCount != right.count()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int index = 0; index < leftCount; ++index) {
|
|
|
|
if (fItemArray[index] != right.fItemArray[index]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SkTArray<T, MEM_COPY>& right) const {
|
|
|
|
return !(*this == right);
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Creates an empty array that will use the passed storage block until it
|
|
|
|
* is insufficiently large to hold the entire array.
|
|
|
|
*/
|
|
|
|
template <int N>
|
|
|
|
SkTArray(SkAlignedSTStorage<N,T>* storage) {
|
|
|
|
this->init(NULL, 0, storage->get(), N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy another array, using preallocated storage if preAllocCount >=
|
|
|
|
* array.count(). Otherwise storage will only be used when array shrinks
|
|
|
|
* to fit.
|
|
|
|
*/
|
|
|
|
template <int N>
|
|
|
|
SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
|
|
|
|
this->init(array.fItemArray, array.fCount, storage->get(), N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a C array, using preallocated storage if preAllocCount >=
|
|
|
|
* count. Otherwise storage will only be used when array shrinks
|
|
|
|
* to fit.
|
|
|
|
*/
|
|
|
|
template <int N>
|
|
|
|
SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
|
|
|
|
this->init(array, count, storage->get(), N);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(const T* array, int count,
|
|
|
|
void* preAllocStorage, int preAllocOrReserveCount) {
|
2011-11-30 18:35:19 +00:00
|
|
|
SkASSERT(count >= 0);
|
|
|
|
SkASSERT(preAllocOrReserveCount >= 0);
|
2011-09-27 19:10:05 +00:00
|
|
|
fCount = count;
|
|
|
|
fReserveCount = (preAllocOrReserveCount > 0) ?
|
|
|
|
preAllocOrReserveCount :
|
|
|
|
gMIN_ALLOC_COUNT;
|
|
|
|
fPreAllocMemArray = preAllocStorage;
|
|
|
|
if (fReserveCount >= fCount &&
|
|
|
|
NULL != preAllocStorage) {
|
|
|
|
fAllocCount = fReserveCount;
|
|
|
|
fMemArray = preAllocStorage;
|
|
|
|
} else {
|
2011-11-30 18:35:19 +00:00
|
|
|
fAllocCount = SkMax32(fCount, fReserveCount);
|
|
|
|
fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
|
2011-10-07 20:54:15 +00:00
|
|
|
SkTArrayExt::copy(this, array);
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
private:
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
static const int gMIN_ALLOC_COUNT = 8;
|
2011-02-14 16:51:21 +00:00
|
|
|
|
2013-06-13 15:13:46 +00:00
|
|
|
// Helper function that makes space for n objects, adjusts the count, but does not initialize
|
|
|
|
// the new objects.
|
|
|
|
void* push_back_raw(int n) {
|
|
|
|
this->checkRealloc(n);
|
|
|
|
void* ptr = fItemArray + fCount;
|
|
|
|
fCount += n;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
inline void checkRealloc(int delta) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(fCount >= 0);
|
|
|
|
SkASSERT(fAllocCount >= 0);
|
2011-02-14 16:51:21 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(-delta <= fCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
int newCount = fCount + delta;
|
2011-10-07 20:54:15 +00:00
|
|
|
int newAllocCount = fAllocCount;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2012-08-14 17:19:08 +00:00
|
|
|
if (newCount > fAllocCount || newCount < (fAllocCount / 3)) {
|
|
|
|
// whether we're growing or shrinking, we leave at least 50% extra space for future
|
|
|
|
// growth (clamped to the reserve count).
|
|
|
|
newAllocCount = SkMax32(newCount + ((newCount + 1) >> 1), fReserveCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2011-10-07 20:54:15 +00:00
|
|
|
if (newAllocCount != fAllocCount) {
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-10-07 20:54:15 +00:00
|
|
|
fAllocCount = newAllocCount;
|
|
|
|
char* newMemArray;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
if (fAllocCount == fReserveCount && NULL != fPreAllocMemArray) {
|
2011-10-07 20:54:15 +00:00
|
|
|
newMemArray = (char*) fPreAllocMemArray;
|
2010-12-22 21:39:39 +00:00
|
|
|
} else {
|
2011-10-07 20:54:15 +00:00
|
|
|
newMemArray = (char*) sk_malloc_throw(fAllocCount*sizeof(T));
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-10-07 20:54:15 +00:00
|
|
|
SkTArrayExt::copyAndDelete<T>(this, newMemArray);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
if (fMemArray != fPreAllocMemArray) {
|
2011-09-14 13:54:05 +00:00
|
|
|
sk_free(fMemArray);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2011-10-07 20:54:15 +00:00
|
|
|
fMemArray = newMemArray;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:13:46 +00:00
|
|
|
friend void* operator new<T>(size_t, SkTArray*, int);
|
|
|
|
|
2014-03-21 19:39:02 +00:00
|
|
|
template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that, int dst, int src);
|
2011-10-07 21:10:39 +00:00
|
|
|
template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that, const X*);
|
|
|
|
template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, true>* that, char*);
|
2011-10-07 20:54:15 +00:00
|
|
|
|
2014-03-21 19:39:02 +00:00
|
|
|
template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that, int dst, int src);
|
2011-10-07 21:10:39 +00:00
|
|
|
template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that, const X*);
|
|
|
|
template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, false>* that, char*);
|
2011-10-07 20:54:15 +00:00
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
int fReserveCount;
|
|
|
|
int fCount;
|
|
|
|
int fAllocCount;
|
2010-12-22 21:39:39 +00:00
|
|
|
void* fPreAllocMemArray;
|
|
|
|
union {
|
|
|
|
T* fItemArray;
|
|
|
|
void* fMemArray;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-06-13 15:13:46 +00:00
|
|
|
// Use the below macro (SkNEW_APPEND_TO_TARRAY) rather than calling this directly
|
|
|
|
template <typename T, bool MEM_COPY>
|
|
|
|
void* operator new(size_t, SkTArray<T, MEM_COPY>* array, int atIndex) {
|
|
|
|
// Currently, we only support adding to the end of the array. When the array class itself
|
|
|
|
// supports random insertion then this should be updated.
|
|
|
|
// SkASSERT(atIndex >= 0 && atIndex <= array->count());
|
|
|
|
SkASSERT(atIndex == array->count());
|
|
|
|
return array->push_back_raw(1);
|
|
|
|
}
|
|
|
|
|
2013-06-14 12:30:50 +00:00
|
|
|
// Skia doesn't use C++ exceptions but it may be compiled with them enabled. Having an op delete
|
|
|
|
// to match the op new silences warnings about missing op delete when a constructor throws an
|
|
|
|
// exception.
|
2013-06-15 07:00:53 +00:00
|
|
|
template <typename T, bool MEM_COPY>
|
2013-06-14 12:45:25 +00:00
|
|
|
void operator delete(void*, SkTArray<T, MEM_COPY>* array, int atIndex) {
|
2013-06-14 12:30:50 +00:00
|
|
|
SK_CRASH();
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:13:46 +00:00
|
|
|
// Constructs a new object as the last element of an SkTArray.
|
|
|
|
#define SkNEW_APPEND_TO_TARRAY(array_ptr, type_name, args) \
|
|
|
|
(new ((array_ptr), (array_ptr)->count()) type_name args)
|
|
|
|
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
/**
|
|
|
|
* Subclass of SkTArray that contains a preallocated memory block for the array.
|
|
|
|
*/
|
2013-02-26 15:40:01 +00:00
|
|
|
template <int N, typename T, bool MEM_COPY = false>
|
|
|
|
class SkSTArray : public SkTArray<T, MEM_COPY> {
|
2011-09-27 19:10:05 +00:00
|
|
|
private:
|
2013-02-26 15:40:01 +00:00
|
|
|
typedef SkTArray<T, MEM_COPY> INHERITED;
|
2011-09-27 19:10:05 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
SkSTArray() : INHERITED(&fStorage) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSTArray(const SkSTArray& array)
|
|
|
|
: INHERITED(array, &fStorage) {
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit SkSTArray(const INHERITED& array)
|
|
|
|
: INHERITED(array, &fStorage) {
|
|
|
|
}
|
|
|
|
|
2013-10-03 15:17:58 +00:00
|
|
|
explicit SkSTArray(int reserveCount)
|
|
|
|
: INHERITED(reserveCount) {
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
SkSTArray(const T* array, int count)
|
|
|
|
: INHERITED(array, count, &fStorage) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSTArray& operator= (const SkSTArray& array) {
|
|
|
|
return *this = *(const INHERITED*)&array;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSTArray& operator= (const INHERITED& array) {
|
|
|
|
INHERITED::operator=(array);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkAlignedSTStorage<N,T> fStorage;
|
|
|
|
};
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#endif
|