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
|
|
|
|
2018-09-07 17:32:47 +00:00
|
|
|
#include "../private/SkSafe32.h"
|
|
|
|
#include "../private/SkTLogic.h"
|
|
|
|
#include "../private/SkTemplates.h"
|
2011-09-14 13:54:05 +00:00
|
|
|
#include "SkTypes.h"
|
2015-08-19 18:56:48 +00:00
|
|
|
|
|
|
|
#include <new>
|
2016-01-05 22:59:40 +00:00
|
|
|
#include <utility>
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2017-03-09 18:34:09 +00:00
|
|
|
/** When MEM_MOVE is true T will be bit copied when moved.
|
|
|
|
When MEM_MOVE 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.
|
|
|
|
*/
|
2017-03-09 18:34:09 +00:00
|
|
|
template <typename T, bool MEM_MOVE = false> 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
|
|
|
|
*/
|
2017-03-16 00:52:35 +00:00
|
|
|
SkTArray() { this->init(); }
|
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 an empty array that will preallocate space for reserveCount
|
2011-09-14 13:54:05 +00:00
|
|
|
* elements.
|
|
|
|
*/
|
2017-03-16 00:52:35 +00:00
|
|
|
explicit SkTArray(int reserveCount) { this->init(0, reserveCount); }
|
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.
|
|
|
|
*/
|
2016-04-20 17:22:20 +00:00
|
|
|
explicit SkTArray(const SkTArray& that) {
|
2017-03-16 00:52:35 +00:00
|
|
|
this->init(that.fCount);
|
2016-04-20 17:22:20 +00:00
|
|
|
this->copy(that.fItemArray);
|
|
|
|
}
|
2017-03-16 00:52:35 +00:00
|
|
|
|
2016-04-20 17:22:20 +00:00
|
|
|
explicit SkTArray(SkTArray&& that) {
|
2017-03-16 00:52:35 +00:00
|
|
|
// TODO: If 'that' owns its memory why don't we just steal the pointer?
|
|
|
|
this->init(that.fCount);
|
2016-04-20 17:22:20 +00:00
|
|
|
that.move(fMemArray);
|
|
|
|
that.fCount = 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) {
|
2017-03-16 00:52:35 +00:00
|
|
|
this->init(count);
|
2016-04-20 17:22:20 +00:00
|
|
|
this->copy(array);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
SkTArray& operator=(const SkTArray& that) {
|
2017-03-22 17:33:21 +00:00
|
|
|
if (this == &that) {
|
|
|
|
return *this;
|
|
|
|
}
|
2016-04-20 17:22:20 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
fCount = 0;
|
|
|
|
this->checkRealloc(that.count());
|
|
|
|
fCount = that.count();
|
|
|
|
this->copy(that.fItemArray);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-03-16 00:52:35 +00:00
|
|
|
SkTArray& operator=(SkTArray&& that) {
|
2017-03-22 17:33:21 +00:00
|
|
|
if (this == &that) {
|
|
|
|
return *this;
|
|
|
|
}
|
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;
|
2016-04-20 17:22:20 +00:00
|
|
|
this->checkRealloc(that.count());
|
|
|
|
fCount = that.count();
|
|
|
|
that.move(fMemArray);
|
|
|
|
that.fCount = 0;
|
2010-12-22 21:39:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-07-31 18:53:11 +00:00
|
|
|
~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();
|
|
|
|
}
|
2017-03-16 00:52:35 +00:00
|
|
|
if (fOwnMemory) {
|
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
|
|
|
/**
|
2017-06-16 10:47:30 +00:00
|
|
|
* Resets to count() == 0 and resets any reserve count.
|
2011-09-14 13:54:05 +00:00
|
|
|
*/
|
2017-06-16 10:47:30 +00:00
|
|
|
void reset() {
|
|
|
|
this->pop_back_n(fCount);
|
|
|
|
fReserved = false;
|
|
|
|
}
|
2011-03-03 13:54:13 +00:00
|
|
|
|
2013-06-05 15:40:59 +00:00
|
|
|
/**
|
2017-06-16 10:47:30 +00:00
|
|
|
* Resets to count() = n newly constructed T objects and resets any reserve count.
|
2013-06-05 15:40:59 +00:00
|
|
|
*/
|
|
|
|
void reset(int n) {
|
|
|
|
SkASSERT(n >= 0);
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
2016-04-20 17:22:20 +00:00
|
|
|
// Set fCount to 0 before calling checkRealloc so that no elements are moved.
|
2013-06-05 15:40:59 +00:00
|
|
|
fCount = 0;
|
|
|
|
this->checkRealloc(n);
|
|
|
|
fCount = n;
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2015-08-26 20:07:48 +00:00
|
|
|
new (fItemArray + i) T;
|
2013-06-05 15:40:59 +00:00
|
|
|
}
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = false;
|
2013-06-05 15:40:59 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
/**
|
2017-06-16 10:47:30 +00:00
|
|
|
* Resets to a copy of a C array and resets any reserve count.
|
2013-04-01 20:06:51 +00:00
|
|
|
*/
|
|
|
|
void reset(const T* array, int count) {
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
2016-04-20 17:22:20 +00:00
|
|
|
fCount = 0;
|
|
|
|
this->checkRealloc(count);
|
2013-04-01 20:06:51 +00:00
|
|
|
fCount = count;
|
2016-02-09 17:14:28 +00:00
|
|
|
this->copy(array);
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures there is enough reserved space for n additional elements. The is guaranteed at least
|
|
|
|
* until the array size grows above n and subsequently shrinks below n, any version of reset()
|
|
|
|
* is called, or reserve() is called again.
|
|
|
|
*/
|
|
|
|
void reserve(int n) {
|
|
|
|
SkASSERT(n >= 0);
|
|
|
|
if (n > 0) {
|
|
|
|
this->checkRealloc(n);
|
|
|
|
fReserved = fOwnMemory;
|
|
|
|
} else {
|
|
|
|
fReserved = false;
|
|
|
|
}
|
2014-03-21 19:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeShuffle(int n) {
|
|
|
|
SkASSERT(n < fCount);
|
|
|
|
int newCount = fCount - 1;
|
|
|
|
fCount = newCount;
|
|
|
|
fItemArray[n].~T();
|
|
|
|
if (n != newCount) {
|
2016-02-09 17:14:28 +00:00
|
|
|
this->move(n, newCount);
|
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() {
|
2016-04-21 15:34:00 +00:00
|
|
|
void* newT = this->push_back_raw(1);
|
|
|
|
return *new (newT) T;
|
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) {
|
2016-04-21 15:34:00 +00:00
|
|
|
void* newT = this->push_back_raw(1);
|
|
|
|
return *new (newT) T(t);
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 21:53:22 +00:00
|
|
|
/**
|
|
|
|
* Version of above that uses a move constructor to initialize the new item
|
|
|
|
*/
|
|
|
|
T& push_back(T&& t) {
|
2016-04-21 15:34:00 +00:00
|
|
|
void* newT = this->push_back_raw(1);
|
|
|
|
return *new (newT) T(std::move(t));
|
2016-03-04 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 19:45:49 +00:00
|
|
|
/**
|
|
|
|
* Construct a new T at the back of this array.
|
|
|
|
*/
|
|
|
|
template<class... Args> T& emplace_back(Args&&... args) {
|
2016-04-21 15:34:00 +00:00
|
|
|
void* newT = this->push_back_raw(1);
|
2016-01-05 22:59:40 +00:00
|
|
|
return *new (newT) T(std::forward<Args>(args)...);
|
2015-09-23 19:45:49 +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);
|
2016-04-21 17:52:03 +00:00
|
|
|
void* newTs = this->push_back_raw(n);
|
2011-02-14 16:51:21 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2016-04-21 17:52:03 +00:00
|
|
|
new (static_cast<char*>(newTs) + i * sizeof(T)) T;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2016-04-21 17:52:03 +00:00
|
|
|
return static_cast<T*>(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);
|
2016-04-21 17:52:03 +00:00
|
|
|
void* newTs = this->push_back_raw(n);
|
2011-09-20 19:06:12 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2016-04-21 17:52:03 +00:00
|
|
|
new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
2016-04-21 17:52:03 +00:00
|
|
|
return static_cast<T*>(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) {
|
2015-08-26 20:07:48 +00:00
|
|
|
new (fItemArray + fCount + i) T(t[i]);
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
|
|
|
fCount += n;
|
|
|
|
return fItemArray + fCount - n;
|
|
|
|
}
|
|
|
|
|
Batched implementation of drawLattice() for GPU
Bechmarks (Nexus 6P):
Src=100x100, Dst=250x250, NumRects=9
Android 77.7us
Skia (without patch) 57.2us
Skia (with patch) 30.9us
Src=100x100, Dst=500x500, NumRects=9
Android 77.0us
Skia (without patch) 56.9us
Skia (with patch) 31.8us
Src=100x100, Dst=1000x1000, NumRects=9
Android 180us
Skia (without patch) 96.8us
Skia (with patch) 70.5us
Src=100x100, Dst=250x250, NumRects=15
Android 208us
Skia (without patch) 155us
Skia (with patch) 38.2us
Src=100x100, Dst=500x500, NumRects=15
Android 207us
Skia (without patch) 152us
Skia (with patch) 38.4us
Src=100x100, Dst=1000x1000, NumRects=15
Android 233us
Skia (without patch) 156us
Skia (with patch) 99.9us
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2255963002
Committed: https://skia.googlesource.com/skia/+/93242c4ae50dfcc0d922cdb3ba80bbc7b4bbe93d
Review-Url: https://codereview.chromium.org/2255963002
2016-08-18 22:46:03 +00:00
|
|
|
/**
|
|
|
|
* Version of above that uses the move constructor to set n items.
|
|
|
|
*/
|
|
|
|
T* move_back_n(int n, T* t) {
|
|
|
|
SkASSERT(n >= 0);
|
|
|
|
this->checkRealloc(n);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
new (fItemArray + fCount + i) T(std::move(t[i]));
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 19:54:28 +00:00
|
|
|
/** Swaps the contents of this array with that array. Does a pointer swap if possible,
|
|
|
|
otherwise copies the T values. */
|
2018-06-18 19:11:00 +00:00
|
|
|
void swap(SkTArray& that) {
|
|
|
|
using std::swap;
|
|
|
|
if (this == &that) {
|
2015-02-11 03:46:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-18 19:11:00 +00:00
|
|
|
if (fOwnMemory && that.fOwnMemory) {
|
|
|
|
swap(fItemArray, that.fItemArray);
|
|
|
|
swap(fCount, that.fCount);
|
|
|
|
swap(fAllocCount, that.fAllocCount);
|
2015-02-06 19:54:28 +00:00
|
|
|
} else {
|
|
|
|
// This could be more optimal...
|
2018-06-18 19:11:00 +00:00
|
|
|
SkTArray copy(std::move(that));
|
|
|
|
that = std::move(*this);
|
2016-04-20 17:22:20 +00:00
|
|
|
*this = std::move(copy);
|
2015-02-06 19:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 18:21:22 +00:00
|
|
|
T* begin() {
|
|
|
|
return fItemArray;
|
|
|
|
}
|
|
|
|
const T* begin() const {
|
|
|
|
return fItemArray;
|
|
|
|
}
|
|
|
|
T* end() {
|
2017-08-28 14:34:05 +00:00
|
|
|
return fItemArray ? fItemArray + fCount : nullptr;
|
2013-03-01 18:21:22 +00:00
|
|
|
}
|
|
|
|
const T* end() const {
|
2017-08-28 14:34:05 +00:00
|
|
|
return fItemArray ? fItemArray + fCount : nullptr;
|
2013-03-01 18:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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];
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:34:09 +00:00
|
|
|
bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
|
2013-03-12 12:26:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:34:09 +00:00
|
|
|
bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
|
2013-03-12 12:26:08 +00:00
|
|
|
return !(*this == right);
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
inline int allocCntForTest() const;
|
|
|
|
|
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) {
|
2017-03-16 00:52:35 +00:00
|
|
|
this->initWithPreallocatedStorage(0, storage->get(), N);
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2017-03-16 00:52:35 +00:00
|
|
|
this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
|
2016-04-20 17:22:20 +00:00
|
|
|
this->copy(array.fItemArray);
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:17:15 +00:00
|
|
|
/**
|
|
|
|
* Move another array, using preallocated storage if preAllocCount >=
|
|
|
|
* array.count(). Otherwise storage will only be used when array shrinks
|
|
|
|
* to fit.
|
|
|
|
*/
|
|
|
|
template <int N>
|
|
|
|
SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
|
2017-03-16 00:52:35 +00:00
|
|
|
this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
|
2017-03-09 17:17:15 +00:00
|
|
|
array.move(fMemArray);
|
|
|
|
array.fCount = 0;
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-03-16 00:52:35 +00:00
|
|
|
this->initWithPreallocatedStorage(count, storage->get(), N);
|
2016-04-20 17:22:20 +00:00
|
|
|
this->copy(array);
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
private:
|
|
|
|
void init(int count = 0, int reserveCount = 0) {
|
2011-11-30 18:35:19 +00:00
|
|
|
SkASSERT(count >= 0);
|
2017-03-16 00:52:35 +00:00
|
|
|
SkASSERT(reserveCount >= 0);
|
|
|
|
fCount = count;
|
|
|
|
if (!count && !reserveCount) {
|
|
|
|
fAllocCount = 0;
|
|
|
|
fMemArray = nullptr;
|
2017-12-14 18:00:05 +00:00
|
|
|
fOwnMemory = true;
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = false;
|
2011-09-27 19:10:05 +00:00
|
|
|
} else {
|
2017-03-16 00:52:35 +00:00
|
|
|
fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
|
2018-01-05 16:20:10 +00:00
|
|
|
fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
|
2017-03-16 00:52:35 +00:00
|
|
|
fOwnMemory = true;
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = reserveCount > 0;
|
2017-03-16 00:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
|
|
|
|
SkASSERT(count >= 0);
|
|
|
|
SkASSERT(preallocCount > 0);
|
|
|
|
SkASSERT(preallocStorage);
|
|
|
|
fCount = count;
|
|
|
|
fMemArray = nullptr;
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = false;
|
2017-03-16 00:52:35 +00:00
|
|
|
if (count > preallocCount) {
|
|
|
|
fAllocCount = SkTMax(count, kMinHeapAllocCount);
|
2018-01-05 16:20:10 +00:00
|
|
|
fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
|
2017-03-16 00:52:35 +00:00
|
|
|
fOwnMemory = true;
|
|
|
|
} else {
|
|
|
|
fAllocCount = preallocCount;
|
|
|
|
fMemArray = preallocStorage;
|
|
|
|
fOwnMemory = false;
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 17:14:28 +00:00
|
|
|
/** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
|
|
|
|
* In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
|
|
|
|
*/
|
2017-03-09 18:34:09 +00:00
|
|
|
void copy(const T* src) {
|
|
|
|
// Some types may be trivially copyable, in which case we *could* use memcopy; but
|
|
|
|
// MEM_MOVE == true implies that the type is trivially movable, and not necessarily
|
|
|
|
// trivially copyable (think sk_sp<>). So short of adding another template arg, we
|
|
|
|
// must be conservative and use copy construction.
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
new (fItemArray + i) T(src[i]);
|
|
|
|
}
|
2016-02-09 17:14:28 +00:00
|
|
|
}
|
2017-03-09 18:34:09 +00:00
|
|
|
|
|
|
|
template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
|
2016-02-09 17:14:28 +00:00
|
|
|
memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
|
|
|
|
}
|
2017-03-09 18:34:09 +00:00
|
|
|
template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
|
2016-02-09 17:14:28 +00:00
|
|
|
sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:34:09 +00:00
|
|
|
template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
|
2016-02-09 17:14:28 +00:00
|
|
|
new (&fItemArray[dst]) T(std::move(fItemArray[src]));
|
|
|
|
fItemArray[src].~T();
|
|
|
|
}
|
2017-03-09 18:34:09 +00:00
|
|
|
template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
|
2016-02-09 17:14:28 +00:00
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2016-04-20 17:22:20 +00:00
|
|
|
new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
|
2016-02-09 17:14:28 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
static constexpr int kMinHeapAllocCount = 8;
|
2017-03-09 21:36:26 +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;
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
void checkRealloc(int delta) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(fCount >= 0);
|
|
|
|
SkASSERT(fAllocCount >= 0);
|
|
|
|
SkASSERT(-delta <= fCount);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2018-05-10 14:55:43 +00:00
|
|
|
// Move into 64bit math temporarily, to avoid local overflows
|
|
|
|
int64_t newCount = fCount + delta;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
// We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
|
2017-06-16 10:47:30 +00:00
|
|
|
// when we're currently using preallocated memory, would allocate less than
|
|
|
|
// kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded.
|
2017-03-16 00:52:35 +00:00
|
|
|
bool mustGrow = newCount > fAllocCount;
|
2017-06-16 10:47:30 +00:00
|
|
|
bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved;
|
2017-03-16 00:52:35 +00:00
|
|
|
if (!mustGrow && !shouldShrink) {
|
|
|
|
return;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 14:55:43 +00:00
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
// Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
|
2018-05-10 14:55:43 +00:00
|
|
|
int64_t newAllocCount = newCount + ((newCount + 1) >> 1);
|
2017-03-16 00:52:35 +00:00
|
|
|
// Align the new allocation count to kMinHeapAllocCount.
|
|
|
|
static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
|
|
|
|
newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
|
|
|
|
// At small sizes the old and new alloc count can both be kMinHeapAllocCount.
|
|
|
|
if (newAllocCount == fAllocCount) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-10 14:55:43 +00:00
|
|
|
|
|
|
|
fAllocCount = Sk64_pin_to_s32(newAllocCount);
|
|
|
|
SkASSERT(fAllocCount >= newCount);
|
2018-01-05 16:20:10 +00:00
|
|
|
void* newMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
|
2017-03-16 00:52:35 +00:00
|
|
|
this->move(newMemArray);
|
|
|
|
if (fOwnMemory) {
|
|
|
|
sk_free(fMemArray);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
}
|
2017-03-16 00:52:35 +00:00
|
|
|
fMemArray = newMemArray;
|
|
|
|
fOwnMemory = true;
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = false;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
|
|
|
T* fItemArray;
|
|
|
|
void* fMemArray;
|
|
|
|
};
|
2017-06-16 10:47:30 +00:00
|
|
|
int fCount;
|
|
|
|
int fAllocCount;
|
|
|
|
bool fOwnMemory : 1;
|
|
|
|
bool fReserved : 1;
|
2010-12-22 21:39:39 +00:00
|
|
|
};
|
|
|
|
|
2018-06-18 19:11:00 +00:00
|
|
|
template <typename T, bool M> static inline void swap(SkTArray<T, M>& a, SkTArray<T, M>& b) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
/**
|
|
|
|
* Subclass of SkTArray that contains a preallocated memory block for the array.
|
|
|
|
*/
|
2017-03-09 18:34:09 +00:00
|
|
|
template <int N, typename T, bool MEM_MOVE= false>
|
|
|
|
class SkSTArray : public SkTArray<T, MEM_MOVE> {
|
2011-09-27 19:10:05 +00:00
|
|
|
private:
|
2017-03-09 18:34:09 +00:00
|
|
|
typedef SkTArray<T, MEM_MOVE> INHERITED;
|
2011-09-27 19:10:05 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
SkSTArray() : INHERITED(&fStorage) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSTArray(const SkSTArray& array)
|
|
|
|
: INHERITED(array, &fStorage) {
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:17:15 +00:00
|
|
|
SkSTArray(SkSTArray&& array)
|
|
|
|
: INHERITED(std::move(array), &fStorage) {
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
explicit SkSTArray(const INHERITED& array)
|
|
|
|
: INHERITED(array, &fStorage) {
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:17:15 +00:00
|
|
|
explicit SkSTArray(INHERITED&& array)
|
|
|
|
: INHERITED(std::move(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) {
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
SkSTArray& operator=(const SkSTArray& array) {
|
2017-03-12 14:40:13 +00:00
|
|
|
INHERITED::operator=(array);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
SkSTArray& operator=(SkSTArray&& array) {
|
2017-03-12 14:40:13 +00:00
|
|
|
INHERITED::operator=(std::move(array));
|
|
|
|
return *this;
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
SkSTArray& operator=(const INHERITED& array) {
|
2011-09-27 19:10:05 +00:00
|
|
|
INHERITED::operator=(array);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
SkSTArray& operator=(INHERITED&& array) {
|
2017-03-12 14:40:13 +00:00
|
|
|
INHERITED::operator=(std::move(array));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-27 19:10:05 +00:00
|
|
|
private:
|
|
|
|
SkAlignedSTStorage<N,T> fStorage;
|
|
|
|
};
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#endif
|