2014-06-30 19:39:45 +04: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: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2005-11-23 22:44:16 +03:00
|
|
|
|
|
|
|
#ifndef nsTArray_h__
|
|
|
|
#define nsTArray_h__
|
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
#include "nsTArrayForwardDeclare.h"
|
2013-08-14 11:00:52 +04:00
|
|
|
#include "mozilla/Alignment.h"
|
2017-02-24 08:14:06 +03:00
|
|
|
#include "mozilla/ArrayIterator.h"
|
2011-12-20 01:58:30 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-06-19 00:46:46 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-17 17:46:24 +04:00
|
|
|
#include "mozilla/BinarySearch.h"
|
2015-05-18 23:50:34 +03:00
|
|
|
#include "mozilla/fallible.h"
|
2014-10-30 06:34:33 +03:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-07-18 17:43:42 +04:00
|
|
|
#include "mozilla/Move.h"
|
2015-02-04 11:07:32 +03:00
|
|
|
#include "mozilla/ReverseIterator.h"
|
2013-02-09 01:18:49 +04:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2017-02-16 12:43:50 +03:00
|
|
|
#include "mozilla/Span.h"
|
2011-12-20 01:58:30 +04:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-11-15 11:32:39 +04:00
|
|
|
#include "nsCycleCollectionNoteChild.h"
|
2011-06-02 16:56:50 +04:00
|
|
|
#include "nsAlgorithm.h"
|
2010-11-09 05:48:59 +03:00
|
|
|
#include "nscore.h"
|
2005-11-23 22:44:16 +03:00
|
|
|
#include "nsQuickSort.h"
|
|
|
|
#include "nsDebug.h"
|
2014-02-27 01:36:35 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2015-11-04 19:49:49 +03:00
|
|
|
#include "nsRegionFwd.h"
|
2016-11-28 19:03:53 +03:00
|
|
|
#include <functional>
|
2016-10-19 07:33:14 +03:00
|
|
|
#include <initializer_list>
|
2013-07-12 04:16:41 +04:00
|
|
|
#include <new>
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
namespace JS {
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class T>
|
2013-06-18 14:00:37 +04:00
|
|
|
class Heap;
|
|
|
|
} /* namespace JS */
|
|
|
|
|
2013-11-27 15:25:28 +04:00
|
|
|
class nsRegion;
|
2014-08-01 07:25:37 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
struct TileClient;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|
2016-08-10 09:18:29 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
struct SerializedStructuredCloneBuffer;
|
2016-12-08 17:51:58 +03:00
|
|
|
class SourceBufferTask;
|
2016-08-10 09:18:29 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace ipc {
|
|
|
|
class StructuredCloneData;
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class ClonedMessageData;
|
|
|
|
class MessagePortMessage;
|
|
|
|
namespace indexedDB {
|
|
|
|
struct StructuredCloneReadInfo;
|
|
|
|
class SerializedStructuredCloneReadInfo;
|
|
|
|
class ObjectStoreCursorResponse;
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
class JSStructuredCloneData;
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// nsTArray is a resizable array class, like std::vector.
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// Unlike std::vector, which follows C++'s construction/destruction rules,
|
|
|
|
// nsTArray assumes that your "T" can be memmoved()'ed safely.
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// The public classes defined in this header are
|
|
|
|
//
|
|
|
|
// nsTArray<T>,
|
|
|
|
// FallibleTArray<T>,
|
2016-02-02 18:36:30 +03:00
|
|
|
// AutoTArray<T, N>, and
|
2012-12-19 05:16:06 +04:00
|
|
|
//
|
2016-02-02 18:36:30 +03:00
|
|
|
// nsTArray and AutoTArray are infallible by default. To opt-in to fallible
|
|
|
|
// behaviour, use the `mozilla::fallible` parameter and check the return value.
|
2016-01-31 21:10:57 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// If you just want to declare the nsTArray types (e.g., if you're in a header
|
|
|
|
// file and don't need the full nsTArray definitions) consider including
|
|
|
|
// nsTArrayForwardDeclare.h instead of nsTArray.h.
|
|
|
|
//
|
|
|
|
// The template parameter (i.e., T in nsTArray<T>) specifies the type of the
|
|
|
|
// elements and has the following requirements:
|
|
|
|
//
|
|
|
|
// T MUST be safely memmove()'able.
|
|
|
|
// T MUST define a copy-constructor.
|
|
|
|
// T MAY define operator< for sorting.
|
|
|
|
// T MAY define operator== for searching.
|
|
|
|
//
|
2013-06-18 14:00:37 +04:00
|
|
|
// (Note that the memmove requirement may be relaxed for certain types - see
|
2013-10-22 18:36:34 +04:00
|
|
|
// nsTArray_CopyChooser below.)
|
2013-06-18 14:00:37 +04:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// For methods taking a Comparator instance, the Comparator must be a class
|
|
|
|
// defining the following methods:
|
|
|
|
//
|
|
|
|
// class Comparator {
|
|
|
|
// public:
|
|
|
|
// /** @return True if the elements are equals; false otherwise. */
|
|
|
|
// bool Equals(const elem_type& a, const Item& b) const;
|
|
|
|
//
|
|
|
|
// /** @return True if (a < b); false otherwise. */
|
|
|
|
// bool LessThan(const elem_type& a, const Item& b) const;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// The Equals method is used for searching, and the LessThan method is used for
|
|
|
|
// searching and sorting. The |Item| type above can be arbitrary, but must
|
|
|
|
// match the Item type passed to the sort or search function.
|
|
|
|
//
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2013-03-21 23:02:20 +04:00
|
|
|
//
|
|
|
|
// nsTArrayFallibleResult and nsTArrayInfallibleResult types are proxy types
|
|
|
|
// which are used because you cannot use a templated type which is bound to
|
|
|
|
// void as an argument to a void function. In order to work around that, we
|
|
|
|
// encode either a void or a boolean inside these proxy objects, and pass them
|
|
|
|
// to the aforementioned function instead, and then use the type information to
|
|
|
|
// decide what to do in the function.
|
|
|
|
//
|
|
|
|
// Note that public nsTArray methods should never return a proxy type. Such
|
|
|
|
// types are only meant to be used in the internal nsTArray helper methods.
|
|
|
|
// Public methods returning non-proxy types cannot be called from other
|
|
|
|
// nsTArray members.
|
|
|
|
//
|
|
|
|
struct nsTArrayFallibleResult
|
|
|
|
{
|
|
|
|
// Note: allows implicit conversions from and to bool
|
2014-06-27 05:35:39 +04:00
|
|
|
MOZ_IMPLICIT nsTArrayFallibleResult(bool aResult) : mResult(aResult) {}
|
2013-03-21 23:02:20 +04:00
|
|
|
|
2015-04-11 06:05:46 +03:00
|
|
|
MOZ_IMPLICIT operator bool() { return mResult; }
|
2013-03-21 23:02:20 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsTArrayInfallibleResult
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// nsTArray*Allocators must all use the same |free()|, to allow swap()'ing
|
|
|
|
// between fallible and infallible variants.
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
|
|
|
|
2013-03-21 23:02:20 +04:00
|
|
|
struct nsTArrayFallibleAllocatorBase
|
|
|
|
{
|
|
|
|
typedef bool ResultType;
|
|
|
|
typedef nsTArrayFallibleResult ResultTypeProxy;
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static ResultType Result(ResultTypeProxy aResult) { return aResult; }
|
|
|
|
static bool Successful(ResultTypeProxy aResult) { return aResult; }
|
|
|
|
static ResultTypeProxy SuccessResult() { return true; }
|
|
|
|
static ResultTypeProxy FailureResult() { return false; }
|
|
|
|
static ResultType ConvertBoolToResultType(bool aValue) { return aValue; }
|
2013-03-21 23:02:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nsTArrayInfallibleAllocatorBase
|
|
|
|
{
|
|
|
|
typedef void ResultType;
|
|
|
|
typedef nsTArrayInfallibleResult ResultTypeProxy;
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static ResultType Result(ResultTypeProxy aResult) {}
|
|
|
|
static bool Successful(ResultTypeProxy) { return true; }
|
|
|
|
static ResultTypeProxy SuccessResult() { return ResultTypeProxy(); }
|
2013-03-21 23:02:20 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static ResultTypeProxy FailureResult()
|
|
|
|
{
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("Infallible nsTArray should never fail");
|
2013-03-21 23:02:20 +04:00
|
|
|
return ResultTypeProxy();
|
|
|
|
}
|
2013-04-02 00:42:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static ResultType ConvertBoolToResultType(bool aValue)
|
|
|
|
{
|
2013-04-02 00:42:54 +04:00
|
|
|
if (!aValue) {
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("infallible nsTArray should never convert false to ResultType");
|
2013-04-02 00:42:54 +04:00
|
|
|
}
|
|
|
|
}
|
2013-03-21 23:02:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nsTArrayFallibleAllocator : nsTArrayFallibleAllocatorBase
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
2015-02-19 07:51:06 +03:00
|
|
|
static void* Malloc(size_t aSize) { return malloc(aSize); }
|
2014-06-27 05:35:39 +04:00
|
|
|
static void* Realloc(void* aPtr, size_t aSize)
|
|
|
|
{
|
2015-02-19 07:51:06 +03:00
|
|
|
return realloc(aPtr, aSize);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 07:51:06 +03:00
|
|
|
static void Free(void* aPtr) { free(aPtr); }
|
2014-06-27 05:35:39 +04:00
|
|
|
static void SizeTooBig(size_t) {}
|
2010-11-09 05:48:59 +03:00
|
|
|
};
|
|
|
|
|
2016-07-20 18:29:36 +03:00
|
|
|
#if defined(MOZALLOC_HAVE_XMALLOC)
|
|
|
|
#include "mozilla/mozalloc_abort.h"
|
|
|
|
|
2013-03-21 23:02:20 +04:00
|
|
|
struct nsTArrayInfallibleAllocator : nsTArrayInfallibleAllocatorBase
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
static void* Malloc(size_t aSize) { return moz_xmalloc(aSize); }
|
|
|
|
static void* Realloc(void* aPtr, size_t aSize)
|
|
|
|
{
|
|
|
|
return moz_xrealloc(aPtr, aSize);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 07:51:06 +03:00
|
|
|
static void Free(void* aPtr) { free(aPtr); }
|
2014-06-27 05:35:39 +04:00
|
|
|
static void SizeTooBig(size_t aSize) { NS_ABORT_OOM(aSize); }
|
2010-11-09 05:48:59 +03:00
|
|
|
};
|
2012-01-26 21:51:45 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2012-12-19 05:16:06 +04:00
|
|
|
|
2013-03-21 23:02:20 +04:00
|
|
|
struct nsTArrayInfallibleAllocator : nsTArrayInfallibleAllocatorBase
|
2012-12-19 05:16:06 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
static void* Malloc(size_t aSize)
|
|
|
|
{
|
|
|
|
void* ptr = malloc(aSize);
|
2012-12-19 05:16:06 +04:00
|
|
|
if (MOZ_UNLIKELY(!ptr)) {
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_ABORT_OOM(aSize);
|
2012-12-19 05:16:06 +04:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static void* Realloc(void* aPtr, size_t aSize)
|
|
|
|
{
|
|
|
|
void* newptr = realloc(aPtr, aSize);
|
|
|
|
if (MOZ_UNLIKELY(!newptr && aSize)) {
|
|
|
|
NS_ABORT_OOM(aSize);
|
2012-12-19 05:16:06 +04:00
|
|
|
}
|
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static void Free(void* aPtr) { free(aPtr); }
|
|
|
|
static void SizeTooBig(size_t aSize) { NS_ABORT_OOM(aSize); }
|
2012-12-19 05:16:06 +04:00
|
|
|
};
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2011-01-07 07:49:35 +03:00
|
|
|
#endif
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// nsTArray_base stores elements into the space allocated beyond
|
|
|
|
// sizeof(*this). This is done to minimize the size of the nsTArray
|
|
|
|
// object when it is empty.
|
2014-08-28 02:47:27 +04:00
|
|
|
struct nsTArrayHeader
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
|
|
|
static nsTArrayHeader sEmptyHdr;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLength;
|
|
|
|
uint32_t mCapacity : 31;
|
|
|
|
uint32_t mIsAutoArray : 1;
|
2010-11-09 05:48:59 +03:00
|
|
|
};
|
|
|
|
|
2011-08-10 09:36:00 +04:00
|
|
|
// This class provides a SafeElementAt method to nsTArray<T*> which does
|
|
|
|
// not take a second default value parameter.
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E, class Derived>
|
2011-08-10 09:36:00 +04:00
|
|
|
struct nsTArray_SafeElementAtHelper
|
|
|
|
{
|
|
|
|
typedef E* elem_type;
|
2014-05-09 05:03:35 +04:00
|
|
|
typedef size_t index_type;
|
2011-08-10 09:36:00 +04:00
|
|
|
|
|
|
|
// No implementation is provided for these two methods, and that is on
|
|
|
|
// purpose, since we don't support these functions on non-pointer type
|
|
|
|
// instantiations.
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type& SafeElementAt(index_type aIndex);
|
|
|
|
const elem_type& SafeElementAt(index_type aIndex) const;
|
2011-08-10 09:36:00 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E, class Derived>
|
2011-08-10 09:36:00 +04:00
|
|
|
struct nsTArray_SafeElementAtHelper<E*, Derived>
|
|
|
|
{
|
|
|
|
typedef E* elem_type;
|
2016-01-06 04:08:45 +03:00
|
|
|
//typedef const E* const_elem_type; XXX: see below
|
2014-05-09 05:03:35 +04:00
|
|
|
typedef size_t index_type;
|
2011-08-10 09:36:00 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type SafeElementAt(index_type aIndex)
|
|
|
|
{
|
|
|
|
return static_cast<Derived*>(this)->SafeElementAt(aIndex, nullptr);
|
2011-08-10 09:36:00 +04:00
|
|
|
}
|
|
|
|
|
2016-01-06 04:08:45 +03:00
|
|
|
// XXX: Probably should return const_elem_type, but callsites must be fixed.
|
|
|
|
// Also, the use of const_elem_type for nsTArray<xpcGCCallback> in
|
|
|
|
// xpcprivate.h causes build failures on Windows because xpcGCCallback is a
|
|
|
|
// function pointer and MSVC doesn't like qualifying it with |const|.
|
|
|
|
elem_type SafeElementAt(index_type aIndex) const
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
|
|
|
return static_cast<const Derived*>(this)->SafeElementAt(aIndex, nullptr);
|
2011-08-10 09:36:00 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-25 20:58:01 +04:00
|
|
|
// E is the base type that the smart pointer is templated over; the
|
|
|
|
// smart pointer can act as E*.
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E, class Derived>
|
2011-10-25 20:58:01 +04:00
|
|
|
struct nsTArray_SafeElementAtSmartPtrHelper
|
|
|
|
{
|
|
|
|
typedef E* elem_type;
|
2016-01-06 04:08:45 +03:00
|
|
|
typedef const E* const_elem_type;
|
2014-05-09 05:03:35 +04:00
|
|
|
typedef size_t index_type;
|
2011-10-25 20:58:01 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type SafeElementAt(index_type aIndex)
|
|
|
|
{
|
|
|
|
return static_cast<Derived*>(this)->SafeElementAt(aIndex, nullptr);
|
2011-10-25 20:58:01 +04:00
|
|
|
}
|
|
|
|
|
2016-01-06 04:08:45 +03:00
|
|
|
// XXX: Probably should return const_elem_type, but callsites must be fixed.
|
|
|
|
elem_type SafeElementAt(index_type aIndex) const
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
|
|
|
return static_cast<const Derived*>(this)->SafeElementAt(aIndex, nullptr);
|
2011-10-25 20:58:01 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class T> class nsCOMPtr;
|
2011-10-25 20:58:01 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E, class Derived>
|
|
|
|
struct nsTArray_SafeElementAtHelper<nsCOMPtr<E>, Derived>
|
|
|
|
: public nsTArray_SafeElementAtSmartPtrHelper<E, Derived>
|
2011-10-25 20:58:01 +04:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E, class Derived>
|
2015-10-18 08:24:48 +03:00
|
|
|
struct nsTArray_SafeElementAtHelper<RefPtr<E>, Derived>
|
2014-06-27 05:35:39 +04:00
|
|
|
: public nsTArray_SafeElementAtSmartPtrHelper<E, Derived>
|
2011-10-25 20:58:01 +04:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
namespace mozilla {
|
|
|
|
template<class T> class OwningNonNull;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2015-04-19 15:28:50 +03:00
|
|
|
|
|
|
|
template<class E, class Derived>
|
2015-08-05 15:28:27 +03:00
|
|
|
struct nsTArray_SafeElementAtHelper<mozilla::OwningNonNull<E>, Derived>
|
2015-04-19 15:28:50 +03:00
|
|
|
{
|
2016-01-06 04:08:45 +03:00
|
|
|
typedef E* elem_type;
|
|
|
|
typedef const E* const_elem_type;
|
|
|
|
typedef size_t index_type;
|
2015-04-19 15:28:50 +03:00
|
|
|
|
|
|
|
elem_type SafeElementAt(index_type aIndex)
|
|
|
|
{
|
|
|
|
if (aIndex < static_cast<Derived*>(this)->Length()) {
|
|
|
|
return static_cast<Derived*>(this)->ElementAt(aIndex);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-06 04:08:45 +03:00
|
|
|
// XXX: Probably should return const_elem_type, but callsites must be fixed.
|
|
|
|
elem_type SafeElementAt(index_type aIndex) const
|
2015-04-19 15:28:50 +03:00
|
|
|
{
|
|
|
|
if (aIndex < static_cast<const Derived*>(this)->Length()) {
|
|
|
|
return static_cast<const Derived*>(this)->ElementAt(aIndex);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-12 07:54:14 +03:00
|
|
|
// Servo bindings.
|
|
|
|
extern "C" void Gecko_EnsureTArrayCapacity(void* aArray,
|
|
|
|
size_t aCapacity,
|
|
|
|
size_t aElementSize);
|
|
|
|
extern "C" void Gecko_ClearPODTArray(void* aArray,
|
|
|
|
size_t aElementSize,
|
|
|
|
size_t aElementAlign);
|
2016-07-14 11:59:37 +03:00
|
|
|
|
2016-08-10 21:40:00 +03:00
|
|
|
MOZ_NORETURN MOZ_COLD void
|
|
|
|
InvalidArrayIndex_CRASH(size_t aIndex, size_t aLength);
|
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
//
|
|
|
|
// This class serves as a base class for nsTArray. It shouldn't be used
|
|
|
|
// directly. It holds common implementation code that does not depend on the
|
|
|
|
// element type of the nsTArray.
|
|
|
|
//
|
2013-06-18 14:00:37 +04:00
|
|
|
template<class Alloc, class Copy>
|
2010-11-09 05:48:59 +03:00
|
|
|
class nsTArray_base
|
|
|
|
{
|
|
|
|
// Allow swapping elements with |nsTArray_base|s created using a
|
|
|
|
// different allocator. This is kosher because all allocators use
|
|
|
|
// the same free().
|
2013-06-18 14:00:37 +04:00
|
|
|
template<class Allocator, class Copier>
|
2010-11-09 05:48:59 +03:00
|
|
|
friend class nsTArray_base;
|
2016-08-12 07:54:14 +03:00
|
|
|
friend void Gecko_EnsureTArrayCapacity(void* aArray, size_t aCapacity,
|
|
|
|
size_t aElemSize);
|
|
|
|
friend void Gecko_ClearPODTArray(void* aTArray, size_t aElementSize,
|
|
|
|
size_t aElementAlign);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef nsTArrayHeader Header;
|
|
|
|
|
|
|
|
public:
|
2014-05-09 05:03:35 +04:00
|
|
|
typedef size_t size_type;
|
|
|
|
typedef size_t index_type;
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// @return The number of elements in the array.
|
2014-06-27 05:35:39 +04:00
|
|
|
size_type Length() const { return mHdr->mLength; }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// @return True if the array is empty or false otherwise.
|
2014-06-27 05:35:39 +04:00
|
|
|
bool IsEmpty() const { return Length() == 0; }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// @return The number of elements that can fit in the array without forcing
|
|
|
|
// the array to be re-allocated. The length of an array is always less
|
|
|
|
// than or equal to its capacity.
|
2014-06-27 05:35:39 +04:00
|
|
|
size_type Capacity() const { return mHdr->mCapacity; }
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2006-11-03 03:52:40 +03:00
|
|
|
#ifdef DEBUG
|
2014-06-27 05:35:39 +04:00
|
|
|
void* DebugGetHeader() const { return mHdr; }
|
2006-11-02 22:33:10 +03:00
|
|
|
#endif
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
protected:
|
|
|
|
nsTArray_base();
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
~nsTArray_base();
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// Resize the storage if necessary to achieve the requested capacity.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aCapacity The requested number of array elements.
|
|
|
|
// @param aElemSize The size of an array element.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return False if insufficient memory is available; true otherwise.
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc>
|
|
|
|
typename ActualAlloc::ResultTypeProxy EnsureCapacity(size_type aCapacity,
|
|
|
|
size_type aElemSize);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2015-11-02 08:53:27 +03:00
|
|
|
// Tries to resize the storage to the minimum required amount. If this fails,
|
|
|
|
// the array is left as-is.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aElemSize The size of an array element.
|
|
|
|
// @param aElemAlign The alignment in bytes of an array element.
|
|
|
|
void ShrinkCapacity(size_type aElemSize, size_t aElemAlign);
|
2013-06-18 14:00:37 +04:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// This method may be called to resize a "gap" in the array by shifting
|
|
|
|
// elements around. It updates mLength appropriately. If the resulting
|
|
|
|
// array has zero elements, then the array's memory is free'd.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aStart The starting index of the gap.
|
|
|
|
// @param aOldLen The current length of the gap.
|
|
|
|
// @param aNewLen The desired length of the gap.
|
|
|
|
// @param aElemSize The size of an array element.
|
|
|
|
// @param aElemAlign The alignment in bytes of an array element.
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
void ShiftData(index_type aStart, size_type aOldLen, size_type aNewLen,
|
|
|
|
size_type aElemSize, size_t aElemAlign);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// This method increments the length member of the array's header.
|
|
|
|
// Note that mHdr may actually be sEmptyHdr in the case where a
|
2014-06-27 05:35:39 +04:00
|
|
|
// zero-length array is inserted into our array. But then aNum should
|
2010-11-09 05:48:59 +03:00
|
|
|
// always be 0.
|
2014-06-27 05:35:39 +04:00
|
|
|
void IncrementLength(size_t aNum)
|
|
|
|
{
|
2013-02-26 20:24:41 +04:00
|
|
|
if (mHdr == EmptyHdr()) {
|
2014-06-27 05:35:39 +04:00
|
|
|
if (MOZ_UNLIKELY(aNum != 0)) {
|
2013-02-26 20:24:41 +04:00
|
|
|
// Writing a non-zero length to the empty header would be extremely bad.
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-27 05:35:39 +04:00
|
|
|
mHdr->mLength += aNum;
|
2013-02-26 20:24:41 +04:00
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method inserts blank slots into the array.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aIndex the place to insert the new elements. This must be no
|
|
|
|
// greater than the current length of the array.
|
|
|
|
// @param aCount the number of slots to insert
|
|
|
|
// @param aElementSize the size of an array element.
|
|
|
|
// @param aElemAlign the alignment in bytes of an array element.
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool InsertSlotsAt(index_type aIndex, size_type aCount,
|
|
|
|
size_type aElementSize, size_t aElemAlign);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc, class Allocator>
|
|
|
|
typename ActualAlloc::ResultTypeProxy
|
2014-06-27 05:35:39 +04:00
|
|
|
SwapArrayElements(nsTArray_base<Allocator, Copy>& aOther,
|
|
|
|
size_type aElemSize,
|
|
|
|
size_t aElemAlign);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2011-09-22 19:22:20 +04:00
|
|
|
// This is an RAII class used in SwapArrayElements.
|
2014-06-27 05:35:39 +04:00
|
|
|
class IsAutoArrayRestorer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IsAutoArrayRestorer(nsTArray_base<Alloc, Copy>& aArray, size_t aElemAlign);
|
|
|
|
~IsAutoArrayRestorer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray_base<Alloc, Copy>& mArray;
|
|
|
|
size_t mElemAlign;
|
|
|
|
bool mIsAuto;
|
2011-09-22 19:22:20 +04:00
|
|
|
};
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// Helper function for SwapArrayElements. Ensures that if the array
|
2016-02-02 18:36:30 +03:00
|
|
|
// is an AutoTArray that it doesn't use the built-in buffer.
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool EnsureNotUsingAutoArrayBuffer(size_type aElemSize);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Returns true if this nsTArray is an AutoTArray with a built-in buffer.
|
2014-06-27 05:35:39 +04:00
|
|
|
bool IsAutoArray() const { return mHdr->mIsAutoArray; }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Returns a Header for the built-in buffer of this AutoTArray.
|
2014-06-27 05:35:39 +04:00
|
|
|
Header* GetAutoArrayBuffer(size_t aElemAlign)
|
|
|
|
{
|
2012-03-04 01:16:13 +04:00
|
|
|
MOZ_ASSERT(IsAutoArray(), "Should be an auto array to call this");
|
2014-06-27 05:35:39 +04:00
|
|
|
return GetAutoArrayBufferUnsafe(aElemAlign);
|
2011-09-22 19:22:20 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
const Header* GetAutoArrayBuffer(size_t aElemAlign) const
|
|
|
|
{
|
2012-03-04 01:16:13 +04:00
|
|
|
MOZ_ASSERT(IsAutoArray(), "Should be an auto array to call this");
|
2014-06-27 05:35:39 +04:00
|
|
|
return GetAutoArrayBufferUnsafe(aElemAlign);
|
2011-10-14 11:06:35 +04:00
|
|
|
}
|
2006-11-02 22:33:10 +03:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Returns a Header for the built-in buffer of this AutoTArray, but doesn't
|
|
|
|
// assert that we are an AutoTArray.
|
2014-06-27 05:35:39 +04:00
|
|
|
Header* GetAutoArrayBufferUnsafe(size_t aElemAlign)
|
|
|
|
{
|
|
|
|
return const_cast<Header*>(static_cast<const nsTArray_base<Alloc, Copy>*>(
|
|
|
|
this)->GetAutoArrayBufferUnsafe(aElemAlign));
|
2011-10-14 11:06:35 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
const Header* GetAutoArrayBufferUnsafe(size_t aElemAlign) const;
|
2010-03-22 17:16:44 +03:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Returns true if this is an AutoTArray and it currently uses the
|
2010-11-09 05:48:59 +03:00
|
|
|
// built-in buffer to store its elements.
|
2011-10-14 11:06:35 +04:00
|
|
|
bool UsesAutoArrayBuffer() const;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// The array's elements (prefixed with a Header). This pointer is never
|
|
|
|
// null. If the array is empty, then this will point to sEmptyHdr.
|
2014-06-27 05:35:39 +04:00
|
|
|
Header* mHdr;
|
2006-11-02 22:33:10 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
Header* Hdr() const { return mHdr; }
|
|
|
|
Header** PtrToHdr() { return &mHdr; }
|
|
|
|
static Header* EmptyHdr() { return &Header::sEmptyHdr; }
|
2005-11-23 22:44:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// This class defines convenience functions for element specific operations.
|
|
|
|
// Specialize this template if necessary.
|
|
|
|
//
|
|
|
|
template<class E>
|
2010-11-09 05:48:59 +03:00
|
|
|
class nsTArrayElementTraits
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Invoke the default constructor in place.
|
2014-06-27 05:35:39 +04:00
|
|
|
static inline void Construct(E* aE)
|
|
|
|
{
|
2010-11-09 05:48:59 +03:00
|
|
|
// Do NOT call "E()"! That triggers C++ "default initialization"
|
|
|
|
// which zeroes out POD ("plain old data") types such as regular
|
|
|
|
// ints. We don't want that because it can be a performance issue
|
|
|
|
// and people don't expect it; nsTArray should work like a regular
|
|
|
|
// C/C++ array in this respect.
|
2016-08-16 21:22:23 +03:00
|
|
|
new (static_cast<void*>(aE)) E;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
// Invoke the copy-constructor in place.
|
|
|
|
template<class A>
|
2014-08-25 23:17:24 +04:00
|
|
|
static inline void Construct(E* aE, A&& aArg)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2014-01-17 20:54:19 +04:00
|
|
|
typedef typename mozilla::RemoveCV<E>::Type E_NoCV;
|
|
|
|
typedef typename mozilla::RemoveCV<A>::Type A_NoCV;
|
|
|
|
static_assert(!mozilla::IsSame<E_NoCV*, A_NoCV>::value,
|
|
|
|
"For safety, we disallow constructing nsTArray<E> elements "
|
|
|
|
"from E* pointers. See bug 960591.");
|
2016-08-16 21:22:23 +03:00
|
|
|
new (static_cast<void*>(aE)) E(mozilla::Forward<A>(aArg));
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
// Invoke the destructor in place.
|
2014-06-27 05:35:39 +04:00
|
|
|
static inline void Destruct(E* aE) { aE->~E(); }
|
2005-11-23 22:44:16 +03:00
|
|
|
};
|
|
|
|
|
2005-12-13 00:28:29 +03:00
|
|
|
// The default comparator used by nsTArray
|
|
|
|
template<class A, class B>
|
2010-11-09 05:48:59 +03:00
|
|
|
class nsDefaultComparator
|
|
|
|
{
|
|
|
|
public:
|
2014-06-27 05:35:39 +04:00
|
|
|
bool Equals(const A& aA, const B& aB) const { return aA == aB; }
|
|
|
|
bool LessThan(const A& aA, const B& aB) const { return aA < aB; }
|
2005-12-13 00:28:29 +03:00
|
|
|
};
|
|
|
|
|
2013-02-09 01:18:49 +04:00
|
|
|
template<bool IsPod, bool IsSameType>
|
2014-06-27 05:35:39 +04:00
|
|
|
struct AssignRangeAlgorithm
|
|
|
|
{
|
2013-02-09 01:18:49 +04:00
|
|
|
template<class Item, class ElemType, class IndexType, class SizeType>
|
2014-06-27 05:35:39 +04:00
|
|
|
static void implementation(ElemType* aElements, IndexType aStart,
|
|
|
|
SizeType aCount, const Item* aValues)
|
|
|
|
{
|
|
|
|
ElemType* iter = aElements + aStart;
|
|
|
|
ElemType* end = iter + aCount;
|
|
|
|
for (; iter != end; ++iter, ++aValues) {
|
|
|
|
nsTArrayElementTraits<ElemType>::Construct(iter, *aValues);
|
|
|
|
}
|
2013-02-09 01:18:49 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2014-06-27 05:35:39 +04:00
|
|
|
struct AssignRangeAlgorithm<true, true>
|
|
|
|
{
|
2013-02-09 01:18:49 +04:00
|
|
|
template<class Item, class ElemType, class IndexType, class SizeType>
|
2014-06-27 05:35:39 +04:00
|
|
|
static void implementation(ElemType* aElements, IndexType aStart,
|
|
|
|
SizeType aCount, const Item* aValues)
|
|
|
|
{
|
|
|
|
memcpy(aElements + aStart, aValues, aCount * sizeof(ElemType));
|
2013-02-09 01:18:49 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
//
|
|
|
|
// Normally elements are copied with memcpy and memmove, but for some element
|
2013-10-22 18:36:34 +04:00
|
|
|
// types that is problematic. The nsTArray_CopyChooser template class can be
|
2013-06-18 14:00:37 +04:00
|
|
|
// specialized to ensure that copying calls constructors and destructors
|
|
|
|
// instead, as is done below for JS::Heap<E> elements.
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// A class that defines how to copy elements using memcpy/memmove.
|
|
|
|
//
|
|
|
|
struct nsTArray_CopyWithMemutils
|
|
|
|
{
|
|
|
|
const static bool allowRealloc = true;
|
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
static void MoveNonOverlappingRegionWithHeader(void* aDest, const void* aSrc,
|
2016-07-08 23:59:05 +03:00
|
|
|
size_t aCount, size_t aElemSize)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
|
|
|
memcpy(aDest, aSrc, sizeof(nsTArrayHeader) + aCount * aElemSize);
|
2013-06-18 14:00:37 +04:00
|
|
|
}
|
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
static void MoveOverlappingRegion(void* aDest, void* aSrc, size_t aCount,
|
|
|
|
size_t aElemSize)
|
|
|
|
{
|
|
|
|
memmove(aDest, aSrc, aCount * aElemSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MoveNonOverlappingRegion(void* aDest, void* aSrc, size_t aCount,
|
|
|
|
size_t aElemSize)
|
|
|
|
{
|
|
|
|
memcpy(aDest, aSrc, aCount * aElemSize);
|
|
|
|
}
|
2013-06-18 14:00:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// A template class that defines how to copy elements calling their constructors
|
|
|
|
// and destructors appropriately.
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class ElemType>
|
2013-06-18 14:00:37 +04:00
|
|
|
struct nsTArray_CopyWithConstructors
|
|
|
|
{
|
|
|
|
typedef nsTArrayElementTraits<ElemType> traits;
|
|
|
|
|
|
|
|
const static bool allowRealloc = false;
|
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
static void MoveNonOverlappingRegionWithHeader(void* aDest, void* aSrc, size_t aCount,
|
2016-07-08 23:59:05 +03:00
|
|
|
size_t aElemSize)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
|
|
|
nsTArrayHeader* destHeader = static_cast<nsTArrayHeader*>(aDest);
|
|
|
|
nsTArrayHeader* srcHeader = static_cast<nsTArrayHeader*>(aSrc);
|
2013-06-18 14:00:37 +04:00
|
|
|
*destHeader = *srcHeader;
|
2016-07-08 23:59:05 +03:00
|
|
|
MoveNonOverlappingRegion(static_cast<uint8_t*>(aDest) + sizeof(nsTArrayHeader),
|
2016-07-08 23:59:05 +03:00
|
|
|
static_cast<uint8_t*>(aSrc) + sizeof(nsTArrayHeader),
|
|
|
|
aCount, aElemSize);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
2016-07-08 23:59:04 +03:00
|
|
|
// These functions are defined by analogy with memmove and memcpy.
|
|
|
|
// What they actually do is slightly different: MoveOverlappingRegion
|
|
|
|
// checks to see which direction the movement needs to take place,
|
|
|
|
// whether from back-to-front of the range to be moved or from
|
|
|
|
// front-to-back. MoveNonOverlappingRegion assumes that moving
|
|
|
|
// front-to-back is always valid. So they're really more like
|
|
|
|
// std::move{_backward,} in that respect. We keep these names because
|
|
|
|
// we think they read slightly better, and MoveNonOverlappingRegion is
|
|
|
|
// only ever called on overlapping regions from MoveOverlappingRegion.
|
2016-07-08 23:59:05 +03:00
|
|
|
static void MoveOverlappingRegion(void* aDest, void* aSrc, size_t aCount,
|
|
|
|
size_t aElemSize)
|
|
|
|
{
|
|
|
|
ElemType* destElem = static_cast<ElemType*>(aDest);
|
|
|
|
ElemType* srcElem = static_cast<ElemType*>(aSrc);
|
|
|
|
ElemType* destElemEnd = destElem + aCount;
|
|
|
|
ElemType* srcElemEnd = srcElem + aCount;
|
|
|
|
if (destElem == srcElem) {
|
|
|
|
return; // In practice, we don't do this.
|
2016-07-08 23:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out whether to copy back-to-front or front-to-back.
|
|
|
|
if (srcElemEnd > destElem && srcElemEnd < destElemEnd) {
|
2016-07-08 23:59:05 +03:00
|
|
|
while (destElemEnd != destElem) {
|
|
|
|
--destElemEnd;
|
|
|
|
--srcElemEnd;
|
|
|
|
traits::Construct(destElemEnd, mozilla::Move(*srcElemEnd));
|
2016-07-08 23:59:04 +03:00
|
|
|
traits::Destruct(srcElemEnd);
|
2016-07-08 23:59:05 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MoveNonOverlappingRegion(aDest, aSrc, aCount, aElemSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MoveNonOverlappingRegion(void* aDest, void* aSrc, size_t aCount,
|
|
|
|
size_t aElemSize)
|
|
|
|
{
|
|
|
|
ElemType* destElem = static_cast<ElemType*>(aDest);
|
|
|
|
ElemType* srcElem = static_cast<ElemType*>(aSrc);
|
|
|
|
ElemType* destElemEnd = destElem + aCount;
|
|
|
|
#ifdef DEBUG
|
|
|
|
ElemType* srcElemEnd = srcElem + aCount;
|
|
|
|
MOZ_ASSERT(srcElemEnd <= destElem || srcElemEnd > destElemEnd);
|
|
|
|
#endif
|
|
|
|
while (destElem != destElemEnd) {
|
|
|
|
traits::Construct(destElem, mozilla::Move(*srcElem));
|
|
|
|
traits::Destruct(srcElem);
|
|
|
|
++destElem;
|
|
|
|
++srcElem;
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 14:00:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// The default behaviour is to use memcpy/memmove for everything.
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E>
|
2015-06-19 00:46:46 +03:00
|
|
|
struct MOZ_NEEDS_MEMMOVABLE_TYPE nsTArray_CopyChooser
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2013-10-22 18:36:34 +04:00
|
|
|
typedef nsTArray_CopyWithMemutils Type;
|
|
|
|
};
|
2013-06-18 14:00:37 +04:00
|
|
|
|
|
|
|
//
|
2013-11-27 15:25:28 +04:00
|
|
|
// Some classes require constructors/destructors to be called, so they are
|
2013-06-18 14:00:37 +04:00
|
|
|
// specialized here.
|
|
|
|
//
|
2016-08-10 09:18:29 +03:00
|
|
|
#define DECLARE_USE_COPY_CONSTRUCTORS(T) \
|
|
|
|
template<> \
|
|
|
|
struct nsTArray_CopyChooser<T> \
|
|
|
|
{ \
|
|
|
|
typedef nsTArray_CopyWithConstructors<T> Type; \
|
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E>
|
|
|
|
struct nsTArray_CopyChooser<JS::Heap<E>>
|
|
|
|
{
|
|
|
|
typedef nsTArray_CopyWithConstructors<JS::Heap<E>> Type;
|
2013-10-22 18:36:34 +04:00
|
|
|
};
|
2013-06-18 14:00:37 +04:00
|
|
|
|
2016-08-10 09:18:29 +03:00
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(nsRegion)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(nsIntRegion)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::layers::TileClient)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::SerializedStructuredCloneBuffer)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::ipc::StructuredCloneData)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::ClonedMessageData)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::indexedDB::StructuredCloneReadInfo);
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::indexedDB::ObjectStoreCursorResponse)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::indexedDB::SerializedStructuredCloneReadInfo);
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(JSStructuredCloneData)
|
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::MessagePortMessage)
|
2016-12-08 17:51:58 +03:00
|
|
|
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::SourceBufferTask)
|
2014-08-01 07:25:37 +04:00
|
|
|
|
2016-11-28 19:03:53 +03:00
|
|
|
template<typename T>
|
|
|
|
struct nsTArray_CopyChooser<std::function<T>>
|
|
|
|
{
|
|
|
|
typedef nsTArray_CopyWithConstructors<std::function<T>> Type;
|
|
|
|
};
|
|
|
|
|
2014-08-01 07:25:37 +04:00
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
//
|
|
|
|
// Base class for nsTArray_Impl that is templated on element type and derived
|
|
|
|
// nsTArray_Impl class, to allow extra conversions to be added for specific
|
|
|
|
// types.
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E, class Derived>
|
|
|
|
struct nsTArray_TypedBase : public nsTArray_SafeElementAtHelper<E, Derived>
|
|
|
|
{
|
|
|
|
};
|
2013-06-18 14:00:37 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Specialization of nsTArray_TypedBase for arrays containing JS::Heap<E>
|
|
|
|
// elements.
|
|
|
|
//
|
|
|
|
// These conversions are safe because JS::Heap<E> and E share the same
|
|
|
|
// representation, and since the result of the conversions are const references
|
|
|
|
// we won't miss any barriers.
|
|
|
|
//
|
|
|
|
// The static_cast is necessary to obtain the correct address for the derived
|
|
|
|
// class since we are a base class used in multiple inheritance.
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E, class Derived>
|
2013-06-18 14:00:37 +04:00
|
|
|
struct nsTArray_TypedBase<JS::Heap<E>, Derived>
|
2014-06-27 05:35:39 +04:00
|
|
|
: public nsTArray_SafeElementAtHelper<JS::Heap<E>, Derived>
|
2013-06-18 14:00:37 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
operator const nsTArray<E>&()
|
|
|
|
{
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(sizeof(E) == sizeof(JS::Heap<E>),
|
|
|
|
"JS::Heap<E> must be binary compatible with E.");
|
2013-06-18 14:00:37 +04:00
|
|
|
Derived* self = static_cast<Derived*>(this);
|
|
|
|
return *reinterpret_cast<nsTArray<E> *>(self);
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
operator const FallibleTArray<E>&()
|
|
|
|
{
|
2013-06-18 14:00:37 +04:00
|
|
|
Derived* self = static_cast<Derived*>(this);
|
|
|
|
return *reinterpret_cast<FallibleTArray<E> *>(self);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-17 17:46:24 +04:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<class Item, class Comparator>
|
2014-09-23 15:15:53 +04:00
|
|
|
struct ItemComparatorEq
|
2014-09-17 17:46:24 +04:00
|
|
|
{
|
|
|
|
const Item& mItem;
|
|
|
|
const Comparator& mComp;
|
2014-09-23 15:15:53 +04:00
|
|
|
ItemComparatorEq(const Item& aItem, const Comparator& aComp)
|
|
|
|
: mItem(aItem)
|
|
|
|
, mComp(aComp)
|
|
|
|
{}
|
|
|
|
template<class T>
|
|
|
|
int operator()(const T& aElement) const {
|
|
|
|
if (mComp.Equals(aElement, mItem)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mComp.LessThan(aElement, mItem) ? 1 : -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Item, class Comparator>
|
|
|
|
struct ItemComparatorFirstElementGT
|
|
|
|
{
|
|
|
|
const Item& mItem;
|
|
|
|
const Comparator& mComp;
|
|
|
|
ItemComparatorFirstElementGT(const Item& aItem, const Comparator& aComp)
|
2014-09-17 17:46:24 +04:00
|
|
|
: mItem(aItem)
|
|
|
|
, mComp(aComp)
|
|
|
|
{}
|
|
|
|
template<class T>
|
|
|
|
int operator()(const T& aElement) const {
|
|
|
|
if (mComp.LessThan(aElement, mItem) ||
|
|
|
|
mComp.Equals(aElement, mItem)) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
2013-06-18 14:00:37 +04:00
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// nsTArray_Impl contains most of the guts supporting nsTArray, FallibleTArray,
|
2016-02-02 18:36:30 +03:00
|
|
|
// AutoTArray.
|
2005-11-23 22:44:16 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// The only situation in which you might need to use nsTArray_Impl in your code
|
|
|
|
// is if you're writing code which mutates a TArray which may or may not be
|
|
|
|
// infallible.
|
2005-11-23 22:44:16 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// Code which merely reads from a TArray which may or may not be infallible can
|
|
|
|
// simply cast the TArray to |const nsTArray&|; both fallible and infallible
|
|
|
|
// TArrays can be cast to |const nsTArray&|.
|
2005-11-23 22:44:16 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
template<class E, class Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
class nsTArray_Impl
|
|
|
|
: public nsTArray_base<Alloc, typename nsTArray_CopyChooser<E>::Type>
|
|
|
|
, public nsTArray_TypedBase<E, nsTArray_Impl<E, Alloc>>
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
2015-05-18 23:50:34 +03:00
|
|
|
private:
|
|
|
|
typedef nsTArrayFallibleAllocator FallibleAlloc;
|
2015-11-02 08:53:27 +03:00
|
|
|
typedef nsTArrayInfallibleAllocator InfallibleAlloc;
|
2015-05-18 23:50:34 +03:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
public:
|
2013-10-22 18:36:34 +04:00
|
|
|
typedef typename nsTArray_CopyChooser<E>::Type copy_type;
|
2013-06-18 14:00:37 +04:00
|
|
|
typedef nsTArray_base<Alloc, copy_type> base_type;
|
|
|
|
typedef typename base_type::size_type size_type;
|
|
|
|
typedef typename base_type::index_type index_type;
|
|
|
|
typedef E elem_type;
|
|
|
|
typedef nsTArray_Impl<E, Alloc> self_type;
|
|
|
|
typedef nsTArrayElementTraits<E> elem_traits;
|
2011-08-10 09:36:00 +04:00
|
|
|
typedef nsTArray_SafeElementAtHelper<E, self_type> safeelementat_helper_type;
|
2017-02-24 08:14:06 +03:00
|
|
|
typedef mozilla::ArrayIterator<elem_type&, nsTArray<E>> iterator;
|
|
|
|
typedef mozilla::ArrayIterator<const elem_type&, nsTArray<E>> const_iterator;
|
2016-09-01 01:42:12 +03:00
|
|
|
typedef mozilla::ReverseIterator<iterator> reverse_iterator;
|
|
|
|
typedef mozilla::ReverseIterator<const_iterator> const_reverse_iterator;
|
2011-08-10 09:36:00 +04:00
|
|
|
|
|
|
|
using safeelementat_helper_type::SafeElementAt;
|
2011-11-10 05:40:09 +04:00
|
|
|
using base_type::EmptyHdr;
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// A special value that is used to indicate an invalid or unknown index
|
|
|
|
// into the array.
|
2014-05-30 07:18:29 +04:00
|
|
|
static const index_type NoIndex = index_type(-1);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
using base_type::Length;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Finalization method
|
|
|
|
//
|
|
|
|
|
2017-06-02 00:38:13 +03:00
|
|
|
~nsTArray_Impl()
|
|
|
|
{
|
|
|
|
if (!base_type::IsEmpty()) {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Initialization methods
|
|
|
|
//
|
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
nsTArray_Impl() {}
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// Initialize this array and pre-allocate some number of elements.
|
2014-06-27 05:35:39 +04:00
|
|
|
explicit nsTArray_Impl(size_type aCapacity) { SetCapacity(aCapacity); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2014-03-15 21:55:00 +04:00
|
|
|
// Initialize this array with an r-value.
|
|
|
|
// Allow different types of allocators, since the allocator doesn't matter.
|
|
|
|
template<typename Allocator>
|
|
|
|
explicit nsTArray_Impl(nsTArray_Impl<E, Allocator>&& aOther)
|
|
|
|
{
|
|
|
|
SwapElements(aOther);
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// The array's copy-constructor performs a 'deep' copy of the given array.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aOther The array object to copy.
|
2012-12-19 05:16:06 +04:00
|
|
|
//
|
|
|
|
// It's very important that we declare this method as taking |const
|
|
|
|
// self_type&| as opposed to taking |const nsTArray_Impl<E, OtherAlloc>| for
|
|
|
|
// an arbitrary OtherAlloc.
|
|
|
|
//
|
|
|
|
// If we don't declare a constructor taking |const self_type&|, C++ generates
|
|
|
|
// a copy-constructor for this class which merely copies the object's
|
|
|
|
// members, which is obviously wrong.
|
|
|
|
//
|
|
|
|
// You can pass an nsTArray_Impl<E, OtherAlloc> to this method because
|
|
|
|
// nsTArray_Impl<E, X> can be cast to const nsTArray_Impl<E, Y>&. So the
|
|
|
|
// effect on the API is the same as if we'd declared this method as taking
|
|
|
|
// |const nsTArray_Impl<E, OtherAlloc>&|.
|
2014-06-27 05:35:39 +04:00
|
|
|
explicit nsTArray_Impl(const self_type& aOther) { AppendElements(aOther); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-01-07 18:53:51 +03:00
|
|
|
explicit nsTArray_Impl(std::initializer_list<E> aIL) { AppendElements(aIL.begin(), aIL.size()); }
|
2012-12-19 05:16:05 +04:00
|
|
|
// Allow converting to a const array with a different kind of allocator,
|
|
|
|
// Since the allocator doesn't matter for const arrays
|
|
|
|
template<typename Allocator>
|
2014-06-27 05:35:39 +04:00
|
|
|
operator const nsTArray_Impl<E, Allocator>&() const
|
|
|
|
{
|
2012-12-19 05:16:06 +04:00
|
|
|
return *reinterpret_cast<const nsTArray_Impl<E, Allocator>*>(this);
|
2012-12-19 05:16:05 +04:00
|
|
|
}
|
|
|
|
// And we have to do this for our subclasses too
|
2014-06-27 05:35:39 +04:00
|
|
|
operator const nsTArray<E>&() const
|
|
|
|
{
|
2012-12-19 05:16:05 +04:00
|
|
|
return *reinterpret_cast<const InfallibleTArray<E>*>(this);
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
operator const FallibleTArray<E>&() const
|
|
|
|
{
|
2012-12-19 05:16:05 +04:00
|
|
|
return *reinterpret_cast<const FallibleTArray<E>*>(this);
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// The array's assignment operator performs a 'deep' copy of the given
|
|
|
|
// array. It is optimized to reuse existing storage if possible.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aOther The array object to copy.
|
|
|
|
self_type& operator=(const self_type& aOther)
|
|
|
|
{
|
2015-07-21 19:42:58 +03:00
|
|
|
if (this != &aOther) {
|
|
|
|
ReplaceElementsAt(0, Length(), aOther.Elements(), aOther.Length());
|
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-03-15 21:55:00 +04:00
|
|
|
// The array's move assignment operator steals the underlying data from
|
|
|
|
// the other array.
|
|
|
|
// @param other The array object to move from.
|
|
|
|
self_type& operator=(self_type&& aOther)
|
|
|
|
{
|
2015-07-21 19:42:58 +03:00
|
|
|
if (this != &aOther) {
|
|
|
|
Clear();
|
|
|
|
SwapElements(aOther);
|
|
|
|
}
|
2014-03-15 21:55:00 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// Return true if this array has the same length and the same
|
2014-06-27 05:35:39 +04:00
|
|
|
// elements as |aOther|.
|
2012-11-29 20:14:14 +04:00
|
|
|
template<typename Allocator>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator==(const nsTArray_Impl<E, Allocator>& aOther) const
|
|
|
|
{
|
2010-11-09 05:48:59 +03:00
|
|
|
size_type len = Length();
|
2014-06-27 05:35:39 +04:00
|
|
|
if (len != aOther.Length()) {
|
2010-11-09 05:48:59 +03:00
|
|
|
return false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// XXX std::equal would be as fast or faster here
|
2014-06-27 05:35:39 +04:00
|
|
|
for (index_type i = 0; i < len; ++i) {
|
|
|
|
if (!(operator[](i) == aOther[i])) {
|
2010-05-22 23:35:41 +04:00
|
|
|
return false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
}
|
2010-05-22 23:35:41 +04:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if this array does not have the same length and the same
|
2014-06-27 05:35:39 +04:00
|
|
|
// elements as |aOther|.
|
|
|
|
bool operator!=(const self_type& aOther) const { return !operator==(aOther); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
template<typename Allocator>
|
2014-06-27 05:35:39 +04:00
|
|
|
self_type& operator=(const nsTArray_Impl<E, Allocator>& aOther)
|
|
|
|
{
|
|
|
|
ReplaceElementsAt(0, Length(), aOther.Elements(), aOther.Length());
|
2010-11-09 05:48:59 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-03-15 21:55:00 +04:00
|
|
|
template<typename Allocator>
|
|
|
|
self_type& operator=(nsTArray_Impl<E, Allocator>&& aOther)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
SwapElements(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
// @return The amount of memory used by this nsTArray_Impl, excluding
|
2015-07-29 09:24:24 +03:00
|
|
|
// sizeof(*this). If you want to measure anything hanging off the array, you
|
|
|
|
// must iterate over the elements and measure them individually; hence the
|
|
|
|
// "Shallow" prefix.
|
|
|
|
size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
|
|
|
if (this->UsesAutoArrayBuffer() || Hdr() == EmptyHdr()) {
|
2011-11-10 05:40:09 +04:00
|
|
|
return 0;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
return aMallocSizeOf(this->Hdr());
|
2011-12-16 02:59:53 +04:00
|
|
|
}
|
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
// @return The amount of memory used by this nsTArray_Impl, including
|
2015-07-29 09:24:24 +03:00
|
|
|
// sizeof(*this). If you want to measure anything hanging off the array, you
|
|
|
|
// must iterate over the elements and measure them individually; hence the
|
|
|
|
// "Shallow" prefix.
|
|
|
|
size_t ShallowSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-07-29 09:24:24 +03:00
|
|
|
return aMallocSizeOf(this) + ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2011-08-01 22:37:00 +04:00
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
|
|
|
// Accessor methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// This method provides direct access to the array elements.
|
|
|
|
// @return A pointer to the first element of the array. If the array is
|
|
|
|
// empty, then this pointer must not be dereferenced.
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* Elements() { return reinterpret_cast<elem_type*>(Hdr() + 1); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// This method provides direct, readonly access to the array elements.
|
|
|
|
// @return A pointer to the first element of the array. If the array is
|
|
|
|
// empty, then this pointer must not be dereferenced.
|
2014-06-27 05:35:39 +04:00
|
|
|
const elem_type* Elements() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const elem_type*>(Hdr() + 1);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
2013-06-18 14:00:37 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// This method provides direct access to an element of the array. The given
|
|
|
|
// index must be within the array bounds.
|
|
|
|
// @param aIndex The index of an element in the array.
|
|
|
|
// @return A reference to the i'th element of the array.
|
|
|
|
elem_type& ElementAt(index_type aIndex)
|
|
|
|
{
|
2016-08-10 21:40:00 +03:00
|
|
|
if (MOZ_UNLIKELY(aIndex >= Length())) {
|
|
|
|
InvalidArrayIndex_CRASH(aIndex, Length());
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
return Elements()[aIndex];
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// This method provides direct, readonly access to an element of the array
|
|
|
|
// The given index must be within the array bounds.
|
|
|
|
// @param aIndex The index of an element in the array.
|
|
|
|
// @return A const reference to the i'th element of the array.
|
|
|
|
const elem_type& ElementAt(index_type aIndex) const
|
|
|
|
{
|
2016-08-10 21:40:00 +03:00
|
|
|
if (MOZ_UNLIKELY(aIndex >= Length())) {
|
|
|
|
InvalidArrayIndex_CRASH(aIndex, Length());
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
return Elements()[aIndex];
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// This method provides direct access to an element of the array in a bounds
|
|
|
|
// safe manner. If the requested index is out of bounds the provided default
|
|
|
|
// value is returned.
|
|
|
|
// @param aIndex The index of an element in the array.
|
|
|
|
// @param aDef The value to return if the index is out of bounds.
|
|
|
|
elem_type& SafeElementAt(index_type aIndex, elem_type& aDef)
|
|
|
|
{
|
|
|
|
return aIndex < Length() ? Elements()[aIndex] : aDef;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// This method provides direct access to an element of the array in a bounds
|
|
|
|
// safe manner. If the requested index is out of bounds the provided default
|
|
|
|
// value is returned.
|
|
|
|
// @param aIndex The index of an element in the array.
|
|
|
|
// @param aDef The value to return if the index is out of bounds.
|
|
|
|
const elem_type& SafeElementAt(index_type aIndex, const elem_type& aDef) const
|
|
|
|
{
|
|
|
|
return aIndex < Length() ? Elements()[aIndex] : aDef;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Shorthand for ElementAt(aIndex)
|
|
|
|
elem_type& operator[](index_type aIndex) { return ElementAt(aIndex); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Shorthand for ElementAt(aIndex)
|
|
|
|
const elem_type& operator[](index_type aIndex) const { return ElementAt(aIndex); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2012-03-27 02:48:25 +04:00
|
|
|
// Shorthand for ElementAt(length - 1)
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type& LastElement() { return ElementAt(Length() - 1); }
|
2012-03-27 02:48:25 +04:00
|
|
|
|
|
|
|
// Shorthand for ElementAt(length - 1)
|
2014-06-27 05:35:39 +04:00
|
|
|
const elem_type& LastElement() const { return ElementAt(Length() - 1); }
|
2012-03-27 02:48:25 +04:00
|
|
|
|
|
|
|
// Shorthand for SafeElementAt(length - 1, def)
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type& SafeLastElement(elem_type& aDef)
|
|
|
|
{
|
|
|
|
return SafeElementAt(Length() - 1, aDef);
|
2012-03-27 02:48:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shorthand for SafeElementAt(length - 1, def)
|
2014-06-27 05:35:39 +04:00
|
|
|
const elem_type& SafeLastElement(const elem_type& aDef) const
|
|
|
|
{
|
|
|
|
return SafeElementAt(Length() - 1, aDef);
|
2012-03-27 02:48:25 +04:00
|
|
|
}
|
|
|
|
|
2015-02-04 11:07:32 +03:00
|
|
|
// Methods for range-based for loops.
|
2016-09-01 01:42:12 +03:00
|
|
|
iterator begin() { return iterator(*this, 0); }
|
|
|
|
const_iterator begin() const { return const_iterator(*this, 0); }
|
2015-02-04 11:07:32 +03:00
|
|
|
const_iterator cbegin() const { return begin(); }
|
2016-09-01 01:42:12 +03:00
|
|
|
iterator end() { return iterator(*this, Length()); }
|
|
|
|
const_iterator end() const { return const_iterator(*this, Length()); }
|
2015-02-04 11:07:32 +03:00
|
|
|
const_iterator cend() const { return end(); }
|
|
|
|
|
2015-02-04 11:07:32 +03:00
|
|
|
// Methods for reverse iterating.
|
|
|
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
|
|
|
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
|
|
|
|
const_reverse_iterator crbegin() const { return rbegin(); }
|
|
|
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator crend() const { return rend(); }
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
// Span integration
|
|
|
|
|
|
|
|
operator mozilla::Span<elem_type>()
|
|
|
|
{
|
|
|
|
return mozilla::Span<elem_type>(Elements(), Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
operator mozilla::Span<const elem_type>() const
|
|
|
|
{
|
|
|
|
return mozilla::Span<const elem_type>(Elements(), Length());
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
|
|
|
// Search methods
|
|
|
|
//
|
|
|
|
|
|
|
|
// This method searches for the first element in this array that is equal
|
|
|
|
// to the given element.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aComp The Comparator used to determine element equality.
|
2011-10-17 18:59:28 +04:00
|
|
|
// @return true if the element was found.
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item, class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool Contains(const Item& aItem, const Comparator& aComp) const
|
|
|
|
{
|
|
|
|
return IndexOf(aItem, 0, aComp) != NoIndex;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the first element in this array that is equal
|
|
|
|
// to the given element. This method assumes that 'operator==' is defined
|
|
|
|
// for elem_type.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
2011-10-17 18:59:28 +04:00
|
|
|
// @return true if the element was found.
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool Contains(const Item& aItem) const
|
|
|
|
{
|
|
|
|
return IndexOf(aItem) != NoIndex;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the offset of the first element in this
|
|
|
|
// array that is equal to the given element.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aStart The index to start from.
|
|
|
|
// @param aComp The Comparator used to determine element equality.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return The index of the found element or NoIndex if not found.
|
|
|
|
template<class Item, class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
index_type IndexOf(const Item& aItem, index_type aStart,
|
|
|
|
const Comparator& aComp) const
|
|
|
|
{
|
2015-02-04 11:07:32 +03:00
|
|
|
const elem_type* iter = Elements() + aStart;
|
|
|
|
const elem_type* iend = Elements() + Length();
|
|
|
|
for (; iter != iend; ++iter) {
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aComp.Equals(*iter, aItem)) {
|
2010-11-17 08:51:05 +03:00
|
|
|
return index_type(iter - Elements());
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the offset of the first element in this
|
|
|
|
// array that is equal to the given element. This method assumes
|
|
|
|
// that 'operator==' is defined for elem_type.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aStart The index to start from.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return The index of the found element or NoIndex if not found.
|
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
index_type IndexOf(const Item& aItem, index_type aStart = 0) const
|
|
|
|
{
|
|
|
|
return IndexOf(aItem, aStart, nsDefaultComparator<elem_type, Item>());
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the offset of the last element in this
|
|
|
|
// array that is equal to the given element.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aStart The index to start from. If greater than or equal to the
|
2010-11-09 05:48:59 +03:00
|
|
|
// length of the array, then the entire array is searched.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aComp The Comparator used to determine element equality.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return The index of the found element or NoIndex if not found.
|
|
|
|
template<class Item, class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
index_type LastIndexOf(const Item& aItem, index_type aStart,
|
|
|
|
const Comparator& aComp) const
|
|
|
|
{
|
|
|
|
size_type endOffset = aStart >= Length() ? Length() : aStart + 1;
|
2015-02-04 11:07:32 +03:00
|
|
|
const elem_type* iend = Elements() - 1;
|
|
|
|
const elem_type* iter = iend + endOffset;
|
|
|
|
for (; iter != iend; --iter) {
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aComp.Equals(*iter, aItem)) {
|
2010-11-09 05:48:59 +03:00
|
|
|
return index_type(iter - Elements());
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the offset of the last element in this
|
|
|
|
// array that is equal to the given element. This method assumes
|
|
|
|
// that 'operator==' is defined for elem_type.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aStart The index to start from. If greater than or equal to the
|
2010-11-09 05:48:59 +03:00
|
|
|
// length of the array, then the entire array is searched.
|
|
|
|
// @return The index of the found element or NoIndex if not found.
|
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
index_type LastIndexOf(const Item& aItem,
|
|
|
|
index_type aStart = NoIndex) const
|
|
|
|
{
|
|
|
|
return LastIndexOf(aItem, aStart, nsDefaultComparator<elem_type, Item>());
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the offset for the element in this array
|
|
|
|
// that is equal to the given element. The array is assumed to be sorted.
|
2014-09-23 15:15:53 +04:00
|
|
|
// If there is more than one equivalent element, there is no guarantee
|
|
|
|
// on which one will be returned.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aComp The Comparator used.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return The index of the found element or NoIndex if not found.
|
|
|
|
template<class Item, class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
index_type BinaryIndexOf(const Item& aItem, const Comparator& aComp) const
|
|
|
|
{
|
2014-09-23 15:15:53 +04:00
|
|
|
using mozilla::BinarySearchIf;
|
|
|
|
typedef ::detail::ItemComparatorEq<Item, Comparator> Cmp;
|
|
|
|
|
|
|
|
size_t index;
|
|
|
|
bool found = BinarySearchIf(*this, 0, Length(), Cmp(aItem, aComp), &index);
|
|
|
|
return found ? index : NoIndex;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method searches for the offset for the element in this array
|
|
|
|
// that is equal to the given element. The array is assumed to be sorted.
|
|
|
|
// This method assumes that 'operator==' and 'operator<' are defined.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return The index of the found element or NoIndex if not found.
|
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
index_type BinaryIndexOf(const Item& aItem) const
|
|
|
|
{
|
|
|
|
return BinaryIndexOf(aItem, nsDefaultComparator<elem_type, Item>());
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Mutation methods
|
|
|
|
//
|
2015-07-07 21:27:03 +03:00
|
|
|
|
|
|
|
template<class Allocator, typename ActualAlloc = Alloc>
|
|
|
|
typename ActualAlloc::ResultType Assign(
|
|
|
|
const nsTArray_Impl<E, Allocator>& aOther)
|
|
|
|
{
|
|
|
|
return ActualAlloc::ConvertBoolToResultType(
|
|
|
|
!!ReplaceElementsAt<E, ActualAlloc>(0, Length(),
|
|
|
|
aOther.Elements(), aOther.Length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Allocator>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-07-07 21:27:03 +03:00
|
|
|
bool Assign(const nsTArray_Impl<E, Allocator>& aOther,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return Assign<Allocator, FallibleAlloc>(aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Allocator>
|
|
|
|
void Assign(nsTArray_Impl<E, Allocator>&& aOther)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
SwapElements(aOther);
|
|
|
|
}
|
|
|
|
|
2013-07-19 18:40:56 +04:00
|
|
|
// This method call the destructor on each element of the array, empties it,
|
|
|
|
// but does not shrink the array's capacity.
|
2013-09-08 06:05:02 +04:00
|
|
|
// See also SetLengthAndRetainStorage.
|
2013-07-19 18:40:56 +04:00
|
|
|
// Make sure to call Compact() if needed to avoid keeping a huge array
|
|
|
|
// around.
|
2014-06-27 05:35:39 +04:00
|
|
|
void ClearAndRetainStorage()
|
|
|
|
{
|
2013-07-19 18:40:56 +04:00
|
|
|
if (base_type::mHdr == EmptyHdr()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DestructRange(0, Length());
|
|
|
|
base_type::mHdr->mLength = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-08 06:05:02 +04:00
|
|
|
// This method modifies the length of the array, but unlike SetLength
|
|
|
|
// it doesn't deallocate/reallocate the current internal storage.
|
|
|
|
// The new length MUST be shorter than or equal to the current capacity.
|
|
|
|
// If the new length is larger than the existing length of the array,
|
|
|
|
// then new elements will be constructed using elem_type's default
|
|
|
|
// constructor. If shorter, elements will be destructed and removed.
|
|
|
|
// See also ClearAndRetainStorage.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aNewLen The desired length of this array.
|
|
|
|
void SetLengthAndRetainStorage(size_type aNewLen)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNewLen <= base_type::Capacity());
|
2013-09-08 06:05:02 +04:00
|
|
|
size_type oldLen = Length();
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aNewLen > oldLen) {
|
|
|
|
InsertElementsAt(oldLen, aNewLen - oldLen);
|
2013-09-08 06:05:02 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aNewLen < oldLen) {
|
|
|
|
DestructRange(aNewLen, oldLen - aNewLen);
|
|
|
|
base_type::mHdr->mLength = aNewLen;
|
2013-09-08 06:05:02 +04:00
|
|
|
}
|
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// This method replaces a range of elements in this array.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aStart The starting index of the elements to replace.
|
|
|
|
// @param aCount The number of elements to replace. This may be zero to
|
2010-11-09 05:48:59 +03:00
|
|
|
// insert elements without removing any existing elements.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aArray The values to copy into this array. Must be non-null,
|
2010-11-09 05:48:59 +03:00
|
|
|
// and these elements must not already exist in the array
|
|
|
|
// being modified.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aArrayLen The number of values to copy into this array.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return A pointer to the new elements in the array, or null if
|
|
|
|
// the operation failed due to insufficient memory.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* ReplaceElementsAt(index_type aStart, size_type aCount,
|
2016-07-08 23:59:05 +03:00
|
|
|
const Item* aArray, size_type aArrayLen);
|
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-05-18 23:50:35 +03:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* ReplaceElementsAt(index_type aStart, size_type aCount,
|
2015-05-18 23:50:35 +03:00
|
|
|
const Item* aArray, size_type aArrayLen,
|
|
|
|
const mozilla::fallible_t&)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-05-18 23:50:35 +03:00
|
|
|
return ReplaceElementsAt<Item, FallibleAlloc>(aStart, aCount,
|
|
|
|
aArray, aArrayLen);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A variation on the ReplaceElementsAt method defined above.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
|
|
|
elem_type* ReplaceElementsAt(index_type aStart, size_type aCount,
|
|
|
|
const nsTArray<Item>& aArray)
|
|
|
|
{
|
|
|
|
return ReplaceElementsAt<Item, ActualAlloc>(
|
|
|
|
aStart, aCount, aArray.Elements(), aArray.Length());
|
|
|
|
}
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
|
|
|
elem_type* ReplaceElementsAt(index_type aStart,
|
|
|
|
size_type aCount,
|
|
|
|
mozilla::Span<const Item> aSpan)
|
|
|
|
{
|
|
|
|
return ReplaceElementsAt<Item, ActualAlloc>(
|
|
|
|
aStart, aCount, aSpan.Elements(), aSpan.Length());
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-05-18 23:50:35 +03:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* ReplaceElementsAt(index_type aStart, size_type aCount,
|
|
|
|
const nsTArray<Item>& aArray,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return ReplaceElementsAt<Item, FallibleAlloc>(aStart, aCount, aArray);
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
template<class Item>
|
|
|
|
MOZ_MUST_USE elem_type* ReplaceElementsAt(index_type aStart,
|
|
|
|
size_type aCount,
|
|
|
|
mozilla::Span<const Item> aSpan,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return ReplaceElementsAt<Item, FallibleAlloc>(aStart, aCount, aSpan);
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:50:35 +03:00
|
|
|
// A variation on the ReplaceElementsAt method defined above.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* ReplaceElementsAt(index_type aStart, size_type aCount,
|
|
|
|
const Item& aItem)
|
|
|
|
{
|
2015-05-18 23:50:35 +03:00
|
|
|
return ReplaceElementsAt<Item, ActualAlloc>(aStart, aCount, &aItem, 1);
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-05-18 23:50:35 +03:00
|
|
|
|
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* ReplaceElementsAt(index_type aStart, size_type aCount,
|
|
|
|
const Item& aItem, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return ReplaceElementsAt<Item, FallibleAlloc>(aStart, aCount, aItem);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
2011-09-20 19:34:35 +04:00
|
|
|
|
|
|
|
// A variation on the ReplaceElementsAt method defined above.
|
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* ReplaceElementAt(index_type aIndex, const Item& aItem)
|
|
|
|
{
|
|
|
|
return ReplaceElementsAt(aIndex, 1, &aItem, 1);
|
2011-09-20 19:34:35 +04:00
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// A variation on the ReplaceElementsAt method defined above.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-06-10 03:27:31 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex, const Item* aArray,
|
|
|
|
size_type aArrayLen)
|
|
|
|
{
|
2015-06-10 03:27:31 +03:00
|
|
|
return ReplaceElementsAt<Item, ActualAlloc>(aIndex, 0, aArray, aArrayLen);
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-06-10 03:27:31 +03:00
|
|
|
|
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-06-10 03:27:31 +03:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex, const Item* aArray,
|
|
|
|
size_type aArrayLen, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementsAt<Item, FallibleAlloc>(aIndex, aArray, aArrayLen);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A variation on the ReplaceElementsAt method defined above.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-06-10 03:27:31 +03:00
|
|
|
template<class Item, class Allocator, typename ActualAlloc = Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex,
|
|
|
|
const nsTArray_Impl<Item, Allocator>& aArray)
|
|
|
|
{
|
2015-06-10 03:27:31 +03:00
|
|
|
return ReplaceElementsAt<Item, ActualAlloc>(
|
|
|
|
aIndex, 0, aArray.Elements(), aArray.Length());
|
|
|
|
}
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
|
|
|
elem_type* InsertElementsAt(index_type aIndex,
|
|
|
|
mozilla::Span<const Item> aSpan)
|
|
|
|
{
|
|
|
|
return ReplaceElementsAt<Item, ActualAlloc>(
|
|
|
|
aIndex, 0, aSpan.Elements(), aSpan.Length());
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-06-10 03:27:31 +03:00
|
|
|
|
|
|
|
template<class Item, class Allocator>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-06-10 03:27:31 +03:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex,
|
|
|
|
const nsTArray_Impl<Item, Allocator>& aArray,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementsAt<Item, Allocator, FallibleAlloc>(aIndex, aArray);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
template<class Item>
|
|
|
|
MOZ_MUST_USE elem_type* InsertElementsAt(index_type aIndex,
|
|
|
|
mozilla::Span<const Item> aSpan,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementsAt<Item, FallibleAlloc>(aIndex, aSpan);
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// Insert a new element without copy-constructing. This is useful to avoid
|
|
|
|
// temporaries.
|
|
|
|
// @return A pointer to the newly inserted element, or null on OOM.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<typename ActualAlloc = Alloc>
|
2016-07-08 23:59:05 +03:00
|
|
|
elem_type* InsertElementAt(index_type aIndex);
|
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* InsertElementAt(index_type aIndex, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementAt<FallibleAlloc>(aIndex);
|
|
|
|
}
|
|
|
|
|
2014-08-28 03:18:42 +04:00
|
|
|
// Insert a new element, move constructing if possible.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2016-07-08 23:59:05 +03:00
|
|
|
elem_type* InsertElementAt(index_type aIndex, Item&& aItem);
|
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2014-08-06 22:49:37 +04:00
|
|
|
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* InsertElementAt(index_type aIndex, Item&& aItem,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementAt<Item, FallibleAlloc>(aIndex,
|
|
|
|
mozilla::Forward<Item>(aItem));
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:59:59 +03:00
|
|
|
// Reconstruct the element at the given index, and return a pointer to the
|
|
|
|
// reconstructed element. This will destroy the existing element and
|
|
|
|
// default-construct a new one, giving you a state much like what single-arg
|
|
|
|
// InsertElementAt(), or no-arg AppendElement() does, but without changing the
|
|
|
|
// length of the array.
|
|
|
|
//
|
|
|
|
// array[idx] = T()
|
|
|
|
//
|
|
|
|
// would accomplish the same thing as long as T has the appropriate moving
|
|
|
|
// operator=, but some types don't for various reasons.
|
|
|
|
elem_type* ReconstructElementAt(index_type aIndex)
|
|
|
|
{
|
|
|
|
elem_type* elem = &ElementAt(aIndex);
|
|
|
|
elem_traits::Destruct(elem);
|
|
|
|
elem_traits::Construct(elem);
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:11:53 +04:00
|
|
|
// This method searches for the smallest index of an element that is strictly
|
2014-06-27 05:35:39 +04:00
|
|
|
// greater than |aItem|. If |aItem| is inserted at this index, the array will
|
|
|
|
// remain sorted and |aItem| would come after all elements that are equal to
|
|
|
|
// it. If |aItem| is greater than or equal to all elements in the array, the
|
2013-02-13 19:11:53 +04:00
|
|
|
// array length is returned.
|
|
|
|
//
|
|
|
|
// Note that consumers who want to know whether there are existing items equal
|
2014-06-27 05:35:39 +04:00
|
|
|
// to |aItem| in the array can just check that the return value here is > 0
|
|
|
|
// and indexing into the previous slot gives something equal to |aItem|.
|
2013-02-13 19:11:53 +04:00
|
|
|
//
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aComp The Comparator used.
|
|
|
|
// @return The index of greatest element <= to |aItem|
|
2010-11-09 05:48:59 +03:00
|
|
|
// @precondition The array is sorted
|
|
|
|
template<class Item, class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
index_type IndexOfFirstElementGt(const Item& aItem,
|
|
|
|
const Comparator& aComp) const
|
|
|
|
{
|
2014-09-17 17:46:24 +04:00
|
|
|
using mozilla::BinarySearchIf;
|
2014-09-23 15:15:53 +04:00
|
|
|
typedef ::detail::ItemComparatorFirstElementGT<Item, Comparator> Cmp;
|
2014-09-17 17:46:24 +04:00
|
|
|
|
|
|
|
size_t index;
|
|
|
|
BinarySearchIf(*this, 0, Length(), Cmp(aItem, aComp), &index);
|
|
|
|
return index;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2013-02-13 19:11:53 +04:00
|
|
|
// A variation on the IndexOfFirstElementGt method defined above.
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item>
|
2013-02-13 19:11:53 +04:00
|
|
|
index_type
|
2014-06-27 05:35:39 +04:00
|
|
|
IndexOfFirstElementGt(const Item& aItem) const
|
|
|
|
{
|
|
|
|
return IndexOfFirstElementGt(aItem, nsDefaultComparator<elem_type, Item>());
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Inserts |aItem| at such an index to guarantee that if the array
|
2010-11-09 05:48:59 +03:00
|
|
|
// was previously sorted, it will remain sorted after this
|
|
|
|
// insertion.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-06-08 23:39:57 +03:00
|
|
|
template<class Item, class Comparator, typename ActualAlloc = Alloc>
|
2015-06-08 23:39:53 +03:00
|
|
|
elem_type* InsertElementSorted(Item&& aItem, const Comparator& aComp)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-06-08 23:39:53 +03:00
|
|
|
index_type index = IndexOfFirstElementGt<Item, Comparator>(aItem, aComp);
|
2015-06-08 23:39:57 +03:00
|
|
|
return InsertElementAt<Item, ActualAlloc>(
|
2015-06-08 23:39:53 +03:00
|
|
|
index, mozilla::Forward<Item>(aItem));
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2015-06-08 23:39:57 +03:00
|
|
|
template<class Item, class Comparator>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-06-08 23:39:57 +03:00
|
|
|
elem_type* InsertElementSorted(Item&& aItem, const Comparator& aComp,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementSorted<Item, Comparator, FallibleAlloc>(
|
|
|
|
mozilla::Forward<Item>(aItem), aComp);
|
|
|
|
}
|
|
|
|
|
2012-05-31 04:32:17 +04:00
|
|
|
// A variation on the InsertElementSorted method defined above.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-06-08 23:39:57 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2015-06-08 23:39:53 +03:00
|
|
|
elem_type* InsertElementSorted(Item&& aItem)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-06-08 23:39:53 +03:00
|
|
|
nsDefaultComparator<elem_type, Item> comp;
|
2015-06-08 23:39:57 +03:00
|
|
|
return InsertElementSorted<Item, decltype(comp), ActualAlloc>(
|
2015-06-08 23:39:53 +03:00
|
|
|
mozilla::Forward<Item>(aItem), comp);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2015-06-08 23:39:57 +03:00
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-06-08 23:39:57 +03:00
|
|
|
elem_type* InsertElementSorted(Item&& aItem, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementSorted<Item, FallibleAlloc>(
|
|
|
|
mozilla::Forward<Item>(aItem));
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// This method appends elements to the end of this array.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aArray The elements to append to this array.
|
|
|
|
// @param aArrayLen The number of elements to append to this array.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return A pointer to the new elements in the array, or null if
|
|
|
|
// the operation failed due to insufficient memory.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2016-07-08 23:59:05 +03:00
|
|
|
elem_type* AppendElements(const Item* aArray, size_type aArrayLen);
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
|
|
|
elem_type* AppendElements(mozilla::Span<const Item> aSpan)
|
|
|
|
{
|
|
|
|
return AppendElements<Item, FallibleAlloc>(aSpan.Elements(),
|
|
|
|
aSpan.Length());
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:11:30 +03:00
|
|
|
template<class Item, size_t Length, typename ActualAlloc = Alloc>
|
|
|
|
elem_type* AppendElements(const mozilla::Array<Item, Length>& aArray)
|
|
|
|
{
|
|
|
|
return AppendElements<Item, ActualAlloc>(&aArray[0], Length);
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
/* MOZ_MUST_USE */
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* AppendElements(const Item* aArray, size_type aArrayLen,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return AppendElements<Item, FallibleAlloc>(aArray, aArrayLen);
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
template<class Item>
|
|
|
|
/* MOZ_MUST_USE */
|
|
|
|
elem_type* AppendElements(mozilla::Span<const Item> aSpan,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return AppendElements<Item, FallibleAlloc>(aSpan.Elements(),
|
|
|
|
aSpan.Length());
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// A variation on the AppendElements method defined above.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item, class Allocator, typename ActualAlloc = Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* AppendElements(const nsTArray_Impl<Item, Allocator>& aArray)
|
|
|
|
{
|
2015-05-18 23:50:35 +03:00
|
|
|
return AppendElements<Item, ActualAlloc>(aArray.Elements(), aArray.Length());
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-05-18 23:50:35 +03:00
|
|
|
|
|
|
|
template<class Item, class Allocator>
|
2016-04-27 07:16:50 +03:00
|
|
|
/* MOZ_MUST_USE */
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* AppendElements(const nsTArray_Impl<Item, Allocator>& aArray,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return AppendElements<Item, Allocator, FallibleAlloc>(aArray);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2015-08-11 18:29:46 +03:00
|
|
|
// Move all elements from another array to the end of this array.
|
|
|
|
// @return A pointer to the newly appended elements, or null on OOM.
|
2015-11-02 08:53:26 +03:00
|
|
|
protected:
|
|
|
|
template<class Item, class Allocator, typename ActualAlloc = Alloc>
|
2016-07-08 23:59:05 +03:00
|
|
|
elem_type* AppendElements(nsTArray_Impl<Item, Allocator>&& aArray);
|
2015-08-11 18:29:46 +03:00
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
template<class Item, class Allocator, typename ActualAlloc = Alloc>
|
2016-04-27 07:16:50 +03:00
|
|
|
/* MOZ_MUST_USE */
|
2015-11-02 08:53:26 +03:00
|
|
|
elem_type* AppendElements(nsTArray_Impl<Item, Allocator>&& aArray,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return AppendElements<Item, Allocator>(mozilla::Move(aArray));
|
|
|
|
}
|
2015-08-11 18:29:46 +03:00
|
|
|
|
2014-08-28 03:18:42 +04:00
|
|
|
// Append a new element, move constructing if possible.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2016-07-08 23:59:05 +03:00
|
|
|
elem_type* AppendElement(Item&& aItem);
|
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2014-07-18 17:43:42 +04:00
|
|
|
|
2015-05-18 23:50:35 +03:00
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
/* MOZ_MUST_USE */
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* AppendElement(Item&& aItem,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return AppendElement<Item, FallibleAlloc>(mozilla::Forward<Item>(aItem));
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// Append new elements without copy-constructing. This is useful to avoid
|
|
|
|
// temporaries.
|
|
|
|
// @return A pointer to the newly appended elements, or null on OOM.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<typename ActualAlloc = Alloc>
|
|
|
|
elem_type* AppendElements(size_type aCount) {
|
|
|
|
if (!ActualAlloc::Successful(this->template EnsureCapacity<ActualAlloc>(
|
2015-05-18 23:50:34 +03:00
|
|
|
Length() + aCount, sizeof(elem_type)))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-08-25 23:17:24 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* elems = Elements() + Length();
|
2010-11-09 05:48:59 +03:00
|
|
|
size_type i;
|
2014-06-27 05:35:39 +04:00
|
|
|
for (i = 0; i < aCount; ++i) {
|
2010-11-09 05:48:59 +03:00
|
|
|
elem_traits::Construct(elems + i);
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
this->IncrementLength(aCount);
|
2010-11-09 05:48:59 +03:00
|
|
|
return elems;
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
/* MOZ_MUST_USE */
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* AppendElements(size_type aCount,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return AppendElements<FallibleAlloc>(aCount);
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// Append a new element without copy-constructing. This is useful to avoid
|
|
|
|
// temporaries.
|
|
|
|
// @return A pointer to the newly appended element, or null on OOM.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:35 +03:00
|
|
|
template<typename ActualAlloc = Alloc>
|
|
|
|
elem_type* AppendElement()
|
|
|
|
{
|
|
|
|
return AppendElements<ActualAlloc>(1);
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-05-18 23:50:35 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
/* MOZ_MUST_USE */
|
2015-05-18 23:50:35 +03:00
|
|
|
elem_type* AppendElement(const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return AppendElement<FallibleAlloc>();
|
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// This method removes a range of elements from this array.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aStart The starting index of the elements to remove.
|
|
|
|
// @param aCount The number of elements to remove.
|
2016-07-08 23:59:05 +03:00
|
|
|
void RemoveElementsAt(index_type aStart, size_type aCount);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// A variation on the RemoveElementsAt method defined above.
|
2014-06-27 05:35:39 +04:00
|
|
|
void RemoveElementAt(index_type aIndex) { RemoveElementsAt(aIndex, 1); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// A variation on the RemoveElementsAt method defined above.
|
2014-06-27 05:35:39 +04:00
|
|
|
void Clear() { RemoveElementsAt(0, Length()); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
// This method removes elements based on the return value of the
|
|
|
|
// callback function aPredicate. If the function returns true for
|
|
|
|
// an element, the element is removed. aPredicate will be called
|
|
|
|
// for each element in order. It is not safe to access the array
|
|
|
|
// inside aPredicate.
|
2016-08-17 10:44:43 +03:00
|
|
|
template<typename Predicate>
|
|
|
|
void RemoveElementsBy(Predicate aPredicate);
|
2016-07-28 05:00:06 +03:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// This helper function combines IndexOf with RemoveElementAt to "search
|
|
|
|
// and destroy" the first element that is equal to the given element.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aComp The Comparator used to determine element equality.
|
2011-10-17 18:59:28 +04:00
|
|
|
// @return true if the element was found
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item, class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool RemoveElement(const Item& aItem, const Comparator& aComp)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aItem, 0, aComp);
|
|
|
|
if (i == NoIndex) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
RemoveElementAt(i);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A variation on the RemoveElement method defined above that assumes
|
|
|
|
// that 'operator==' is defined for elem_type.
|
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool RemoveElement(const Item& aItem)
|
|
|
|
{
|
|
|
|
return RemoveElement(aItem, nsDefaultComparator<elem_type, Item>());
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2013-02-13 19:11:53 +04:00
|
|
|
// This helper function combines IndexOfFirstElementGt with
|
|
|
|
// RemoveElementAt to "search and destroy" the last element that
|
2010-11-09 05:48:59 +03:00
|
|
|
// is equal to the given element.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aItem The item to search for.
|
|
|
|
// @param aComp The Comparator used to determine element equality.
|
2011-10-17 18:59:28 +04:00
|
|
|
// @return true if the element was found
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item, class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool RemoveElementSorted(const Item& aItem, const Comparator& aComp)
|
|
|
|
{
|
|
|
|
index_type index = IndexOfFirstElementGt(aItem, aComp);
|
|
|
|
if (index > 0 && aComp.Equals(ElementAt(index - 1), aItem)) {
|
2013-02-13 19:11:53 +04:00
|
|
|
RemoveElementAt(index - 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A variation on the RemoveElementSorted method defined above.
|
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
bool RemoveElementSorted(const Item& aItem)
|
|
|
|
{
|
|
|
|
return RemoveElementSorted(aItem, nsDefaultComparator<elem_type, Item>());
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method causes the elements contained in this array and the given
|
|
|
|
// array to be swapped.
|
|
|
|
template<class Allocator>
|
2014-06-27 05:35:39 +04:00
|
|
|
typename Alloc::ResultType SwapElements(nsTArray_Impl<E, Allocator>& aOther)
|
|
|
|
{
|
2015-05-18 23:50:34 +03:00
|
|
|
return Alloc::Result(this->template SwapArrayElements<Alloc>(
|
|
|
|
aOther, sizeof(elem_type), MOZ_ALIGNOF(elem_type)));
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Allocation
|
|
|
|
//
|
|
|
|
|
2014-06-02 13:26:12 +04:00
|
|
|
// This method may increase the capacity of this array object by the
|
|
|
|
// specified amount. This method may be called in advance of several
|
|
|
|
// AppendElement operations to minimize heap re-allocations. This method
|
|
|
|
// will not reduce the number of elements in this array.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aCapacity The desired capacity of this array.
|
2010-11-09 05:48:59 +03:00
|
|
|
// @return True if the operation succeeded; false if we ran out of memory
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc = Alloc>
|
|
|
|
typename ActualAlloc::ResultType SetCapacity(size_type aCapacity)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-05-18 23:50:34 +03:00
|
|
|
return ActualAlloc::Result(this->template EnsureCapacity<ActualAlloc>(
|
2015-05-18 23:50:34 +03:00
|
|
|
aCapacity, sizeof(elem_type)));
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:34 +03:00
|
|
|
bool SetCapacity(size_type aCapacity, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return SetCapacity<FallibleAlloc>(aCapacity);
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// This method modifies the length of the array. If the new length is
|
|
|
|
// larger than the existing length of the array, then new elements will be
|
|
|
|
// constructed using elem_type's default constructor. Otherwise, this call
|
|
|
|
// removes elements from the array (see also RemoveElementsAt).
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aNewLen The desired length of this array.
|
|
|
|
// @return True if the operation succeeded; false otherwise.
|
2014-08-25 23:17:24 +04:00
|
|
|
// See also TruncateLength if the new length is guaranteed to be smaller than
|
2014-06-27 05:35:39 +04:00
|
|
|
// the old.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc = Alloc>
|
|
|
|
typename ActualAlloc::ResultType SetLength(size_type aNewLen)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2010-11-09 05:48:59 +03:00
|
|
|
size_type oldLen = Length();
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aNewLen > oldLen) {
|
2015-05-18 23:50:34 +03:00
|
|
|
return ActualAlloc::ConvertBoolToResultType(
|
|
|
|
InsertElementsAt<ActualAlloc>(oldLen, aNewLen - oldLen) != nullptr);
|
2010-02-20 18:26:12 +03:00
|
|
|
}
|
2013-06-18 14:00:37 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
TruncateLength(aNewLen);
|
2015-05-18 23:50:34 +03:00
|
|
|
return ActualAlloc::ConvertBoolToResultType(true);
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-05-18 23:50:34 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:34 +03:00
|
|
|
bool SetLength(size_type aNewLen, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return SetLength<FallibleAlloc>(aNewLen);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method modifies the length of the array, but may only be
|
|
|
|
// called when the new length is shorter than the old. It can
|
|
|
|
// therefore be called when elem_type has no default constructor,
|
|
|
|
// unlike SetLength. It removes elements from the array (see also
|
|
|
|
// RemoveElementsAt).
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aNewLen The desired length of this array.
|
|
|
|
void TruncateLength(size_type aNewLen)
|
|
|
|
{
|
2010-11-09 05:48:59 +03:00
|
|
|
size_type oldLen = Length();
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aNewLen <= oldLen,
|
|
|
|
"caller should use SetLength instead");
|
2014-06-27 05:35:39 +04:00
|
|
|
RemoveElementsAt(aNewLen, oldLen - aNewLen);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method ensures that the array has length at least the given
|
|
|
|
// length. If the current length is shorter than the given length,
|
|
|
|
// then new elements will be constructed using elem_type's default
|
|
|
|
// constructor.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aMinLen The desired minimum length of this array.
|
|
|
|
// @return True if the operation succeeded; false otherwise.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc = Alloc>
|
|
|
|
typename ActualAlloc::ResultType EnsureLengthAtLeast(size_type aMinLen)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2010-11-09 05:48:59 +03:00
|
|
|
size_type oldLen = Length();
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aMinLen > oldLen) {
|
2015-05-18 23:50:34 +03:00
|
|
|
return ActualAlloc::ConvertBoolToResultType(
|
|
|
|
!!InsertElementsAt<ActualAlloc>(oldLen, aMinLen - oldLen));
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
2015-05-18 23:50:34 +03:00
|
|
|
return ActualAlloc::ConvertBoolToResultType(true);
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2015-05-18 23:50:34 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:34 +03:00
|
|
|
bool EnsureLengthAtLeast(size_type aMinLen, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return EnsureLengthAtLeast<FallibleAlloc>(aMinLen);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method inserts elements into the array, constructing
|
|
|
|
// them using elem_type's default constructor.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aIndex the place to insert the new elements. This must be no
|
|
|
|
// greater than the current length of the array.
|
|
|
|
// @param aCount the number of elements to insert
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:34 +03:00
|
|
|
template<typename ActualAlloc = Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex, size_type aCount)
|
|
|
|
{
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!base_type::template InsertSlotsAt<ActualAlloc>(aIndex, aCount,
|
|
|
|
sizeof(elem_type),
|
|
|
|
MOZ_ALIGNOF(elem_type))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the extra array elements
|
2015-02-04 11:07:32 +03:00
|
|
|
elem_type* iter = Elements() + aIndex;
|
|
|
|
elem_type* iend = iter + aCount;
|
|
|
|
for (; iter != iend; ++iter) {
|
2010-11-09 05:48:59 +03:00
|
|
|
elem_traits::Construct(iter);
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return Elements() + aIndex;
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:34 +03:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex, size_type aCount,
|
|
|
|
const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementsAt<FallibleAlloc>(aIndex, aCount);
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// This method inserts elements into the array, constructing them
|
|
|
|
// elem_type's copy constructor (or whatever one-arg constructor
|
|
|
|
// happens to match the Item type).
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aIndex the place to insert the new elements. This must be no
|
|
|
|
// greater than the current length of the array.
|
|
|
|
// @param aCount the number of elements to insert.
|
|
|
|
// @param aItem the value to use when constructing the new elements.
|
2015-06-11 00:30:41 +03:00
|
|
|
protected:
|
2015-05-18 23:50:34 +03:00
|
|
|
template<class Item, typename ActualAlloc = Alloc>
|
2014-06-27 05:35:39 +04:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex, size_type aCount,
|
2016-07-08 23:59:05 +03:00
|
|
|
const Item& aItem);
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2015-06-11 00:30:41 +03:00
|
|
|
public:
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2015-05-18 23:50:34 +03:00
|
|
|
template<class Item>
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-05-18 23:50:34 +03:00
|
|
|
elem_type* InsertElementsAt(index_type aIndex, size_type aCount,
|
|
|
|
const Item& aItem, const mozilla::fallible_t&)
|
|
|
|
{
|
|
|
|
return InsertElementsAt<Item, FallibleAlloc>(aIndex, aCount, aItem);
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
// This method may be called to minimize the memory used by this array.
|
2014-06-27 05:35:39 +04:00
|
|
|
void Compact()
|
|
|
|
{
|
2015-11-02 08:53:27 +03:00
|
|
|
ShrinkCapacity(sizeof(elem_type), MOZ_ALIGNOF(elem_type));
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sorting
|
|
|
|
//
|
2013-06-18 14:00:37 +04:00
|
|
|
|
2012-03-22 03:52:24 +04:00
|
|
|
// This function is meant to be used with the NS_QuickSort function. It
|
|
|
|
// maps the callback API expected by NS_QuickSort to the Comparator API
|
2012-12-19 05:16:06 +04:00
|
|
|
// used by nsTArray_Impl. See nsTArray_Impl::Sort.
|
2012-03-22 03:52:24 +04:00
|
|
|
template<class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
static int Compare(const void* aE1, const void* aE2, void* aData)
|
|
|
|
{
|
|
|
|
const Comparator* c = reinterpret_cast<const Comparator*>(aData);
|
|
|
|
const elem_type* a = static_cast<const elem_type*>(aE1);
|
|
|
|
const elem_type* b = static_cast<const elem_type*>(aE2);
|
2012-03-22 03:52:24 +04:00
|
|
|
return c->LessThan(*a, *b) ? -1 : (c->Equals(*a, *b) ? 0 : 1);
|
|
|
|
}
|
2010-11-09 05:48:59 +03:00
|
|
|
|
|
|
|
// This method sorts the elements of the array. It uses the LessThan
|
|
|
|
// method defined on the given Comparator object to collate elements.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aComp The Comparator used to collate elements.
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Comparator>
|
2014-06-27 05:35:39 +04:00
|
|
|
void Sort(const Comparator& aComp)
|
|
|
|
{
|
2010-11-09 05:48:59 +03:00
|
|
|
NS_QuickSort(Elements(), Length(), sizeof(elem_type),
|
2014-06-27 05:35:39 +04:00
|
|
|
Compare<Comparator>, const_cast<Comparator*>(&aComp));
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A variation on the Sort method defined above that assumes that
|
|
|
|
// 'operator<' is defined for elem_type.
|
2014-06-27 05:35:39 +04:00
|
|
|
void Sort() { Sort(nsDefaultComparator<elem_type, elem_type>()); }
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2016-12-30 10:23:26 +03:00
|
|
|
// This method reverses the array in place.
|
|
|
|
void Reverse()
|
|
|
|
{
|
|
|
|
elem_type* elements = Elements();
|
|
|
|
const size_type len = Length();
|
|
|
|
for (index_type i = 0, iend = len / 2; i < iend; ++i) {
|
|
|
|
mozilla::Swap(elements[i], elements[len - i - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
protected:
|
|
|
|
using base_type::Hdr;
|
|
|
|
using base_type::ShrinkCapacity;
|
|
|
|
|
|
|
|
// This method invokes elem_type's destructor on a range of elements.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aStart The index of the first element to destroy.
|
|
|
|
// @param aCount The number of elements to destroy.
|
|
|
|
void DestructRange(index_type aStart, size_type aCount)
|
|
|
|
{
|
2015-02-04 11:07:32 +03:00
|
|
|
elem_type* iter = Elements() + aStart;
|
|
|
|
elem_type *iend = iter + aCount;
|
|
|
|
for (; iter != iend; ++iter) {
|
2010-11-09 05:48:59 +03:00
|
|
|
elem_traits::Destruct(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method invokes elem_type's copy-constructor on a range of elements.
|
2014-06-27 05:35:39 +04:00
|
|
|
// @param aStart The index of the first element to construct.
|
|
|
|
// @param aCount The number of elements to construct.
|
|
|
|
// @param aValues The array of elements to copy.
|
2010-11-09 05:48:59 +03:00
|
|
|
template<class Item>
|
2014-06-27 05:35:39 +04:00
|
|
|
void AssignRange(index_type aStart, size_type aCount, const Item* aValues)
|
|
|
|
{
|
2013-02-09 10:59:54 +04:00
|
|
|
AssignRangeAlgorithm<mozilla::IsPod<Item>::value,
|
|
|
|
mozilla::IsSame<Item, elem_type>::value>
|
2014-06-27 05:35:39 +04:00
|
|
|
::implementation(Elements(), aStart, aCount, aValues);
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
};
|
2010-05-05 21:21:23 +04:00
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
template<typename E, class Alloc>
|
|
|
|
template<class Item, typename ActualAlloc>
|
|
|
|
auto
|
|
|
|
nsTArray_Impl<E, Alloc>::ReplaceElementsAt(index_type aStart, size_type aCount,
|
2016-07-20 18:29:36 +03:00
|
|
|
const Item* aArray, size_type aArrayLen) -> elem_type*
|
2016-07-08 23:59:05 +03:00
|
|
|
{
|
2017-03-18 02:40:53 +03:00
|
|
|
if (MOZ_UNLIKELY(aStart > Length())) {
|
|
|
|
InvalidArrayIndex_CRASH(aStart, Length());
|
|
|
|
}
|
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
// Adjust memory allocation up-front to catch errors.
|
|
|
|
if (!ActualAlloc::Successful(this->template EnsureCapacity<ActualAlloc>(
|
|
|
|
Length() + aArrayLen - aCount, sizeof(elem_type)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
DestructRange(aStart, aCount);
|
|
|
|
this->template ShiftData<ActualAlloc>(aStart, aCount, aArrayLen,
|
|
|
|
sizeof(elem_type),
|
|
|
|
MOZ_ALIGNOF(elem_type));
|
|
|
|
AssignRange(aStart, aArrayLen, aArray);
|
|
|
|
return Elements() + aStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename E, class Alloc>
|
|
|
|
void
|
|
|
|
nsTArray_Impl<E, Alloc>::RemoveElementsAt(index_type aStart, size_type aCount)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCount == 0 || aStart < Length(), "Invalid aStart index");
|
|
|
|
MOZ_ASSERT(aStart + aCount <= Length(), "Invalid length");
|
|
|
|
// Check that the previous assert didn't overflow
|
|
|
|
MOZ_ASSERT(aStart <= aStart + aCount, "Start index plus length overflows");
|
|
|
|
DestructRange(aStart, aCount);
|
|
|
|
this->template ShiftData<InfallibleAlloc>(aStart, aCount, 0,
|
|
|
|
sizeof(elem_type),
|
|
|
|
MOZ_ALIGNOF(elem_type));
|
|
|
|
}
|
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
template<typename E, class Alloc>
|
2016-08-17 10:44:43 +03:00
|
|
|
template<typename Predicate>
|
2016-07-28 05:00:06 +03:00
|
|
|
void
|
2016-08-17 10:44:43 +03:00
|
|
|
nsTArray_Impl<E, Alloc>::RemoveElementsBy(Predicate aPredicate)
|
2016-07-28 05:00:06 +03:00
|
|
|
{
|
|
|
|
if (base_type::mHdr == EmptyHdr()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
index_type j = 0;
|
|
|
|
index_type len = Length();
|
|
|
|
for (index_type i = 0; i < len; ++i) {
|
|
|
|
if (aPredicate(Elements()[i])) {
|
|
|
|
elem_traits::Destruct(Elements() + i);
|
|
|
|
} else {
|
|
|
|
if (j < i) {
|
|
|
|
copy_type::MoveNonOverlappingRegion(Elements() + j, Elements() + i,
|
|
|
|
1, sizeof(elem_type));
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
base_type::mHdr->mLength = j;
|
|
|
|
}
|
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
template<typename E, class Alloc>
|
|
|
|
template<class Item, typename ActualAlloc>
|
|
|
|
auto
|
|
|
|
nsTArray_Impl<E, Alloc>::InsertElementsAt(index_type aIndex, size_type aCount,
|
|
|
|
const Item& aItem) -> elem_type*
|
|
|
|
{
|
|
|
|
if (!base_type::template InsertSlotsAt<ActualAlloc>(aIndex, aCount,
|
|
|
|
sizeof(elem_type),
|
|
|
|
MOZ_ALIGNOF(elem_type))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the extra array elements
|
|
|
|
elem_type* iter = Elements() + aIndex;
|
|
|
|
elem_type* iend = iter + aCount;
|
|
|
|
for (; iter != iend; ++iter) {
|
|
|
|
elem_traits::Construct(iter, aItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Elements() + aIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename E, class Alloc>
|
|
|
|
template<typename ActualAlloc>
|
|
|
|
auto
|
|
|
|
nsTArray_Impl<E, Alloc>::InsertElementAt(index_type aIndex) -> elem_type*
|
|
|
|
{
|
2017-03-18 02:40:53 +03:00
|
|
|
if (MOZ_UNLIKELY(aIndex > Length())) {
|
|
|
|
InvalidArrayIndex_CRASH(aIndex, Length());
|
|
|
|
}
|
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
if (!ActualAlloc::Successful(this->template EnsureCapacity<ActualAlloc>(
|
|
|
|
Length() + 1, sizeof(elem_type)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
this->template ShiftData<ActualAlloc>(aIndex, 0, 1, sizeof(elem_type),
|
|
|
|
MOZ_ALIGNOF(elem_type));
|
|
|
|
elem_type* elem = Elements() + aIndex;
|
|
|
|
elem_traits::Construct(elem);
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename E, class Alloc>
|
|
|
|
template<class Item, typename ActualAlloc>
|
|
|
|
auto
|
|
|
|
nsTArray_Impl<E, Alloc>::InsertElementAt(index_type aIndex, Item&& aItem) -> elem_type*
|
|
|
|
{
|
2017-03-18 02:40:53 +03:00
|
|
|
if (MOZ_UNLIKELY(aIndex > Length())) {
|
|
|
|
InvalidArrayIndex_CRASH(aIndex, Length());
|
|
|
|
}
|
|
|
|
|
2016-07-08 23:59:05 +03:00
|
|
|
if (!ActualAlloc::Successful(this->template EnsureCapacity<ActualAlloc>(
|
|
|
|
Length() + 1, sizeof(elem_type)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
this->template ShiftData<ActualAlloc>(aIndex, 0, 1, sizeof(elem_type),
|
|
|
|
MOZ_ALIGNOF(elem_type));
|
|
|
|
elem_type* elem = Elements() + aIndex;
|
|
|
|
elem_traits::Construct(elem, mozilla::Forward<Item>(aItem));
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename E, class Alloc>
|
|
|
|
template<class Item, typename ActualAlloc>
|
|
|
|
auto
|
|
|
|
nsTArray_Impl<E, Alloc>::AppendElements(const Item* aArray, size_type aArrayLen) -> elem_type*
|
|
|
|
{
|
|
|
|
if (!ActualAlloc::Successful(this->template EnsureCapacity<ActualAlloc>(
|
|
|
|
Length() + aArrayLen, sizeof(elem_type)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
index_type len = Length();
|
|
|
|
AssignRange(len, aArrayLen, aArray);
|
|
|
|
this->IncrementLength(aArrayLen);
|
|
|
|
return Elements() + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename E, class Alloc>
|
|
|
|
template<class Item, class Allocator, typename ActualAlloc>
|
|
|
|
auto
|
|
|
|
nsTArray_Impl<E, Alloc>::AppendElements(nsTArray_Impl<Item, Allocator>&& aArray) -> elem_type*
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(&aArray != this, "argument must be different aArray");
|
|
|
|
if (Length() == 0) {
|
|
|
|
SwapElements<ActualAlloc>(aArray);
|
|
|
|
return Elements();
|
|
|
|
}
|
|
|
|
|
|
|
|
index_type len = Length();
|
|
|
|
index_type otherLen = aArray.Length();
|
|
|
|
if (!Alloc::Successful(this->template EnsureCapacity<Alloc>(
|
|
|
|
len + otherLen, sizeof(elem_type)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-08 23:59:05 +03:00
|
|
|
copy_type::MoveNonOverlappingRegion(Elements() + len, aArray.Elements(), otherLen,
|
2016-07-08 23:59:05 +03:00
|
|
|
sizeof(elem_type));
|
2016-07-08 23:59:05 +03:00
|
|
|
this->IncrementLength(otherLen);
|
|
|
|
aArray.template ShiftData<Alloc>(0, otherLen, 0, sizeof(elem_type),
|
|
|
|
MOZ_ALIGNOF(elem_type));
|
|
|
|
return Elements() + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename E, class Alloc>
|
|
|
|
template<class Item, typename ActualAlloc>
|
|
|
|
auto
|
|
|
|
nsTArray_Impl<E, Alloc>::AppendElement(Item&& aItem) -> elem_type*
|
|
|
|
{
|
|
|
|
if (!ActualAlloc::Successful(this->template EnsureCapacity<ActualAlloc>(
|
|
|
|
Length() + 1, sizeof(elem_type)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
elem_type* elem = Elements() + Length();
|
|
|
|
elem_traits::Construct(elem, mozilla::Forward<Item>(aItem));
|
2017-08-03 02:21:23 +03:00
|
|
|
this->mHdr->mLength += 1;
|
2016-07-08 23:59:05 +03:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename E, typename Alloc>
|
2012-11-15 11:32:39 +04:00
|
|
|
inline void
|
2012-12-19 05:16:06 +04:00
|
|
|
ImplCycleCollectionUnlink(nsTArray_Impl<E, Alloc>& aField)
|
2012-11-15 11:32:39 +04:00
|
|
|
{
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename E, typename Alloc>
|
2012-11-15 11:32:39 +04:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
2012-12-19 05:16:06 +04:00
|
|
|
nsTArray_Impl<E, Alloc>& aField,
|
2012-11-15 11:32:39 +04:00
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
aFlags |= CycleCollectionEdgeNameArrayFlag;
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t length = aField.Length();
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2012-11-15 11:32:39 +04:00
|
|
|
ImplCycleCollectionTraverse(aCallback, aField[i], aName, aFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
2012-12-19 05:16:06 +04:00
|
|
|
// nsTArray is an infallible vector class. See the comment at the top of this
|
|
|
|
// file for more details.
|
2010-11-09 05:48:59 +03:00
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E>
|
2012-12-19 05:16:06 +04:00
|
|
|
class nsTArray : public nsTArray_Impl<E, nsTArrayInfallibleAllocator>
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
|
|
|
public:
|
2012-12-19 05:16:06 +04:00
|
|
|
typedef nsTArray_Impl<E, nsTArrayInfallibleAllocator> base_type;
|
|
|
|
typedef nsTArray<E> self_type;
|
|
|
|
typedef typename base_type::size_type size_type;
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
nsTArray() {}
|
2014-06-27 05:35:39 +04:00
|
|
|
explicit nsTArray(size_type aCapacity) : base_type(aCapacity) {}
|
|
|
|
explicit nsTArray(const nsTArray& aOther) : base_type(aOther) {}
|
2015-04-21 01:42:56 +03:00
|
|
|
MOZ_IMPLICIT nsTArray(nsTArray&& aOther) : base_type(mozilla::Move(aOther)) {}
|
2016-01-07 18:53:51 +03:00
|
|
|
MOZ_IMPLICIT nsTArray(std::initializer_list<E> aIL) : base_type(aIL) {}
|
2012-11-15 11:32:39 +04:00
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
template<class Allocator>
|
2014-06-27 05:35:39 +04:00
|
|
|
explicit nsTArray(const nsTArray_Impl<E, Allocator>& aOther)
|
|
|
|
: base_type(aOther)
|
|
|
|
{
|
|
|
|
}
|
2014-03-15 21:55:00 +04:00
|
|
|
template<class Allocator>
|
2014-09-02 16:20:14 +04:00
|
|
|
MOZ_IMPLICIT nsTArray(nsTArray_Impl<E, Allocator>&& aOther)
|
2014-03-15 21:55:00 +04:00
|
|
|
: base_type(mozilla::Move(aOther))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
self_type& operator=(const self_type& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<class Allocator>
|
|
|
|
self_type& operator=(const nsTArray_Impl<E, Allocator>& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
self_type& operator=(self_type&& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(mozilla::Move(aOther));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<class Allocator>
|
|
|
|
self_type& operator=(nsTArray_Impl<E, Allocator>&& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(mozilla::Move(aOther));
|
|
|
|
return *this;
|
|
|
|
}
|
2015-06-11 00:30:41 +03:00
|
|
|
|
|
|
|
using base_type::AppendElement;
|
|
|
|
using base_type::AppendElements;
|
|
|
|
using base_type::EnsureLengthAtLeast;
|
|
|
|
using base_type::InsertElementAt;
|
|
|
|
using base_type::InsertElementsAt;
|
|
|
|
using base_type::InsertElementSorted;
|
|
|
|
using base_type::ReplaceElementsAt;
|
|
|
|
using base_type::SetCapacity;
|
|
|
|
using base_type::SetLength;
|
2012-12-19 05:16:06 +04:00
|
|
|
};
|
2012-11-15 11:32:39 +04:00
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
//
|
|
|
|
// FallibleTArray is a fallible vector class.
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class E>
|
2012-12-19 05:16:06 +04:00
|
|
|
class FallibleTArray : public nsTArray_Impl<E, nsTArrayFallibleAllocator>
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
|
|
|
public:
|
2012-12-19 05:16:06 +04:00
|
|
|
typedef nsTArray_Impl<E, nsTArrayFallibleAllocator> base_type;
|
|
|
|
typedef FallibleTArray<E> self_type;
|
|
|
|
typedef typename base_type::size_type size_type;
|
2012-11-15 11:32:39 +04:00
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
FallibleTArray() {}
|
2014-06-27 05:35:39 +04:00
|
|
|
explicit FallibleTArray(size_type aCapacity) : base_type(aCapacity) {}
|
|
|
|
explicit FallibleTArray(const FallibleTArray<E>& aOther) : base_type(aOther) {}
|
2015-09-03 17:51:42 +03:00
|
|
|
FallibleTArray(FallibleTArray<E>&& aOther)
|
2014-03-15 21:55:00 +04:00
|
|
|
: base_type(mozilla::Move(aOther))
|
|
|
|
{
|
|
|
|
}
|
2012-11-15 11:32:39 +04:00
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
template<class Allocator>
|
2014-06-27 05:35:39 +04:00
|
|
|
explicit FallibleTArray(const nsTArray_Impl<E, Allocator>& aOther)
|
|
|
|
: base_type(aOther)
|
|
|
|
{
|
|
|
|
}
|
2014-03-15 21:55:00 +04:00
|
|
|
template<class Allocator>
|
|
|
|
explicit FallibleTArray(nsTArray_Impl<E, Allocator>&& aOther)
|
|
|
|
: base_type(mozilla::Move(aOther))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
self_type& operator=(const self_type& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<class Allocator>
|
|
|
|
self_type& operator=(const nsTArray_Impl<E, Allocator>& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
self_type& operator=(self_type&& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(mozilla::Move(aOther));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<class Allocator>
|
|
|
|
self_type& operator=(nsTArray_Impl<E, Allocator>&& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(mozilla::Move(aOther));
|
|
|
|
return *this;
|
|
|
|
}
|
2012-12-19 05:16:06 +04:00
|
|
|
};
|
2005-11-23 22:44:16 +03:00
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
//
|
2016-02-02 18:36:30 +03:00
|
|
|
// AutoTArray<E, N> is like nsTArray<E>, but with N elements of inline storage.
|
|
|
|
// Storing more than N elements is fine, but it will cause a heap allocation.
|
2012-12-19 05:16:06 +04:00
|
|
|
//
|
2016-02-02 18:36:30 +03:00
|
|
|
template<class E, size_t N>
|
|
|
|
class MOZ_NON_MEMMOVABLE AutoTArray : public nsTArray<E>
|
2010-11-09 05:48:59 +03:00
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
static_assert(N != 0, "AutoTArray<E, 0> should be specialized");
|
2010-11-09 05:48:59 +03:00
|
|
|
public:
|
2016-02-02 18:36:30 +03:00
|
|
|
typedef AutoTArray<E, N> self_type;
|
|
|
|
typedef nsTArray<E> base_type;
|
2010-11-09 05:48:59 +03:00
|
|
|
typedef typename base_type::Header Header;
|
|
|
|
typedef typename base_type::elem_type elem_type;
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray()
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
Init();
|
2012-12-19 05:16:06 +04:00
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray(const self_type& aOther)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2011-09-16 09:16:14 +04:00
|
|
|
Init();
|
2014-02-14 01:07:46 +04:00
|
|
|
this->AppendElements(aOther);
|
2011-09-16 09:16:14 +04:00
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
explicit AutoTArray(const base_type& aOther)
|
2014-03-15 21:55:00 +04:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
this->AppendElements(aOther);
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
explicit AutoTArray(base_type&& aOther)
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
this->SwapElements(aOther);
|
|
|
|
}
|
|
|
|
|
2014-09-03 20:33:03 +04:00
|
|
|
template<typename Allocator>
|
2016-02-02 18:36:30 +03:00
|
|
|
explicit AutoTArray(nsTArray_Impl<elem_type, Allocator>&& aOther)
|
2014-03-15 21:55:00 +04:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
this->SwapElements(aOther);
|
|
|
|
}
|
|
|
|
|
2016-08-09 17:21:26 +03:00
|
|
|
MOZ_IMPLICIT AutoTArray(std::initializer_list<E> aIL)
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
this->AppendElements(aIL.begin(), aIL.size());
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
self_type& operator=(const self_type& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Allocator>
|
|
|
|
self_type& operator=(const nsTArray_Impl<elem_type, Allocator>& aOther)
|
|
|
|
{
|
|
|
|
base_type::operator=(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-16 09:16:14 +04:00
|
|
|
private:
|
2011-10-05 17:11:17 +04:00
|
|
|
// nsTArray_base casts itself as an nsAutoArrayBase in order to get a pointer
|
|
|
|
// to mAutoBuf.
|
2013-06-18 14:00:37 +04:00
|
|
|
template<class Allocator, class Copier>
|
2011-10-05 17:11:17 +04:00
|
|
|
friend class nsTArray_base;
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
void Init()
|
|
|
|
{
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(MOZ_ALIGNOF(elem_type) <= 8,
|
|
|
|
"can't handle alignments greater than 8, "
|
|
|
|
"see nsTArray_base::UsesAutoArrayBuffer()");
|
2012-06-13 17:37:24 +04:00
|
|
|
// Temporary work around for VS2012 RC compiler crash
|
|
|
|
Header** phdr = base_type::PtrToHdr();
|
|
|
|
*phdr = reinterpret_cast<Header*>(&mAutoBuf);
|
|
|
|
(*phdr)->mLength = 0;
|
|
|
|
(*phdr)->mCapacity = N;
|
|
|
|
(*phdr)->mIsAutoArray = 1;
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2012-03-04 01:16:13 +04:00
|
|
|
MOZ_ASSERT(base_type::GetAutoArrayBuffer(MOZ_ALIGNOF(elem_type)) ==
|
|
|
|
reinterpret_cast<Header*>(&mAutoBuf),
|
|
|
|
"GetAutoArrayBuffer needs to be fixed");
|
2010-11-09 05:48:59 +03:00
|
|
|
}
|
|
|
|
|
2011-10-05 17:11:17 +04:00
|
|
|
// Declare mAutoBuf aligned to the maximum of the header's alignment and
|
|
|
|
// elem_type's alignment. We need to use a union rather than
|
|
|
|
// MOZ_ALIGNED_DECL because GCC is picky about what goes into
|
|
|
|
// __attribute__((aligned(foo))).
|
2014-06-27 05:35:39 +04:00
|
|
|
union
|
|
|
|
{
|
2011-10-05 17:11:17 +04:00
|
|
|
char mAutoBuf[sizeof(nsTArrayHeader) + N * sizeof(elem_type)];
|
2012-10-02 04:38:21 +04:00
|
|
|
// Do the max operation inline to ensure that it is a compile-time constant.
|
2014-06-27 05:35:39 +04:00
|
|
|
mozilla::AlignedElem<(MOZ_ALIGNOF(Header) > MOZ_ALIGNOF(elem_type)) ?
|
|
|
|
MOZ_ALIGNOF(Header) : MOZ_ALIGNOF(elem_type)> mAlign;
|
2010-11-09 05:48:59 +03:00
|
|
|
};
|
|
|
|
};
|
2010-05-05 21:21:23 +04:00
|
|
|
|
2014-05-29 19:59:37 +04:00
|
|
|
//
|
2016-02-02 18:36:30 +03:00
|
|
|
// Specialization of AutoTArray<E, N> for the case where N == 0.
|
|
|
|
// AutoTArray<E, 0> behaves exactly like nsTArray<E>, but without this
|
|
|
|
// specialization, it stores a useless inline header.
|
2014-05-29 19:59:37 +04:00
|
|
|
//
|
2016-02-02 18:36:30 +03:00
|
|
|
// We do have many AutoTArray<E, 0> objects in memory: about 2,000 per tab as
|
|
|
|
// of May 2014. These are typically not explicitly AutoTArray<E, 0> but rather
|
|
|
|
// AutoTArray<E, N> for some value N depending on template parameters, in
|
|
|
|
// generic code.
|
2014-05-29 19:59:37 +04:00
|
|
|
//
|
|
|
|
// For that reason, we optimize this case with the below partial specialization,
|
2016-02-02 18:36:30 +03:00
|
|
|
// which ensures that AutoTArray<E, 0> is just like nsTArray<E>, without any
|
|
|
|
// inline header overhead.
|
2016-01-31 21:10:57 +03:00
|
|
|
//
|
2016-02-02 18:36:30 +03:00
|
|
|
template<class E>
|
|
|
|
class AutoTArray<E, 0> : public nsTArray<E>
|
2016-01-31 21:10:57 +03:00
|
|
|
{
|
2006-11-02 22:33:10 +03:00
|
|
|
};
|
|
|
|
|
2015-07-14 17:05:10 +03:00
|
|
|
template<class E, size_t N>
|
2016-02-02 18:36:30 +03:00
|
|
|
struct nsTArray_CopyChooser<AutoTArray<E, N>>
|
2015-07-14 17:05:10 +03:00
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
typedef nsTArray_CopyWithConstructors<AutoTArray<E, N>> Type;
|
2015-07-14 17:05:10 +03:00
|
|
|
};
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
// Span integration
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template<class ElementType, class TArrayAlloc>
|
|
|
|
Span<ElementType>
|
|
|
|
MakeSpan(nsTArray_Impl<ElementType, TArrayAlloc>& aTArray)
|
|
|
|
{
|
|
|
|
return aTArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ElementType, class TArrayAlloc>
|
|
|
|
Span<const ElementType>
|
|
|
|
MakeSpan(const nsTArray_Impl<ElementType, TArrayAlloc>& aTArray)
|
|
|
|
{
|
|
|
|
return aTArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// Assert that AutoTArray doesn't have any extra padding inside.
|
2012-12-19 05:16:06 +04:00
|
|
|
//
|
|
|
|
// It's important that the data stored in this auto array takes up a multiple of
|
2016-02-02 18:36:30 +03:00
|
|
|
// 8 bytes; e.g. AutoTArray<uint32_t, 1> wouldn't work. Since AutoTArray
|
2012-12-19 05:16:06 +04:00
|
|
|
// contains a pointer, its size must be a multiple of alignof(void*). (This is
|
|
|
|
// because any type may be placed into an array, and there's no padding between
|
|
|
|
// elements of an array.) The compiler pads the end of the structure to
|
|
|
|
// enforce this rule.
|
|
|
|
//
|
2016-02-02 18:36:30 +03:00
|
|
|
// If we used AutoTArray<uint32_t, 1> below, this assertion would fail on a
|
2012-12-19 05:16:06 +04:00
|
|
|
// 64-bit system, where the compiler inserts 4 bytes of padding at the end of
|
|
|
|
// the auto array to make its size a multiple of alignof(void*) == 8 bytes.
|
2007-12-20 10:30:04 +03:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
static_assert(sizeof(AutoTArray<uint32_t, 2>) ==
|
2013-07-18 21:59:53 +04:00
|
|
|
sizeof(void*) + sizeof(nsTArrayHeader) + sizeof(uint32_t) * 2,
|
2016-02-02 18:36:30 +03:00
|
|
|
"AutoTArray shouldn't contain any extra padding, "
|
2013-07-18 21:59:53 +04:00
|
|
|
"see the comment");
|
2010-11-09 05:48:59 +03:00
|
|
|
|
2012-12-19 05:16:06 +04:00
|
|
|
// Definitions of nsTArray_Impl methods
|
2010-11-09 05:48:59 +03:00
|
|
|
#include "nsTArray-inl.h"
|
|
|
|
|
2005-11-23 22:44:16 +03:00
|
|
|
#endif // nsTArray_h__
|