1998-05-23 01:56:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-09-29 00:14:13 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-05-23 01:56:30 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-05-23 01:56:30 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-02-26 02:38:40 +03:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-07-04 09:42:46 +04:00
|
|
|
* Contributor(s):
|
|
|
|
* Scott Collins <scc@mozilla.org>: |do_QueryElementAt|
|
1999-12-01 02:36:32 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-05-23 01:56:30 +04:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
#include <string.h>
|
2004-02-26 02:38:40 +03:00
|
|
|
#include "prbit.h"
|
1999-02-23 11:10:24 +03:00
|
|
|
#include "nsSupportsArray.h"
|
|
|
|
#include "nsSupportsArrayEnumerator.h"
|
2002-11-14 21:16:31 +03:00
|
|
|
#include "nsAString.h"
|
2001-07-31 23:05:34 +04:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2001-08-07 10:23:40 +04:00
|
|
|
|
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
#define MAXSUPPORTS 20
|
|
|
|
|
|
|
|
class SupportsStats {
|
|
|
|
public:
|
|
|
|
SupportsStats();
|
|
|
|
~SupportsStats();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sizesUsed; // number of the elements of the arrays used
|
|
|
|
static int sizesAlloced[MAXSUPPORTS]; // sizes of the allocations. sorted
|
|
|
|
static int NumberOfSize[MAXSUPPORTS]; // number of this allocation size (1 per array)
|
|
|
|
static int AllocedOfSize[MAXSUPPORTS]; // number of this allocation size (each size for array used)
|
|
|
|
static int GrowInPlace[MAXSUPPORTS];
|
|
|
|
|
2004-02-26 02:38:40 +03:00
|
|
|
// these are per-allocation
|
2001-08-07 10:23:40 +04:00
|
|
|
static int MaxElements[3000];
|
|
|
|
|
|
|
|
// very evil
|
|
|
|
#define ADD_TO_STATS(x,size) do {int i; for (i = 0; i < sizesUsed; i++) \
|
|
|
|
{ \
|
|
|
|
if (sizesAlloced[i] == (int)(size)) \
|
|
|
|
{ ((x)[i])++; break; } \
|
|
|
|
} \
|
|
|
|
if (i >= sizesUsed && sizesUsed < MAXSUPPORTS) \
|
|
|
|
{ sizesAlloced[sizesUsed] = (size); \
|
|
|
|
((x)[sizesUsed++])++; break; \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
#define SUB_FROM_STATS(x,size) do {int i; for (i = 0; i < sizesUsed; i++) \
|
|
|
|
{ \
|
|
|
|
if (sizesAlloced[i] == (int)(size)) \
|
|
|
|
{ ((x)[i])--; break; } \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
|
|
|
|
SupportsStats::SupportsStats()
|
|
|
|
{
|
|
|
|
sizesUsed = 1;
|
|
|
|
sizesAlloced[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SupportsStats::~SupportsStats()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizesUsed; i++)
|
|
|
|
{
|
|
|
|
printf("Size %d:\n",sizesAlloced[i]);
|
|
|
|
printf("\tNumber of SupportsArrays this size (max): %d\n",NumberOfSize[i]);
|
|
|
|
printf("\tNumber of allocations this size (total): %d\n",AllocedOfSize[i]);
|
|
|
|
printf("\tNumber of GrowsInPlace this size (total): %d\n",GrowInPlace[i]);
|
|
|
|
}
|
|
|
|
printf("Max Size of SupportsArray:\n");
|
|
|
|
for (i = 0; i < (int)(sizeof(MaxElements)/sizeof(MaxElements[0])); i++)
|
|
|
|
{
|
|
|
|
if (MaxElements[i])
|
|
|
|
printf("\t%d: %d\n",i,MaxElements[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just so constructor/destructor get called
|
|
|
|
SupportsStats gSupportsStats;
|
|
|
|
#endif
|
1998-05-23 01:56:30 +04:00
|
|
|
|
2001-10-23 07:50:24 +04:00
|
|
|
nsresult
|
2001-07-04 09:42:46 +04:00
|
|
|
nsQueryElementAt::operator()( const nsIID& aIID, void** aResult ) const
|
|
|
|
{
|
|
|
|
nsresult status = mCollection
|
|
|
|
? mCollection->QueryElementAt(mIndex, aIID, aResult)
|
|
|
|
: NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if ( mErrorPtr )
|
|
|
|
*mErrorPtr = status;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
static const PRInt32 kGrowArrayBy = 8;
|
|
|
|
static const PRInt32 kLinearThreshold = 16 * sizeof(nsISupports *);
|
1998-05-23 01:56:30 +04:00
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
nsSupportsArray::nsSupportsArray()
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
mArray = mAutoArray;
|
1998-05-23 01:56:30 +04:00
|
|
|
mArraySize = kAutoArraySize;
|
|
|
|
mCount = 0;
|
2001-08-07 10:23:40 +04:00
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
mMaxCount = 0;
|
|
|
|
mMaxSize = 0;
|
|
|
|
ADD_TO_STATS(NumberOfSize,kAutoArraySize*sizeof(mArray[0]));
|
|
|
|
MaxElements[0]++;
|
|
|
|
#endif
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
nsSupportsArray::~nsSupportsArray()
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
|
|
|
DeleteArray();
|
|
|
|
}
|
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
PRBool nsSupportsArray::GrowArrayBy(PRInt32 aGrowBy)
|
|
|
|
{
|
|
|
|
// We have to grow the array. Grow by kGrowArrayBy slots if we're smaller
|
|
|
|
// than kLinearThreshold bytes, or a power of two if we're larger.
|
|
|
|
// This is much more efficient with most memory allocators, especially
|
|
|
|
// if it's very large, or of the allocator is binned.
|
|
|
|
if (aGrowBy < kGrowArrayBy)
|
|
|
|
aGrowBy = kGrowArrayBy;
|
|
|
|
|
|
|
|
PRUint32 newCount = mArraySize + aGrowBy; // Minimum increase
|
|
|
|
PRUint32 newSize = sizeof(mArray[0]) * newCount;
|
|
|
|
|
|
|
|
if (newSize >= (PRUint32) kLinearThreshold)
|
|
|
|
{
|
|
|
|
// newCount includes enough space for at least kGrowArrayBy new slots.
|
2004-02-26 02:38:40 +03:00
|
|
|
// Select the next power-of-two size in bytes above that if newSize is
|
|
|
|
// not a power of two.
|
|
|
|
if (newSize & (newSize - 1))
|
|
|
|
newSize = PR_BIT(PR_CeilingLog2(newSize));
|
2001-08-07 10:23:40 +04:00
|
|
|
|
2004-02-26 02:38:40 +03:00
|
|
|
newCount = newSize / sizeof(mArray[0]);
|
2001-08-07 10:23:40 +04:00
|
|
|
}
|
|
|
|
// XXX This would be far more efficient in many allocators if we used
|
|
|
|
// XXX PR_Realloc(), etc
|
|
|
|
nsISupports** oldArray = mArray;
|
2004-02-26 02:38:40 +03:00
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
mArray = new nsISupports*[newCount];
|
|
|
|
if (!mArray) { // ran out of memory
|
|
|
|
mArray = oldArray;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
mArraySize = newCount;
|
|
|
|
|
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
if (oldArray == mArray) // can't happen without use of realloc
|
|
|
|
ADD_TO_STATS(GrowInPlace,mCount);
|
|
|
|
ADD_TO_STATS(AllocedOfSize,mArraySize*sizeof(mArray[0]));
|
|
|
|
if (mArraySize > mMaxSize)
|
|
|
|
{
|
|
|
|
ADD_TO_STATS(NumberOfSize,mArraySize*sizeof(mArray[0]));
|
|
|
|
if (oldArray != &(mAutoArray[0]))
|
|
|
|
SUB_FROM_STATS(NumberOfSize,mCount*sizeof(mArray[0]));
|
|
|
|
mMaxSize = mArraySize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (oldArray) { // need to move old data
|
|
|
|
if (0 < mCount) {
|
|
|
|
::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*));
|
|
|
|
}
|
|
|
|
if (oldArray != &(mAutoArray[0])) {
|
|
|
|
delete[] oldArray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_METHOD
|
|
|
|
nsSupportsArray::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2001-08-21 12:51:19 +04:00
|
|
|
nsCOMPtr<nsISupportsArray> it = new nsSupportsArray();
|
|
|
|
if (!it)
|
1999-05-26 05:38:36 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-08-21 12:51:19 +04:00
|
|
|
return it->QueryInterface(aIID, aResult);
|
1999-05-26 05:38:36 +04:00
|
|
|
}
|
|
|
|
|
2008-02-21 23:39:23 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsSupportsArray, nsISupportsArray, nsICollection, nsISerializable)
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::Read(nsIObjectInputStream *aStream)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
PRUint32 newArraySize;
|
|
|
|
rv = aStream->Read32(&newArraySize);
|
|
|
|
|
|
|
|
if (newArraySize <= kAutoArraySize) {
|
|
|
|
if (mArray != mAutoArray) {
|
2001-08-07 10:23:40 +04:00
|
|
|
delete[] mArray;
|
2001-07-31 23:05:34 +04:00
|
|
|
mArray = mAutoArray;
|
|
|
|
}
|
2004-02-26 02:38:40 +03:00
|
|
|
newArraySize = kAutoArraySize;
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (newArraySize <= mArraySize) {
|
|
|
|
// Keep non-default-size mArray, it's more than big enough.
|
|
|
|
newArraySize = mArraySize;
|
|
|
|
}
|
|
|
|
else {
|
2004-02-26 02:38:40 +03:00
|
|
|
nsISupports** array = new nsISupports*[newArraySize];
|
2001-07-31 23:05:34 +04:00
|
|
|
if (!array)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
if (mArray != mAutoArray)
|
2001-08-07 10:23:40 +04:00
|
|
|
delete[] mArray;
|
2001-07-31 23:05:34 +04:00
|
|
|
mArray = array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mArraySize = newArraySize;
|
|
|
|
|
|
|
|
rv = aStream->Read32(&mCount);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2004-02-26 02:38:40 +03:00
|
|
|
NS_ASSERTION(mCount <= mArraySize, "overlarge mCount!");
|
|
|
|
if (mCount > mArraySize)
|
|
|
|
mCount = mArraySize;
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
for (PRUint32 i = 0; i < mCount; i++) {
|
|
|
|
rv = aStream->ReadObject(PR_TRUE, &mArray[i]);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::Write(nsIObjectOutputStream *aStream)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = aStream->Write32(mArraySize);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = aStream->Write32(mCount);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mCount; i++) {
|
|
|
|
rv = aStream->WriteObject(mArray[i], PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-05-23 01:56:30 +04:00
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
void nsSupportsArray::DeleteArray(void)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
if (mArray != &(mAutoArray[0])) {
|
|
|
|
delete[] mArray;
|
2001-07-31 23:05:34 +04:00
|
|
|
mArray = mAutoArray;
|
1998-05-23 01:56:30 +04:00
|
|
|
mArraySize = kAutoArraySize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
1999-06-02 06:06:32 +04:00
|
|
|
nsSupportsArray::Equals(const nsISupportsArray* aOther)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
2001-07-18 09:50:04 +04:00
|
|
|
if (aOther) {
|
|
|
|
PRUint32 countOther;
|
2007-07-08 11:08:04 +04:00
|
|
|
nsISupportsArray* other = const_cast<nsISupportsArray*>(aOther);
|
2004-02-26 02:38:40 +03:00
|
|
|
nsresult rv = other->Count(&countOther);
|
2001-07-18 09:50:04 +04:00
|
|
|
if (NS_FAILED( rv ))
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
if (mCount == countOther) {
|
2004-02-26 02:38:40 +03:00
|
|
|
PRUint32 index = mCount;
|
|
|
|
nsCOMPtr<nsISupports> otherElem;
|
|
|
|
while (index--) {
|
|
|
|
if (NS_FAILED(other->GetElementAt(index, getter_AddRefs(otherElem))))
|
2001-07-18 09:50:04 +04:00
|
|
|
return PR_FALSE;
|
2004-02-26 02:38:40 +03:00
|
|
|
if (mArray[index] != otherElem)
|
1998-05-23 01:56:30 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(nsISupports*)
|
1999-06-02 06:06:32 +04:00
|
|
|
nsSupportsArray::ElementAt(PRUint32 aIndex)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-07-17 07:28:41 +04:00
|
|
|
if (aIndex < mCount) {
|
1998-05-23 01:56:30 +04:00
|
|
|
nsISupports* element = mArray[aIndex];
|
2001-08-21 12:51:19 +04:00
|
|
|
NS_IF_ADDREF(element);
|
1998-05-23 01:56:30 +04:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRInt32)
|
1999-06-02 06:06:32 +04:00
|
|
|
nsSupportsArray::IndexOf(const nsISupports* aPossibleElement)
|
|
|
|
{
|
|
|
|
return IndexOfStartingAt(aPossibleElement, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(PRInt32)
|
|
|
|
nsSupportsArray::IndexOfStartingAt(const nsISupports* aPossibleElement,
|
|
|
|
PRUint32 aStartIndex)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-07-17 07:28:41 +04:00
|
|
|
if (aStartIndex < mCount) {
|
1998-05-23 01:56:30 +04:00
|
|
|
const nsISupports** start = (const nsISupports**)mArray; // work around goofy compiler behavior
|
|
|
|
const nsISupports** ep = (start + aStartIndex);
|
|
|
|
const nsISupports** end = (start + mCount);
|
|
|
|
while (ep < end) {
|
|
|
|
if (aPossibleElement == *ep) {
|
|
|
|
return (ep - start);
|
|
|
|
}
|
|
|
|
ep++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRInt32)
|
1999-06-02 06:06:32 +04:00
|
|
|
nsSupportsArray::LastIndexOf(const nsISupports* aPossibleElement)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
|
|
|
if (0 < mCount) {
|
|
|
|
const nsISupports** start = (const nsISupports**)mArray; // work around goofy compiler behavior
|
|
|
|
const nsISupports** ep = (start + mCount);
|
|
|
|
while (start <= --ep) {
|
|
|
|
if (aPossibleElement == *ep) {
|
|
|
|
return (ep - start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
nsSupportsArray::InsertElementAt(nsISupports* aElement, PRUint32 aIndex)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-07-17 07:28:41 +04:00
|
|
|
if (aIndex <= mCount) {
|
2001-08-07 10:23:40 +04:00
|
|
|
if (mArraySize < (mCount + 1)) {
|
|
|
|
// need to grow the array
|
|
|
|
if (!GrowArrayBy(1))
|
1998-05-23 01:56:30 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2001-08-07 10:23:40 +04:00
|
|
|
|
|
|
|
// Could be slightly more efficient if GrowArrayBy knew about the
|
|
|
|
// split, but the difference is trivial.
|
|
|
|
PRUint32 slide = (mCount - aIndex);
|
|
|
|
if (0 < slide) {
|
|
|
|
::memmove(mArray + aIndex + 1, mArray + aIndex, slide * sizeof(nsISupports*));
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mArray[aIndex] = aElement;
|
2001-08-21 12:51:19 +04:00
|
|
|
NS_IF_ADDREF(aElement);
|
1998-05-23 01:56:30 +04:00
|
|
|
mCount++;
|
2001-08-07 10:23:40 +04:00
|
|
|
|
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
if (mCount > mMaxCount &&
|
|
|
|
mCount < (PRInt32)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
|
|
|
{
|
|
|
|
MaxElements[mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mCount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
nsSupportsArray::InsertElementsAt(nsISupportsArray* aElements, PRUint32 aIndex)
|
|
|
|
{
|
|
|
|
if (!aElements) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
PRUint32 countElements;
|
|
|
|
if (NS_FAILED( aElements->Count( &countElements ) ))
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
if (aIndex <= mCount) {
|
|
|
|
if (mArraySize < (mCount + countElements)) {
|
|
|
|
// need to grow the array
|
|
|
|
if (!GrowArrayBy(countElements))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Could be slightly more efficient if GrowArrayBy knew about the
|
|
|
|
// split, but the difference is trivial.
|
|
|
|
PRUint32 slide = (mCount - aIndex);
|
|
|
|
if (0 < slide) {
|
|
|
|
::memmove(mArray + aIndex + countElements, mArray + aIndex,
|
|
|
|
slide * sizeof(nsISupports*));
|
|
|
|
}
|
|
|
|
|
2004-02-26 02:38:40 +03:00
|
|
|
for (PRUint32 i = 0; i < countElements; ++i, ++mCount) {
|
2001-08-07 10:23:40 +04:00
|
|
|
// use GetElementAt to copy and do AddRef for us
|
|
|
|
if (NS_FAILED( aElements->GetElementAt( i, mArray + aIndex + i) ))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
if (mCount > mMaxCount &&
|
|
|
|
mCount < (PRInt32)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
|
|
|
{
|
|
|
|
MaxElements[mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mCount;
|
|
|
|
}
|
|
|
|
#endif
|
1998-05-23 01:56:30 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
nsSupportsArray::ReplaceElementAt(nsISupports* aElement, PRUint32 aIndex)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-07-17 07:28:41 +04:00
|
|
|
if (aIndex < mCount) {
|
2001-08-21 12:51:19 +04:00
|
|
|
NS_IF_ADDREF(aElement); // addref first in case it's the same object!
|
|
|
|
NS_IF_RELEASE(mArray[aIndex]);
|
1998-05-23 01:56:30 +04:00
|
|
|
mArray[aIndex] = aElement;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
2001-08-07 10:23:40 +04:00
|
|
|
nsSupportsArray::RemoveElementsAt(PRUint32 aIndex, PRUint32 aCount)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
2004-02-26 02:38:40 +03:00
|
|
|
if (aIndex + aCount <= mCount) {
|
2001-08-07 10:23:40 +04:00
|
|
|
for (PRUint32 i = 0; i < aCount; i++)
|
2001-08-21 12:51:19 +04:00
|
|
|
NS_IF_RELEASE(mArray[aIndex+i]);
|
2001-08-07 10:23:40 +04:00
|
|
|
mCount -= aCount;
|
1998-05-23 01:56:30 +04:00
|
|
|
PRInt32 slide = (mCount - aIndex);
|
|
|
|
if (0 < slide) {
|
2001-08-07 10:23:40 +04:00
|
|
|
::memmove(mArray + aIndex, mArray + aIndex + aCount,
|
1998-05-23 01:56:30 +04:00
|
|
|
slide * sizeof(nsISupports*));
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
nsSupportsArray::RemoveElement(const nsISupports* aElement, PRUint32 aStartIndex)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
2001-08-07 10:23:40 +04:00
|
|
|
PRInt32 theIndex = IndexOfStartingAt(aElement,aStartIndex);
|
|
|
|
if (theIndex >= 0)
|
|
|
|
return RemoveElementAt(theIndex);
|
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
nsSupportsArray::RemoveLastElement(const nsISupports* aElement)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
2001-08-07 10:23:40 +04:00
|
|
|
PRInt32 theIndex = LastIndexOf(aElement);
|
|
|
|
if (theIndex >= 0)
|
|
|
|
return RemoveElementAt(theIndex);
|
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
2001-08-07 10:23:40 +04:00
|
|
|
nsSupportsArray::MoveElement(PRInt32 aFrom, PRInt32 aTo)
|
1998-11-03 23:14:08 +03:00
|
|
|
{
|
2001-08-07 10:23:40 +04:00
|
|
|
nsISupports *tempElement;
|
1998-11-03 23:14:08 +03:00
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
if (aTo == aFrom)
|
|
|
|
return PR_TRUE;
|
2001-07-18 09:50:04 +04:00
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
if (aTo < 0 || aFrom < 0 ||
|
|
|
|
(PRUint32) aTo >= mCount || (PRUint32) aFrom >= mCount)
|
|
|
|
{
|
|
|
|
// can't extend the array when moving an element. Also catches mImpl = null
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
tempElement = mArray[aFrom];
|
1998-11-03 23:14:08 +03:00
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
if (aTo < aFrom)
|
|
|
|
{
|
|
|
|
// Moving one element closer to the head; the elements inbetween move down
|
|
|
|
::memmove(mArray + aTo + 1, mArray + aTo,
|
|
|
|
(aFrom-aTo) * sizeof(mArray[0]));
|
|
|
|
mArray[aTo] = tempElement;
|
1998-11-03 23:14:08 +03:00
|
|
|
}
|
2001-08-07 10:23:40 +04:00
|
|
|
else // already handled aFrom == aTo
|
|
|
|
{
|
|
|
|
// Moving one element closer to the tail; the elements inbetween move up
|
|
|
|
::memmove(mArray + aFrom, mArray + aFrom + 1,
|
|
|
|
(aTo-aFrom) * sizeof(mArray[0]));
|
|
|
|
mArray[aTo] = tempElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
1998-11-03 23:14:08 +03:00
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::Clear(void)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-02-24 02:31:49 +03:00
|
|
|
if (0 < mCount) {
|
|
|
|
do {
|
|
|
|
--mCount;
|
2001-08-21 12:51:19 +04:00
|
|
|
NS_IF_RELEASE(mArray[mCount]);
|
1999-02-24 02:31:49 +03:00
|
|
|
} while (0 != mCount);
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
1999-02-23 11:10:24 +03:00
|
|
|
return NS_OK;
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
|
1999-06-02 06:06:32 +04:00
|
|
|
NS_IMETHODIMP
|
1999-02-23 11:10:24 +03:00
|
|
|
nsSupportsArray::Compact(void)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
2001-08-07 10:23:40 +04:00
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
PRUint32 oldArraySize = mArraySize;
|
|
|
|
#endif
|
1998-05-23 01:56:30 +04:00
|
|
|
if ((mArraySize != mCount) && (kAutoArraySize < mArraySize)) {
|
|
|
|
nsISupports** oldArray = mArray;
|
|
|
|
if (mCount <= kAutoArraySize) {
|
2001-07-31 23:05:34 +04:00
|
|
|
mArray = mAutoArray;
|
1998-05-23 01:56:30 +04:00
|
|
|
mArraySize = kAutoArraySize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mArray = new nsISupports*[mCount];
|
2001-07-31 23:05:34 +04:00
|
|
|
if (!mArray) {
|
|
|
|
mArray = oldArray;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-05-23 01:56:30 +04:00
|
|
|
mArraySize = mCount;
|
|
|
|
}
|
2001-08-07 10:23:40 +04:00
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
if (oldArray == mArray &&
|
|
|
|
oldArray != &(mAutoArray[0])) // can't happen without use of realloc
|
|
|
|
ADD_TO_STATS(GrowInPlace,oldArraySize);
|
|
|
|
if (oldArray != &(mAutoArray[0]))
|
|
|
|
ADD_TO_STATS(AllocedOfSize,mArraySize*sizeof(mArray[0]));
|
|
|
|
#endif
|
1998-05-23 01:56:30 +04:00
|
|
|
::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*));
|
|
|
|
delete[] oldArray;
|
|
|
|
}
|
1999-06-02 06:06:32 +04:00
|
|
|
return NS_OK;
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
nsSupportsArray::SizeTo(PRInt32 aSize)
|
|
|
|
{
|
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
PRUint32 oldArraySize = mArraySize;
|
|
|
|
#endif
|
2004-02-26 02:38:40 +03:00
|
|
|
NS_ASSERTION(aSize >= 0, "negative aSize!");
|
|
|
|
|
2001-08-07 10:23:40 +04:00
|
|
|
// XXX for aSize < mCount we could resize to mCount
|
2004-02-26 02:38:40 +03:00
|
|
|
if (mArraySize == (PRUint32) aSize || (PRUint32) aSize < mCount)
|
|
|
|
return PR_TRUE; // nothing to do
|
2001-08-07 10:23:40 +04:00
|
|
|
|
|
|
|
// switch back to autoarray if possible
|
|
|
|
nsISupports** oldArray = mArray;
|
2004-02-26 02:38:40 +03:00
|
|
|
if ((PRUint32) aSize <= kAutoArraySize) {
|
2001-08-07 10:23:40 +04:00
|
|
|
mArray = mAutoArray;
|
|
|
|
mArraySize = kAutoArraySize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mArray = new nsISupports*[aSize];
|
|
|
|
if (!mArray) {
|
|
|
|
mArray = oldArray;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
mArraySize = aSize;
|
|
|
|
}
|
|
|
|
#if DEBUG_SUPPORTSARRAY
|
|
|
|
if (oldArray == mArray &&
|
|
|
|
oldArray != &(mAutoArray[0])) // can't happen without use of realloc
|
|
|
|
ADD_TO_STATS(GrowInPlace,oldArraySize);
|
|
|
|
if (oldArray != &(mAutoArray[0]))
|
|
|
|
ADD_TO_STATS(AllocedOfSize,mArraySize*sizeof(mArray[0]));
|
|
|
|
#endif
|
|
|
|
::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*));
|
|
|
|
if (oldArray != mAutoArray)
|
|
|
|
delete[] oldArray;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
1999-06-02 06:06:32 +04:00
|
|
|
nsSupportsArray::EnumerateForwards(nsISupportsArrayEnumFunc aFunc, void* aData)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-06-02 06:06:32 +04:00
|
|
|
PRInt32 aIndex = -1;
|
1998-05-23 01:56:30 +04:00
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
1999-06-02 06:06:32 +04:00
|
|
|
while (running && (++aIndex < (PRInt32)mCount)) {
|
|
|
|
running = (*aFunc)(mArray[aIndex], aData);
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP_(PRBool)
|
1999-06-02 06:06:32 +04:00
|
|
|
nsSupportsArray::EnumerateBackwards(nsISupportsArrayEnumFunc aFunc, void* aData)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-06-02 06:06:32 +04:00
|
|
|
PRUint32 aIndex = mCount;
|
1998-05-23 01:56:30 +04:00
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
1999-06-02 06:06:32 +04:00
|
|
|
while (running && (0 < aIndex--)) {
|
|
|
|
running = (*aFunc)(mArray[aIndex], aData);
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::Enumerate(nsIEnumerator* *result)
|
|
|
|
{
|
|
|
|
nsSupportsArrayEnumerator* e = new nsSupportsArrayEnumerator(this);
|
|
|
|
if (!e)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = e;
|
|
|
|
NS_ADDREF(e);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-05-23 01:56:30 +04:00
|
|
|
|
2000-06-09 11:51:13 +04:00
|
|
|
static PRBool
|
|
|
|
CopyElement(nsISupports* aElement, void *aData)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsISupportsArray* newArray = (nsISupportsArray*)aData;
|
|
|
|
rv = newArray->AppendElement(aElement);
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::Clone(nsISupportsArray* *result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsISupportsArray* newArray;
|
|
|
|
rv = NS_NewISupportsArray(&newArray);
|
|
|
|
PRBool ok = EnumerateForwards(CopyElement, newArray);
|
|
|
|
if (!ok) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = newArray;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
NS_COM nsresult
|
1999-02-23 11:10:24 +03:00
|
|
|
NS_NewISupportsArray(nsISupportsArray** aInstancePtrResult)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
1999-05-26 05:38:36 +04:00
|
|
|
nsresult rv;
|
1999-12-01 02:36:32 +03:00
|
|
|
rv = nsSupportsArray::Create(NULL, NS_GET_IID(nsISupportsArray),
|
1999-05-26 05:38:36 +04:00
|
|
|
(void**)aInstancePtrResult);
|
|
|
|
return rv;
|
1998-05-23 01:56:30 +04:00
|
|
|
}
|
|
|
|
|
2008-02-21 23:39:23 +03:00
|
|
|
class nsArrayEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports interface
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsISimpleEnumerator interface
|
|
|
|
NS_IMETHOD HasMoreElements(PRBool* aResult);
|
|
|
|
NS_IMETHOD GetNext(nsISupports** aResult);
|
|
|
|
|
|
|
|
// nsArrayEnumerator methods
|
|
|
|
nsArrayEnumerator(nsISupportsArray* aValueArray);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsArrayEnumerator(void);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsISupportsArray* mValueArray;
|
|
|
|
PRInt32 mIndex;
|
|
|
|
};
|
|
|
|
|
2006-08-08 22:18:50 +04:00
|
|
|
nsArrayEnumerator::nsArrayEnumerator(nsISupportsArray* aValueArray)
|
|
|
|
: mValueArray(aValueArray),
|
|
|
|
mIndex(0)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(mValueArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsArrayEnumerator::~nsArrayEnumerator(void)
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mValueArray);
|
|
|
|
}
|
|
|
|
|
2008-02-21 23:39:23 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(nsArrayEnumerator, nsISimpleEnumerator)
|
|
|
|
|
2006-08-08 22:18:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsArrayEnumerator::HasMoreElements(PRBool* aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (!mValueArray) {
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 cnt;
|
|
|
|
nsresult rv = mValueArray->Count(&cnt);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
*aResult = (mIndex < (PRInt32) cnt);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsArrayEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (!mValueArray) {
|
|
|
|
*aResult = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 cnt;
|
|
|
|
nsresult rv = mValueArray->Count(&cnt);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (mIndex >= (PRInt32) cnt)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
*aResult = mValueArray->ElementAt(mIndex++);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
|
|
|
|
nsISupportsArray* array)
|
|
|
|
{
|
|
|
|
nsArrayEnumerator* enumer = new nsArrayEnumerator(array);
|
|
|
|
if (enumer == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = enumer;
|
|
|
|
NS_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|