2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
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.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkTDArray_DEFINED
|
|
|
|
#define SkTDArray_DEFINED
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2014-07-09 18:13:55 +00:00
|
|
|
template <typename T> class SkTDArray {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
SkTDArray() {
|
|
|
|
fReserve = fCount = 0;
|
|
|
|
fArray = NULL;
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
fData = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2013-10-16 17:48:11 +00:00
|
|
|
SkTDArray(const T src[], int count) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(src || count == 0);
|
|
|
|
|
|
|
|
fReserve = fCount = 0;
|
|
|
|
fArray = NULL;
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
fData = NULL;
|
|
|
|
#endif
|
|
|
|
if (count) {
|
|
|
|
fArray = (T*)sk_malloc_throw(count * sizeof(T));
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
fData = (ArrayT*)fArray;
|
|
|
|
#endif
|
|
|
|
memcpy(fArray, src, sizeof(T) * count);
|
|
|
|
fReserve = fCount = count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkTDArray(const SkTDArray<T>& src) {
|
|
|
|
fReserve = fCount = 0;
|
|
|
|
fArray = NULL;
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
fData = NULL;
|
|
|
|
#endif
|
|
|
|
SkTDArray<T> tmp(src.fArray, src.fCount);
|
|
|
|
this->swap(tmp);
|
|
|
|
}
|
|
|
|
~SkTDArray() {
|
|
|
|
sk_free(fArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTDArray<T>& operator=(const SkTDArray<T>& src) {
|
|
|
|
if (this != &src) {
|
|
|
|
if (src.fCount > fReserve) {
|
|
|
|
SkTDArray<T> tmp(src.fArray, src.fCount);
|
|
|
|
this->swap(tmp);
|
|
|
|
} else {
|
|
|
|
memcpy(fArray, src.fArray, sizeof(T) * src.fCount);
|
|
|
|
fCount = src.fCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-07-20 19:55:42 +00:00
|
|
|
friend bool operator==(const SkTDArray<T>& a, const SkTDArray<T>& b) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return a.fCount == b.fCount &&
|
|
|
|
(a.fCount == 0 ||
|
|
|
|
!memcmp(a.fArray, b.fArray, a.fCount * sizeof(T)));
|
|
|
|
}
|
2013-05-29 18:05:52 +00:00
|
|
|
friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
void swap(SkTDArray<T>& other) {
|
|
|
|
SkTSwap(fArray, other.fArray);
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
SkTSwap(fData, other.fData);
|
|
|
|
#endif
|
|
|
|
SkTSwap(fReserve, other.fReserve);
|
|
|
|
SkTSwap(fCount, other.fCount);
|
|
|
|
}
|
|
|
|
|
2009-08-25 16:03:59 +00:00
|
|
|
/** Return a ptr to the array of data, to be freed with sk_free. This also
|
|
|
|
resets the SkTDArray to be empty.
|
|
|
|
*/
|
|
|
|
T* detach() {
|
|
|
|
T* array = fArray;
|
|
|
|
fArray = NULL;
|
|
|
|
fReserve = fCount = 0;
|
|
|
|
SkDEBUGCODE(fData = NULL;)
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool isEmpty() const { return fCount == 0; }
|
2011-11-28 19:54:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of elements in the array
|
|
|
|
*/
|
2013-10-16 17:48:11 +00:00
|
|
|
int count() const { return fCount; }
|
2011-11-28 19:54:12 +00:00
|
|
|
|
2014-02-11 10:17:02 +00:00
|
|
|
/**
|
|
|
|
* Return the total number of elements allocated.
|
|
|
|
* reserved() - count() gives you the number of elements you can add
|
|
|
|
* without causing an allocation.
|
|
|
|
*/
|
|
|
|
int reserved() const { return fReserve; }
|
|
|
|
|
2011-11-28 19:54:12 +00:00
|
|
|
/**
|
|
|
|
* return the number of bytes in the array: count * sizeof(T)
|
|
|
|
*/
|
|
|
|
size_t bytes() const { return fCount * sizeof(T); }
|
|
|
|
|
2013-02-28 19:03:13 +00:00
|
|
|
T* begin() { return fArray; }
|
|
|
|
const T* begin() const { return fArray; }
|
|
|
|
T* end() { return fArray ? fArray + fCount : NULL; }
|
|
|
|
const T* end() const { return fArray ? fArray + fCount : NULL; }
|
|
|
|
|
|
|
|
T& operator[](int index) {
|
2013-10-16 17:48:11 +00:00
|
|
|
SkASSERT(index < fCount);
|
2013-02-28 19:03:13 +00:00
|
|
|
return fArray[index];
|
|
|
|
}
|
|
|
|
const T& operator[](int index) const {
|
2013-10-16 17:48:11 +00:00
|
|
|
SkASSERT(index < fCount);
|
2008-12-17 15:59:43 +00:00
|
|
|
return fArray[index];
|
|
|
|
}
|
2013-01-15 02:01:40 +00:00
|
|
|
|
2013-02-28 19:03:13 +00:00
|
|
|
T& getAt(int index) {
|
|
|
|
return (*this)[index];
|
|
|
|
}
|
|
|
|
const T& getAt(int index) const {
|
2013-01-14 18:49:19 +00:00
|
|
|
return (*this)[index];
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
void reset() {
|
|
|
|
if (fArray) {
|
|
|
|
sk_free(fArray);
|
|
|
|
fArray = NULL;
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
fData = NULL;
|
|
|
|
#endif
|
|
|
|
fReserve = fCount = 0;
|
|
|
|
} else {
|
|
|
|
SkASSERT(fReserve == 0 && fCount == 0);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void rewind() {
|
|
|
|
// same as setCount(0)
|
|
|
|
fCount = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-11 18:22:04 +00:00
|
|
|
/**
|
|
|
|
* Sets the number of elements in the array.
|
|
|
|
* If the array does not have space for count elements, it will increase
|
|
|
|
* the storage allocated to some amount greater than that required.
|
|
|
|
* It will never shrink the shrink the storage.
|
|
|
|
*/
|
2013-10-16 17:48:11 +00:00
|
|
|
void setCount(int count) {
|
2014-02-11 18:22:04 +00:00
|
|
|
SkASSERT(count >= 0);
|
2008-12-17 15:59:43 +00:00
|
|
|
if (count > fReserve) {
|
2014-02-11 18:22:04 +00:00
|
|
|
this->resizeStorageToAtLeast(count);
|
2014-02-11 15:07:26 +00:00
|
|
|
}
|
2014-02-11 18:22:04 +00:00
|
|
|
fCount = count;
|
|
|
|
}
|
|
|
|
|
2013-10-16 17:48:11 +00:00
|
|
|
void setReserve(int reserve) {
|
2008-12-17 15:59:43 +00:00
|
|
|
if (reserve > fReserve) {
|
2014-02-11 18:22:04 +00:00
|
|
|
this->resizeStorageToAtLeast(reserve);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* prepend() {
|
2014-02-11 18:22:04 +00:00
|
|
|
this->adjustCount(1);
|
2008-12-17 15:59:43 +00:00
|
|
|
memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T));
|
|
|
|
return fArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* append() {
|
|
|
|
return this->append(1, NULL);
|
|
|
|
}
|
2013-10-16 17:48:11 +00:00
|
|
|
T* append(int count, const T* src = NULL) {
|
|
|
|
int oldCount = fCount;
|
2008-12-17 15:59:43 +00:00
|
|
|
if (count) {
|
|
|
|
SkASSERT(src == NULL || fArray == NULL ||
|
|
|
|
src + count <= fArray || fArray + oldCount <= src);
|
|
|
|
|
2014-02-11 18:22:04 +00:00
|
|
|
this->adjustCount(count);
|
2008-12-17 15:59:43 +00:00
|
|
|
if (src) {
|
|
|
|
memcpy(fArray + oldCount, src, sizeof(T) * count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fArray + oldCount;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
T* appendClear() {
|
2012-08-23 18:09:54 +00:00
|
|
|
T* result = this->append();
|
2008-12-17 15:59:43 +00:00
|
|
|
*result = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-16 17:48:11 +00:00
|
|
|
T* insert(int index) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->insert(index, 1, NULL);
|
|
|
|
}
|
2013-10-16 17:48:11 +00:00
|
|
|
T* insert(int index, int count, const T* src = NULL) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(count);
|
|
|
|
SkASSERT(index <= fCount);
|
2012-05-17 15:38:00 +00:00
|
|
|
size_t oldCount = fCount;
|
2014-02-11 18:22:04 +00:00
|
|
|
this->adjustCount(count);
|
2008-12-17 15:59:43 +00:00
|
|
|
T* dst = fArray + index;
|
|
|
|
memmove(dst + count, dst, sizeof(T) * (oldCount - index));
|
|
|
|
if (src) {
|
|
|
|
memcpy(dst, src, sizeof(T) * count);
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2013-10-16 17:48:11 +00:00
|
|
|
void remove(int index, int count = 1) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(index + count <= fCount);
|
|
|
|
fCount = fCount - count;
|
|
|
|
memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - index));
|
|
|
|
}
|
|
|
|
|
2013-10-16 17:48:11 +00:00
|
|
|
void removeShuffle(int index) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(index < fCount);
|
2013-10-16 17:48:11 +00:00
|
|
|
int newCount = fCount - 1;
|
2008-12-17 15:59:43 +00:00
|
|
|
fCount = newCount;
|
|
|
|
if (index != newCount) {
|
|
|
|
memcpy(fArray + index, fArray + newCount, sizeof(T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int find(const T& elem) const {
|
|
|
|
const T* iter = fArray;
|
|
|
|
const T* stop = fArray + fCount;
|
|
|
|
|
|
|
|
for (; iter < stop; iter++) {
|
|
|
|
if (*iter == elem) {
|
|
|
|
return (int) (iter - fArray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rfind(const T& elem) const {
|
|
|
|
const T* iter = fArray + fCount;
|
|
|
|
const T* stop = fArray;
|
|
|
|
|
|
|
|
while (iter > stop) {
|
|
|
|
if (*--iter == elem) {
|
2014-02-04 16:03:51 +00:00
|
|
|
return SkToInt(iter - stop);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-13 14:53:18 +00:00
|
|
|
/**
|
|
|
|
* Returns true iff the array contains this element.
|
|
|
|
*/
|
|
|
|
bool contains(const T& elem) const {
|
|
|
|
return (this->find(elem) >= 0);
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:43:15 +00:00
|
|
|
/**
|
|
|
|
* Copies up to max elements into dst. The number of items copied is
|
|
|
|
* capped by count - index. The actual number copied is returned.
|
|
|
|
*/
|
2013-10-16 17:48:11 +00:00
|
|
|
int copyRange(T* dst, int index, int max) const {
|
2012-06-07 21:43:15 +00:00
|
|
|
SkASSERT(max >= 0);
|
|
|
|
SkASSERT(!max || dst);
|
|
|
|
if (index >= fCount) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int count = SkMin32(max, fCount - index);
|
|
|
|
memcpy(dst, fArray + index, sizeof(T) * count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy(T* dst) const {
|
2013-07-01 17:04:32 +00:00
|
|
|
this->copyRange(dst, 0, fCount);
|
2012-06-07 21:43:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// routines to treat the array like a stack
|
2014-09-11 13:36:11 +00:00
|
|
|
T* push() { return this->append(); }
|
|
|
|
void push(const T& elem) { *this->append() = elem; }
|
|
|
|
const T& top() const { return (*this)[fCount - 1]; }
|
|
|
|
T& top() { return (*this)[fCount - 1]; }
|
|
|
|
void pop(T* elem) { SkASSERT(fCount > 0); if (elem) *elem = (*this)[fCount - 1]; --fCount; }
|
|
|
|
void pop() { SkASSERT(fCount > 0); --fCount; }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
void deleteAll() {
|
|
|
|
T* iter = fArray;
|
|
|
|
T* stop = fArray + fCount;
|
|
|
|
while (iter < stop) {
|
2012-08-16 20:30:18 +00:00
|
|
|
SkDELETE (*iter);
|
2008-12-17 15:59:43 +00:00
|
|
|
iter += 1;
|
|
|
|
}
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeAll() {
|
|
|
|
T* iter = fArray;
|
|
|
|
T* stop = fArray + fCount;
|
|
|
|
while (iter < stop) {
|
|
|
|
sk_free(*iter);
|
|
|
|
iter += 1;
|
|
|
|
}
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unrefAll() {
|
|
|
|
T* iter = fArray;
|
|
|
|
T* stop = fArray + fCount;
|
|
|
|
while (iter < stop) {
|
|
|
|
(*iter)->unref();
|
|
|
|
iter += 1;
|
|
|
|
}
|
|
|
|
this->reset();
|
|
|
|
}
|
2009-07-03 02:52:27 +00:00
|
|
|
|
|
|
|
void safeUnrefAll() {
|
|
|
|
T* iter = fArray;
|
|
|
|
T* stop = fArray + fCount;
|
|
|
|
while (iter < stop) {
|
|
|
|
SkSafeUnref(*iter);
|
|
|
|
iter += 1;
|
|
|
|
}
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:03:13 +00:00
|
|
|
void visitAll(void visitor(T&)) {
|
2013-01-07 17:23:00 +00:00
|
|
|
T* stop = this->end();
|
|
|
|
for (T* curr = this->begin(); curr < stop; curr++) {
|
|
|
|
if (*curr) {
|
|
|
|
visitor(*curr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
void validate() const {
|
|
|
|
SkASSERT((fReserve == 0 && fArray == NULL) ||
|
|
|
|
(fReserve > 0 && fArray != NULL));
|
|
|
|
SkASSERT(fCount <= fReserve);
|
|
|
|
SkASSERT(fData == (ArrayT*)fArray);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-10 01:22:41 +00:00
|
|
|
void shrinkToFit() {
|
|
|
|
fReserve = fCount;
|
|
|
|
fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
enum {
|
|
|
|
kDebugArraySize = 16
|
|
|
|
};
|
|
|
|
typedef T ArrayT[kDebugArraySize];
|
|
|
|
ArrayT* fData;
|
|
|
|
#endif
|
|
|
|
T* fArray;
|
2013-10-16 17:48:11 +00:00
|
|
|
int fReserve;
|
|
|
|
int fCount;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-02-11 18:22:04 +00:00
|
|
|
/**
|
|
|
|
* Adjusts the number of elements in the array.
|
|
|
|
* This is the same as calling setCount(count() + delta).
|
|
|
|
*/
|
|
|
|
void adjustCount(int delta) {
|
|
|
|
this->setCount(fCount + delta);
|
|
|
|
}
|
2014-02-11 15:56:57 +00:00
|
|
|
|
2014-02-11 18:22:04 +00:00
|
|
|
/**
|
|
|
|
* Increase the storage allocation such that it can hold (fCount + extra)
|
|
|
|
* elements.
|
|
|
|
* It never shrinks the allocation, and it may increase the allocation by
|
|
|
|
* more than is strictly required, based on a private growth heuristic.
|
|
|
|
*
|
|
|
|
* note: does NOT modify fCount
|
|
|
|
*/
|
|
|
|
void resizeStorageToAtLeast(int count) {
|
|
|
|
SkASSERT(count > fReserve);
|
2014-02-13 18:35:54 +00:00
|
|
|
fReserve = count + 4;
|
|
|
|
fReserve += fReserve / 4;
|
|
|
|
fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
fData = (ArrayT*)fArray;
|
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|