2005-11-02 19:04:54 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; c-file-offsets: ((substatement-open . 0)) -*- */
|
2005-11-02 19:04:51 +03:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2005-11-02 19:05:20 +03:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2005-11-02 19:05:20 +03: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/
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2005-11-02 19:04:51 +03: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-04-14 00:24:54 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2005-11-02 19:05:20 +03:00
|
|
|
* The Initial Developer of the Original Code is
|
2005-11-02 19:04:51 +03: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
|
|
|
*
|
2005-11-02 19:04:51 +03:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2005-11-02 19:05:20 +03: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"),
|
2005-11-02 19:04:51 +03: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
|
2005-11-02 19:05:20 +03:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2005-11-02 19:04:51 +03: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
|
2005-11-02 19:05:20 +03:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2005-11-02 19:04:51 +03:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2005-11-02 23:38:21 +03:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsVoidArray.h"
|
2005-11-02 19:04:54 +03:00
|
|
|
#include "nsQuickSort.h"
|
2005-11-02 19:04:33 +03:00
|
|
|
#include "prbit.h"
|
2005-11-02 23:38:21 +03:00
|
|
|
#include "nsISupportsImpl.h" // for nsTraceRefcnt
|
|
|
|
#include "nsCRTGlue.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2005-11-02 19:04:33 +03:00
|
|
|
/**
|
|
|
|
* Grow the array by at least this many elements at a time.
|
|
|
|
*/
|
|
|
|
static const PRInt32 kMinGrowArrayBy = 8;
|
2005-11-02 19:04:54 +03:00
|
|
|
static const PRInt32 kMaxGrowArrayBy = 1024;
|
2005-11-02 19:05:24 +03:00
|
|
|
static const PRInt32 kAutoClearCompactSizeFactor = 4;
|
2005-11-02 19:04:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the threshold (in bytes) of the mImpl struct, past which
|
|
|
|
* we'll force the array to grow geometrically
|
|
|
|
*/
|
2005-11-02 19:04:31 +03:00
|
|
|
static const PRInt32 kLinearThreshold = 24 * sizeof(void *);
|
1999-12-01 03:37:11 +03:00
|
|
|
|
2005-11-02 19:04:33 +03:00
|
|
|
/**
|
|
|
|
* Compute the number of bytes requires for the mImpl struct that will
|
|
|
|
* hold |n| elements.
|
|
|
|
*/
|
|
|
|
#define SIZEOF_IMPL(n_) (sizeof(Impl) + sizeof(void *) * ((n_) - 1))
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the number of elements that an mImpl struct of |n| bytes
|
|
|
|
* will hold.
|
|
|
|
*/
|
|
|
|
#define CAPACITYOF_IMPL(n_) ((((n_) - sizeof(Impl)) / sizeof(void *)) + 1)
|
2005-11-02 19:04:31 +03:00
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
#define MAXVOID 10
|
|
|
|
|
|
|
|
class VoidStats {
|
|
|
|
public:
|
|
|
|
VoidStats();
|
|
|
|
~VoidStats();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sizesUsed; // number of the elements of the arrays used
|
|
|
|
static int sizesAlloced[MAXVOID]; // sizes of the allocations. sorted
|
|
|
|
static int NumberOfSize[MAXVOID]; // number of this allocation size (1 per array)
|
|
|
|
static int AllocedOfSize[MAXVOID]; // number of this allocation size (each size for array used)
|
|
|
|
static int MaxAuto[MAXVOID]; // AutoArrays that maxed out at this size
|
|
|
|
static int GrowInPlace[MAXVOID]; // arrays this size that grew in-place via realloc
|
|
|
|
|
|
|
|
// these are per-allocation
|
|
|
|
static int MaxElements[2000]; // # of arrays that maxed out at each size.
|
|
|
|
|
|
|
|
// statistics macros
|
|
|
|
#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 < MAXVOID) \
|
|
|
|
{ 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)
|
|
|
|
|
|
|
|
|
|
|
|
VoidStats::VoidStats()
|
|
|
|
{
|
|
|
|
sizesUsed = 1;
|
|
|
|
sizesAlloced[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VoidStats::~VoidStats()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizesUsed; i++)
|
|
|
|
{
|
|
|
|
printf("Size %d:\n",sizesAlloced[i]);
|
|
|
|
printf("\tNumber of VoidArrays this size (max): %d\n",NumberOfSize[i]-MaxAuto[i]);
|
|
|
|
printf("\tNumber of AutoVoidArrays this size (max): %d\n",MaxAuto[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 VoidArray:\n");
|
|
|
|
for (i = 0; i < (int)(sizeof(MaxElements)/sizeof(MaxElements[0])); i++)
|
|
|
|
{
|
|
|
|
if (MaxElements[i])
|
|
|
|
printf("\t%d: %d\n",i,MaxElements[i]);
|
|
|
|
}
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
// Just so constructor/destructor's get called
|
|
|
|
VoidStats gVoidStats;
|
|
|
|
#endif
|
|
|
|
|
2006-08-03 17:34:20 +04:00
|
|
|
void
|
2005-11-02 19:05:24 +03:00
|
|
|
nsVoidArray::SetArray(Impl *newImpl, PRInt32 aSize, PRInt32 aCount,
|
|
|
|
PRBool aOwner, PRBool aHasAuto)
|
1999-12-01 03:37:11 +03:00
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
// old mImpl has been realloced and so we don't free/delete it
|
|
|
|
NS_PRECONDITION(newImpl, "can't set size");
|
|
|
|
mImpl = newImpl;
|
|
|
|
mImpl->mCount = aCount;
|
2007-07-08 11:08:04 +04:00
|
|
|
mImpl->mBits = static_cast<PRUint32>(aSize & kArraySizeMask) |
|
2005-11-02 19:05:24 +03:00
|
|
|
(aOwner ? kArrayOwnerMask : 0) |
|
|
|
|
(aHasAuto ? kArrayHasAutoBufferMask : 0);
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
// This does all allocation/reallocation of the array.
|
|
|
|
// It also will compact down to N - good for things that might grow a lot
|
|
|
|
// at times, but usually are smaller, like JS deferred GC releases.
|
|
|
|
PRBool nsVoidArray::SizeTo(PRInt32 aSize)
|
1999-12-01 03:37:11 +03:00
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
PRUint32 oldsize = GetArraySize();
|
2005-11-02 19:05:24 +03:00
|
|
|
PRBool isOwner = IsArrayOwner();
|
|
|
|
PRBool hasAuto = HasAutoBuffer();
|
2005-11-02 19:04:31 +03:00
|
|
|
|
|
|
|
if (aSize == (PRInt32) oldsize)
|
|
|
|
return PR_TRUE; // no change
|
|
|
|
|
|
|
|
if (aSize <= 0)
|
|
|
|
{
|
|
|
|
// free the array if allocated
|
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (isOwner)
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
free(reinterpret_cast<char *>(mImpl));
|
2005-11-02 19:05:24 +03:00
|
|
|
if (hasAuto) {
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<nsAutoVoidArray*>(this)->ResetToAutoBuffer();
|
2005-11-02 19:05:24 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mImpl = nsnull;
|
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mImpl->mCount = 0; // nsAutoVoidArray
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
if (mImpl && isOwner)
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
|
|
|
// We currently own an array impl. Resize it appropriately.
|
|
|
|
if (aSize < mImpl->mCount)
|
|
|
|
{
|
|
|
|
// XXX Note: we could also just resize to mCount
|
|
|
|
return PR_TRUE; // can't make it that small, ignore request
|
|
|
|
}
|
|
|
|
|
2005-11-02 23:38:21 +03:00
|
|
|
char* bytes = (char *) realloc(mImpl,SIZEOF_IMPL(aSize));
|
2007-07-08 11:08:04 +04:00
|
|
|
Impl* newImpl = reinterpret_cast<Impl*>(bytes);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (!newImpl)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl == newImpl)
|
|
|
|
ADD_TO_STATS(GrowInPlace,oldsize);
|
|
|
|
ADD_TO_STATS(AllocedOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (aSize > mMaxSize)
|
|
|
|
{
|
|
|
|
ADD_TO_STATS(NumberOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (oldsize)
|
|
|
|
SUB_FROM_STATS(NumberOfSize,oldsize);
|
|
|
|
mMaxSize = aSize;
|
|
|
|
if (mIsAuto)
|
|
|
|
{
|
|
|
|
ADD_TO_STATS(MaxAuto,SIZEOF_IMPL(aSize));
|
|
|
|
SUB_FROM_STATS(MaxAuto,oldsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-11-02 19:05:24 +03:00
|
|
|
SetArray(newImpl, aSize, newImpl->mCount, PR_TRUE, hasAuto);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-11-28 03:32:30 +03:00
|
|
|
if ((PRUint32) aSize < oldsize) {
|
2005-11-02 19:05:24 +03:00
|
|
|
// No point in allocating if it won't free the current Impl anyway.
|
2005-11-02 19:04:31 +03:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// just allocate an array
|
|
|
|
// allocate the exact size requested
|
2005-11-02 23:38:21 +03:00
|
|
|
char* bytes = (char *) malloc(SIZEOF_IMPL(aSize));
|
2007-07-08 11:08:04 +04:00
|
|
|
Impl* newImpl = reinterpret_cast<Impl*>(bytes);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (!newImpl)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
ADD_TO_STATS(AllocedOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (aSize > mMaxSize)
|
|
|
|
{
|
|
|
|
ADD_TO_STATS(NumberOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (oldsize && !mImpl)
|
|
|
|
SUB_FROM_STATS(NumberOfSize,oldsize);
|
|
|
|
mMaxSize = aSize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
ADD_TO_STATS(MaxAuto,SIZEOF_IMPL(aSize));
|
|
|
|
SUB_FROM_STATS(MaxAuto,0);
|
|
|
|
SUB_FROM_STATS(NumberOfSize,0);
|
|
|
|
mIsAuto = PR_TRUE;
|
|
|
|
#endif
|
|
|
|
// We must be growing an nsAutoVoidArray - copy since we didn't
|
|
|
|
// realloc.
|
2005-11-02 19:04:59 +03:00
|
|
|
memcpy(newImpl->mArray, mImpl->mArray,
|
2005-11-02 19:04:31 +03:00
|
|
|
mImpl->mCount * sizeof(mImpl->mArray[0]));
|
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
SetArray(newImpl, aSize, mImpl ? mImpl->mCount : 0, PR_TRUE, hasAuto);
|
2005-11-02 19:04:31 +03:00
|
|
|
// no memset; handled later in ReplaceElementAt if needed
|
|
|
|
return PR_TRUE;
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
PRBool nsVoidArray::GrowArrayBy(PRInt32 aGrowBy)
|
|
|
|
{
|
2005-11-02 19:04:33 +03:00
|
|
|
// We have to grow the array. Grow by kMinGrowArrayBy 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 < kMinGrowArrayBy)
|
|
|
|
aGrowBy = kMinGrowArrayBy;
|
|
|
|
|
|
|
|
PRUint32 newCapacity = GetArraySize() + aGrowBy; // Minimum increase
|
|
|
|
PRUint32 newSize = SIZEOF_IMPL(newCapacity);
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (newSize >= (PRUint32) kLinearThreshold)
|
|
|
|
{
|
2005-11-02 19:04:33 +03:00
|
|
|
// newCount includes enough space for at least kMinGrowArrayBy new
|
|
|
|
// slots. Select the next power-of-two size in bytes above or
|
|
|
|
// equal to that.
|
2005-11-02 19:04:54 +03:00
|
|
|
// Also, limit the increase in size to about a VM page or two.
|
|
|
|
if (GetArraySize() >= kMaxGrowArrayBy)
|
|
|
|
{
|
|
|
|
newCapacity = GetArraySize() + PR_MAX(kMaxGrowArrayBy,aGrowBy);
|
|
|
|
newSize = SIZEOF_IMPL(newCapacity);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-11-14 19:36:39 +03:00
|
|
|
PR_CEILING_LOG2(newSize, newSize);
|
|
|
|
newCapacity = CAPACITYOF_IMPL(PR_BIT(newSize));
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
}
|
|
|
|
// frees old mImpl IF this succeeds
|
2005-11-02 19:04:33 +03:00
|
|
|
if (!SizeTo(newCapacity))
|
2005-11-02 19:04:31 +03:00
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
nsVoidArray::nsVoidArray()
|
2005-11-02 19:04:22 +03:00
|
|
|
: mImpl(nsnull)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
MOZ_COUNT_CTOR(nsVoidArray);
|
2005-11-02 19:04:31 +03:00
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
mMaxCount = 0;
|
|
|
|
mMaxSize = 0;
|
|
|
|
mIsAuto = PR_FALSE;
|
|
|
|
ADD_TO_STATS(NumberOfSize,0);
|
|
|
|
MaxElements[0]++;
|
|
|
|
#endif
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-06-12 02:48:25 +04:00
|
|
|
nsVoidArray::nsVoidArray(PRInt32 aCount)
|
2005-11-02 19:04:22 +03:00
|
|
|
: mImpl(nsnull)
|
1998-06-12 02:48:25 +04:00
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
MOZ_COUNT_CTOR(nsVoidArray);
|
2005-11-02 19:04:31 +03:00
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
mMaxCount = 0;
|
|
|
|
mMaxSize = 0;
|
|
|
|
mIsAuto = PR_FALSE;
|
|
|
|
MaxElements[0]++;
|
|
|
|
#endif
|
|
|
|
SizeTo(aCount);
|
1998-06-12 02:48:25 +04:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
nsVoidArray& nsVoidArray::operator=(const nsVoidArray& other)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 otherCount = other.Count();
|
2005-11-02 19:04:31 +03:00
|
|
|
PRInt32 maxCount = GetArraySize();
|
|
|
|
if (otherCount)
|
|
|
|
{
|
|
|
|
if (otherCount > maxCount)
|
|
|
|
{
|
|
|
|
// frees old mImpl IF this succeeds
|
|
|
|
if (!GrowArrayBy(otherCount-maxCount))
|
|
|
|
return *this; // XXX The allocation failed - don't do anything
|
|
|
|
|
2005-11-02 19:04:59 +03:00
|
|
|
memcpy(mImpl->mArray, other.mImpl->mArray, otherCount * sizeof(mImpl->mArray[0]));
|
2005-11-02 19:04:22 +03:00
|
|
|
mImpl->mCount = otherCount;
|
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// the old array can hold the new array
|
2005-11-02 19:04:59 +03:00
|
|
|
memcpy(mImpl->mArray, other.mImpl->mArray, otherCount * sizeof(mImpl->mArray[0]));
|
2005-11-02 19:04:31 +03:00
|
|
|
mImpl->mCount = otherCount;
|
|
|
|
// if it shrank a lot, compact it anyways
|
|
|
|
if ((otherCount*2) < maxCount && maxCount > 100)
|
|
|
|
{
|
|
|
|
Compact(); // shrank by at least 50 entries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
|
|
|
mImpl->mCount < (PRInt32)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
else
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
// Why do we drop the buffer here when we don't in Clear()?
|
|
|
|
SizeTo(0);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsVoidArray::~nsVoidArray()
|
|
|
|
{
|
1999-12-01 03:37:11 +03:00
|
|
|
MOZ_COUNT_DTOR(nsVoidArray);
|
2005-11-02 19:04:22 +03:00
|
|
|
if (mImpl && IsArrayOwner())
|
2007-07-08 11:08:04 +04:00
|
|
|
free(reinterpret_cast<char*>(mImpl));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
1999-08-31 07:04:37 +04:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
PRInt32 nsVoidArray::IndexOf(void* aPossibleElement) const
|
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
2005-11-02 19:04:31 +03:00
|
|
|
while (ap < end)
|
|
|
|
{
|
|
|
|
if (*ap == aPossibleElement)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 oldCount = Count();
|
2005-11-02 19:04:54 +03:00
|
|
|
NS_ASSERTION(aIndex >= 0,"InsertElementAt(negative index)");
|
2005-11-02 19:04:31 +03:00
|
|
|
if (PRUint32(aIndex) > PRUint32(oldCount))
|
|
|
|
{
|
1998-04-14 00:24:54 +04:00
|
|
|
// An invalid index causes the insertion to fail
|
2005-11-02 19:04:31 +03:00
|
|
|
// Invalid indexes are ones that add more than one entry to the
|
|
|
|
// array (i.e., they can append).
|
1998-04-14 00:24:54 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (oldCount >= GetArraySize())
|
|
|
|
{
|
|
|
|
if (!GrowArrayBy(1))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
// else the array is already large enough
|
|
|
|
|
|
|
|
PRInt32 slide = oldCount - aIndex;
|
|
|
|
if (0 != slide)
|
|
|
|
{
|
|
|
|
// Slide data over to make room for the insertion
|
2005-11-02 19:05:03 +03:00
|
|
|
memmove(mImpl->mArray + aIndex + 1, mImpl->mArray + aIndex,
|
|
|
|
slide * sizeof(mImpl->mArray[0]));
|
2005-11-02 19:04:31 +03:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
mImpl->mArray[aIndex] = aElement;
|
|
|
|
mImpl->mCount++;
|
2005-11-02 19:04:22 +03:00
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
|
|
|
mImpl->mCount < (PRInt32)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
2005-11-02 19:04:22 +03:00
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
PRBool nsVoidArray::InsertElementsAt(const nsVoidArray& other, PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
PRInt32 oldCount = Count();
|
|
|
|
PRInt32 otherCount = other.Count();
|
2005-11-02 19:04:22 +03:00
|
|
|
|
2005-11-02 19:04:54 +03:00
|
|
|
NS_ASSERTION(aIndex >= 0,"InsertElementsAt(negative index)");
|
2005-11-02 19:04:31 +03:00
|
|
|
if (PRUint32(aIndex) > PRUint32(oldCount))
|
|
|
|
{
|
|
|
|
// An invalid index causes the insertion to fail
|
|
|
|
// Invalid indexes are ones that are more than one entry past the end of
|
|
|
|
// the array (i.e., they can append).
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:22 +03:00
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
|
|
|
|
if (oldCount + otherCount > GetArraySize())
|
|
|
|
{
|
|
|
|
if (!GrowArrayBy(otherCount))
|
|
|
|
return PR_FALSE;;
|
|
|
|
}
|
|
|
|
// else the array is already large enough
|
|
|
|
|
|
|
|
PRInt32 slide = oldCount - aIndex;
|
|
|
|
if (0 != slide)
|
|
|
|
{
|
|
|
|
// Slide data over to make room for the insertion
|
2005-11-02 19:05:03 +03:00
|
|
|
memmove(mImpl->mArray + aIndex + otherCount, mImpl->mArray + aIndex,
|
|
|
|
slide * sizeof(mImpl->mArray[0]));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
for (PRInt32 i = 0; i < otherCount; i++)
|
|
|
|
{
|
|
|
|
// copy all the elements (destroys aIndex)
|
|
|
|
mImpl->mArray[aIndex++] = other.mImpl->mArray[i];
|
|
|
|
mImpl->mCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
|
|
|
mImpl->mCount < (PRInt32)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::ReplaceElementAt(void* aElement, PRInt32 aIndex)
|
|
|
|
{
|
2005-11-02 19:04:54 +03:00
|
|
|
NS_ASSERTION(aIndex >= 0,"ReplaceElementAt(negative index)");
|
|
|
|
if (aIndex < 0)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
// Unlike InsertElementAt, ReplaceElementAt can implicitly add more
|
|
|
|
// than just the one element to the array.
|
|
|
|
if (PRUint32(aIndex) >= PRUint32(GetArraySize()))
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 oldCount = Count();
|
1998-07-02 04:28:56 +04:00
|
|
|
PRInt32 requestedCount = aIndex + 1;
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 growDelta = requestedCount - oldCount;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
// frees old mImpl IF this succeeds
|
|
|
|
if (!GrowArrayBy(growDelta))
|
|
|
|
return PR_FALSE;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
|
|
|
|
mImpl->mArray[aIndex] = aElement;
|
|
|
|
if (aIndex >= mImpl->mCount)
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
|
|
|
// Make sure that any entries implicitly added to the array by this
|
|
|
|
// ReplaceElementAt are cleared to 0. Some users of this assume that.
|
|
|
|
// This code means we don't have to memset when we allocate an array.
|
|
|
|
if (aIndex > mImpl->mCount) // note: not >=
|
|
|
|
{
|
|
|
|
// For example, if mCount is 2, and we do a ReplaceElementAt for
|
|
|
|
// element[5], then we need to set three entries ([2], [3], and [4])
|
|
|
|
// to 0.
|
2005-11-02 19:05:01 +03:00
|
|
|
memset(&mImpl->mArray[mImpl->mCount], 0,
|
|
|
|
(aIndex - mImpl->mCount) * sizeof(mImpl->mArray[0]));
|
2005-11-02 19:04:31 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:22 +03:00
|
|
|
mImpl->mCount = aIndex + 1;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
|
|
|
mImpl->mCount < (PRInt32)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// useful for doing LRU arrays
|
|
|
|
PRBool nsVoidArray::MoveElement(PRInt32 aFrom, PRInt32 aTo)
|
|
|
|
{
|
|
|
|
void *tempElement;
|
|
|
|
|
|
|
|
if (aTo == aFrom)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:54 +03:00
|
|
|
NS_ASSERTION(aTo >= 0 && aFrom >= 0,"MoveElement(negative index)");
|
|
|
|
if (aTo >= Count() || aFrom >= Count())
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
|
|
|
// can't extend the array when moving an element. Also catches mImpl = null
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
tempElement = mImpl->mArray[aFrom];
|
|
|
|
|
|
|
|
if (aTo < aFrom)
|
|
|
|
{
|
|
|
|
// Moving one element closer to the head; the elements inbetween move down
|
2005-11-02 19:05:03 +03:00
|
|
|
memmove(mImpl->mArray + aTo + 1, mImpl->mArray + aTo,
|
|
|
|
(aFrom-aTo) * sizeof(mImpl->mArray[0]));
|
2005-11-02 19:04:31 +03:00
|
|
|
mImpl->mArray[aTo] = tempElement;
|
|
|
|
}
|
|
|
|
else // already handled aFrom == aTo
|
|
|
|
{
|
|
|
|
// Moving one element closer to the tail; the elements inbetween move up
|
2005-11-02 19:05:03 +03:00
|
|
|
memmove(mImpl->mArray + aFrom, mImpl->mArray + aFrom + 1,
|
|
|
|
(aTo-aFrom) * sizeof(mImpl->mArray[0]));
|
2005-11-02 19:04:31 +03:00
|
|
|
mImpl->mArray[aTo] = tempElement;
|
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
PRBool nsVoidArray::RemoveElementsAt(PRInt32 aIndex, PRInt32 aCount)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 oldCount = Count();
|
2005-11-02 19:04:54 +03:00
|
|
|
NS_ASSERTION(aIndex >= 0,"RemoveElementsAt(negative index)");
|
2005-11-02 19:04:31 +03:00
|
|
|
if (PRUint32(aIndex) >= PRUint32(oldCount))
|
|
|
|
{
|
1998-04-14 00:24:54 +04:00
|
|
|
// An invalid index causes the replace to fail
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
// Limit to available entries starting at aIndex
|
|
|
|
if (aCount + aIndex > oldCount)
|
|
|
|
aCount = oldCount - aIndex;
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
// We don't need to move any elements if we're removing the
|
|
|
|
// last element in the array
|
2005-11-02 19:04:31 +03:00
|
|
|
if (aIndex < (oldCount - aCount))
|
|
|
|
{
|
2005-11-02 19:05:03 +03:00
|
|
|
memmove(mImpl->mArray + aIndex, mImpl->mArray + aIndex + aCount,
|
|
|
|
(oldCount - (aIndex + aCount)) * sizeof(mImpl->mArray[0]));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
mImpl->mCount -= aCount;
|
1998-04-14 00:24:54 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::RemoveElement(void* aElement)
|
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
PRInt32 theIndex = IndexOf(aElement);
|
|
|
|
if (theIndex != -1)
|
|
|
|
return RemoveElementAt(theIndex);
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Clear()
|
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
mImpl->mCount = 0;
|
2005-11-02 19:05:24 +03:00
|
|
|
// We don't have to free on Clear, but if we have a built-in buffer,
|
|
|
|
// it's worth considering.
|
|
|
|
if (HasAutoBuffer() && IsArrayOwner() &&
|
|
|
|
GetArraySize() > kAutoClearCompactSizeFactor * kAutoBufSize) {
|
|
|
|
SizeTo(0);
|
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Compact()
|
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
// XXX NOTE: this is quite inefficient in many cases if we're only
|
|
|
|
// compacting by a little, but some callers care more about memory use.
|
2005-11-02 19:05:24 +03:00
|
|
|
PRInt32 count = Count();
|
|
|
|
if (HasAutoBuffer() && count <= kAutoBufSize)
|
|
|
|
{
|
|
|
|
Impl* oldImpl = mImpl;
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<nsAutoVoidArray*>(this)->ResetToAutoBuffer();
|
2005-11-02 19:05:24 +03:00
|
|
|
memcpy(mImpl->mArray, oldImpl->mArray,
|
|
|
|
count * sizeof(mImpl->mArray[0]));
|
2007-07-08 11:08:04 +04:00
|
|
|
free(reinterpret_cast<char *>(oldImpl));
|
2005-11-02 19:05:24 +03:00
|
|
|
}
|
|
|
|
else if (GetArraySize() > count)
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
|
|
|
SizeTo(Count());
|
1998-06-06 01:38:36 +04:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
1998-05-21 00:31:58 +04:00
|
|
|
|
2005-11-02 19:04:30 +03:00
|
|
|
// Needed because we want to pass the pointer to the item in the array
|
|
|
|
// to the comparator function, not a pointer to the pointer in the array.
|
|
|
|
struct VoidArrayComparatorContext {
|
|
|
|
nsVoidArrayComparatorFunc mComparatorFunc;
|
|
|
|
void* mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
PR_STATIC_CALLBACK(int)
|
|
|
|
VoidArrayComparator(const void* aElement1, const void* aElement2, void* aData)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
VoidArrayComparatorContext* ctx = static_cast<VoidArrayComparatorContext*>(aData);
|
|
|
|
return (*ctx->mComparatorFunc)(*static_cast<void* const*>(aElement1),
|
|
|
|
*static_cast<void* const*>(aElement2),
|
2005-11-02 19:04:30 +03:00
|
|
|
ctx->mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Sort(nsVoidArrayComparatorFunc aFunc, void* aData)
|
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl && mImpl->mCount > 1)
|
|
|
|
{
|
2005-11-02 19:04:30 +03:00
|
|
|
VoidArrayComparatorContext ctx = {aFunc, aData};
|
2005-11-02 19:04:31 +03:00
|
|
|
NS_QuickSort(mImpl->mArray, mImpl->mCount, sizeof(mImpl->mArray[0]),
|
2005-11-02 19:04:30 +03:00
|
|
|
VoidArrayComparator, &ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-05-21 00:31:58 +04:00
|
|
|
PRBool nsVoidArray::EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRInt32 index = -1;
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
while (running && (++index < mImpl->mCount))
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
running = (*aFunc)(mImpl->mArray[index], aData);
|
|
|
|
}
|
1998-05-21 00:31:58 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsVoidArray::EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
2005-11-02 19:04:31 +03:00
|
|
|
while (running && (0 <= --index))
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
running = (*aFunc)(mImpl->mArray[index], aData);
|
|
|
|
}
|
1998-05-21 00:31:58 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
1999-12-01 03:37:11 +03:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// nsAutoVoidArray
|
|
|
|
|
|
|
|
nsAutoVoidArray::nsAutoVoidArray()
|
|
|
|
: nsVoidArray()
|
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
// Don't need to clear it. Some users just call ReplaceElementAt(),
|
|
|
|
// but we'll clear it at that time if needed to save CPU cycles.
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
mIsAuto = PR_TRUE;
|
|
|
|
ADD_TO_STATS(MaxAuto,0);
|
|
|
|
#endif
|
2005-11-02 19:05:24 +03:00
|
|
|
ResetToAutoBuffer();
|
1999-12-01 03:37:11 +03:00
|
|
|
}
|
|
|
|
|
1999-01-23 09:58:34 +03:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// nsStringArray
|
|
|
|
|
|
|
|
nsStringArray::nsStringArray(void)
|
|
|
|
: nsVoidArray()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
nsStringArray::nsStringArray(PRInt32 aCount)
|
|
|
|
: nsVoidArray(aCount)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-01-23 09:58:34 +03:00
|
|
|
nsStringArray::~nsStringArray(void)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStringArray&
|
|
|
|
nsStringArray::operator=(const nsStringArray& other)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
// Copy the pointers
|
|
|
|
nsVoidArray::operator=(other);
|
|
|
|
|
|
|
|
// Now copy the strings
|
2008-01-15 00:01:40 +03:00
|
|
|
PRInt32 count = Count();
|
|
|
|
for (PRInt32 i = 0; i < count; ++i)
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsString* oldString = static_cast<nsString*>(other.ElementAt(i));
|
2008-01-15 00:01:40 +03:00
|
|
|
nsString* newString = new nsString(*oldString);
|
|
|
|
if (!newString)
|
|
|
|
{
|
|
|
|
mImpl->mCount = i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
mImpl->mArray[i] = newString;
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
|
1999-01-23 09:58:34 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-11-02 19:05:04 +03:00
|
|
|
nsStringArray::StringAt(PRInt32 aIndex, nsAString& aString) const
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsString* string = static_cast<nsString*>(nsVoidArray::ElementAt(aIndex));
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsnull != string)
|
|
|
|
{
|
2005-11-02 19:04:27 +03:00
|
|
|
aString.Assign(*string);
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
else
|
|
|
|
{
|
1999-01-23 09:58:34 +03:00
|
|
|
aString.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString*
|
|
|
|
nsStringArray::StringAt(PRInt32 aIndex) const
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<nsString*>(nsVoidArray::ElementAt(aIndex));
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
2005-11-02 19:05:04 +03:00
|
|
|
nsStringArray::IndexOf(const nsAString& aPossibleString) const
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
2005-11-02 19:04:31 +03:00
|
|
|
while (ap < end)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsString* string = static_cast<nsString*>(*ap);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (string->Equals(aPossibleString))
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2005-11-02 19:05:04 +03:00
|
|
|
nsStringArray::InsertStringAt(const nsAString& aString, PRInt32 aIndex)
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
|
|
|
nsString* string = new nsString(aString);
|
2008-01-15 00:01:40 +03:00
|
|
|
if (!string)
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsVoidArray::InsertElementAt(string, aIndex))
|
1999-01-23 09:58:34 +03:00
|
|
|
return PR_TRUE;
|
2008-01-15 00:01:40 +03:00
|
|
|
|
1999-01-23 09:58:34 +03:00
|
|
|
delete string;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2005-11-02 19:05:04 +03:00
|
|
|
nsStringArray::ReplaceStringAt(const nsAString& aString,
|
2005-11-02 19:04:27 +03:00
|
|
|
PRInt32 aIndex)
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsString* string = static_cast<nsString*>(nsVoidArray::ElementAt(aIndex));
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsnull != string)
|
|
|
|
{
|
1999-01-23 09:58:34 +03:00
|
|
|
*string = aString;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2005-11-02 19:05:04 +03:00
|
|
|
nsStringArray::RemoveString(const nsAString& aString)
|
1999-01-23 09:58:34 +03:00
|
|
|
{
|
|
|
|
PRInt32 index = IndexOf(aString);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (-1 < index)
|
|
|
|
{
|
1999-01-23 09:58:34 +03:00
|
|
|
return RemoveStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsStringArray::RemoveStringAt(PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
nsString* string = StringAt(aIndex);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsnull != string)
|
|
|
|
{
|
1999-01-23 09:58:34 +03:00
|
|
|
nsVoidArray::RemoveElementAt(aIndex);
|
|
|
|
delete string;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsStringArray::Clear(void)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
2005-11-02 19:04:31 +03:00
|
|
|
while (0 <= --index)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsString* string = static_cast<nsString*>(mImpl->mArray[index]);
|
1999-01-23 09:58:34 +03:00
|
|
|
delete string;
|
|
|
|
}
|
|
|
|
nsVoidArray::Clear();
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:30 +03:00
|
|
|
PR_STATIC_CALLBACK(int)
|
|
|
|
CompareString(const nsString* aString1, const nsString* aString2, void*)
|
|
|
|
{
|
2005-11-02 23:38:21 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2005-11-02 19:04:30 +03:00
|
|
|
return Compare(*aString1, *aString2);
|
2005-11-02 23:38:21 +03:00
|
|
|
#else
|
|
|
|
const PRUnichar* s1;
|
|
|
|
const PRUnichar* s2;
|
|
|
|
PRUint32 len1 = NS_StringGetData(*aString1, &s1);
|
|
|
|
PRUint32 len2 = NS_StringGetData(*aString2, &s2);
|
|
|
|
int r = memcmp(s1, s2, sizeof(PRUnichar) * PR_MIN(len1, len2));
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (len1 < len2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (len1 > len2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
2005-11-02 19:04:30 +03:00
|
|
|
}
|
1999-01-23 09:58:34 +03:00
|
|
|
|
2005-11-02 19:04:30 +03:00
|
|
|
void nsStringArray::Sort(void)
|
|
|
|
{
|
|
|
|
Sort(CompareString, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsStringArray::Sort(nsStringArrayComparatorFunc aFunc, void* aData)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsVoidArray::Sort(reinterpret_cast<nsVoidArrayComparatorFunc>(aFunc), aData);
|
2005-11-02 19:04:30 +03:00
|
|
|
}
|
1999-01-23 09:58:34 +03:00
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStringArray::EnumerateForwards(nsStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRInt32 index = -1;
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
while (running && (++index < mImpl->mCount))
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
running = (*aFunc)(*static_cast<nsString*>(mImpl->mArray[index]), aData);
|
2005-11-02 19:04:22 +03:00
|
|
|
}
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsStringArray::EnumerateBackwards(nsStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
1999-01-23 09:58:34 +03:00
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
while (running && (0 <= --index))
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
running = (*aFunc)(*static_cast<nsString*>(mImpl->mArray[index]), aData);
|
2005-11-02 19:04:22 +03:00
|
|
|
}
|
1999-01-23 09:58:34 +03:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
1999-10-30 00:47:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// nsCStringArray
|
|
|
|
|
|
|
|
nsCStringArray::nsCStringArray(void)
|
|
|
|
: nsVoidArray()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:02 +03:00
|
|
|
// Parses a given string using the delimiter passed in and appends items
|
|
|
|
// parsed to the array.
|
2008-01-15 00:01:40 +03:00
|
|
|
PRBool
|
2005-11-02 19:05:02 +03:00
|
|
|
nsCStringArray::ParseString(const char* string, const char* delimiter)
|
|
|
|
{
|
|
|
|
if (string && *string && delimiter && *delimiter) {
|
2005-11-02 23:38:21 +03:00
|
|
|
char *rest = strdup(string);
|
2008-01-15 00:01:40 +03:00
|
|
|
if (!rest)
|
|
|
|
return PR_FALSE;
|
2005-11-02 23:38:21 +03:00
|
|
|
char *newStr = rest;
|
|
|
|
char *token = NS_strtok(delimiter, &newStr);
|
2005-11-02 19:05:02 +03:00
|
|
|
|
2008-01-15 00:01:40 +03:00
|
|
|
PRInt32 count = Count();
|
2005-11-02 19:05:02 +03:00
|
|
|
while (token) {
|
|
|
|
if (*token) {
|
|
|
|
/* calling AppendElement(void*) to avoid extra nsCString copy */
|
2008-01-15 00:01:40 +03:00
|
|
|
nsCString *cstring = new nsCString(token);
|
|
|
|
if (cstring && !AppendElement(cstring)) {
|
|
|
|
// AppendElement failed, release and null cstring so we fall
|
|
|
|
// through to the case below.
|
|
|
|
delete cstring;
|
|
|
|
cstring = nsnull;
|
|
|
|
}
|
|
|
|
if (!cstring) {
|
|
|
|
// We've run out of memory. Remove all newly appended elements from
|
|
|
|
// our array so we don't leave ourselves in a partially added state.
|
|
|
|
// When we return, the array will be precisely as it was when this
|
|
|
|
// function was called.
|
|
|
|
RemoveElementsAt(count, Count() - count);
|
|
|
|
free(rest);
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2005-11-02 19:05:02 +03:00
|
|
|
}
|
2005-11-02 23:38:21 +03:00
|
|
|
token = NS_strtok(delimiter, &newStr);
|
2005-11-02 19:05:02 +03:00
|
|
|
}
|
2005-11-02 23:38:21 +03:00
|
|
|
free(rest);
|
2005-11-02 19:05:02 +03:00
|
|
|
}
|
2008-01-15 00:01:40 +03:00
|
|
|
return PR_TRUE;
|
2005-11-02 19:05:02 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
nsCStringArray::nsCStringArray(PRInt32 aCount)
|
|
|
|
: nsVoidArray(aCount)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-10-30 00:47:17 +04:00
|
|
|
nsCStringArray::~nsCStringArray(void)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringArray&
|
|
|
|
nsCStringArray::operator=(const nsCStringArray& other)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
// Copy the pointers
|
|
|
|
nsVoidArray::operator=(other);
|
|
|
|
|
|
|
|
// Now copy the strings
|
2008-01-15 00:01:40 +03:00
|
|
|
PRInt32 count = Count();
|
|
|
|
for (PRInt32 i = 0; i < count; ++i)
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsCString* oldString = static_cast<nsCString*>(other.ElementAt(i));
|
2008-01-15 00:01:40 +03:00
|
|
|
nsCString* newString = new nsCString(*oldString);
|
|
|
|
if (!newString)
|
|
|
|
{
|
|
|
|
mImpl->mCount = i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
mImpl->mArray[i] = newString;
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
2005-11-02 19:04:22 +03:00
|
|
|
|
1999-10-30 00:47:17 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-11-02 19:05:07 +03:00
|
|
|
nsCStringArray::CStringAt(PRInt32 aIndex, nsACString& aCString) const
|
1999-10-30 00:47:17 +04:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsCString* string = static_cast<nsCString*>(nsVoidArray::ElementAt(aIndex));
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsnull != string)
|
|
|
|
{
|
1999-10-30 00:47:17 +04:00
|
|
|
aCString = *string;
|
|
|
|
}
|
2005-11-02 19:04:31 +03:00
|
|
|
else
|
|
|
|
{
|
1999-10-30 00:47:17 +04:00
|
|
|
aCString.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString*
|
|
|
|
nsCStringArray::CStringAt(PRInt32 aIndex) const
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<nsCString*>(nsVoidArray::ElementAt(aIndex));
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
2005-11-02 19:05:11 +03:00
|
|
|
nsCStringArray::IndexOf(const nsACString& aPossibleString) const
|
1999-10-30 00:47:17 +04:00
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
2005-11-02 19:04:31 +03:00
|
|
|
while (ap < end)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsCString* string = static_cast<nsCString*>(*ap);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (string->Equals(aPossibleString))
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-02 23:38:21 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
1999-10-30 00:47:17 +04:00
|
|
|
PRInt32
|
2005-11-02 19:05:11 +03:00
|
|
|
nsCStringArray::IndexOfIgnoreCase(const nsACString& aPossibleString) const
|
1999-10-30 00:47:17 +04:00
|
|
|
{
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
2005-11-02 19:04:31 +03:00
|
|
|
while (ap < end)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsCString* string = static_cast<nsCString*>(*ap);
|
2005-11-02 19:05:11 +03:00
|
|
|
if (string->Equals(aPossibleString, nsCaseInsensitiveCStringComparator()))
|
2005-11-02 19:04:31 +03:00
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-02 23:38:21 +03:00
|
|
|
#endif
|
1999-10-30 00:47:17 +04:00
|
|
|
|
|
|
|
PRBool
|
2005-11-02 19:05:11 +03:00
|
|
|
nsCStringArray::InsertCStringAt(const nsACString& aCString, PRInt32 aIndex)
|
1999-10-30 00:47:17 +04:00
|
|
|
{
|
|
|
|
nsCString* string = new nsCString(aCString);
|
2008-01-15 00:01:40 +03:00
|
|
|
if (!string)
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsVoidArray::InsertElementAt(string, aIndex))
|
1999-10-30 00:47:17 +04:00
|
|
|
return PR_TRUE;
|
2008-01-15 00:01:40 +03:00
|
|
|
|
1999-10-30 00:47:17 +04:00
|
|
|
delete string;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2005-11-02 19:05:11 +03:00
|
|
|
nsCStringArray::ReplaceCStringAt(const nsACString& aCString, PRInt32 aIndex)
|
1999-10-30 00:47:17 +04:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsCString* string = static_cast<nsCString*>(nsVoidArray::ElementAt(aIndex));
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsnull != string)
|
|
|
|
{
|
1999-10-30 00:47:17 +04:00
|
|
|
*string = aCString;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2005-11-02 19:05:11 +03:00
|
|
|
nsCStringArray::RemoveCString(const nsACString& aCString)
|
1999-10-30 00:47:17 +04:00
|
|
|
{
|
|
|
|
PRInt32 index = IndexOf(aCString);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (-1 < index)
|
|
|
|
{
|
1999-10-30 00:47:17 +04:00
|
|
|
return RemoveCStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2005-11-02 23:38:21 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
1999-10-30 00:47:17 +04:00
|
|
|
PRBool
|
2005-11-02 19:05:11 +03:00
|
|
|
nsCStringArray::RemoveCStringIgnoreCase(const nsACString& aCString)
|
1999-10-30 00:47:17 +04:00
|
|
|
{
|
|
|
|
PRInt32 index = IndexOfIgnoreCase(aCString);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (-1 < index)
|
|
|
|
{
|
1999-10-30 00:47:17 +04:00
|
|
|
return RemoveCStringAt(index);
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2005-11-02 23:38:21 +03:00
|
|
|
#endif
|
1999-10-30 00:47:17 +04:00
|
|
|
|
|
|
|
PRBool nsCStringArray::RemoveCStringAt(PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
nsCString* string = CStringAt(aIndex);
|
2005-11-02 19:04:31 +03:00
|
|
|
if (nsnull != string)
|
|
|
|
{
|
1999-10-30 00:47:17 +04:00
|
|
|
nsVoidArray::RemoveElementAt(aIndex);
|
|
|
|
delete string;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCStringArray::Clear(void)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
2005-11-02 19:04:31 +03:00
|
|
|
while (0 <= --index)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsCString* string = static_cast<nsCString*>(mImpl->mArray[index]);
|
1999-10-30 00:47:17 +04:00
|
|
|
delete string;
|
|
|
|
}
|
|
|
|
nsVoidArray::Clear();
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:30 +03:00
|
|
|
PR_STATIC_CALLBACK(int)
|
|
|
|
CompareCString(const nsCString* aCString1, const nsCString* aCString2, void*)
|
|
|
|
{
|
2005-11-02 23:38:21 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2005-11-02 19:04:30 +03:00
|
|
|
return Compare(*aCString1, *aCString2);
|
2005-11-02 23:38:21 +03:00
|
|
|
#else
|
|
|
|
const char* s1;
|
|
|
|
const char* s2;
|
|
|
|
PRUint32 len1 = NS_CStringGetData(*aCString1, &s1);
|
|
|
|
PRUint32 len2 = NS_CStringGetData(*aCString2, &s2);
|
|
|
|
int r = memcmp(s1, s2, PR_MIN(len1, len2));
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (len1 < len2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (len1 > len2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
2005-11-02 19:04:30 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 23:38:21 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2005-11-02 19:04:30 +03:00
|
|
|
PR_STATIC_CALLBACK(int)
|
|
|
|
CompareCStringIgnoreCase(const nsCString* aCString1, const nsCString* aCString2, void*)
|
|
|
|
{
|
|
|
|
return Compare(*aCString1, *aCString2, nsCaseInsensitiveCStringComparator());
|
|
|
|
}
|
2005-11-02 23:38:21 +03:00
|
|
|
#endif
|
1999-10-30 00:47:17 +04:00
|
|
|
|
2005-11-02 19:04:30 +03:00
|
|
|
void nsCStringArray::Sort(void)
|
|
|
|
{
|
|
|
|
Sort(CompareCString, nsnull);
|
|
|
|
}
|
|
|
|
|
2005-11-02 23:38:21 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2005-11-02 19:04:30 +03:00
|
|
|
void nsCStringArray::SortIgnoreCase(void)
|
|
|
|
{
|
|
|
|
Sort(CompareCStringIgnoreCase, nsnull);
|
|
|
|
}
|
2005-11-02 23:38:21 +03:00
|
|
|
#endif
|
2005-11-02 19:04:30 +03:00
|
|
|
|
|
|
|
void nsCStringArray::Sort(nsCStringArrayComparatorFunc aFunc, void* aData)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsVoidArray::Sort(reinterpret_cast<nsVoidArrayComparatorFunc>(aFunc), aData);
|
2005-11-02 19:04:30 +03:00
|
|
|
}
|
1999-10-30 00:47:17 +04:00
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::EnumerateForwards(nsCStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = -1;
|
2005-11-02 19:04:31 +03:00
|
|
|
while (running && (++index < mImpl->mCount))
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
running = (*aFunc)(*static_cast<nsCString*>(mImpl->mArray[index]), aData);
|
2005-11-02 19:04:22 +03:00
|
|
|
}
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCStringArray::EnumerateBackwards(nsCStringArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
PRBool running = PR_TRUE;
|
|
|
|
|
2005-11-02 19:04:31 +03:00
|
|
|
if (mImpl)
|
|
|
|
{
|
2005-11-02 19:04:22 +03:00
|
|
|
PRInt32 index = Count();
|
2005-11-02 19:04:31 +03:00
|
|
|
while (running && (0 <= --index))
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
running = (*aFunc)(*static_cast<nsCString*>(mImpl->mArray[index]), aData);
|
2005-11-02 19:04:22 +03:00
|
|
|
}
|
1999-10-30 00:47:17 +04:00
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// NOTE: nsSmallVoidArray elements MUST all have the low bit as 0.
|
|
|
|
// This means that normally it's only used for pointers, and in particular
|
|
|
|
// structures or objects.
|
|
|
|
nsSmallVoidArray::~nsSmallVoidArray()
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle())
|
2005-11-02 19:04:54 +03:00
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
// Have to null out mImpl before the nsVoidArray dtor runs.
|
|
|
|
mImpl = nsnull;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSmallVoidArray&
|
|
|
|
nsSmallVoidArray::operator=(nsSmallVoidArray& other)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
PRInt32 count = other.Count();
|
|
|
|
switch (count) {
|
|
|
|
case 0:
|
|
|
|
Clear();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
Clear();
|
|
|
|
AppendElement(other.ElementAt(0));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (GetArraySize() >= count || SizeTo(count)) {
|
|
|
|
*AsArray() = *other.AsArray();
|
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
2005-11-02 19:04:54 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSmallVoidArray::GetArraySize() const
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
return 1;
|
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->GetArraySize();
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSmallVoidArray::Count() const
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
2005-11-02 19:04:54 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->Count();
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2005-11-02 19:05:24 +03:00
|
|
|
nsSmallVoidArray::FastElementAt(PRInt32 aIndex) const
|
2005-11-02 19:04:54 +03:00
|
|
|
{
|
2006-09-06 04:22:52 +04:00
|
|
|
NS_ASSERTION(0 <= aIndex && aIndex < Count(), "nsSmallVoidArray::FastElementAt: index out of range");
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
if (HasSingle()) {
|
|
|
|
return GetSingle();
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->FastElementAt(aIndex);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSmallVoidArray::IndexOf(void* aPossibleElement) const
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
return aPossibleElement == GetSingle() ? 0 : -1;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->IndexOf(aPossibleElement);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aElement) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
if (aIndex == 0 && IsEmpty()) {
|
|
|
|
SetSingle(aElement);
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
if (!EnsureArray()) {
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->InsertElementAt(aElement, aIndex);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
PRBool nsSmallVoidArray::InsertElementsAt(const nsVoidArray &aOther, PRInt32 aIndex)
|
2005-11-02 19:04:54 +03:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2005-11-02 19:05:24 +03:00
|
|
|
for (int i = 0; i < aOther.Count(); i++) {
|
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aOther.ElementAt(i)) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
2005-11-02 19:04:55 +03:00
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
#endif
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
if (aIndex == 0 && IsEmpty() && aOther.Count() == 1) {
|
|
|
|
SetSingle(aOther.FastElementAt(0));
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
if (!EnsureArray()) {
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
return AsArray()->InsertElementsAt(aOther, aIndex);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::ReplaceElementAt(void* aElement, PRInt32 aIndex)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aElement) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
if (aIndex == 0 && (IsEmpty() || HasSingle())) {
|
|
|
|
SetSingle(aElement);
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
if (!EnsureArray()) {
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
return AsArray()->ReplaceElementAt(aElement, aIndex);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::AppendElement(void* aElement)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aElement) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
if (IsEmpty()) {
|
|
|
|
SetSingle(aElement);
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
if (!EnsureArray()) {
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->AppendElement(aElement);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::RemoveElement(void* aElement)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
if (aElement == GetSingle()) {
|
|
|
|
mImpl = nsnull;
|
2005-11-02 19:04:54 +03:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->RemoveElement(aElement);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::RemoveElementAt(PRInt32 aIndex)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
if (aIndex == 0) {
|
|
|
|
mImpl = nsnull;
|
|
|
|
|
2005-11-02 19:04:54 +03:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->RemoveElementAt(aIndex);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::RemoveElementsAt(PRInt32 aIndex, PRInt32 aCount)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
if (aIndex == 0) {
|
|
|
|
if (aCount > 0) {
|
|
|
|
mImpl = nsnull;
|
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return PR_FALSE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->RemoveElementsAt(aIndex, aCount);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSmallVoidArray::Clear()
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
mImpl = nsnull;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
else {
|
|
|
|
AsArray()->Clear();
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::SizeTo(PRInt32 aMin)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (!HasSingle()) {
|
|
|
|
return AsArray()->SizeTo(aMin);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
if (aMin <= 0) {
|
|
|
|
mImpl = nsnull;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
|
|
|
|
if (aMin == 1) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* single = GetSingle();
|
|
|
|
mImpl = nsnull;
|
|
|
|
if (!AsArray()->SizeTo(aMin)) {
|
|
|
|
SetSingle(single);
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsArray()->AppendElement(single);
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSmallVoidArray::Compact()
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (!HasSingle()) {
|
|
|
|
AsArray()->Compact();
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSmallVoidArray::Sort(nsVoidArrayComparatorFunc aFunc, void* aData)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (!HasSingle()) {
|
|
|
|
AsArray()->Sort(aFunc,aData);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
return (*aFunc)(GetSingle(), aData);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->EnumerateForwards(aFunc,aData);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (HasSingle()) {
|
|
|
|
return (*aFunc)(GetSingle(), aData);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
2005-11-02 19:05:24 +03:00
|
|
|
return AsArray()->EnumerateBackwards(aFunc,aData);
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
PRBool
|
|
|
|
nsSmallVoidArray::EnsureArray()
|
2005-11-02 19:04:54 +03:00
|
|
|
{
|
2005-11-02 19:05:24 +03:00
|
|
|
if (!HasSingle()) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
void* single = GetSingle();
|
|
|
|
mImpl = nsnull;
|
|
|
|
if (!AsArray()->AppendElement(single)) {
|
|
|
|
SetSingle(single);
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2005-11-02 19:04:54 +03:00
|
|
|
|
2005-11-02 19:05:24 +03:00
|
|
|
return PR_TRUE;
|
2005-11-02 19:04:54 +03:00
|
|
|
}
|