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
|
|
|
|
2019-04-04 22:00:05 +00:00
|
|
|
#include "include/core/SkMath.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-04 22:00:05 +00:00
|
|
|
#include "include/private/SkMalloc.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkSafe32.h"
|
|
|
|
#include "include/private/SkTLogic.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
2020-10-13 13:54:38 +00:00
|
|
|
#include "include/private/SkTo.h"
|
2015-08-19 18:56:48 +00:00
|
|
|
|
2019-04-04 22:00:05 +00:00
|
|
|
#include <string.h>
|
2020-10-30 17:35:23 +00:00
|
|
|
#include <initializer_list>
|
2019-04-04 22:00:05 +00:00
|
|
|
#include <memory>
|
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
|
|
|
|
2020-05-26 16:03:14 +00:00
|
|
|
/** SkTArray<T> implements a typical, mostly std::vector-like array.
|
|
|
|
Each T will be default-initialized on allocation, and ~T will be called on destruction.
|
|
|
|
|
|
|
|
MEM_MOVE controls the behavior when a T needs to be moved (e.g. when the array is resized)
|
|
|
|
- true: T will be bit-copied via memcpy.
|
|
|
|
- false: T will be moved via move-constructors.
|
|
|
|
|
|
|
|
Modern implementations of std::vector<T> will generally provide similar performance
|
|
|
|
characteristics when used with appropriate care. Consider using std::vector<T> in new code.
|
2011-10-07 20:54:15 +00:00
|
|
|
*/
|
2017-03-09 18:34:09 +00:00
|
|
|
template <typename T, bool MEM_MOVE = false> class SkTArray {
|
2020-11-11 01:29:15 +00:00
|
|
|
private:
|
|
|
|
enum ReallocType { kExactFit, kGrowing, kShrinking };
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
public:
|
2020-10-30 17:35:23 +00:00
|
|
|
using value_type = T;
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Creates an empty array with no initial storage
|
|
|
|
*/
|
2020-11-11 01:29:15 +00:00
|
|
|
SkTArray() { this->init(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 an empty array that will preallocate space for reserveCount
|
2011-09-14 13:54:05 +00:00
|
|
|
* elements.
|
|
|
|
*/
|
2020-11-11 01:29:15 +00:00
|
|
|
explicit SkTArray(int reserveCount) : SkTArray() { this->reserve_back(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.
|
|
|
|
*/
|
2020-11-09 20:15:54 +00:00
|
|
|
SkTArray(const SkTArray& that)
|
|
|
|
: SkTArray(that.fItemArray, that.fCount) {}
|
2017-03-16 00:52:35 +00:00
|
|
|
|
2018-10-19 14:16:01 +00:00
|
|
|
SkTArray(SkTArray&& that) {
|
2020-05-15 16:53:56 +00:00
|
|
|
if (that.fOwnMemory) {
|
|
|
|
fItemArray = that.fItemArray;
|
|
|
|
fCount = that.fCount;
|
|
|
|
fAllocCount = that.fAllocCount;
|
|
|
|
fOwnMemory = true;
|
|
|
|
fReserved = that.fReserved;
|
|
|
|
|
|
|
|
that.fItemArray = nullptr;
|
|
|
|
that.fCount = 0;
|
|
|
|
that.fAllocCount = 0;
|
|
|
|
that.fOwnMemory = true;
|
|
|
|
that.fReserved = false;
|
|
|
|
} else {
|
|
|
|
this->init(that.fCount);
|
|
|
|
that.move(fItemArray);
|
|
|
|
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
|
|
|
}
|
2020-10-30 17:35:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a SkTArray by copying contents of an initializer list.
|
|
|
|
*/
|
2020-11-09 20:15:54 +00:00
|
|
|
SkTArray(std::initializer_list<T> data)
|
|
|
|
: SkTArray(data.begin(), data.size()) {}
|
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;
|
|
|
|
}
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2018-10-10 21:01:37 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
2016-04-20 17:22:20 +00:00
|
|
|
fCount = 0;
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(that.count(), kExactFit);
|
2020-10-13 13:54:38 +00:00
|
|
|
fCount = that.fCount;
|
2016-04-20 17:22:20 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2018-10-10 21:01:37 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount = 0;
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(that.count(), kExactFit);
|
2020-10-13 13:54:38 +00:00
|
|
|
fCount = that.fCount;
|
2019-12-02 20:52:41 +00:00
|
|
|
that.move(fItemArray);
|
2016-04-20 17:22:20 +00:00
|
|
|
that.fCount = 0;
|
2010-12-22 21:39:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-07-31 18:53:11 +00:00
|
|
|
~SkTArray() {
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2018-10-10 21:01:37 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
2017-03-16 00:52:35 +00:00
|
|
|
if (fOwnMemory) {
|
2019-12-02 20:52:41 +00:00
|
|
|
sk_free(fItemArray);
|
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);
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2018-10-10 21:01:37 +00:00
|
|
|
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;
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(n, kExactFit);
|
2013-06-05 15:40:59 +00:00
|
|
|
fCount = n;
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2018-10-10 21:01:37 +00:00
|
|
|
new (fItemArray + i) T;
|
|
|
|
}
|
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) {
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2018-10-10 21:01:37 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
2016-04-20 17:22:20 +00:00
|
|
|
fCount = 0;
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(count, kExactFit);
|
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()
|
2020-10-14 20:57:41 +00:00
|
|
|
* is called, or reserve_back() is called again.
|
2017-06-16 10:47:30 +00:00
|
|
|
*/
|
2020-10-14 20:57:41 +00:00
|
|
|
void reserve_back(int n) {
|
2017-06-16 10:47:30 +00:00
|
|
|
SkASSERT(n >= 0);
|
|
|
|
if (n > 0) {
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(n, kExactFit);
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = fOwnMemory;
|
|
|
|
} else {
|
|
|
|
fReserved = false;
|
|
|
|
}
|
2014-03-21 19:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeShuffle(int n) {
|
2020-10-13 13:54:38 +00:00
|
|
|
SkASSERT(n < this->count());
|
2014-03-21 19:39:02 +00:00
|
|
|
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.
|
|
|
|
*/
|
2018-10-10 21:01:37 +00:00
|
|
|
T& push_back() {
|
|
|
|
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
|
|
|
|
*/
|
2018-10-10 21:01:37 +00:00
|
|
|
T& push_back(const T& t) {
|
|
|
|
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
|
|
|
|
*/
|
2018-10-10 21:01:37 +00:00
|
|
|
T& push_back(T&& t) {
|
|
|
|
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) {
|
2018-10-10 21:01:37 +00:00
|
|
|
void* newT = this->push_back_raw(1);
|
|
|
|
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);
|
2018-10-10 21:01:37 +00:00
|
|
|
void* newTs = this->push_back_raw(n);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
new (static_cast<char*>(newTs) + i * sizeof(T)) T;
|
|
|
|
}
|
|
|
|
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);
|
2018-10-10 21:01:37 +00:00
|
|
|
void* newTs = this->push_back_raw(n);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
2018-10-10 21:01:37 +00:00
|
|
|
T* push_back_n(int n, const T t[]) {
|
2011-09-20 19:06:12 +00:00
|
|
|
SkASSERT(n >= 0);
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(n, kGrowing);
|
2018-10-10 21:01:37 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
new (fItemArray + fCount + i) T(t[i]);
|
|
|
|
}
|
|
|
|
fCount += n;
|
|
|
|
return fItemArray + fCount - n;
|
2011-09-20 19:06:12 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-10-10 21:01:37 +00:00
|
|
|
T* move_back_n(int n, T* t) {
|
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
|
|
|
SkASSERT(n >= 0);
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(n, kGrowing);
|
2018-10-10 21:01:37 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
new (fItemArray + fCount + i) T(std::move(t[i]));
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(0, kShrinking);
|
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);
|
2020-10-13 13:54:38 +00:00
|
|
|
SkASSERT(this->count() >= n);
|
2010-12-22 21:39:39 +00:00
|
|
|
fCount -= n;
|
2018-10-10 21:01:37 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
fItemArray[fCount + i].~T();
|
|
|
|
}
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(0, kShrinking);
|
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
|
|
|
|
2020-10-13 13:54:38 +00:00
|
|
|
if (newCount > this->count()) {
|
2013-06-05 15:40:59 +00:00
|
|
|
this->push_back_n(newCount - fCount);
|
2020-10-13 13:54:38 +00:00
|
|
|
} else if (newCount < this->count()) {
|
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);
|
2020-10-13 13:54:38 +00:00
|
|
|
|
|
|
|
auto count = fCount;
|
|
|
|
fCount = that.fCount;
|
|
|
|
that.fCount = count;
|
|
|
|
|
|
|
|
auto allocCount = fAllocCount;
|
|
|
|
fAllocCount = that.fAllocCount;
|
|
|
|
that.fAllocCount = allocCount;
|
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
|
|
|
}
|
2018-10-19 14:16:01 +00:00
|
|
|
T* data() { return fItemArray; }
|
|
|
|
const T* data() const { return fItemArray; }
|
|
|
|
size_t size() const { return (size_t)fCount; }
|
|
|
|
void resize(size_t count) { this->resize_back((int)count); }
|
2013-03-01 18:21:22 +00:00
|
|
|
|
2020-10-30 17:35:23 +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) {
|
2020-10-13 13:54:38 +00:00
|
|
|
SkASSERT(i < this->count());
|
2011-09-14 13:54:05 +00:00
|
|
|
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 {
|
2020-10-13 13:54:38 +00:00
|
|
|
SkASSERT(i < this->count());
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(i >= 0);
|
2010-12-22 21:39:39 +00:00
|
|
|
return fItemArray[i];
|
|
|
|
}
|
|
|
|
|
2020-06-19 22:34:51 +00:00
|
|
|
T& at(int i) { return (*this)[i]; }
|
|
|
|
const T& at(int i) const { return (*this)[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);
|
2020-10-13 13:54:38 +00:00
|
|
|
SkASSERT(i < this->count());
|
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);
|
2020-10-13 13:54:38 +00:00
|
|
|
SkASSERT(i < this->count());
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:20:03 +00:00
|
|
|
int capacity() const {
|
|
|
|
return fAllocCount;
|
|
|
|
}
|
2017-03-16 00:52:35 +00:00
|
|
|
|
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 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:
|
2020-11-11 01:29:15 +00:00
|
|
|
void init(int count) {
|
2020-10-13 13:54:38 +00:00
|
|
|
fCount = SkToU32(count);
|
2020-11-11 01:29:15 +00:00
|
|
|
if (!count) {
|
2017-03-16 00:52:35 +00:00
|
|
|
fAllocCount = 0;
|
2019-12-02 20:52:41 +00:00
|
|
|
fItemArray = nullptr;
|
2011-09-27 19:10:05 +00:00
|
|
|
} else {
|
2020-11-11 01:29:15 +00:00
|
|
|
fAllocCount = SkToU32(std::max(count, kMinHeapAllocCount));
|
2020-06-01 20:50:19 +00:00
|
|
|
fItemArray = (T*)sk_malloc_throw((size_t)fAllocCount, sizeof(T));
|
2017-03-16 00:52:35 +00:00
|
|
|
}
|
2020-11-11 01:29:15 +00:00
|
|
|
fOwnMemory = true;
|
|
|
|
fReserved = false;
|
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;
|
2019-12-02 20:52:41 +00:00
|
|
|
fItemArray = nullptr;
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = false;
|
2017-03-16 00:52:35 +00:00
|
|
|
if (count > preallocCount) {
|
2020-02-07 15:36:46 +00:00
|
|
|
fAllocCount = std::max(count, kMinHeapAllocCount);
|
2019-12-02 20:52:41 +00:00
|
|
|
fItemArray = (T*)sk_malloc_throw(fAllocCount, sizeof(T));
|
2017-03-16 00:52:35 +00:00
|
|
|
fOwnMemory = true;
|
|
|
|
} else {
|
|
|
|
fAllocCount = preallocCount;
|
2019-12-02 20:52:41 +00:00
|
|
|
fItemArray = (T*)preallocStorage;
|
2017-03-16 00:52:35 +00:00
|
|
|
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.
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2018-10-10 21:01:37 +00:00
|
|
|
new (fItemArray + i) T(src[i]);
|
|
|
|
}
|
2016-02-09 17:14:28 +00:00
|
|
|
}
|
2017-03-09 18:34:09 +00:00
|
|
|
|
2020-04-30 11:45:25 +00:00
|
|
|
template <bool E = MEM_MOVE> std::enable_if_t<E, void> move(int dst, int src) {
|
2016-02-09 17:14:28 +00:00
|
|
|
memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
|
|
|
|
}
|
2020-04-30 11:45:25 +00:00
|
|
|
template <bool E = MEM_MOVE> std::enable_if_t<E, void> move(void* dst) {
|
2019-12-02 20:52:41 +00:00
|
|
|
sk_careful_memcpy(dst, fItemArray, fCount * sizeof(T));
|
2016-02-09 17:14:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 11:45:25 +00:00
|
|
|
template <bool E = MEM_MOVE> std::enable_if_t<!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();
|
|
|
|
}
|
2020-04-30 11:45:25 +00:00
|
|
|
template <bool E = MEM_MOVE> std::enable_if_t<!E, void> move(void* dst) {
|
2020-10-13 13:54:38 +00:00
|
|
|
for (int i = 0; i < this->count(); ++i) {
|
2020-06-01 20:50:19 +00:00
|
|
|
new (static_cast<char*>(dst) + sizeof(T) * (size_t)i) T(std::move(fItemArray[i]));
|
2018-10-10 21:01:37 +00:00
|
|
|
fItemArray[i].~T();
|
|
|
|
}
|
2016-02-09 17:14:28 +00:00
|
|
|
}
|
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.
|
2018-10-10 21:01:37 +00:00
|
|
|
void* push_back_raw(int n) {
|
2020-11-11 01:29:15 +00:00
|
|
|
this->checkRealloc(n, kGrowing);
|
2018-10-10 21:01:37 +00:00
|
|
|
void* ptr = fItemArray + fCount;
|
2013-06-13 15:13:46 +00:00
|
|
|
fCount += n;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:29:15 +00:00
|
|
|
void checkRealloc(int delta, ReallocType reallocType) {
|
2011-09-14 13:54:05 +00:00
|
|
|
SkASSERT(fCount >= 0);
|
|
|
|
SkASSERT(fAllocCount >= 0);
|
2020-10-13 13:54:38 +00:00
|
|
|
SkASSERT(-delta <= this->count());
|
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
|
|
|
}
|
|
|
|
|
2020-11-11 01:29:15 +00:00
|
|
|
int64_t newAllocCount = newCount;
|
|
|
|
if (reallocType != kExactFit) {
|
|
|
|
// Whether we're growing or shrinking, leave at least 50% extra space for future growth.
|
|
|
|
newAllocCount += ((newCount + 1) >> 1);
|
|
|
|
// Align the new allocation count to kMinHeapAllocCount.
|
|
|
|
static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
|
|
|
|
newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
|
|
|
|
}
|
2018-05-10 14:55:43 +00:00
|
|
|
|
2017-03-16 00:52:35 +00:00
|
|
|
// 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
|
|
|
|
2020-10-13 13:54:38 +00:00
|
|
|
fAllocCount = SkToU32(Sk64_pin_to_s32(newAllocCount));
|
2018-05-10 14:55:43 +00:00
|
|
|
SkASSERT(fAllocCount >= newCount);
|
2020-06-01 20:50:19 +00:00
|
|
|
T* newItemArray = (T*)sk_malloc_throw((size_t)fAllocCount, sizeof(T));
|
2019-12-02 20:52:41 +00:00
|
|
|
this->move(newItemArray);
|
2017-03-16 00:52:35 +00:00
|
|
|
if (fOwnMemory) {
|
2019-12-02 20:52:41 +00:00
|
|
|
sk_free(fItemArray);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2019-12-02 20:52:41 +00:00
|
|
|
fItemArray = newItemArray;
|
2017-03-16 00:52:35 +00:00
|
|
|
fOwnMemory = true;
|
2017-06-16 10:47:30 +00:00
|
|
|
fReserved = false;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 20:52:41 +00:00
|
|
|
T* fItemArray;
|
2020-10-13 13:54:38 +00:00
|
|
|
uint32_t fOwnMemory : 1;
|
|
|
|
uint32_t fCount : 31;
|
|
|
|
uint32_t fReserved : 1;
|
|
|
|
uint32_t fAllocCount : 31;
|
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.
|
|
|
|
*/
|
2020-10-30 17:35:23 +00:00
|
|
|
template <int N, typename T, bool MEM_MOVE = false>
|
2020-11-09 16:27:41 +00:00
|
|
|
class SkSTArray : private SkAlignedSTStorage<N,T>, public SkTArray<T, MEM_MOVE> {
|
2011-09-27 19:10:05 +00:00
|
|
|
private:
|
2020-11-09 16:27:41 +00:00
|
|
|
using STORAGE = SkAlignedSTStorage<N,T>;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkTArray<T, MEM_MOVE>;
|
2011-09-27 19:10:05 +00:00
|
|
|
|
|
|
|
public:
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray()
|
|
|
|
: STORAGE{}, INHERITED(static_cast<STORAGE*>(this)) {}
|
2011-09-27 19:10:05 +00:00
|
|
|
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray(const T* array, int count)
|
|
|
|
: STORAGE{}, INHERITED(array, count, static_cast<STORAGE*>(this)) {}
|
2011-09-27 19:10:05 +00:00
|
|
|
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray(std::initializer_list<T> data)
|
|
|
|
: SkSTArray(data.begin(), data.size()) {}
|
2017-03-09 17:17:15 +00:00
|
|
|
|
2013-10-03 15:17:58 +00:00
|
|
|
explicit SkSTArray(int reserveCount)
|
2020-11-11 01:29:15 +00:00
|
|
|
: SkSTArray() {
|
|
|
|
this->reserve_back(reserveCount);
|
|
|
|
}
|
2013-10-03 15:17:58 +00:00
|
|
|
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray (const SkSTArray& that) : SkSTArray() { *this = that; }
|
|
|
|
explicit SkSTArray(const INHERITED& that) : SkSTArray() { *this = that; }
|
|
|
|
SkSTArray ( SkSTArray&& that) : SkSTArray() { *this = std::move(that); }
|
|
|
|
explicit SkSTArray( INHERITED&& that) : SkSTArray() { *this = std::move(that); }
|
2011-09-27 19:10:05 +00:00
|
|
|
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray& operator=(const SkSTArray& that) {
|
|
|
|
INHERITED::operator=(that);
|
2017-03-12 14:40:13 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray& operator=(const INHERITED& that) {
|
|
|
|
INHERITED::operator=(that);
|
2017-03-12 14:40:13 +00:00
|
|
|
return *this;
|
2011-09-27 19:10:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray& operator=(SkSTArray&& that) {
|
|
|
|
INHERITED::operator=(std::move(that));
|
2011-09-27 19:10:05 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2020-11-09 20:15:54 +00:00
|
|
|
SkSTArray& operator=(INHERITED&& that) {
|
|
|
|
INHERITED::operator=(std::move(that));
|
2017-03-12 14:40:13 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2011-09-27 19:10:05 +00:00
|
|
|
};
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#endif
|