[PATCH] smartset: add a "toset" method
Jun Wu
quark at fb.com
Sat Jun 3 19:03:21 UTC 2017
# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1496515319 25200
# Sat Jun 03 11:41:59 2017 -0700
# Node ID 649ebbdd8944d2a7d8a9bb5e938886e7c26de1a2
# Parent 783394c0c97807e83daad9da561179bd0719e159
# Available At https://bitbucket.org/quark-zju/hg-draft
# hg pull https://bitbucket.org/quark-zju/hg-draft -r 649ebbdd8944
smartset: add a "toset" method
This allows us to convert a smartset to a Python set using a customized
approach. Namely, baseset may have a "_set" property already which could be
used directly to avoid __iter__ overhead.
diff --git a/mercurial/smartset.py b/mercurial/smartset.py
--- a/mercurial/smartset.py
+++ b/mercurial/smartset.py
@@ -156,4 +156,10 @@ class abstractsmartset(object):
return filteredset(self, condition, condrepr)
+ def toset(self):
+ """Convert to unordered Python set"""
+ if not util.safehasattr(self, '_set'):
+ self._set = set(self)
+ return self._set
+
class baseset(abstractsmartset):
"""Basic data structure that represents a revset and contains the basic
@@ -169,5 +175,6 @@ class baseset(abstractsmartset):
Construct by a set:
- >>> xs = baseset(set(x))
+ >>> xset = set(x)
+ >>> xs = baseset(xset)
>>> ys = baseset(set(y))
>>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
@@ -175,4 +182,6 @@ class baseset(abstractsmartset):
>>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
['addset', 'baseset', 'baseset']
+ >>> xset is xs.toset()
+ True
Construct by a list-like:
@@ -584,4 +593,10 @@ class addset(abstractsmartset):
>>> [x for x in rs]
[5, 4, 3, 2, 0]
+
+ convert to Python set:
+ >>> rs.toset() is rs.toset()
+ True
+ >>> sorted(rs.toset())
+ [0, 2, 3, 4, 5]
"""
def __init__(self, revs1, revs2, ascending=None):
More information about the Mercurial-devel
mailing list