2010-12-22 21:39:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2010 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef GrTArray_DEFINED
|
|
|
|
#define GrTArray_DEFINED
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
#include "GrTypes.h"
|
2011-04-20 15:47:04 +00:00
|
|
|
#include "GrTemplates.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
// DATA_TYPE indicates that T has a trivial cons, destructor
|
|
|
|
// and can be shallow-copied
|
|
|
|
template <typename T, bool DATA_TYPE = false> class GrTArray {
|
|
|
|
public:
|
|
|
|
GrTArray() {
|
|
|
|
fCount = 0;
|
|
|
|
fReserveCount = MIN_ALLOC_COUNT;
|
|
|
|
fAllocCount = 0;
|
|
|
|
fMemArray = NULL;
|
|
|
|
fPreAllocMemArray = NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
explicit GrTArray(int reserveCount) {
|
|
|
|
GrAssert(reserveCount >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount = 0;
|
2011-02-14 16:51:21 +00:00
|
|
|
fReserveCount = reserveCount > MIN_ALLOC_COUNT ? reserveCount :
|
|
|
|
MIN_ALLOC_COUNT;
|
2010-12-22 21:39:39 +00:00
|
|
|
fAllocCount = fReserveCount;
|
|
|
|
fMemArray = GrMalloc(sizeof(T) * fReserveCount);
|
|
|
|
fPreAllocMemArray = NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-20 15:47:04 +00:00
|
|
|
template <int N>
|
|
|
|
GrTArray(GrAlignedSTStorage<N,T>* storage) {
|
|
|
|
GrAssert(N > 0);
|
|
|
|
fCount = 0;
|
|
|
|
fReserveCount = N;
|
|
|
|
fAllocCount = N;
|
|
|
|
fMemArray = storage->get();
|
|
|
|
fPreAllocMemArray = storage->get();
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
GrTArray(void* preAllocStorage, int preAllocCount) {
|
|
|
|
GrAssert(preAllocCount >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
// we allow NULL,0 args and revert to the default cons. behavior
|
|
|
|
// this makes it possible for a owner-object to use same constructor
|
|
|
|
// to get either prealloc or nonprealloc behavior based using same line
|
|
|
|
GrAssert((NULL == preAllocStorage) == !preAllocCount);
|
|
|
|
|
|
|
|
fCount = 0;
|
|
|
|
fReserveCount = preAllocCount > 0 ? preAllocCount :
|
|
|
|
MIN_ALLOC_COUNT;
|
|
|
|
fAllocCount = preAllocCount;
|
|
|
|
fMemArray = preAllocStorage;
|
|
|
|
fPreAllocMemArray = preAllocStorage;
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
explicit GrTArray(const GrTArray& array) {
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount = array.count();
|
|
|
|
fReserveCount = MIN_ALLOC_COUNT;
|
|
|
|
fAllocCount = GrMax(fReserveCount, fCount);
|
|
|
|
fMemArray = GrMalloc(sizeof(T) * fAllocCount);
|
|
|
|
fPreAllocMemArray = NULL;
|
2011-04-20 15:47:04 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
if (DATA_TYPE) {
|
|
|
|
memcpy(fMemArray, array.fMemArray, sizeof(T) * fCount);
|
|
|
|
} else {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
new (fItemArray + i) T(array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
GrTArray(const T* array, int count) {
|
|
|
|
GrAssert(count >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount = count;
|
|
|
|
fReserveCount = MIN_ALLOC_COUNT;
|
|
|
|
fAllocCount = GrMax(fReserveCount, fCount);
|
|
|
|
fMemArray = GrMalloc(sizeof(T) * fAllocCount);
|
|
|
|
fPreAllocMemArray = NULL;
|
|
|
|
if (DATA_TYPE) {
|
|
|
|
memcpy(fMemArray, array, sizeof(T) * fCount);
|
|
|
|
} else {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
new (fItemArray + i) T(array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GrTArray(const GrTArray& array,
|
2011-02-14 16:51:21 +00:00
|
|
|
void* preAllocStorage, int preAllocCount) {
|
|
|
|
|
|
|
|
GrAssert(preAllocCount >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
// for same reason as non-copying cons we allow NULL, 0 for prealloc
|
|
|
|
GrAssert((NULL == preAllocStorage) == !preAllocCount);
|
|
|
|
|
|
|
|
fCount = array.count();
|
|
|
|
fReserveCount = preAllocCount > 0 ? preAllocCount :
|
|
|
|
MIN_ALLOC_COUNT;
|
|
|
|
fPreAllocMemArray = preAllocStorage;
|
|
|
|
|
|
|
|
if (fReserveCount >= fCount && preAllocCount) {
|
|
|
|
fAllocCount = fReserveCount;
|
|
|
|
fMemArray = preAllocStorage;
|
|
|
|
} else {
|
|
|
|
fAllocCount = GrMax(fCount, fReserveCount);
|
|
|
|
fMemArray = GrMalloc(fAllocCount * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DATA_TYPE) {
|
|
|
|
memcpy(fMemArray, array.fMemArray, sizeof(T) * fCount);
|
|
|
|
} else {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
new (fItemArray + i) T(array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
GrTArray(const T* array, int count,
|
|
|
|
void* preAllocStorage, int preAllocCount) {
|
|
|
|
|
|
|
|
GrAssert(count >= 0);
|
|
|
|
GrAssert(preAllocCount >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
// for same reason as non-copying cons we allow NULL, 0 for prealloc
|
|
|
|
GrAssert((NULL == preAllocStorage) == !preAllocCount);
|
|
|
|
|
|
|
|
fCount = count;
|
|
|
|
fReserveCount = (preAllocCount > 0) ? preAllocCount :
|
|
|
|
MIN_ALLOC_COUNT;
|
|
|
|
fPreAllocMemArray = preAllocStorage;
|
|
|
|
|
|
|
|
if (fReserveCount >= fCount && preAllocCount) {
|
|
|
|
fAllocCount = fReserveCount;
|
|
|
|
fMemArray = preAllocStorage;
|
|
|
|
} else {
|
|
|
|
fAllocCount = GrMax(fCount, fReserveCount);
|
|
|
|
fMemArray = GrMalloc(fAllocCount * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DATA_TYPE) {
|
|
|
|
memcpy(fMemArray, array, sizeof(T) * fCount);
|
|
|
|
} else {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
new (fItemArray + i) T(array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GrTArray& operator =(const GrTArray& 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;
|
|
|
|
checkRealloc((int)array.count());
|
|
|
|
fCount = array.count();
|
|
|
|
if (DATA_TYPE) {
|
|
|
|
memcpy(fMemArray, array.fMemArray, sizeof(T) * fCount);
|
|
|
|
} else {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
new (fItemArray + i) T(array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~GrTArray() {
|
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) {
|
|
|
|
GrFree(fMemArray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 13:54:13 +00:00
|
|
|
void reset() { this->pop_back_n(fCount); }
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
int count() const { return fCount; }
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
bool empty() const { return !fCount; }
|
|
|
|
|
|
|
|
T& push_back() {
|
|
|
|
checkRealloc(1);
|
|
|
|
new ((char*)fMemArray+sizeof(T)*fCount) T;
|
|
|
|
++fCount;
|
|
|
|
return fItemArray[fCount-1];
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
void push_back_n(int n) {
|
|
|
|
GrAssert(n >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
checkRealloc(n);
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
new (fItemArray + fCount + i) T;
|
|
|
|
}
|
|
|
|
fCount += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_back() {
|
2011-02-14 16:51:21 +00:00
|
|
|
GrAssert(fCount > 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
--fCount;
|
|
|
|
fItemArray[fCount].~T();
|
|
|
|
checkRealloc(0);
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
void pop_back_n(int n) {
|
|
|
|
GrAssert(n >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
GrAssert(fCount >= n);
|
|
|
|
fCount -= n;
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
checkRealloc(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushes or pops from the back to resize
|
2011-02-14 16:51:21 +00:00
|
|
|
void resize_back(int newCount) {
|
|
|
|
GrAssert(newCount >= 0);
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
if (newCount > fCount) {
|
|
|
|
push_back_n(newCount - fCount);
|
|
|
|
} else if (newCount < fCount) {
|
|
|
|
pop_back_n(fCount - newCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
T& operator[] (int i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
GrAssert(i < fCount);
|
2011-02-14 16:51:21 +00:00
|
|
|
GrAssert(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 {
|
2010-12-22 21:39:39 +00:00
|
|
|
GrAssert(i < fCount);
|
2011-02-14 16:51:21 +00:00
|
|
|
GrAssert(i >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
return fItemArray[i];
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
T& front() { GrAssert(fCount > 0); return fItemArray[0];}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
const T& front() const { GrAssert(fCount > 0); return fItemArray[0];}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
T& back() { GrAssert(fCount); return fItemArray[fCount - 1];}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
const T& back() const { GrAssert(fCount > 0); return fItemArray[fCount - 1];}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
T& fromBack(int i) {
|
|
|
|
GrAssert(i >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
GrAssert(i < fCount);
|
|
|
|
return fItemArray[fCount - i - 1];
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
const T& fromBack(int i) const {
|
|
|
|
GrAssert(i >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
GrAssert(i < fCount);
|
|
|
|
return fItemArray[fCount - i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
static const int MIN_ALLOC_COUNT = 8;
|
|
|
|
|
|
|
|
inline void checkRealloc(int delta) {
|
|
|
|
GrAssert(fCount >= 0);
|
|
|
|
GrAssert(fAllocCount >= 0);
|
|
|
|
|
|
|
|
GrAssert(-delta <= fCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
int newCount = fCount + delta;
|
|
|
|
int fNewAllocCount = fAllocCount;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
if (newCount > fAllocCount) {
|
|
|
|
fNewAllocCount = GrMax(newCount + ((newCount + 1) >> 1),
|
|
|
|
fReserveCount);
|
|
|
|
} else if (newCount < fAllocCount / 3) {
|
|
|
|
fNewAllocCount = GrMax(fAllocCount / 2, fReserveCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fNewAllocCount != fAllocCount) {
|
|
|
|
|
|
|
|
fAllocCount = fNewAllocCount;
|
|
|
|
char* fNewMemArray;
|
|
|
|
|
|
|
|
if (fAllocCount == fReserveCount && NULL != fPreAllocMemArray) {
|
|
|
|
fNewMemArray = (char*) fPreAllocMemArray;
|
|
|
|
} else {
|
|
|
|
fNewMemArray = (char*) GrMalloc(fAllocCount*sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DATA_TYPE) {
|
|
|
|
memcpy(fNewMemArray, fMemArray, fCount * sizeof(T));
|
|
|
|
} else {
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2010-12-22 21:39:39 +00:00
|
|
|
new (fNewMemArray + sizeof(T) * i) T(fItemArray[i]);
|
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fMemArray != fPreAllocMemArray) {
|
|
|
|
GrFree(fMemArray);
|
|
|
|
}
|
|
|
|
fMemArray = fNewMemArray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|