Modified ReplaceElementAt to grow the array if an index out of the

allocated boundary was requested.
This commit is contained in:
jevering 1998-07-02 00:28:56 +00:00
Родитель 882a80b31c
Коммит 3705e61240
3 изменённых файлов: 54 добавлений и 9 удалений

Просмотреть файл

@ -128,11 +128,26 @@ PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex)
PRBool nsVoidArray::ReplaceElementAt(void* aElement, PRInt32 aIndex)
{
if (PRUint32(aIndex) >= PRUint32(mCount)) {
// An invalid index causes the replace to fail
return PR_FALSE;
if (PRUint32(aIndex) >= PRUint32(mArraySize)) {
PRInt32 requestedCount = aIndex + 1;
PRInt32 growDelta = requestedCount - mCount;
PRInt32 newCount = mCount + (growDelta > kGrowArrayBy ? growDelta : kGrowArrayBy);
void** newArray = new void*[newCount];
nsCRT::memset(newArray, 0, newCount * sizeof(void*));
if (newArray==nsnull)
return PR_FALSE;
if (mArray != nsnull && aIndex != 0) {
nsCRT::memcpy(newArray, mArray, mCount * sizeof(void*));
if (mArray != nsnull)
delete [] mArray;
}
mArray = newArray;
mArraySize = newCount;
}
mArray[aIndex] = aElement;
if (aIndex >= mCount)
mCount = aIndex+1;
return PR_TRUE;
}

Просмотреть файл

@ -128,11 +128,26 @@ PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex)
PRBool nsVoidArray::ReplaceElementAt(void* aElement, PRInt32 aIndex)
{
if (PRUint32(aIndex) >= PRUint32(mCount)) {
// An invalid index causes the replace to fail
return PR_FALSE;
if (PRUint32(aIndex) >= PRUint32(mArraySize)) {
PRInt32 requestedCount = aIndex + 1;
PRInt32 growDelta = requestedCount - mCount;
PRInt32 newCount = mCount + (growDelta > kGrowArrayBy ? growDelta : kGrowArrayBy);
void** newArray = new void*[newCount];
nsCRT::memset(newArray, 0, newCount * sizeof(void*));
if (newArray==nsnull)
return PR_FALSE;
if (mArray != nsnull && aIndex != 0) {
nsCRT::memcpy(newArray, mArray, mCount * sizeof(void*));
if (mArray != nsnull)
delete [] mArray;
}
mArray = newArray;
mArraySize = newCount;
}
mArray[aIndex] = aElement;
if (aIndex >= mCount)
mCount = aIndex+1;
return PR_TRUE;
}

Просмотреть файл

@ -128,11 +128,26 @@ PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex)
PRBool nsVoidArray::ReplaceElementAt(void* aElement, PRInt32 aIndex)
{
if (PRUint32(aIndex) >= PRUint32(mCount)) {
// An invalid index causes the replace to fail
return PR_FALSE;
if (PRUint32(aIndex) >= PRUint32(mArraySize)) {
PRInt32 requestedCount = aIndex + 1;
PRInt32 growDelta = requestedCount - mCount;
PRInt32 newCount = mCount + (growDelta > kGrowArrayBy ? growDelta : kGrowArrayBy);
void** newArray = new void*[newCount];
nsCRT::memset(newArray, 0, newCount * sizeof(void*));
if (newArray==nsnull)
return PR_FALSE;
if (mArray != nsnull && aIndex != 0) {
nsCRT::memcpy(newArray, mArray, mCount * sizeof(void*));
if (mArray != nsnull)
delete [] mArray;
}
mArray = newArray;
mArraySize = newCount;
}
mArray[aIndex] = aElement;
if (aIndex >= mCount)
mCount = aIndex+1;
return PR_TRUE;
}