[cfe-commits] r49312 - /cfe/trunk/lib/AST/ASTContext.cpp

Chris Lattner sabre at nondot.org
Sun Apr 6 22:05:42 PDT 2008


Author: lattner
Date: Mon Apr  7 00:05:41 2008
New Revision: 49312

URL: http://llvm.org/viewvc/llvm-project?rev=49312&view=rev
Log:
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.


Modified:
    cfe/trunk/lib/AST/ASTContext.cpp

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=49312&r1=49311&r2=49312&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Apr  7 00:05:41 2008
@@ -1478,21 +1478,33 @@
   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





More information about the cfe-commits mailing list