Bug 1843181 - Make EqualRange return a std::pair. r=sergesanspaille

mozilla::CompactPair doesn't seem appropriate because its base class optimization doesn't apply.

Differential Revision: https://phabricator.services.mozilla.com/D178843
This commit is contained in:
Markus Stange 2023-07-14 21:51:27 +00:00
parent d33bf29823
commit d65e61e97a
4 changed files with 21 additions and 23 deletions

View File

@ -8,9 +8,9 @@
#define mozilla_BinarySearch_h
#include "mozilla/Assertions.h"
#include "mozilla/CompactPair.h"
#include <stddef.h>
#include <utility>
namespace mozilla {
@ -216,9 +216,8 @@ size_t UpperBound(const Container& aContainer, size_t aBegin, size_t aEnd,
}
template <typename Container, typename Comparator>
CompactPair<size_t, size_t> EqualRange(const Container& aContainer,
size_t aBegin, size_t aEnd,
const Comparator& aCompare) {
std::pair<size_t, size_t> EqualRange(const Container& aContainer, size_t aBegin,
size_t aEnd, const Comparator& aCompare) {
MOZ_ASSERT(aBegin <= aEnd);
size_t low = aBegin;
@ -235,13 +234,12 @@ CompactPair<size_t, size_t> EqualRange(const Container& aContainer,
} else if (result > 0) {
low = middle + 1;
} else {
return MakeCompactPair(
LowerBound(aContainer, low, middle, aCompare),
UpperBound(aContainer, middle + 1, high, aCompare));
return {LowerBound(aContainer, low, middle, aCompare),
UpperBound(aContainer, middle + 1, high, aCompare)};
}
}
return MakeCompactPair(low, high);
return {low, high};
}
} // namespace mozilla

View File

@ -126,26 +126,26 @@ static void TestEqualRange() {
for (int i = -1; i < kMaxNumber + 1; ++i) {
auto bounds = EqualRange(sortedArray, 0, sortedArray.length(), CompareN(i));
MOZ_RELEASE_ASSERT(bounds.first() <= sortedArray.length());
MOZ_RELEASE_ASSERT(bounds.second() <= sortedArray.length());
MOZ_RELEASE_ASSERT(bounds.first() <= bounds.second());
MOZ_RELEASE_ASSERT(bounds.first <= sortedArray.length());
MOZ_RELEASE_ASSERT(bounds.second <= sortedArray.length());
MOZ_RELEASE_ASSERT(bounds.first <= bounds.second);
if (bounds.first() == 0) {
if (bounds.first == 0) {
MOZ_RELEASE_ASSERT(sortedArray[0] >= i);
} else if (bounds.first() == sortedArray.length()) {
} else if (bounds.first == sortedArray.length()) {
MOZ_RELEASE_ASSERT(sortedArray[sortedArray.length() - 1] < i);
} else {
MOZ_RELEASE_ASSERT(sortedArray[bounds.first() - 1] < i);
MOZ_RELEASE_ASSERT(sortedArray[bounds.first()] >= i);
MOZ_RELEASE_ASSERT(sortedArray[bounds.first - 1] < i);
MOZ_RELEASE_ASSERT(sortedArray[bounds.first] >= i);
}
if (bounds.second() == 0) {
if (bounds.second == 0) {
MOZ_RELEASE_ASSERT(sortedArray[0] > i);
} else if (bounds.second() == sortedArray.length()) {
} else if (bounds.second == sortedArray.length()) {
MOZ_RELEASE_ASSERT(sortedArray[sortedArray.length() - 1] <= i);
} else {
MOZ_RELEASE_ASSERT(sortedArray[bounds.second() - 1] <= i);
MOZ_RELEASE_ASSERT(sortedArray[bounds.second()] > i);
MOZ_RELEASE_ASSERT(sortedArray[bounds.second - 1] <= i);
MOZ_RELEASE_ASSERT(sortedArray[bounds.second] > i);
}
}
}

View File

@ -796,11 +796,11 @@ NS_IMETHODIMP AboutThirdParty::LookupApplication(
// If more than one application includes the module, we return null
// because there is no way to know which is the real owner.
if (bounds.second() - bounds.first() != 1) {
if (bounds.second - bounds.first != 1) {
return NS_OK;
}
app = mLocations[bounds.first()].second();
app = mLocations[bounds.first].second();
app.forget(aResult);
return NS_OK;
}

View File

@ -115,11 +115,11 @@ TEST(AboutThirdParty, InstallLocations)
for (const auto& testCase : kTestCases) {
auto bounds = EqualRange(locations, 0, locations.Length(),
InstallLocationComparator(testCase.mFile));
if (bounds.second() - bounds.first() != 1) {
if (bounds.second - bounds.first != 1) {
EXPECT_TRUE(testCase.mInstallPath.IsEmpty());
continue;
}
EXPECT_EQ(locations[bounds.first()].first(), testCase.mInstallPath);
EXPECT_EQ(locations[bounds.first].first(), testCase.mInstallPath);
}
}