Backed out changeset 8c2984643f74 (bug 1308317)

This commit is contained in:
Carsten "Tomcat" Book 2016-10-14 14:58:38 +02:00
parent 3470d6dfe0
commit b79e4e00cc
4 changed files with 41 additions and 12 deletions

View File

@ -34,10 +34,14 @@ class nsISupportsArray;
interface nsISupportsArray : nsICollection {
[notxpcom] long IndexOf([const] in nsISupports aPossibleElement);
[notxpcom] long IndexOfStartingAt([const] in nsISupports aPossibleElement,
in unsigned long aStartIndex);
[notxpcom] long LastIndexOf([const] in nsISupports aPossibleElement);
// xpcom-compatible versions
long GetIndexOf(in nsISupports aPossibleElement);
long GetIndexOfStartingAt(in nsISupports aPossibleElement,
in unsigned long aStartIndex);
long GetLastIndexOf(in nsISupports aPossibleElement);
[notxpcom] boolean InsertElementAt(in nsISupports aElement,

View File

@ -209,14 +209,23 @@ nsSupportsArray::GetElementAt(uint32_t aIndex, nsISupports** aOutPtr)
NS_IMETHODIMP_(int32_t)
nsSupportsArray::IndexOf(const nsISupports* aPossibleElement)
{
const nsISupports** start = (const nsISupports**)mArray; // work around goofy compiler behavior
const nsISupports** ep = start;
const nsISupports** end = (start + mCount);
while (ep < end) {
if (aPossibleElement == *ep) {
return (ep - start);
return IndexOfStartingAt(aPossibleElement, 0);
}
NS_IMETHODIMP_(int32_t)
nsSupportsArray::IndexOfStartingAt(const nsISupports* aPossibleElement,
uint32_t aStartIndex)
{
if (aStartIndex < mCount) {
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++;
}
ep++;
}
return -1;
}
@ -303,7 +312,7 @@ nsSupportsArray::RemoveElementsAt(uint32_t aIndex, uint32_t aCount)
NS_IMETHODIMP
nsSupportsArray::RemoveElement(nsISupports* aElement)
{
int32_t theIndex = IndexOf(aElement);
int32_t theIndex = IndexOfStartingAt(aElement, 0);
if (theIndex >= 0) {
return RemoveElementAt(theIndex) ? NS_OK : NS_ERROR_FAILURE;
}

View File

@ -61,6 +61,8 @@ public:
// nsISupportsArray methods:
NS_IMETHOD_(int32_t) IndexOf(const nsISupports* aPossibleElement) override;
NS_IMETHOD_(int32_t) IndexOfStartingAt(const nsISupports* aPossibleElement,
uint32_t aStartIndex = 0) override;
NS_IMETHOD_(int32_t) LastIndexOf(const nsISupports* aPossibleElement) override;
NS_IMETHOD GetIndexOf(nsISupports* aPossibleElement, int32_t* aResult) override
@ -69,6 +71,13 @@ public:
return NS_OK;
}
NS_IMETHOD GetIndexOfStartingAt(nsISupports* aPossibleElement,
uint32_t aStartIndex, int32_t* aResult) override
{
*aResult = IndexOfStartingAt(aPossibleElement, aStartIndex);
return NS_OK;
}
NS_IMETHOD GetLastIndexOf(nsISupports* aPossibleElement, int32_t* aResult) override
{
*aResult = LastIndexOf(aPossibleElement);

View File

@ -119,12 +119,19 @@ TEST(Array, main)
// test IndexOf && LastIndexOf
int32_t expectedIndex = 0;
int32_t expectedIndex[5] = {0, 4, 6, 12, -1};
int32_t count = 0;
int32_t index = array->IndexOf(foo);
EXPECT_EQ(index, expectedIndex);
expectedIndex = 12;
EXPECT_EQ(index, expectedIndex[count]);
while (-1 != index) {
count++;
index = array->IndexOfStartingAt(foo, index + 1);
if (-1 != index)
EXPECT_EQ(index, expectedIndex[count]);
}
index = array->LastIndexOf(foo);
EXPECT_EQ(index, expectedIndex);
count--;
EXPECT_EQ(index, expectedIndex[count]);
// test ReplaceElementAt
array->ReplaceElementAt(foo, 8);