Skip to content

Commit b070e85

Browse files
committed
[ASTScope] Simplify binary search code in findChildContaining()
The comparision happens only by 'comp(element, value)' not 'comp(value, element)'. We only need `operator()(const ASTScopeImpl *, SourceLoc)`. Use llvm::lower_bound() instead of std::lower_bound().
1 parent bc20856 commit b070e85

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

lib/AST/ASTScopeLookup.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "swift/AST/TypeRepr.h"
3131
#include "swift/Basic/STLExtras.h"
3232
#include "llvm/Support/Compiler.h"
33-
#include <algorithm>
3433

3534
using namespace swift;
3635
using namespace namelookup;
@@ -170,22 +169,15 @@ NullablePtr<ASTScopeImpl>
170169
ASTScopeImpl::findChildContaining(SourceLoc loc,
171170
SourceManager &sourceMgr) const {
172171
// Use binary search to find the child that contains this location.
173-
struct CompareLocs {
174-
SourceManager &sourceMgr;
175-
176-
bool operator()(const ASTScopeImpl *scope, SourceLoc loc) {
177-
ASTScopeAssert(scope->checkSourceRangeOfThisASTNode(), "Bad range.");
178-
auto rangeOfScope = scope->getSourceRangeOfScope();
179-
loc = translateLocForReplacedRange(sourceMgr, rangeOfScope, loc);
180-
return -1 == ASTScopeImpl::compare(rangeOfScope, loc, sourceMgr,
181-
/*ensureDisjoint=*/false);
182-
}
183-
bool operator()(SourceLoc loc, const ASTScopeImpl *scope) {
184-
return !(*this)(scope, loc);
185-
}
186-
};
187-
auto *const *child = std::lower_bound(
188-
getChildren().begin(), getChildren().end(), loc, CompareLocs{sourceMgr});
172+
auto *const *child = llvm::lower_bound(
173+
getChildren(), loc,
174+
[&sourceMgr](const ASTScopeImpl *scope, SourceLoc loc) {
175+
ASTScopeAssert(scope->checkSourceRangeOfThisASTNode(), "Bad range.");
176+
auto rangeOfScope = scope->getSourceRangeOfScope();
177+
loc = translateLocForReplacedRange(sourceMgr, rangeOfScope, loc);
178+
return -1 == ASTScopeImpl::compare(rangeOfScope, loc, sourceMgr,
179+
/*ensureDisjoint=*/false);
180+
});
189181

190182
if (child != getChildren().end()) {
191183
auto rangeOfScope = (*child)->getSourceRangeOfScope();

0 commit comments

Comments
 (0)