mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-04-10 09:58:30 +00:00
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:
parent
d33bf29823
commit
d65e61e97a
@ -8,9 +8,9 @@
|
|||||||
#define mozilla_BinarySearch_h
|
#define mozilla_BinarySearch_h
|
||||||
|
|
||||||
#include "mozilla/Assertions.h"
|
#include "mozilla/Assertions.h"
|
||||||
#include "mozilla/CompactPair.h"
|
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace mozilla {
|
namespace mozilla {
|
||||||
|
|
||||||
@ -216,9 +216,8 @@ size_t UpperBound(const Container& aContainer, size_t aBegin, size_t aEnd,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Container, typename Comparator>
|
template <typename Container, typename Comparator>
|
||||||
CompactPair<size_t, size_t> EqualRange(const Container& aContainer,
|
std::pair<size_t, size_t> EqualRange(const Container& aContainer, size_t aBegin,
|
||||||
size_t aBegin, size_t aEnd,
|
size_t aEnd, const Comparator& aCompare) {
|
||||||
const Comparator& aCompare) {
|
|
||||||
MOZ_ASSERT(aBegin <= aEnd);
|
MOZ_ASSERT(aBegin <= aEnd);
|
||||||
|
|
||||||
size_t low = aBegin;
|
size_t low = aBegin;
|
||||||
@ -235,13 +234,12 @@ CompactPair<size_t, size_t> EqualRange(const Container& aContainer,
|
|||||||
} else if (result > 0) {
|
} else if (result > 0) {
|
||||||
low = middle + 1;
|
low = middle + 1;
|
||||||
} else {
|
} else {
|
||||||
return MakeCompactPair(
|
return {LowerBound(aContainer, low, middle, aCompare),
|
||||||
LowerBound(aContainer, low, middle, aCompare),
|
UpperBound(aContainer, middle + 1, high, aCompare)};
|
||||||
UpperBound(aContainer, middle + 1, high, aCompare));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeCompactPair(low, high);
|
return {low, high};
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace mozilla
|
} // namespace mozilla
|
||||||
|
@ -126,26 +126,26 @@ static void TestEqualRange() {
|
|||||||
for (int i = -1; i < kMaxNumber + 1; ++i) {
|
for (int i = -1; i < kMaxNumber + 1; ++i) {
|
||||||
auto bounds = EqualRange(sortedArray, 0, sortedArray.length(), CompareN(i));
|
auto bounds = EqualRange(sortedArray, 0, sortedArray.length(), CompareN(i));
|
||||||
|
|
||||||
MOZ_RELEASE_ASSERT(bounds.first() <= sortedArray.length());
|
MOZ_RELEASE_ASSERT(bounds.first <= sortedArray.length());
|
||||||
MOZ_RELEASE_ASSERT(bounds.second() <= sortedArray.length());
|
MOZ_RELEASE_ASSERT(bounds.second <= sortedArray.length());
|
||||||
MOZ_RELEASE_ASSERT(bounds.first() <= bounds.second());
|
MOZ_RELEASE_ASSERT(bounds.first <= bounds.second);
|
||||||
|
|
||||||
if (bounds.first() == 0) {
|
if (bounds.first == 0) {
|
||||||
MOZ_RELEASE_ASSERT(sortedArray[0] >= i);
|
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);
|
MOZ_RELEASE_ASSERT(sortedArray[sortedArray.length() - 1] < i);
|
||||||
} else {
|
} else {
|
||||||
MOZ_RELEASE_ASSERT(sortedArray[bounds.first() - 1] < i);
|
MOZ_RELEASE_ASSERT(sortedArray[bounds.first - 1] < i);
|
||||||
MOZ_RELEASE_ASSERT(sortedArray[bounds.first()] >= i);
|
MOZ_RELEASE_ASSERT(sortedArray[bounds.first] >= i);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bounds.second() == 0) {
|
if (bounds.second == 0) {
|
||||||
MOZ_RELEASE_ASSERT(sortedArray[0] > i);
|
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);
|
MOZ_RELEASE_ASSERT(sortedArray[sortedArray.length() - 1] <= i);
|
||||||
} else {
|
} else {
|
||||||
MOZ_RELEASE_ASSERT(sortedArray[bounds.second() - 1] <= i);
|
MOZ_RELEASE_ASSERT(sortedArray[bounds.second - 1] <= i);
|
||||||
MOZ_RELEASE_ASSERT(sortedArray[bounds.second()] > i);
|
MOZ_RELEASE_ASSERT(sortedArray[bounds.second] > i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -796,11 +796,11 @@ NS_IMETHODIMP AboutThirdParty::LookupApplication(
|
|||||||
|
|
||||||
// If more than one application includes the module, we return null
|
// If more than one application includes the module, we return null
|
||||||
// because there is no way to know which is the real owner.
|
// 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;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
app = mLocations[bounds.first()].second();
|
app = mLocations[bounds.first].second();
|
||||||
app.forget(aResult);
|
app.forget(aResult);
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
@ -115,11 +115,11 @@ TEST(AboutThirdParty, InstallLocations)
|
|||||||
for (const auto& testCase : kTestCases) {
|
for (const auto& testCase : kTestCases) {
|
||||||
auto bounds = EqualRange(locations, 0, locations.Length(),
|
auto bounds = EqualRange(locations, 0, locations.Length(),
|
||||||
InstallLocationComparator(testCase.mFile));
|
InstallLocationComparator(testCase.mFile));
|
||||||
if (bounds.second() - bounds.first() != 1) {
|
if (bounds.second - bounds.first != 1) {
|
||||||
EXPECT_TRUE(testCase.mInstallPath.IsEmpty());
|
EXPECT_TRUE(testCase.mInstallPath.IsEmpty());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
EXPECT_EQ(locations[bounds.first()].first(), testCase.mInstallPath);
|
EXPECT_EQ(locations[bounds.first].first(), testCase.mInstallPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user