Patchwork [6,of,6] baseset: allow a set as argument

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 17, 2014, 5:50 p.m.
Message ID <b50e5000f0fc15d050fd.1413568216@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/6382/
State Deferred
Headers show

Comments

Pierre-Yves David - Oct. 17, 2014, 5:50 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1413538344 25200
#      Fri Oct 17 02:32:24 2014 -0700
# Node ID b50e5000f0fc15d050fdf014293f6857fc24c08a
# Parent  5bfc877a5cc24c59b1faae436e9765248e221ff6
baseset: allow a set as argument

If a set is provided it is directly reused for membership testing. As set are
unordered, we decide such baseset are ascending. The list used for iteration are
lazily created. We make `_list` a property cache for this purpose.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2320,31 +2320,48 @@  class baseset(abstractsmartset):
     True (or False) the baseset will assume the data are ascending (or
     descending, respectively) and be optimised accordingly.
 
     BEWARE that the value provided in `isasc`  MUST match the order of the data
     otherwise, buggy behavior will be upon you!
+
+    If the provided data are a set, we clearly do not care about ordering in
+    the data. So the set is directly reused and the baseset will be considered
+    ascending.
     """
     def __init__(self, data=(), isasc=None):
-        if not isinstance(data, list):
-            data = list(data)
-        self._list = data
-        if isasc is not None:
-            isasc = bool(isasc)
-            if not isasc:
-                data = data[::-1]
-            self._asclist = data
-        self._ascending = isasc
+        if isinstance(data, set):
+            self._set = data
+            assert isasc is None
+            self._ascending = True
+        else:
+            if not isinstance(data, list):
+                data = list(data)
+            self._list = data
+            if isasc is not None:
+                isasc = bool(isasc)
+                if not isasc:
+                    data = data[::-1]
+                self._asclist = data
+            self._ascending = isasc
+
+    # we have cyclic dependency here so the __init__ MUST set one of theses.
 
     @util.propertycache
     def _set(self):
         return set(self._list)
 
     @util.propertycache
+    def _list(self):
+        return self._asclist
+
+    @util.propertycache
     def _asclist(self):
-        asclist = self._list[:]
-        asclist.sort()
-        return asclist
+        if '_list' in vars(self):
+            data = self._list
+        else:
+            data = self._set
+        return sorted(data)
 
     def __iter__(self):
         if self._ascending is None:
             return iter(self._list)
         elif self._ascending: