summaryrefslogtreecommitdiffstats
path: root/clang/lib/AST/ASTContext.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-04-07 05:05:41 +0000
committerChris Lattner <sabre@nondot.org>2008-04-07 05:05:41 +0000
commitae947fe3f9a96ee762fb7188be6b9a8efad95aaa (patch)
treea0f3e1bf628e165e3e29778f3f90a7d0181fc43b /clang/lib/AST/ASTContext.cpp
parentfde076173549de31cfe3e4039ac83d6ee597f2b3 (diff)
downloadbcm5719-llvm-ae947fe3f9a96ee762fb7188be6b9a8efad95aaa.tar.gz
bcm5719-llvm-ae947fe3f9a96ee762fb7188be6b9a8efad95aaa.zip
Replace an O(n^2) algorithm in areCompatObjCQualInterfaces with
an O(n) algorithm by taking advantage of the fact that the protocol qualifier list is already guaranteed sorted. llvm-svn: 49312
Diffstat (limited to 'clang/lib/AST/ASTContext.cpp')
-rw-r--r--clang/lib/AST/ASTContext.cpp40
1 files changed, 26 insertions, 14 deletions
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 5119cfb026c..65c4e0567f7 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1478,21 +1478,33 @@ areCompatObjCQualInterfaces(const ObjCQualifiedInterfaceType *LHS,
if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
return false;
- // All protocols in LHS must have a presence in RHS.
- for (unsigned i = 0; i != LHS->getNumProtocols(); ++i) {
- bool match = false;
- ObjCProtocolDecl *lhsProto = LHS->getProtocols(i);
- for (unsigned j = 0; j != RHS->getNumProtocols(); ++j) {
- ObjCProtocolDecl *rhsProto = RHS->getProtocols(j);
- if (lhsProto == rhsProto) {
- match = true;
- break;
- }
- }
- if (!match)
- return false;
+ // All protocols in LHS must have a presence in RHS. Since the protocol lists
+ // are both sorted alphabetically and have no duplicates, we can scan RHS and
+ // LHS in a single parallel scan until we run out of elements in LHS.
+ ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHS->qual_begin();
+ ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHS->qual_end();
+ ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHS->qual_begin();
+ ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHS->qual_end();
+
+ assert(LHSPI != LHSPE && "Empty LHS protocol list?");
+ ObjCProtocolDecl *LHSProto = *LHSPI;
+
+ while (RHSPI != RHSPE) {
+ ObjCProtocolDecl *RHSProto = *RHSPI++;
+ // If the RHS has a protocol that the LHS doesn't, ignore it.
+ if (RHSProto != LHSProto)
+ continue;
+
+ // Otherwise, the RHS does have this element.
+ ++LHSPI;
+ if (LHSPI == LHSPE)
+ return true; // All protocols in LHS exist in RHS.
+
+ LHSProto = *LHSPI;
}
- return true;
+
+ // If we got here, we didn't find one of the LHS's protocols in the RHS list.
+ return false;
}
/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
OpenPOWER on IntegriCloud