revset: add helper to build balanced addsets from chained 'or' operations
authorYuya Nishihara <yuya@tcha.org>
Sun, 24 May 2015 14:10:52 +0900
changeset 25308 036c26b08b71
parent 25307 4d1e56b29a91
child 25309 b333ca94403d
revset: add helper to build balanced addsets from chained 'or' operations This function will be used by revset.orset() and scmutil.revrange() to reduce the stack depth from O(n) to O(log(n)). We've bikeshed the interface of this function, but we couldn't come to an agreement. So we decided to attempt to make it move forward. marmoute: - new factory function isn't necessary for balanced addsets - addset.__init__ can just recurse, should handle "len(subsets) == 2+" yuja: - want to write all "len(subsets) == 0, 1, 2, 3+" cases in the same function - no recursion in __init__ for cosmetic reason: can't return, can't call __init__ directly I've changed it to a private function so that nobody would be tempted to utilize it.
mercurial/revset.py
--- a/mercurial/revset.py	Sun Apr 26 18:27:32 2015 +0900
+++ b/mercurial/revset.py	Sun May 24 14:10:52 2015 +0900
@@ -2944,6 +2944,20 @@
     def __repr__(self):
         return '<%s %r>' % (type(self).__name__, self._subset)
 
+# this function will be removed, or merged to addset or orset, when
+# - scmutil.revrange() can be rewritten to not combine calculated smartsets
+# - or addset can handle more than two sets without balanced tree
+def _combinesets(subsets):
+    """Create balanced tree of addsets representing union of given sets"""
+    if not subsets:
+        return baseset()
+    if len(subsets) == 1:
+        return subsets[0]
+    p = len(subsets) // 2
+    xs = _combinesets(subsets[:p])
+    ys = _combinesets(subsets[p:])
+    return addset(xs, ys)
+
 def _iterordered(ascending, iter1, iter2):
     """produce an ordered iteration from two iterators with the same order