Bug 1839051 - Use nsTArray::Sort for Sort and have also StableSort in nsCOMArray. r=necko-reviewers,kershaw,emilio,places-reviewers,mak

Depends on D181344

Differential Revision: https://phabricator.services.mozilla.com/D181889
This commit is contained in:
Jens Stutte 2023-12-11 06:53:26 +00:00
parent d7c16125cf
commit 95be758f8e
7 changed files with 82 additions and 141 deletions

View File

@ -4512,8 +4512,7 @@ static nsresult parsePrefData(const nsCString& aData, PrefValueKind aKind) {
return NS_OK;
}
static int pref_CompareFileNames(nsIFile* aFile1, nsIFile* aFile2,
void* /* unused */) {
static int pref_CompareFileNames(nsIFile* aFile1, nsIFile* aFile2) {
nsAutoCString filename1, filename2;
aFile1->GetNativeLeafName(filename1);
aFile2->GetNativeLeafName(filename2);
@ -4568,7 +4567,7 @@ static nsresult pref_LoadPrefsInDir(nsIFile* aDir) {
return rv;
}
prefFiles.Sort(pref_CompareFileNames, nullptr);
prefFiles.Sort(pref_CompareFileNames);
uint32_t arrayCount = prefFiles.Count();
uint32_t i;

View File

@ -35,7 +35,7 @@ nsDirectoryIndexStream::nsDirectoryIndexStream() {
MOZ_LOG(gLog, LogLevel::Debug, ("nsDirectoryIndexStream[%p]: created", this));
}
static int compare(nsIFile* aElement1, nsIFile* aElement2, void* aData) {
static int compare(nsIFile* aElement1, nsIFile* aElement2) {
if (!NS_IsNativeUTF8()) {
// don't check for errors, because we can't report them anyway
nsAutoString name1, name2;
@ -93,7 +93,7 @@ nsresult nsDirectoryIndexStream::Init(nsIFile* aDir) {
mArray.AppendObject(file); // addrefs
}
mArray.Sort(compare, nullptr);
mArray.Sort(compare);
mBuf.AppendLiteral("300: ");
nsAutoCString url;

View File

@ -893,7 +893,7 @@ nsNavHistoryContainerResultNode::GetSortingComparator(uint16_t aSortType) {
*/
void nsNavHistoryContainerResultNode::RecursiveSort(
SortComparator aComparator) {
mChildren.Sort(aComparator, nullptr);
mChildren.Sort(aComparator);
for (nsNavHistoryResultNode* child : mChildren) {
if (child->IsContainer()) {
child->GetAsContainer()->RecursiveSort(aComparator);
@ -917,14 +917,14 @@ int32_t nsNavHistoryContainerResultNode::FindInsertionPoint(
// The common case is the beginning or the end because this is used to insert
// new items that are added to history, which is usually sorted by date.
int32_t res;
res = aComparator(aNode, mChildren[0], nullptr);
res = aComparator(aNode, mChildren[0]);
if (res <= 0) {
if (aItemExists && res == 0) {
(*aItemExists) = true;
}
return 0;
}
res = aComparator(aNode, mChildren[mChildren.Count() - 1], nullptr);
res = aComparator(aNode, mChildren[mChildren.Count() - 1]);
if (res >= 0) {
if (aItemExists && res == 0) {
(*aItemExists) = true;
@ -936,7 +936,7 @@ int32_t nsNavHistoryContainerResultNode::FindInsertionPoint(
int32_t endRange = mChildren.Count(); // exclusive
while (beginRange < endRange) {
int32_t center = beginRange + (endRange - beginRange) / 2;
int32_t res = aComparator(aNode, mChildren[center], nullptr);
int32_t res = aComparator(aNode, mChildren[center]);
if (res <= 0) {
endRange = center; // left side
if (aItemExists && res == 0) {
@ -966,13 +966,13 @@ bool nsNavHistoryContainerResultNode::DoesChildNeedResorting(
if (aIndex > 0) {
// compare to previous item
if (aComparator(mChildren[aIndex - 1], mChildren[aIndex], nullptr) > 0) {
if (aComparator(mChildren[aIndex - 1], mChildren[aIndex]) > 0) {
return true;
}
}
if (aIndex < mChildren.Count() - 1) {
// compare to next item
if (aComparator(mChildren[aIndex], mChildren[aIndex + 1], nullptr) > 0) {
if (aComparator(mChildren[aIndex], mChildren[aIndex + 1]) > 0) {
return true;
}
}
@ -997,7 +997,7 @@ int32_t nsNavHistoryContainerResultNode::SortComparison_StringLess(
* everything will be -1 and we don't worry about sorting.
*/
int32_t nsNavHistoryContainerResultNode::SortComparison_Bookmark(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return a->mBookmarkIndex - b->mBookmarkIndex;
}
@ -1010,7 +1010,7 @@ int32_t nsNavHistoryContainerResultNode::SortComparison_Bookmark(
* The collation object must be allocated before sorting on title!
*/
int32_t nsNavHistoryContainerResultNode::SortComparison_TitleLess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
uint32_t aType;
a->GetType(&aType);
@ -1025,16 +1025,15 @@ int32_t nsNavHistoryContainerResultNode::SortComparison_TitleLess(
// resolve by date
value = ComparePRTime(a->mTime, b->mTime);
if (value == 0) {
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(
a, b, closure);
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b);
}
}
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_TitleGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -SortComparison_TitleLess(a, b, closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -SortComparison_TitleLess(a, b);
}
/**
@ -1042,60 +1041,55 @@ int32_t nsNavHistoryContainerResultNode::SortComparison_TitleGreater(
* deterministic ordering of the results so they don't move around.
*/
int32_t nsNavHistoryContainerResultNode::SortComparison_DateLess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
int32_t value = ComparePRTime(a->mTime, b->mTime);
if (value == 0) {
value = SortComparison_StringLess(NS_ConvertUTF8toUTF16(a->mTitle),
NS_ConvertUTF8toUTF16(b->mTitle));
if (value == 0) {
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b,
closure);
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b);
}
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_DateGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -nsNavHistoryContainerResultNode::SortComparison_DateLess(a, b,
closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -nsNavHistoryContainerResultNode::SortComparison_DateLess(a, b);
}
int32_t nsNavHistoryContainerResultNode::SortComparison_DateAddedLess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
int32_t value = ComparePRTime(a->mDateAdded, b->mDateAdded);
if (value == 0) {
value = SortComparison_StringLess(NS_ConvertUTF8toUTF16(a->mTitle),
NS_ConvertUTF8toUTF16(b->mTitle));
if (value == 0) {
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b,
closure);
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b);
}
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_DateAddedGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -nsNavHistoryContainerResultNode::SortComparison_DateAddedLess(
a, b, closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -nsNavHistoryContainerResultNode::SortComparison_DateAddedLess(a, b);
}
int32_t nsNavHistoryContainerResultNode::SortComparison_LastModifiedLess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
int32_t value = ComparePRTime(a->mLastModified, b->mLastModified);
if (value == 0) {
value = SortComparison_StringLess(NS_ConvertUTF8toUTF16(a->mTitle),
NS_ConvertUTF8toUTF16(b->mTitle));
if (value == 0) {
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b,
closure);
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b);
}
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_LastModifiedGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -nsNavHistoryContainerResultNode::SortComparison_LastModifiedLess(
a, b, closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -nsNavHistoryContainerResultNode::SortComparison_LastModifiedLess(a,
b);
}
/**
@ -1103,7 +1097,7 @@ int32_t nsNavHistoryContainerResultNode::SortComparison_LastModifiedGreater(
* valid (like days or hosts).
*/
int32_t nsNavHistoryContainerResultNode::SortComparison_URILess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
int32_t value;
if (a->IsURI() && b->IsURI()) {
// normal URI or visit
@ -1122,40 +1116,37 @@ int32_t nsNavHistoryContainerResultNode::SortComparison_URILess(
if (value == 0) {
value = ComparePRTime(a->mTime, b->mTime);
if (value == 0) {
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b,
closure);
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b);
}
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_URIGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -SortComparison_URILess(a, b, closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -SortComparison_URILess(a, b);
}
/**
* Fall back on dates for conflict resolution
*/
int32_t nsNavHistoryContainerResultNode::SortComparison_VisitCountLess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
int32_t value = CompareIntegers(a->mAccessCount, b->mAccessCount);
if (value == 0) {
value = ComparePRTime(a->mTime, b->mTime);
if (value == 0) {
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b,
closure);
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b);
}
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_VisitCountGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -nsNavHistoryContainerResultNode::SortComparison_VisitCountLess(
a, b, closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -nsNavHistoryContainerResultNode::SortComparison_VisitCountLess(a, b);
}
int32_t nsNavHistoryContainerResultNode::SortComparison_TagsLess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
int32_t value = 0;
nsAutoString aTags, bTags;
@ -1169,36 +1160,34 @@ int32_t nsNavHistoryContainerResultNode::SortComparison_TagsLess(
// fall back to title sorting
if (value == 0) {
value = SortComparison_TitleLess(a, b, closure);
value = SortComparison_TitleLess(a, b);
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_TagsGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -SortComparison_TagsLess(a, b, closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -SortComparison_TagsLess(a, b);
}
/**
* Fall back on date and bookmarked status, for conflict resolution.
*/
int32_t nsNavHistoryContainerResultNode::SortComparison_FrecencyLess(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
int32_t value = CompareIntegers(a->mFrecency, b->mFrecency);
if (value == 0) {
value = ComparePRTime(a->mTime, b->mTime);
if (value == 0) {
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b,
closure);
value = nsNavHistoryContainerResultNode::SortComparison_Bookmark(a, b);
}
}
return value;
}
int32_t nsNavHistoryContainerResultNode::SortComparison_FrecencyGreater(
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b, void* closure) {
return -nsNavHistoryContainerResultNode::SortComparison_FrecencyLess(a, b,
closure);
nsNavHistoryResultNode* a, nsNavHistoryResultNode* b) {
return -nsNavHistoryContainerResultNode::SortComparison_FrecencyLess(a, b);
}
/**
@ -2065,7 +2054,7 @@ uint16_t nsNavHistoryQueryResultNode::GetSortType() {
void nsNavHistoryQueryResultNode::RecursiveSort(SortComparator aComparator) {
if (!IsContainersQuery()) {
mChildren.Sort(aComparator, nullptr);
mChildren.Sort(aComparator);
}
for (int32_t i = 0; i < mChildren.Count(); ++i) {

View File

@ -550,56 +550,39 @@ class nsNavHistoryContainerResultNode
const nsAString& b);
static int32_t SortComparison_Bookmark(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_TitleLess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_TitleGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_DateLess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_DateGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_URILess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_URIGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_VisitCountLess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_VisitCountGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_DateAddedLess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_DateAddedGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_LastModifiedLess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_LastModifiedGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_TagsLess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_TagsGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_FrecencyLess(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
static int32_t SortComparison_FrecencyGreater(nsNavHistoryResultNode* a,
nsNavHistoryResultNode* b,
void* closure);
nsNavHistoryResultNode* b);
// finding children: THESE DO NOT ADDREF
nsNavHistoryResultNode* FindChildByURI(const nsACString& aSpec,

View File

@ -210,12 +210,10 @@ static nsresult CreateEntryEnumerator(nsTHashtable<CategoryLeaf>& aTable,
}
}
entries.Sort(
[](nsICategoryEntry* aA, nsICategoryEntry* aB, void*) {
return strcmp(CategoryEntry::Cast(aA)->Key(),
CategoryEntry::Cast(aB)->Key());
},
nullptr);
entries.Sort([](nsICategoryEntry* aA, nsICategoryEntry* aB) {
return strcmp(CategoryEntry::Cast(aA)->Key(),
CategoryEntry::Cast(aB)->Key());
});
return NS_NewArrayEnumerator(aResult, entries, NS_GET_IID(nsICategoryEntry));
}

View File

@ -8,7 +8,6 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/OperatorNewExtensions.h"
#include "nsQuickSort.h"
#include "nsCOMPtr.h"
@ -96,22 +95,6 @@ bool nsCOMArray_base::EnumerateBackwards(nsBaseArrayEnumFunc aFunc,
return true;
}
int nsCOMArray_base::VoidStarComparator(const void* aElement1,
const void* aElement2, void* aData) {
auto ctx = static_cast<nsISupportsComparatorContext*>(aData);
return (*ctx->mComparatorFunc)(*static_cast<nsISupports* const*>(aElement1),
*static_cast<nsISupports* const*>(aElement2),
ctx->mData);
}
void nsCOMArray_base::Sort(nsISupportsComparatorFunc aFunc, void* aData) {
if (mArray.Length() > 1) {
nsISupportsComparatorContext ctx = {aFunc, aData};
NS_QuickSort(mArray.Elements(), mArray.Length(), sizeof(nsISupports*),
VoidStarComparator, &ctx);
}
}
bool nsCOMArray_base::InsertObjectAt(nsISupports* aObject, int32_t aIndex) {
if ((uint32_t)aIndex > mArray.Length()) {
return false;

View File

@ -47,18 +47,6 @@ class nsCOMArray_base {
bool EnumerateBackwards(nsBaseArrayEnumFunc aFunc, void* aData) const;
typedef int (*nsISupportsComparatorFunc)(nsISupports* aElement1,
nsISupports* aElement2, void* aData);
struct nsISupportsComparatorContext {
nsISupportsComparatorFunc mComparatorFunc;
void* mData;
};
static int VoidStarComparator(const void* aElement1, const void* aElement2,
void* aData);
void Sort(nsISupportsComparatorFunc aFunc, void* aData);
bool InsertObjectAt(nsISupports* aObject, int32_t aIndex);
void InsertElementAt(uint32_t aIndex, nsISupports* aElement);
void InsertElementAt(uint32_t aIndex, already_AddRefed<nsISupports> aElement);
@ -165,10 +153,11 @@ class nsCOMArray_base {
return mArray.ShallowSizeOfExcludingThis(aMallocSizeOf);
}
private:
protected:
// the actual storage
nsTArray<nsISupports*> mArray;
private:
// don't implement these, defaults will muck with refcounts!
nsCOMArray_base& operator=(const nsCOMArray_base& aOther) = delete;
};
@ -297,23 +286,23 @@ class nsCOMArray : public nsCOMArray_base {
nsCOMArray_base::ReplaceElementAt(aIndex, aElement);
}
typedef int (*TComparatorFunc)(T* aElement1, T* aElement2, void* aData);
using TComparatorFunc = int (*)(T*, T*);
struct TComparatorContext {
TComparatorFunc mComparatorFunc;
void* mData;
};
static int nsISupportsComparator(nsISupports* aElement1,
nsISupports* aElement2, void* aData) {
auto ctx = static_cast<TComparatorContext*>(aData);
return (*ctx->mComparatorFunc)(static_cast<T*>(aElement1),
static_cast<T*>(aElement2), ctx->mData);
// The default sort function uses nsTArray::Sort.
// Note that the order of equal items is unstable with this.
void Sort(TComparatorFunc aFunc) {
mArray.Sort(
[aFunc](nsISupports* const& aLeft, nsISupports* const& aRight) -> int {
return aFunc(static_cast<T*>(aLeft), static_cast<T*>(aRight));
});
}
void Sort(TComparatorFunc aFunc, void* aData) {
TComparatorContext ctx = {aFunc, aData};
nsCOMArray_base::Sort(nsISupportsComparator, &ctx);
// Sort with a stable algorithm, uses nsTArray::StableSort.
void StableSort(TComparatorFunc aFunc) {
mArray.StableSort(
[aFunc](nsISupports* const& aLeft, nsISupports* const& aRight) -> int {
return aFunc(static_cast<T*>(aLeft), static_cast<T*>(aRight));
});
}
// append an object, growing the array as necessary