2017-05-10 06:07:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* A vector of pointers space-optimized for a small number of elements. */
|
2018-06-07 13:53:54 +03:00
|
|
|
|
2017-05-10 06:07:37 +03:00
|
|
|
#ifndef mozilla_SmallPointerArray_h
|
|
|
|
#define mozilla_SmallPointerArray_h
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2019-07-02 21:50:04 +03:00
|
|
|
#include "mozilla/PodOperations.h"
|
2018-06-07 13:53:54 +03:00
|
|
|
|
2017-09-12 21:11:47 +03:00
|
|
|
#include <algorithm>
|
2017-05-10 06:07:37 +03:00
|
|
|
#include <iterator>
|
2018-06-07 13:53:54 +03:00
|
|
|
#include <new>
|
2017-05-10 06:07:37 +03:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
// Array class for situations where a small number of NON-NULL elements (<= 2)
|
2019-07-02 21:50:04 +03:00
|
|
|
// is expected, a large number of elements must be accommodated if necessary,
|
2017-05-10 06:07:37 +03:00
|
|
|
// and the size of the class must be minimal. Typical vector implementations
|
|
|
|
// will fulfill the first two requirements by simply adding inline storage
|
|
|
|
// alongside the rest of their member variables. While this strategy works,
|
|
|
|
// it brings unnecessary storage overhead for vectors with an expected small
|
|
|
|
// number of elements. This class is intended to deal with that problem.
|
|
|
|
//
|
|
|
|
// This class is similar in performance to a vector class. Accessing its
|
|
|
|
// elements when it has not grown over a size of 2 does not require an extra
|
|
|
|
// level of indirection and will therefore be faster.
|
|
|
|
//
|
|
|
|
// The minimum (inline) size is 2 * sizeof(void*).
|
|
|
|
//
|
|
|
|
// Any modification of the array invalidates any outstanding iterators.
|
|
|
|
template <typename T>
|
|
|
|
class SmallPointerArray {
|
|
|
|
public:
|
|
|
|
SmallPointerArray() {
|
2018-06-07 13:53:54 +03:00
|
|
|
// List-initialization would be nicer, but it only lets you initialize the
|
|
|
|
// first union member.
|
|
|
|
mArray[0].mValue = nullptr;
|
|
|
|
mArray[1].mVector = nullptr;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
2018-06-07 13:53:54 +03:00
|
|
|
|
2017-05-10 06:07:37 +03:00
|
|
|
~SmallPointerArray() {
|
2018-06-07 13:53:54 +03:00
|
|
|
if (!first()) {
|
|
|
|
delete maybeVector();
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 21:50:04 +03:00
|
|
|
SmallPointerArray(SmallPointerArray&& aOther) {
|
|
|
|
PodCopy(mArray, aOther.mArray, 2);
|
|
|
|
aOther.mArray[0].mValue = nullptr;
|
|
|
|
aOther.mArray[1].mVector = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallPointerArray& operator=(SmallPointerArray&& aOther) {
|
|
|
|
std::swap(mArray, aOther.mArray);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-05-10 06:07:37 +03:00
|
|
|
void Clear() {
|
2018-06-07 13:53:54 +03:00
|
|
|
if (first()) {
|
|
|
|
first() = nullptr;
|
|
|
|
new (&mArray[1].mValue) std::vector<T*>*(nullptr);
|
2017-05-10 06:07:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-06-07 13:53:54 +03:00
|
|
|
|
|
|
|
delete maybeVector();
|
|
|
|
mArray[1].mVector = nullptr;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppendElement(T* aElement) {
|
|
|
|
// Storing nullptr as an element is not permitted, but we do check for it
|
|
|
|
// to avoid corruption issues in non-debug builds.
|
|
|
|
|
|
|
|
// In addition to this we assert in debug builds to point out mistakes to
|
|
|
|
// users of the class.
|
|
|
|
MOZ_ASSERT(aElement != nullptr);
|
2018-06-07 13:53:54 +03:00
|
|
|
if (aElement == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-10 06:07:37 +03:00
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (!first()) {
|
|
|
|
auto* vec = maybeVector();
|
|
|
|
if (!vec) {
|
|
|
|
first() = aElement;
|
|
|
|
new (&mArray[1].mValue) T*(nullptr);
|
2017-05-10 06:07:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
vec->push_back(aElement);
|
2017-05-10 06:07:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (!second()) {
|
|
|
|
second() = aElement;
|
2017-05-10 06:07:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
auto* vec = new std::vector<T*>({first(), second(), aElement});
|
|
|
|
first() = nullptr;
|
|
|
|
new (&mArray[1].mVector) std::vector<T*>*(vec);
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2017-09-12 21:11:47 +03:00
|
|
|
bool RemoveElement(T* aElement) {
|
2017-05-10 06:07:37 +03:00
|
|
|
MOZ_ASSERT(aElement != nullptr);
|
|
|
|
if (aElement == nullptr) {
|
2017-09-12 21:11:47 +03:00
|
|
|
return false;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (first() == aElement) {
|
|
|
|
// Expected case.
|
|
|
|
T* maybeSecond = second();
|
|
|
|
first() = maybeSecond;
|
|
|
|
if (maybeSecond) {
|
|
|
|
second() = nullptr;
|
|
|
|
} else {
|
|
|
|
new (&mArray[1].mVector) std::vector<T*>*(nullptr);
|
|
|
|
}
|
|
|
|
|
2017-09-12 21:11:47 +03:00
|
|
|
return true;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (first()) {
|
|
|
|
if (second() == aElement) {
|
|
|
|
second() = nullptr;
|
2017-09-12 21:11:47 +03:00
|
|
|
return true;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
2017-09-12 21:11:47 +03:00
|
|
|
return false;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (auto* vec = maybeVector()) {
|
|
|
|
for (auto iter = vec->begin(); iter != vec->end(); iter++) {
|
2017-05-10 06:07:37 +03:00
|
|
|
if (*iter == aElement) {
|
2018-06-07 13:53:54 +03:00
|
|
|
vec->erase(iter);
|
2017-09-12 21:11:47 +03:00
|
|
|
return true;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-12 21:11:47 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Contains(T* aElement) const {
|
|
|
|
MOZ_ASSERT(aElement != nullptr);
|
|
|
|
if (aElement == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (T* v = first()) {
|
|
|
|
return v == aElement || second() == aElement;
|
2017-09-12 21:11:47 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (auto* vec = maybeVector()) {
|
|
|
|
return std::find(vec->begin(), vec->end(), aElement) != vec->end();
|
2017-09-12 21:11:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Length() const {
|
2018-06-07 13:53:54 +03:00
|
|
|
if (first()) {
|
|
|
|
return second() ? 2 : 1;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (auto* vec = maybeVector()) {
|
|
|
|
return vec->size();
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-02 21:50:04 +03:00
|
|
|
bool IsEmpty() const { return Length() == 0; }
|
|
|
|
|
2017-05-10 06:07:37 +03:00
|
|
|
T* ElementAt(size_t aIndex) const {
|
|
|
|
MOZ_ASSERT(aIndex < Length());
|
2018-06-07 13:53:54 +03:00
|
|
|
if (first()) {
|
|
|
|
return mArray[aIndex].mValue;
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
auto* vec = maybeVector();
|
|
|
|
MOZ_ASSERT(vec, "must have backing vector if accessing an element");
|
|
|
|
return (*vec)[aIndex];
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
T* operator[](size_t aIndex) const { return ElementAt(aIndex); }
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
using iterator = T**;
|
|
|
|
using const_iterator = const T**;
|
2017-05-10 06:07:37 +03:00
|
|
|
|
|
|
|
// Methods for range-based for loops. Manipulation invalidates these.
|
|
|
|
iterator begin() { return beginInternal(); }
|
|
|
|
const_iterator begin() const { return beginInternal(); }
|
|
|
|
const_iterator cbegin() const { return begin(); }
|
|
|
|
iterator end() { return beginInternal() + Length(); }
|
|
|
|
const_iterator end() const { return beginInternal() + Length(); }
|
|
|
|
const_iterator cend() const { return end(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T** beginInternal() const {
|
2018-06-07 13:53:54 +03:00
|
|
|
if (first()) {
|
|
|
|
static_assert(sizeof(T*) == sizeof(Element),
|
|
|
|
"pointer ops on &first() must produce adjacent "
|
|
|
|
"Element::mValue arms");
|
|
|
|
return &first();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* vec = maybeVector();
|
|
|
|
if (!vec) {
|
|
|
|
return &first();
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
if (vec->empty()) {
|
2017-05-10 06:07:37 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
return &(*vec)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors for |mArray| element union arms.
|
|
|
|
|
|
|
|
T*& first() const { return const_cast<T*&>(mArray[0].mValue); }
|
|
|
|
|
|
|
|
T*& second() const {
|
|
|
|
MOZ_ASSERT(first(), "first() must be non-null to have a T* second pointer");
|
|
|
|
return const_cast<T*&>(mArray[1].mValue);
|
2017-05-10 06:07:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:53:54 +03:00
|
|
|
std::vector<T*>* maybeVector() const {
|
|
|
|
MOZ_ASSERT(!first(),
|
|
|
|
"function must only be called when this is either empty or has "
|
|
|
|
"std::vector-backed elements");
|
|
|
|
return mArray[1].mVector;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In C++ active-union-arm terms:
|
|
|
|
//
|
|
|
|
// - mArray[0].mValue is always active: a possibly null T*;
|
|
|
|
// - if mArray[0].mValue is null, mArray[1].mVector is active: a possibly
|
|
|
|
// null std::vector<T*>*; if mArray[0].mValue isn't null, mArray[1].mValue
|
|
|
|
// is active: a possibly null T*.
|
|
|
|
//
|
|
|
|
// SmallPointerArray begins empty, with mArray[1].mVector active and null.
|
|
|
|
// Code that makes mArray[0].mValue non-null, i.e. assignments to first(),
|
|
|
|
// must placement-new mArray[1].mValue with the proper value; code that goes
|
|
|
|
// the opposite direction, making mArray[0].mValue null, must placement-new
|
|
|
|
// mArray[1].mVector with the proper value.
|
2017-05-10 06:07:37 +03:00
|
|
|
//
|
2018-06-07 13:53:54 +03:00
|
|
|
// When !mArray[0].mValue && !mArray[1].mVector, the array is empty.
|
2017-05-10 06:07:37 +03:00
|
|
|
//
|
2018-06-07 13:53:54 +03:00
|
|
|
// When mArray[0].mValue && !mArray[1].mValue, the array has size 1 and
|
|
|
|
// contains mArray[0].mValue.
|
2017-05-10 06:07:37 +03:00
|
|
|
//
|
2018-06-07 13:53:54 +03:00
|
|
|
// When mArray[0] && mArray[1], the array has size 2 and contains
|
|
|
|
// mArray[0].mValue and mArray[1].mValue.
|
2017-05-10 06:07:37 +03:00
|
|
|
//
|
2018-06-07 13:53:54 +03:00
|
|
|
// When !mArray[0].mValue && mArray[1].mVector, mArray[1].mVector contains
|
|
|
|
// the contents of an array of arbitrary size (even less than two if it ever
|
|
|
|
// contained three elements and elements were removed).
|
|
|
|
union Element {
|
|
|
|
T* mValue;
|
|
|
|
std::vector<T*>* mVector;
|
|
|
|
} mArray[2];
|
2017-05-10 06:07:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_SmallPointerArray_h
|